blob: eadd19ab39a870dde45cd8eddb3eea784900bb27 [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
Hristo Venev75b28af2019-08-26 17:23:46 +0000869static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600870{
871 /* See comment at the top of this file */
872 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000873 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600874}
875
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300876static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
877{
878 struct io_rings *rings = ctx->rings;
879
880 /* make sure SQ entry isn't read before tail */
881 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
882}
883
Jens Axboedef596e2019-01-09 08:59:42 -0700884/*
885 * Find and free completed poll iocbs
886 */
887static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
888 struct list_head *done)
889{
890 void *reqs[IO_IOPOLL_BATCH];
891 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600892 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700893
Jens Axboe09bb8392019-03-13 12:39:28 -0600894 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700895 while (!list_empty(done)) {
896 req = list_first_entry(done, struct io_kiocb, list);
897 list_del(&req->list);
898
Jens Axboe9e645e112019-05-10 16:07:28 -0600899 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700900 (*nr_events)++;
901
Jens Axboe09bb8392019-03-13 12:39:28 -0600902 if (refcount_dec_and_test(&req->refs)) {
903 /* If we're not using fixed files, we have to pair the
904 * completion part with the file put. Use regular
905 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600906 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600907 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600908 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
909 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600910 reqs[to_free++] = req;
911 if (to_free == ARRAY_SIZE(reqs))
912 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700913 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600914 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700915 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700916 }
Jens Axboedef596e2019-01-09 08:59:42 -0700917 }
Jens Axboedef596e2019-01-09 08:59:42 -0700918
Jens Axboe09bb8392019-03-13 12:39:28 -0600919 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700920 io_free_req_many(ctx, reqs, &to_free);
921}
922
923static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
924 long min)
925{
926 struct io_kiocb *req, *tmp;
927 LIST_HEAD(done);
928 bool spin;
929 int ret;
930
931 /*
932 * Only spin for completions if we don't have multiple devices hanging
933 * off our complete list, and we're under the requested amount.
934 */
935 spin = !ctx->poll_multi_file && *nr_events < min;
936
937 ret = 0;
938 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
939 struct kiocb *kiocb = &req->rw;
940
941 /*
942 * Move completed entries to our local list. If we find a
943 * request that requires polling, break out and complete
944 * the done list first, if we have entries there.
945 */
946 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
947 list_move_tail(&req->list, &done);
948 continue;
949 }
950 if (!list_empty(&done))
951 break;
952
953 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
954 if (ret < 0)
955 break;
956
957 if (ret && spin)
958 spin = false;
959 ret = 0;
960 }
961
962 if (!list_empty(&done))
963 io_iopoll_complete(ctx, nr_events, &done);
964
965 return ret;
966}
967
968/*
969 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
970 * non-spinning poll check - we'll still enter the driver poll loop, but only
971 * as a non-spinning completion check.
972 */
973static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
974 long min)
975{
Jens Axboe08f54392019-08-21 22:19:11 -0600976 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700977 int ret;
978
979 ret = io_do_iopoll(ctx, nr_events, min);
980 if (ret < 0)
981 return ret;
982 if (!min || *nr_events >= min)
983 return 0;
984 }
985
986 return 1;
987}
988
989/*
990 * We can't just wait for polled events to come to us, we have to actively
991 * find and complete them.
992 */
993static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
994{
995 if (!(ctx->flags & IORING_SETUP_IOPOLL))
996 return;
997
998 mutex_lock(&ctx->uring_lock);
999 while (!list_empty(&ctx->poll_list)) {
1000 unsigned int nr_events = 0;
1001
1002 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001003
1004 /*
1005 * Ensure we allow local-to-the-cpu processing to take place,
1006 * in this case we need to ensure that we reap all events.
1007 */
1008 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001009 }
1010 mutex_unlock(&ctx->uring_lock);
1011}
1012
Jens Axboe2b2ed972019-10-25 10:06:15 -06001013static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1014 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001015{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001016 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001017
1018 do {
1019 int tmin = 0;
1020
Jens Axboe500f9fb2019-08-19 12:15:59 -06001021 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001022 * Don't enter poll loop if we already have events pending.
1023 * If we do, we can potentially be spinning for commands that
1024 * already triggered a CQE (eg in error).
1025 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001026 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -06001027 break;
1028
1029 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001030 * If a submit got punted to a workqueue, we can have the
1031 * application entering polling for a command before it gets
1032 * issued. That app will hold the uring_lock for the duration
1033 * of the poll right here, so we need to take a breather every
1034 * now and then to ensure that the issue has a chance to add
1035 * the poll to the issued list. Otherwise we can spin here
1036 * forever, while the workqueue is stuck trying to acquire the
1037 * very same mutex.
1038 */
1039 if (!(++iters & 7)) {
1040 mutex_unlock(&ctx->uring_lock);
1041 mutex_lock(&ctx->uring_lock);
1042 }
1043
Jens Axboedef596e2019-01-09 08:59:42 -07001044 if (*nr_events < min)
1045 tmin = min - *nr_events;
1046
1047 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1048 if (ret <= 0)
1049 break;
1050 ret = 0;
1051 } while (min && !*nr_events && !need_resched());
1052
Jens Axboe2b2ed972019-10-25 10:06:15 -06001053 return ret;
1054}
1055
1056static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1057 long min)
1058{
1059 int ret;
1060
1061 /*
1062 * We disallow the app entering submit/complete with polling, but we
1063 * still need to lock the ring to prevent racing with polled issue
1064 * that got punted to a workqueue.
1065 */
1066 mutex_lock(&ctx->uring_lock);
1067 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001068 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001069 return ret;
1070}
1071
Jens Axboe491381ce2019-10-17 09:20:46 -06001072static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073{
Jens Axboe491381ce2019-10-17 09:20:46 -06001074 /*
1075 * Tell lockdep we inherited freeze protection from submission
1076 * thread.
1077 */
1078 if (req->flags & REQ_F_ISREG) {
1079 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001080
Jens Axboe491381ce2019-10-17 09:20:46 -06001081 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001083 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084}
1085
Jens Axboeba816ad2019-09-28 11:36:45 -06001086static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087{
1088 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1089
Jens Axboe491381ce2019-10-17 09:20:46 -06001090 if (kiocb->ki_flags & IOCB_WRITE)
1091 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001092
Jens Axboe9e645e112019-05-10 16:07:28 -06001093 if ((req->flags & REQ_F_LINK) && res != req->result)
1094 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001095 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001096}
1097
1098static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1099{
1100 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1101
1102 io_complete_rw_common(kiocb, res);
1103 io_put_req(req, NULL);
1104}
1105
1106static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1107{
1108 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1109
1110 io_complete_rw_common(kiocb, res);
1111 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001112}
1113
Jens Axboedef596e2019-01-09 08:59:42 -07001114static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1115{
1116 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1117
Jens Axboe491381ce2019-10-17 09:20:46 -06001118 if (kiocb->ki_flags & IOCB_WRITE)
1119 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001120
Jens Axboe9e645e112019-05-10 16:07:28 -06001121 if ((req->flags & REQ_F_LINK) && res != req->result)
1122 req->flags |= REQ_F_FAIL_LINK;
1123 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001124 if (res != -EAGAIN)
1125 req->flags |= REQ_F_IOPOLL_COMPLETED;
1126}
1127
1128/*
1129 * After the iocb has been issued, it's safe to be found on the poll list.
1130 * Adding the kiocb to the list AFTER submission ensures that we don't
1131 * find it from a io_iopoll_getevents() thread before the issuer is done
1132 * accessing the kiocb cookie.
1133 */
1134static void io_iopoll_req_issued(struct io_kiocb *req)
1135{
1136 struct io_ring_ctx *ctx = req->ctx;
1137
1138 /*
1139 * Track whether we have multiple files in our lists. This will impact
1140 * how we do polling eventually, not spinning if we're on potentially
1141 * different devices.
1142 */
1143 if (list_empty(&ctx->poll_list)) {
1144 ctx->poll_multi_file = false;
1145 } else if (!ctx->poll_multi_file) {
1146 struct io_kiocb *list_req;
1147
1148 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1149 list);
1150 if (list_req->rw.ki_filp != req->rw.ki_filp)
1151 ctx->poll_multi_file = true;
1152 }
1153
1154 /*
1155 * For fast devices, IO may have already completed. If it has, add
1156 * it to the front so we find it first.
1157 */
1158 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1159 list_add(&req->list, &ctx->poll_list);
1160 else
1161 list_add_tail(&req->list, &ctx->poll_list);
1162}
1163
Jens Axboe3d6770f2019-04-13 11:50:54 -06001164static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001165{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001166 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001167 int diff = state->has_refs - state->used_refs;
1168
1169 if (diff)
1170 fput_many(state->file, diff);
1171 state->file = NULL;
1172 }
1173}
1174
1175/*
1176 * Get as many references to a file as we have IOs left in this submission,
1177 * assuming most submissions are for one file, or at least that each file
1178 * has more than one submission.
1179 */
1180static struct file *io_file_get(struct io_submit_state *state, int fd)
1181{
1182 if (!state)
1183 return fget(fd);
1184
1185 if (state->file) {
1186 if (state->fd == fd) {
1187 state->used_refs++;
1188 state->ios_left--;
1189 return state->file;
1190 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001191 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001192 }
1193 state->file = fget_many(fd, state->ios_left);
1194 if (!state->file)
1195 return NULL;
1196
1197 state->fd = fd;
1198 state->has_refs = state->ios_left;
1199 state->used_refs = 1;
1200 state->ios_left--;
1201 return state->file;
1202}
1203
Jens Axboe2b188cc2019-01-07 10:46:33 -07001204/*
1205 * If we tracked the file through the SCM inflight mechanism, we could support
1206 * any file. For now, just ensure that anything potentially problematic is done
1207 * inline.
1208 */
1209static bool io_file_supports_async(struct file *file)
1210{
1211 umode_t mode = file_inode(file)->i_mode;
1212
1213 if (S_ISBLK(mode) || S_ISCHR(mode))
1214 return true;
1215 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1216 return true;
1217
1218 return false;
1219}
1220
Pavel Begunkov267bc902019-11-07 01:41:08 +03001221static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001222{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001223 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001224 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001225 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001226 unsigned ioprio;
1227 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228
Jens Axboe09bb8392019-03-13 12:39:28 -06001229 if (!req->file)
1230 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001231
Jens Axboe491381ce2019-10-17 09:20:46 -06001232 if (S_ISREG(file_inode(req->file)->i_mode))
1233 req->flags |= REQ_F_ISREG;
1234
1235 /*
1236 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1237 * we know to async punt it even if it was opened O_NONBLOCK
1238 */
1239 if (force_nonblock && !io_file_supports_async(req->file)) {
1240 req->flags |= REQ_F_MUST_PUNT;
1241 return -EAGAIN;
1242 }
Jens Axboe6b063142019-01-10 22:13:58 -07001243
Jens Axboe2b188cc2019-01-07 10:46:33 -07001244 kiocb->ki_pos = READ_ONCE(sqe->off);
1245 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1246 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1247
1248 ioprio = READ_ONCE(sqe->ioprio);
1249 if (ioprio) {
1250 ret = ioprio_check_cap(ioprio);
1251 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001252 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253
1254 kiocb->ki_ioprio = ioprio;
1255 } else
1256 kiocb->ki_ioprio = get_current_ioprio();
1257
1258 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1259 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001260 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001261
1262 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001263 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1264 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001265 req->flags |= REQ_F_NOWAIT;
1266
1267 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001268 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001269
Jens Axboedef596e2019-01-09 08:59:42 -07001270 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001271 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1272 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001273 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274
Jens Axboedef596e2019-01-09 08:59:42 -07001275 kiocb->ki_flags |= IOCB_HIPRI;
1276 kiocb->ki_complete = io_complete_rw_iopoll;
1277 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001278 if (kiocb->ki_flags & IOCB_HIPRI)
1279 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001280 kiocb->ki_complete = io_complete_rw;
1281 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283}
1284
1285static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1286{
1287 switch (ret) {
1288 case -EIOCBQUEUED:
1289 break;
1290 case -ERESTARTSYS:
1291 case -ERESTARTNOINTR:
1292 case -ERESTARTNOHAND:
1293 case -ERESTART_RESTARTBLOCK:
1294 /*
1295 * We can't just restart the syscall, since previously
1296 * submitted sqes may already be in progress. Just fail this
1297 * IO with EINTR.
1298 */
1299 ret = -EINTR;
1300 /* fall through */
1301 default:
1302 kiocb->ki_complete(kiocb, ret, 0);
1303 }
1304}
1305
Jens Axboeba816ad2019-09-28 11:36:45 -06001306static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1307 bool in_async)
1308{
1309 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1310 *nxt = __io_complete_rw(kiocb, ret);
1311 else
1312 io_rw_done(kiocb, ret);
1313}
1314
Jens Axboeedafcce2019-01-09 09:16:05 -07001315static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1316 const struct io_uring_sqe *sqe,
1317 struct iov_iter *iter)
1318{
1319 size_t len = READ_ONCE(sqe->len);
1320 struct io_mapped_ubuf *imu;
1321 unsigned index, buf_index;
1322 size_t offset;
1323 u64 buf_addr;
1324
1325 /* attempt to use fixed buffers without having provided iovecs */
1326 if (unlikely(!ctx->user_bufs))
1327 return -EFAULT;
1328
1329 buf_index = READ_ONCE(sqe->buf_index);
1330 if (unlikely(buf_index >= ctx->nr_user_bufs))
1331 return -EFAULT;
1332
1333 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1334 imu = &ctx->user_bufs[index];
1335 buf_addr = READ_ONCE(sqe->addr);
1336
1337 /* overflow */
1338 if (buf_addr + len < buf_addr)
1339 return -EFAULT;
1340 /* not inside the mapped region */
1341 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1342 return -EFAULT;
1343
1344 /*
1345 * May not be a start of buffer, set size appropriately
1346 * and advance us to the beginning.
1347 */
1348 offset = buf_addr - imu->ubuf;
1349 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001350
1351 if (offset) {
1352 /*
1353 * Don't use iov_iter_advance() here, as it's really slow for
1354 * using the latter parts of a big fixed buffer - it iterates
1355 * over each segment manually. We can cheat a bit here, because
1356 * we know that:
1357 *
1358 * 1) it's a BVEC iter, we set it up
1359 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1360 * first and last bvec
1361 *
1362 * So just find our index, and adjust the iterator afterwards.
1363 * If the offset is within the first bvec (or the whole first
1364 * bvec, just use iov_iter_advance(). This makes it easier
1365 * since we can just skip the first segment, which may not
1366 * be PAGE_SIZE aligned.
1367 */
1368 const struct bio_vec *bvec = imu->bvec;
1369
1370 if (offset <= bvec->bv_len) {
1371 iov_iter_advance(iter, offset);
1372 } else {
1373 unsigned long seg_skip;
1374
1375 /* skip first vec */
1376 offset -= bvec->bv_len;
1377 seg_skip = 1 + (offset >> PAGE_SHIFT);
1378
1379 iter->bvec = bvec + seg_skip;
1380 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001381 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001382 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001383 }
1384 }
1385
Jens Axboeedafcce2019-01-09 09:16:05 -07001386 return 0;
1387}
1388
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001389static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1390 const struct sqe_submit *s, struct iovec **iovec,
1391 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001392{
1393 const struct io_uring_sqe *sqe = s->sqe;
1394 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1395 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001396 u8 opcode;
1397
1398 /*
1399 * We're reading ->opcode for the second time, but the first read
1400 * doesn't care whether it's _FIXED or not, so it doesn't matter
1401 * whether ->opcode changes concurrently. The first read does care
1402 * about whether it is a READ or a WRITE, so we don't trust this read
1403 * for that purpose and instead let the caller pass in the read/write
1404 * flag.
1405 */
1406 opcode = READ_ONCE(sqe->opcode);
1407 if (opcode == IORING_OP_READ_FIXED ||
1408 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001409 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001410 *iovec = NULL;
1411 return ret;
1412 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001413
1414 if (!s->has_user)
1415 return -EFAULT;
1416
1417#ifdef CONFIG_COMPAT
1418 if (ctx->compat)
1419 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1420 iovec, iter);
1421#endif
1422
1423 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1424}
1425
Jens Axboe32960612019-09-23 11:05:34 -06001426/*
1427 * For files that don't have ->read_iter() and ->write_iter(), handle them
1428 * by looping over ->read() or ->write() manually.
1429 */
1430static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1431 struct iov_iter *iter)
1432{
1433 ssize_t ret = 0;
1434
1435 /*
1436 * Don't support polled IO through this interface, and we can't
1437 * support non-blocking either. For the latter, this just causes
1438 * the kiocb to be handled from an async context.
1439 */
1440 if (kiocb->ki_flags & IOCB_HIPRI)
1441 return -EOPNOTSUPP;
1442 if (kiocb->ki_flags & IOCB_NOWAIT)
1443 return -EAGAIN;
1444
1445 while (iov_iter_count(iter)) {
1446 struct iovec iovec = iov_iter_iovec(iter);
1447 ssize_t nr;
1448
1449 if (rw == READ) {
1450 nr = file->f_op->read(file, iovec.iov_base,
1451 iovec.iov_len, &kiocb->ki_pos);
1452 } else {
1453 nr = file->f_op->write(file, iovec.iov_base,
1454 iovec.iov_len, &kiocb->ki_pos);
1455 }
1456
1457 if (nr < 0) {
1458 if (!ret)
1459 ret = nr;
1460 break;
1461 }
1462 ret += nr;
1463 if (nr != iovec.iov_len)
1464 break;
1465 iov_iter_advance(iter, nr);
1466 }
1467
1468 return ret;
1469}
1470
Pavel Begunkov267bc902019-11-07 01:41:08 +03001471static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1472 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001473{
1474 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1475 struct kiocb *kiocb = &req->rw;
1476 struct iov_iter iter;
1477 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001478 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001479 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480
Pavel Begunkov267bc902019-11-07 01:41:08 +03001481 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482 if (ret)
1483 return ret;
1484 file = kiocb->ki_filp;
1485
Jens Axboe2b188cc2019-01-07 10:46:33 -07001486 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001487 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488
Pavel Begunkov267bc902019-11-07 01:41:08 +03001489 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001490 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001491 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001492
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001493 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001494 if (req->flags & REQ_F_LINK)
1495 req->result = read_size;
1496
Jens Axboe31b51512019-01-18 22:56:34 -07001497 iov_count = iov_iter_count(&iter);
1498 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499 if (!ret) {
1500 ssize_t ret2;
1501
Jens Axboe32960612019-09-23 11:05:34 -06001502 if (file->f_op->read_iter)
1503 ret2 = call_read_iter(file, kiocb, &iter);
1504 else
1505 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1506
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001507 /*
1508 * In case of a short read, punt to async. This can happen
1509 * if we have data partially cached. Alternatively we can
1510 * return the short read, in which case the application will
1511 * need to issue another SQE and wait for it. That SQE will
1512 * need async punt anyway, so it's more efficient to do it
1513 * here.
1514 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001515 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1516 (req->flags & REQ_F_ISREG) &&
1517 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001518 ret2 = -EAGAIN;
1519 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001520 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001521 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001522 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001523 ret = -EAGAIN;
1524 }
1525 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001526 return ret;
1527}
1528
Pavel Begunkov267bc902019-11-07 01:41:08 +03001529static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1530 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001531{
1532 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1533 struct kiocb *kiocb = &req->rw;
1534 struct iov_iter iter;
1535 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001536 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001537 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538
Pavel Begunkov267bc902019-11-07 01:41:08 +03001539 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001540 if (ret)
1541 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001542
Jens Axboe2b188cc2019-01-07 10:46:33 -07001543 file = kiocb->ki_filp;
1544 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001545 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001546
Pavel Begunkov267bc902019-11-07 01:41:08 +03001547 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001548 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001549 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001550
Jens Axboe9e645e112019-05-10 16:07:28 -06001551 if (req->flags & REQ_F_LINK)
1552 req->result = ret;
1553
Jens Axboe31b51512019-01-18 22:56:34 -07001554 iov_count = iov_iter_count(&iter);
1555
1556 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001557 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001558 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001559
1560 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001561 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001562 ssize_t ret2;
1563
Jens Axboe2b188cc2019-01-07 10:46:33 -07001564 /*
1565 * Open-code file_start_write here to grab freeze protection,
1566 * which will be released by another thread in
1567 * io_complete_rw(). Fool lockdep by telling it the lock got
1568 * released so that it doesn't complain about the held lock when
1569 * we return to userspace.
1570 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001571 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001572 __sb_start_write(file_inode(file)->i_sb,
1573 SB_FREEZE_WRITE, true);
1574 __sb_writers_release(file_inode(file)->i_sb,
1575 SB_FREEZE_WRITE);
1576 }
1577 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001578
Jens Axboe32960612019-09-23 11:05:34 -06001579 if (file->f_op->write_iter)
1580 ret2 = call_write_iter(file, kiocb, &iter);
1581 else
1582 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001583 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001584 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001585 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001586 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001587 }
Jens Axboe31b51512019-01-18 22:56:34 -07001588out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001590 return ret;
1591}
1592
1593/*
1594 * IORING_OP_NOP just posts a completion event, nothing else.
1595 */
1596static int io_nop(struct io_kiocb *req, u64 user_data)
1597{
1598 struct io_ring_ctx *ctx = req->ctx;
1599 long err = 0;
1600
Jens Axboedef596e2019-01-09 08:59:42 -07001601 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1602 return -EINVAL;
1603
Jens Axboec71ffb62019-05-13 20:58:29 -06001604 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001605 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606 return 0;
1607}
1608
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001609static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1610{
Jens Axboe6b063142019-01-10 22:13:58 -07001611 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001612
Jens Axboe09bb8392019-03-13 12:39:28 -06001613 if (!req->file)
1614 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001615
Jens Axboe6b063142019-01-10 22:13:58 -07001616 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001617 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001618 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001619 return -EINVAL;
1620
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001621 return 0;
1622}
1623
1624static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001625 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001626{
1627 loff_t sqe_off = READ_ONCE(sqe->off);
1628 loff_t sqe_len = READ_ONCE(sqe->len);
1629 loff_t end = sqe_off + sqe_len;
1630 unsigned fsync_flags;
1631 int ret;
1632
1633 fsync_flags = READ_ONCE(sqe->fsync_flags);
1634 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1635 return -EINVAL;
1636
1637 ret = io_prep_fsync(req, sqe);
1638 if (ret)
1639 return ret;
1640
1641 /* fsync always requires a blocking context */
1642 if (force_nonblock)
1643 return -EAGAIN;
1644
1645 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1646 end > 0 ? end : LLONG_MAX,
1647 fsync_flags & IORING_FSYNC_DATASYNC);
1648
Jens Axboe9e645e112019-05-10 16:07:28 -06001649 if (ret < 0 && (req->flags & REQ_F_LINK))
1650 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001651 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001652 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001653 return 0;
1654}
1655
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001656static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1657{
1658 struct io_ring_ctx *ctx = req->ctx;
1659 int ret = 0;
1660
1661 if (!req->file)
1662 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001663
1664 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1665 return -EINVAL;
1666 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1667 return -EINVAL;
1668
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001669 return ret;
1670}
1671
1672static int io_sync_file_range(struct io_kiocb *req,
1673 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001674 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001675 bool force_nonblock)
1676{
1677 loff_t sqe_off;
1678 loff_t sqe_len;
1679 unsigned flags;
1680 int ret;
1681
1682 ret = io_prep_sfr(req, sqe);
1683 if (ret)
1684 return ret;
1685
1686 /* sync_file_range always requires a blocking context */
1687 if (force_nonblock)
1688 return -EAGAIN;
1689
1690 sqe_off = READ_ONCE(sqe->off);
1691 sqe_len = READ_ONCE(sqe->len);
1692 flags = READ_ONCE(sqe->sync_range_flags);
1693
1694 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1695
Jens Axboe9e645e112019-05-10 16:07:28 -06001696 if (ret < 0 && (req->flags & REQ_F_LINK))
1697 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001698 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001699 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001700 return 0;
1701}
1702
Jens Axboe0fa03c62019-04-19 13:34:07 -06001703#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001704static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001705 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001706 long (*fn)(struct socket *, struct user_msghdr __user *,
1707 unsigned int))
1708{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001709 struct socket *sock;
1710 int ret;
1711
1712 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1713 return -EINVAL;
1714
1715 sock = sock_from_file(req->file, &ret);
1716 if (sock) {
1717 struct user_msghdr __user *msg;
1718 unsigned flags;
1719
1720 flags = READ_ONCE(sqe->msg_flags);
1721 if (flags & MSG_DONTWAIT)
1722 req->flags |= REQ_F_NOWAIT;
1723 else if (force_nonblock)
1724 flags |= MSG_DONTWAIT;
1725
1726 msg = (struct user_msghdr __user *) (unsigned long)
1727 READ_ONCE(sqe->addr);
1728
Jens Axboeaa1fa282019-04-19 13:38:09 -06001729 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001730 if (force_nonblock && ret == -EAGAIN)
1731 return ret;
1732 }
1733
1734 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001735 if (ret < 0 && (req->flags & REQ_F_LINK))
1736 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001737 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001738 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001739}
1740#endif
1741
1742static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001743 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001744{
1745#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001746 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1747 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001748#else
1749 return -EOPNOTSUPP;
1750#endif
1751}
1752
1753static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001754 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001755{
1756#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001757 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1758 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001759#else
1760 return -EOPNOTSUPP;
1761#endif
1762}
1763
Jens Axboe17f2fe32019-10-17 14:42:58 -06001764static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1765 struct io_kiocb **nxt, bool force_nonblock)
1766{
1767#if defined(CONFIG_NET)
1768 struct sockaddr __user *addr;
1769 int __user *addr_len;
1770 unsigned file_flags;
1771 int flags, ret;
1772
1773 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1774 return -EINVAL;
1775 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1776 return -EINVAL;
1777
1778 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1779 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1780 flags = READ_ONCE(sqe->accept_flags);
1781 file_flags = force_nonblock ? O_NONBLOCK : 0;
1782
1783 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1784 if (ret == -EAGAIN && force_nonblock) {
1785 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1786 return -EAGAIN;
1787 }
1788 if (ret < 0 && (req->flags & REQ_F_LINK))
1789 req->flags |= REQ_F_FAIL_LINK;
1790 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1791 io_put_req(req, nxt);
1792 return 0;
1793#else
1794 return -EOPNOTSUPP;
1795#endif
1796}
1797
Jens Axboe221c5eb2019-01-17 09:41:58 -07001798static void io_poll_remove_one(struct io_kiocb *req)
1799{
1800 struct io_poll_iocb *poll = &req->poll;
1801
1802 spin_lock(&poll->head->lock);
1803 WRITE_ONCE(poll->canceled, true);
1804 if (!list_empty(&poll->wait.entry)) {
1805 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001806 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001807 }
1808 spin_unlock(&poll->head->lock);
1809
1810 list_del_init(&req->list);
1811}
1812
1813static void io_poll_remove_all(struct io_ring_ctx *ctx)
1814{
1815 struct io_kiocb *req;
1816
1817 spin_lock_irq(&ctx->completion_lock);
1818 while (!list_empty(&ctx->cancel_list)) {
1819 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1820 io_poll_remove_one(req);
1821 }
1822 spin_unlock_irq(&ctx->completion_lock);
1823}
1824
1825/*
1826 * Find a running poll command that matches one specified in sqe->addr,
1827 * and remove it if found.
1828 */
1829static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1830{
1831 struct io_ring_ctx *ctx = req->ctx;
1832 struct io_kiocb *poll_req, *next;
1833 int ret = -ENOENT;
1834
1835 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1836 return -EINVAL;
1837 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1838 sqe->poll_events)
1839 return -EINVAL;
1840
1841 spin_lock_irq(&ctx->completion_lock);
1842 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1843 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1844 io_poll_remove_one(poll_req);
1845 ret = 0;
1846 break;
1847 }
1848 }
1849 spin_unlock_irq(&ctx->completion_lock);
1850
Jens Axboec71ffb62019-05-13 20:58:29 -06001851 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001852 if (ret < 0 && (req->flags & REQ_F_LINK))
1853 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001854 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001855 return 0;
1856}
1857
Jens Axboe8c838782019-03-12 15:48:16 -06001858static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1859 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001860{
Jens Axboe8c838782019-03-12 15:48:16 -06001861 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001862 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001863 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001864}
1865
Jens Axboe561fb042019-10-24 07:25:42 -06001866static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001867{
Jens Axboe561fb042019-10-24 07:25:42 -06001868 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001869 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1870 struct io_poll_iocb *poll = &req->poll;
1871 struct poll_table_struct pt = { ._key = poll->events };
1872 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07001873 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001874 __poll_t mask = 0;
1875
Jens Axboe561fb042019-10-24 07:25:42 -06001876 if (work->flags & IO_WQ_WORK_CANCEL)
1877 WRITE_ONCE(poll->canceled, true);
1878
Jens Axboe221c5eb2019-01-17 09:41:58 -07001879 if (!READ_ONCE(poll->canceled))
1880 mask = vfs_poll(poll->file, &pt) & poll->events;
1881
1882 /*
1883 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1884 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1885 * synchronize with them. In the cancellation case the list_del_init
1886 * itself is not actually needed, but harmless so we keep it in to
1887 * avoid further branches in the fast path.
1888 */
1889 spin_lock_irq(&ctx->completion_lock);
1890 if (!mask && !READ_ONCE(poll->canceled)) {
1891 add_wait_queue(poll->head, &poll->wait);
1892 spin_unlock_irq(&ctx->completion_lock);
1893 return;
1894 }
1895 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001896 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001897 spin_unlock_irq(&ctx->completion_lock);
1898
Jens Axboe8c838782019-03-12 15:48:16 -06001899 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001900
1901 io_put_req(req, &nxt);
1902 if (nxt)
1903 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001904}
1905
1906static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1907 void *key)
1908{
1909 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1910 wait);
1911 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1912 struct io_ring_ctx *ctx = req->ctx;
1913 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001914 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001915
1916 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001917 if (mask && !(mask & poll->events))
1918 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001919
1920 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001921
1922 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1923 list_del(&req->list);
1924 io_poll_complete(ctx, req, mask);
1925 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1926
1927 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001928 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001929 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001930 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001931 }
1932
Jens Axboe221c5eb2019-01-17 09:41:58 -07001933 return 1;
1934}
1935
1936struct io_poll_table {
1937 struct poll_table_struct pt;
1938 struct io_kiocb *req;
1939 int error;
1940};
1941
1942static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1943 struct poll_table_struct *p)
1944{
1945 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1946
1947 if (unlikely(pt->req->poll.head)) {
1948 pt->error = -EINVAL;
1949 return;
1950 }
1951
1952 pt->error = 0;
1953 pt->req->poll.head = head;
1954 add_wait_queue(head, &pt->req->poll.wait);
1955}
1956
Jens Axboe89723d02019-11-05 15:32:58 -07001957static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1958 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001959{
1960 struct io_poll_iocb *poll = &req->poll;
1961 struct io_ring_ctx *ctx = req->ctx;
1962 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001963 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001964 __poll_t mask;
1965 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001966
1967 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1968 return -EINVAL;
1969 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1970 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001971 if (!poll->file)
1972 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001973
Jens Axboe6cc47d12019-09-18 11:18:23 -06001974 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06001975 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001976 events = READ_ONCE(sqe->poll_events);
1977 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1978
Jens Axboe221c5eb2019-01-17 09:41:58 -07001979 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001980 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001981 poll->canceled = false;
1982
1983 ipt.pt._qproc = io_poll_queue_proc;
1984 ipt.pt._key = poll->events;
1985 ipt.req = req;
1986 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1987
1988 /* initialized the list so that we can do list_empty checks */
1989 INIT_LIST_HEAD(&poll->wait.entry);
1990 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1991
Jens Axboe36703242019-07-25 10:20:18 -06001992 INIT_LIST_HEAD(&req->list);
1993
Jens Axboe221c5eb2019-01-17 09:41:58 -07001994 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001995
1996 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001997 if (likely(poll->head)) {
1998 spin_lock(&poll->head->lock);
1999 if (unlikely(list_empty(&poll->wait.entry))) {
2000 if (ipt.error)
2001 cancel = true;
2002 ipt.error = 0;
2003 mask = 0;
2004 }
2005 if (mask || ipt.error)
2006 list_del_init(&poll->wait.entry);
2007 else if (cancel)
2008 WRITE_ONCE(poll->canceled, true);
2009 else if (!poll->done) /* actually waiting for an event */
2010 list_add_tail(&req->list, &ctx->cancel_list);
2011 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002012 }
Jens Axboe8c838782019-03-12 15:48:16 -06002013 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002014 ipt.error = 0;
2015 io_poll_complete(ctx, req, mask);
2016 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002017 spin_unlock_irq(&ctx->completion_lock);
2018
Jens Axboe8c838782019-03-12 15:48:16 -06002019 if (mask) {
2020 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002021 io_put_req(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002022 }
Jens Axboe8c838782019-03-12 15:48:16 -06002023 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002024}
2025
Jens Axboe5262f562019-09-17 12:26:57 -06002026static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2027{
2028 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002029 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002030 unsigned long flags;
2031
2032 req = container_of(timer, struct io_kiocb, timeout.timer);
2033 ctx = req->ctx;
2034 atomic_inc(&ctx->cq_timeouts);
2035
2036 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002037 /*
Jens Axboe11365042019-10-16 09:08:32 -06002038 * We could be racing with timeout deletion. If the list is empty,
2039 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002040 */
Jens Axboe842f9612019-10-29 12:34:10 -06002041 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002042 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002043
Jens Axboe11365042019-10-16 09:08:32 -06002044 /*
2045 * Adjust the reqs sequence before the current one because it
2046 * will consume a slot in the cq_ring and the the cq_tail
2047 * pointer will be increased, otherwise other timeout reqs may
2048 * return in advance without waiting for enough wait_nr.
2049 */
2050 prev = req;
2051 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2052 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002053 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002054 }
Jens Axboe842f9612019-10-29 12:34:10 -06002055
2056 io_cqring_fill_event(ctx, req->user_data, -ETIME);
2057 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002058 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2059
Jens Axboe842f9612019-10-29 12:34:10 -06002060 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002061 if (req->flags & REQ_F_LINK)
2062 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe842f9612019-10-29 12:34:10 -06002063 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002064 return HRTIMER_NORESTART;
2065}
2066
2067/*
2068 * Remove or update an existing timeout command
2069 */
2070static int io_timeout_remove(struct io_kiocb *req,
2071 const struct io_uring_sqe *sqe)
2072{
2073 struct io_ring_ctx *ctx = req->ctx;
2074 struct io_kiocb *treq;
2075 int ret = -ENOENT;
2076 __u64 user_data;
2077 unsigned flags;
2078
2079 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2080 return -EINVAL;
2081 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2082 return -EINVAL;
2083 flags = READ_ONCE(sqe->timeout_flags);
2084 if (flags)
2085 return -EINVAL;
2086
2087 user_data = READ_ONCE(sqe->addr);
2088 spin_lock_irq(&ctx->completion_lock);
2089 list_for_each_entry(treq, &ctx->timeout_list, list) {
2090 if (user_data == treq->user_data) {
2091 list_del_init(&treq->list);
2092 ret = 0;
2093 break;
2094 }
2095 }
2096
2097 /* didn't find timeout */
2098 if (ret) {
2099fill_ev:
2100 io_cqring_fill_event(ctx, req->user_data, ret);
2101 io_commit_cqring(ctx);
2102 spin_unlock_irq(&ctx->completion_lock);
2103 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002104 if (req->flags & REQ_F_LINK)
2105 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe11365042019-10-16 09:08:32 -06002106 io_put_req(req, NULL);
2107 return 0;
2108 }
2109
2110 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2111 if (ret == -1) {
2112 ret = -EBUSY;
2113 goto fill_ev;
2114 }
2115
2116 io_cqring_fill_event(ctx, req->user_data, 0);
2117 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2118 io_commit_cqring(ctx);
2119 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002120 io_cqring_ev_posted(ctx);
2121
Jens Axboe11365042019-10-16 09:08:32 -06002122 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002123 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002124 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002125}
2126
2127static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2128{
yangerkun5da0fb12019-10-15 21:59:29 +08002129 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002130 struct io_ring_ctx *ctx = req->ctx;
2131 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002132 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002133 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002134 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002135 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002136
2137 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2138 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002139 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2140 return -EINVAL;
2141 flags = READ_ONCE(sqe->timeout_flags);
2142 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002143 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002144
2145 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002146 return -EFAULT;
2147
Jens Axboe11365042019-10-16 09:08:32 -06002148 if (flags & IORING_TIMEOUT_ABS)
2149 mode = HRTIMER_MODE_ABS;
2150 else
2151 mode = HRTIMER_MODE_REL;
2152
2153 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2154
Jens Axboe5262f562019-09-17 12:26:57 -06002155 /*
2156 * sqe->off holds how many events that need to occur for this
2157 * timeout event to be satisfied.
2158 */
2159 count = READ_ONCE(sqe->off);
2160 if (!count)
2161 count = 1;
2162
2163 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002164 /* reuse it to store the count */
2165 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002166 req->flags |= REQ_F_TIMEOUT;
2167
2168 /*
2169 * Insertion sort, ensuring the first entry in the list is always
2170 * the one we need first.
2171 */
Jens Axboe5262f562019-09-17 12:26:57 -06002172 spin_lock_irq(&ctx->completion_lock);
2173 list_for_each_prev(entry, &ctx->timeout_list) {
2174 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002175 unsigned nxt_sq_head;
2176 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002177
yangerkun5da0fb12019-10-15 21:59:29 +08002178 /*
2179 * Since cached_sq_head + count - 1 can overflow, use type long
2180 * long to store it.
2181 */
2182 tmp = (long long)ctx->cached_sq_head + count - 1;
2183 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2184 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2185
2186 /*
2187 * cached_sq_head may overflow, and it will never overflow twice
2188 * once there is some timeout req still be valid.
2189 */
2190 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002191 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002192
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002193 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002194 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002195
2196 /*
2197 * Sequence of reqs after the insert one and itself should
2198 * be adjusted because each timeout req consumes a slot.
2199 */
2200 span++;
2201 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002202 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002203 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002204 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002205 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002206 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002207 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002208 return 0;
2209}
2210
Jens Axboe62755e32019-10-28 21:49:21 -06002211static bool io_cancel_cb(struct io_wq_work *work, void *data)
2212{
2213 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2214
2215 return req->user_data == (unsigned long) data;
2216}
2217
Jens Axboee977d6d2019-11-05 12:39:45 -07002218static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002219{
Jens Axboe62755e32019-10-28 21:49:21 -06002220 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002221 int ret = 0;
2222
Jens Axboe62755e32019-10-28 21:49:21 -06002223 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2224 switch (cancel_ret) {
2225 case IO_WQ_CANCEL_OK:
2226 ret = 0;
2227 break;
2228 case IO_WQ_CANCEL_RUNNING:
2229 ret = -EALREADY;
2230 break;
2231 case IO_WQ_CANCEL_NOTFOUND:
2232 ret = -ENOENT;
2233 break;
2234 }
2235
Jens Axboee977d6d2019-11-05 12:39:45 -07002236 return ret;
2237}
2238
2239static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2240 struct io_kiocb **nxt)
2241{
2242 struct io_ring_ctx *ctx = req->ctx;
2243 void *sqe_addr;
2244 int ret;
2245
2246 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2247 return -EINVAL;
2248 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2249 sqe->cancel_flags)
2250 return -EINVAL;
2251
2252 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2253 ret = io_async_cancel_one(ctx, sqe_addr);
2254
Jens Axboe62755e32019-10-28 21:49:21 -06002255 if (ret < 0 && (req->flags & REQ_F_LINK))
2256 req->flags |= REQ_F_FAIL_LINK;
2257 io_cqring_add_event(req->ctx, sqe->user_data, ret);
2258 io_put_req(req, nxt);
2259 return 0;
2260}
2261
Pavel Begunkov267bc902019-11-07 01:41:08 +03002262static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002263{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002264 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002265 struct io_uring_sqe *sqe_copy;
2266
2267 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2268 return 0;
2269
2270 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2271 if (!sqe_copy)
2272 return -EAGAIN;
2273
2274 spin_lock_irq(&ctx->completion_lock);
2275 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2276 spin_unlock_irq(&ctx->completion_lock);
2277 kfree(sqe_copy);
2278 return 0;
2279 }
2280
2281 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2282 req->submit.sqe = sqe_copy;
2283
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002284 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002285 list_add_tail(&req->list, &ctx->defer_list);
2286 spin_unlock_irq(&ctx->completion_lock);
2287 return -EIOCBQUEUED;
2288}
2289
Jens Axboe2b188cc2019-01-07 10:46:33 -07002290static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002291 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292{
Jens Axboee0c5c572019-03-12 10:18:47 -06002293 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002294 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002295
Jens Axboe9e645e112019-05-10 16:07:28 -06002296 req->user_data = READ_ONCE(s->sqe->user_data);
2297
Jens Axboe2b188cc2019-01-07 10:46:33 -07002298 opcode = READ_ONCE(s->sqe->opcode);
2299 switch (opcode) {
2300 case IORING_OP_NOP:
2301 ret = io_nop(req, req->user_data);
2302 break;
2303 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002304 if (unlikely(s->sqe->buf_index))
2305 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002306 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307 break;
2308 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002309 if (unlikely(s->sqe->buf_index))
2310 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002311 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002312 break;
2313 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002314 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002315 break;
2316 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002317 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002318 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002319 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002320 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002321 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002322 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002323 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002324 break;
2325 case IORING_OP_POLL_REMOVE:
2326 ret = io_poll_remove(req, s->sqe);
2327 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002328 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002329 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002330 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002331 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002332 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002333 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002334 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002335 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002336 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002337 case IORING_OP_TIMEOUT:
2338 ret = io_timeout(req, s->sqe);
2339 break;
Jens Axboe11365042019-10-16 09:08:32 -06002340 case IORING_OP_TIMEOUT_REMOVE:
2341 ret = io_timeout_remove(req, s->sqe);
2342 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002343 case IORING_OP_ACCEPT:
2344 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2345 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002346 case IORING_OP_ASYNC_CANCEL:
2347 ret = io_async_cancel(req, s->sqe, nxt);
2348 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002349 default:
2350 ret = -EINVAL;
2351 break;
2352 }
2353
Jens Axboedef596e2019-01-09 08:59:42 -07002354 if (ret)
2355 return ret;
2356
2357 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002358 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002359 return -EAGAIN;
2360
2361 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002362 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002363 mutex_lock(&ctx->uring_lock);
2364 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002365 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002366 mutex_unlock(&ctx->uring_lock);
2367 }
2368
2369 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002370}
2371
Jens Axboe561fb042019-10-24 07:25:42 -06002372static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002373{
Jens Axboe561fb042019-10-24 07:25:42 -06002374 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002375 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002376 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002377 struct sqe_submit *s = &req->submit;
2378 const struct io_uring_sqe *sqe = s->sqe;
2379 struct io_kiocb *nxt = NULL;
2380 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002381
Jens Axboe561fb042019-10-24 07:25:42 -06002382 /* Ensure we clear previously set non-block flag */
2383 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002384
Jens Axboe561fb042019-10-24 07:25:42 -06002385 if (work->flags & IO_WQ_WORK_CANCEL)
2386 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002387
Jens Axboe561fb042019-10-24 07:25:42 -06002388 if (!ret) {
2389 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2390 s->in_async = true;
2391 do {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002392 ret = __io_submit_sqe(ctx, req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002393 /*
2394 * We can get EAGAIN for polled IO even though we're
2395 * forcing a sync submission from here, since we can't
2396 * wait for request slots on the block side.
2397 */
2398 if (ret != -EAGAIN)
2399 break;
2400 cond_resched();
2401 } while (1);
2402 }
Jens Axboe31b51512019-01-18 22:56:34 -07002403
Jens Axboe561fb042019-10-24 07:25:42 -06002404 /* drop submission reference */
2405 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002406
Jens Axboe561fb042019-10-24 07:25:42 -06002407 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002408 if (req->flags & REQ_F_LINK)
2409 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe561fb042019-10-24 07:25:42 -06002410 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002411 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002412 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002413
Jens Axboe561fb042019-10-24 07:25:42 -06002414 /* async context always use a copy of the sqe */
2415 kfree(sqe);
2416
2417 /* if a dependent link is ready, pass it back */
2418 if (!ret && nxt) {
2419 io_prep_async_work(nxt);
2420 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002421 }
Jens Axboe31b51512019-01-18 22:56:34 -07002422}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002423
Jens Axboe09bb8392019-03-13 12:39:28 -06002424static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2425{
2426 int op = READ_ONCE(sqe->opcode);
2427
2428 switch (op) {
2429 case IORING_OP_NOP:
2430 case IORING_OP_POLL_REMOVE:
2431 return false;
2432 default:
2433 return true;
2434 }
2435}
2436
Jens Axboe65e19f52019-10-26 07:20:21 -06002437static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2438 int index)
2439{
2440 struct fixed_file_table *table;
2441
2442 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2443 return table->files[index & IORING_FILE_TABLE_MASK];
2444}
2445
Pavel Begunkov267bc902019-11-07 01:41:08 +03002446static int io_req_set_file(struct io_ring_ctx *ctx,
Jens Axboe09bb8392019-03-13 12:39:28 -06002447 struct io_submit_state *state, struct io_kiocb *req)
2448{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002449 struct sqe_submit *s = &req->submit;
Jens Axboe09bb8392019-03-13 12:39:28 -06002450 unsigned flags;
2451 int fd;
2452
2453 flags = READ_ONCE(s->sqe->flags);
2454 fd = READ_ONCE(s->sqe->fd);
2455
Jackie Liu4fe2c962019-09-09 20:50:40 +08002456 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002457 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002458 /*
2459 * All io need record the previous position, if LINK vs DARIN,
2460 * it can be used to mark the position of the first IO in the
2461 * link list.
2462 */
2463 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002464
Jens Axboe60c112b2019-06-21 10:20:18 -06002465 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002466 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002467
2468 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002469 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002470 (unsigned) fd >= ctx->nr_user_files))
2471 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002472 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002473 req->file = io_file_from_index(ctx, fd);
2474 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002475 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002476 req->flags |= REQ_F_FIXED_FILE;
2477 } else {
2478 if (s->needs_fixed_file)
2479 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002480 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002481 req->file = io_file_get(state, fd);
2482 if (unlikely(!req->file))
2483 return -EBADF;
2484 }
2485
2486 return 0;
2487}
2488
Jens Axboefcb323c2019-10-24 12:39:47 -06002489static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2490{
2491 int ret = -EBADF;
2492
2493 rcu_read_lock();
2494 spin_lock_irq(&ctx->inflight_lock);
2495 /*
2496 * We use the f_ops->flush() handler to ensure that we can flush
2497 * out work accessing these files if the fd is closed. Check if
2498 * the fd has changed since we started down this path, and disallow
2499 * this operation if it has.
2500 */
2501 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2502 list_add(&req->inflight_entry, &ctx->inflight_list);
2503 req->flags |= REQ_F_INFLIGHT;
2504 req->work.files = current->files;
2505 ret = 0;
2506 }
2507 spin_unlock_irq(&ctx->inflight_lock);
2508 rcu_read_unlock();
2509
2510 return ret;
2511}
2512
Jens Axboe2665abf2019-11-05 12:40:47 -07002513static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2514{
2515 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2516 timeout.timer);
2517 struct io_ring_ctx *ctx = req->ctx;
2518 struct io_kiocb *prev = NULL;
2519 unsigned long flags;
2520 int ret = -ETIME;
2521
2522 spin_lock_irqsave(&ctx->completion_lock, flags);
2523
2524 /*
2525 * We don't expect the list to be empty, that will only happen if we
2526 * race with the completion of the linked work.
2527 */
2528 if (!list_empty(&req->list)) {
2529 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
2530 list_del_init(&req->list);
2531 }
2532
2533 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2534
2535 if (prev) {
2536 void *user_data = (void *) (unsigned long) prev->user_data;
2537 ret = io_async_cancel_one(ctx, user_data);
2538 }
2539
2540 io_cqring_add_event(ctx, req->user_data, ret);
2541 io_put_req(req, NULL);
2542 return HRTIMER_NORESTART;
2543}
2544
2545static int io_queue_linked_timeout(struct io_kiocb *req, struct io_kiocb *nxt)
2546{
2547 const struct io_uring_sqe *sqe = nxt->submit.sqe;
2548 enum hrtimer_mode mode;
2549 struct timespec64 ts;
2550 int ret = -EINVAL;
2551
2552 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2553 goto err;
2554 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2555 goto err;
2556 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) {
2557 ret = -EFAULT;
2558 goto err;
2559 }
2560
2561 req->flags |= REQ_F_LINK_TIMEOUT;
2562
2563 if (sqe->timeout_flags & IORING_TIMEOUT_ABS)
2564 mode = HRTIMER_MODE_ABS;
2565 else
2566 mode = HRTIMER_MODE_REL;
2567 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, mode);
2568 nxt->timeout.timer.function = io_link_timeout_fn;
2569 hrtimer_start(&nxt->timeout.timer, timespec64_to_ktime(ts), mode);
2570 ret = 0;
2571err:
2572 /* drop submission reference */
2573 io_put_req(nxt, NULL);
2574
2575 if (ret) {
2576 struct io_ring_ctx *ctx = req->ctx;
2577
2578 /*
2579 * Break the link and fail linked timeout, parent will get
2580 * failed by the regular submission path.
2581 */
2582 list_del(&nxt->list);
2583 io_cqring_fill_event(ctx, nxt->user_data, ret);
2584 trace_io_uring_fail_link(req, nxt);
2585 io_commit_cqring(ctx);
2586 io_put_req(nxt, NULL);
2587 ret = -ECANCELED;
2588 }
2589
2590 return ret;
2591}
2592
2593static inline struct io_kiocb *io_get_linked_timeout(struct io_kiocb *req)
2594{
2595 struct io_kiocb *nxt;
2596
2597 if (!(req->flags & REQ_F_LINK))
2598 return NULL;
2599
2600 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
2601 if (nxt && nxt->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT)
2602 return nxt;
2603
2604 return NULL;
2605}
2606
Pavel Begunkov267bc902019-11-07 01:41:08 +03002607static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002608{
Jens Axboe2665abf2019-11-05 12:40:47 -07002609 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002610 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002611
Jens Axboe2665abf2019-11-05 12:40:47 -07002612 nxt = io_get_linked_timeout(req);
2613 if (unlikely(nxt)) {
2614 ret = io_queue_linked_timeout(req, nxt);
2615 if (ret)
2616 goto err;
2617 }
2618
Pavel Begunkov267bc902019-11-07 01:41:08 +03002619 ret = __io_submit_sqe(ctx, req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002620
2621 /*
2622 * We async punt it if the file wasn't marked NOWAIT, or if the file
2623 * doesn't support non-blocking read/write attempts
2624 */
2625 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2626 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002627 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002628 struct io_uring_sqe *sqe_copy;
2629
Jackie Liu954dab12019-09-18 10:37:52 +08002630 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002632 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002633 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2634 ret = io_grab_files(ctx, req);
2635 if (ret) {
2636 kfree(sqe_copy);
2637 goto err;
2638 }
2639 }
Jens Axboee65ef562019-03-12 10:16:44 -06002640
2641 /*
2642 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002643 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002644 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002645 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002646 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002647 }
2648 }
Jens Axboee65ef562019-03-12 10:16:44 -06002649
2650 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002651err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002652 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002653
2654 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002655 if (ret) {
2656 io_cqring_add_event(ctx, req->user_data, ret);
2657 if (req->flags & REQ_F_LINK)
2658 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002659 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002660 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002661
2662 return ret;
2663}
2664
Pavel Begunkov267bc902019-11-07 01:41:08 +03002665static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002666{
2667 int ret;
2668
Pavel Begunkov267bc902019-11-07 01:41:08 +03002669 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002670 if (ret) {
2671 if (ret != -EIOCBQUEUED) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002672 io_cqring_add_event(ctx, req->submit.sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002673 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002674 }
2675 return 0;
2676 }
2677
Pavel Begunkov267bc902019-11-07 01:41:08 +03002678 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002679}
2680
2681static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002682 struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002683{
2684 int ret;
2685 int need_submit = false;
2686
2687 if (!shadow)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002688 return io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002689
2690 /*
2691 * Mark the first IO in link list as DRAIN, let all the following
2692 * IOs enter the defer list. all IO needs to be completed before link
2693 * list.
2694 */
2695 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002696 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002697 if (ret) {
2698 if (ret != -EIOCBQUEUED) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002699 io_cqring_add_event(ctx, req->submit.sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002700 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002701 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002702 return 0;
2703 }
2704 } else {
2705 /*
2706 * If ret == 0 means that all IOs in front of link io are
2707 * running done. let's queue link head.
2708 */
2709 need_submit = true;
2710 }
2711
2712 /* Insert shadow req to defer_list, blocking next IOs */
2713 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002714 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002715 list_add_tail(&shadow->list, &ctx->defer_list);
2716 spin_unlock_irq(&ctx->completion_lock);
2717
2718 if (need_submit)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002719 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002720
2721 return 0;
2722}
2723
Jens Axboe9e645e112019-05-10 16:07:28 -06002724#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2725
Pavel Begunkov196be952019-11-07 01:41:06 +03002726static void io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002727 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002728{
2729 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002730 struct sqe_submit *s = &req->submit;
Jens Axboe9e645e112019-05-10 16:07:28 -06002731 int ret;
2732
2733 /* enforce forwards compatibility on users */
2734 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2735 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002736 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002737 }
2738
Pavel Begunkov267bc902019-11-07 01:41:08 +03002739 ret = io_req_set_file(ctx, state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002740 if (unlikely(ret)) {
2741err_req:
Jens Axboe9e645e112019-05-10 16:07:28 -06002742 io_cqring_add_event(ctx, s->sqe->user_data, ret);
Pavel Begunkov267bc902019-11-07 01:41:08 +03002743 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002744 return;
2745 }
2746
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002747 req->user_data = s->sqe->user_data;
2748
Jens Axboe9e645e112019-05-10 16:07:28 -06002749 /*
2750 * If we already have a head request, queue this one for async
2751 * submittal once the head completes. If we don't have a head but
2752 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2753 * submitted sync once the chain is complete. If none of those
2754 * conditions are true (normal request), then just queue it.
2755 */
2756 if (*link) {
2757 struct io_kiocb *prev = *link;
2758
2759 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2760 if (!sqe_copy) {
2761 ret = -EAGAIN;
2762 goto err_req;
2763 }
2764
2765 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002766 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002767 list_add_tail(&req->list, &prev->link_list);
2768 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2769 req->flags |= REQ_F_LINK;
2770
Jens Axboe9e645e112019-05-10 16:07:28 -06002771 INIT_LIST_HEAD(&req->link_list);
2772 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002773 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2774 /* Only valid as a linked SQE */
2775 ret = -EINVAL;
2776 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002777 } else {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002778 io_queue_sqe(ctx, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002779 }
2780}
2781
Jens Axboe9a56a232019-01-09 09:06:50 -07002782/*
2783 * Batched submission is done, ensure local IO is flushed out.
2784 */
2785static void io_submit_state_end(struct io_submit_state *state)
2786{
2787 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002788 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002789 if (state->free_reqs)
2790 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2791 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002792}
2793
2794/*
2795 * Start submission side cache.
2796 */
2797static void io_submit_state_start(struct io_submit_state *state,
2798 struct io_ring_ctx *ctx, unsigned max_ios)
2799{
2800 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002801 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002802 state->file = NULL;
2803 state->ios_left = max_ios;
2804}
2805
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806static void io_commit_sqring(struct io_ring_ctx *ctx)
2807{
Hristo Venev75b28af2019-08-26 17:23:46 +00002808 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002809
Hristo Venev75b28af2019-08-26 17:23:46 +00002810 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002811 /*
2812 * Ensure any loads from the SQEs are done at this point,
2813 * since once we write the new head, the application could
2814 * write new data to them.
2815 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002816 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002817 }
2818}
2819
2820/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002821 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2822 * that is mapped by userspace. This means that care needs to be taken to
2823 * ensure that reads are stable, as we cannot rely on userspace always
2824 * being a good citizen. If members of the sqe are validated and then later
2825 * used, it's important that those reads are done through READ_ONCE() to
2826 * prevent a re-load down the line.
2827 */
2828static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2829{
Hristo Venev75b28af2019-08-26 17:23:46 +00002830 struct io_rings *rings = ctx->rings;
2831 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832 unsigned head;
2833
2834 /*
2835 * The cached sq head (or cq tail) serves two purposes:
2836 *
2837 * 1) allows us to batch the cost of updating the user visible
2838 * head updates.
2839 * 2) allows the kernel side to track the head on its own, even
2840 * though the application is the one updating it.
2841 */
2842 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002843 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002844 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002845 return false;
2846
Hristo Venev75b28af2019-08-26 17:23:46 +00002847 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002849 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002851 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852 ctx->cached_sq_head++;
2853 return true;
2854 }
2855
2856 /* drop invalid entries */
2857 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002858 ctx->cached_sq_dropped++;
2859 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002860 return false;
2861}
2862
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002863static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002864 struct file *ring_file, int ring_fd,
2865 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002866{
2867 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002868 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002869 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002870 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002871 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002872
2873 if (nr > IO_PLUG_THRESHOLD) {
2874 io_submit_state_start(&state, ctx, nr);
2875 statep = &state;
2876 }
2877
2878 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002879 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03002880 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002881
Pavel Begunkov196be952019-11-07 01:41:06 +03002882 req = io_get_req(ctx, statep);
2883 if (unlikely(!req)) {
2884 if (!submitted)
2885 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002886 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03002887 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002888 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002889 __io_free_req(req);
2890 break;
2891 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002892
Pavel Begunkov50585b92019-11-07 01:41:07 +03002893 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002894 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2895 if (!mm_fault) {
2896 use_mm(ctx->sqo_mm);
2897 *mm = ctx->sqo_mm;
2898 }
2899 }
2900
Pavel Begunkov50585b92019-11-07 01:41:07 +03002901 sqe_flags = req->submit.sqe->flags;
2902
2903 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002904 if (!shadow_req) {
2905 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002906 if (unlikely(!shadow_req))
2907 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002908 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2909 refcount_dec(&shadow_req->refs);
2910 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002911 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002912 }
2913
Jackie Liua1041c22019-09-18 17:25:52 +08002914out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03002915 req->submit.ring_file = ring_file;
2916 req->submit.ring_fd = ring_fd;
2917 req->submit.has_user = *mm != NULL;
2918 req->submit.in_async = async;
2919 req->submit.needs_fixed_file = async;
2920 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
2921 true, async);
Pavel Begunkov267bc902019-11-07 01:41:08 +03002922 io_submit_sqe(ctx, req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002923 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03002924
2925 /*
2926 * If previous wasn't linked and we have a linked command,
2927 * that's the end of the chain. Submit the previous link.
2928 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03002929 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002930 io_queue_link_head(ctx, link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03002931 link = NULL;
2932 shadow_req = NULL;
2933 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002934 }
2935
Jens Axboe9e645e112019-05-10 16:07:28 -06002936 if (link)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002937 io_queue_link_head(ctx, link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002938 if (statep)
2939 io_submit_state_end(&state);
2940
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002941 /* Commit SQ ring head once we've consumed and submitted all SQEs */
2942 io_commit_sqring(ctx);
2943
Jens Axboe6c271ce2019-01-10 11:22:30 -07002944 return submitted;
2945}
2946
2947static int io_sq_thread(void *data)
2948{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002949 struct io_ring_ctx *ctx = data;
2950 struct mm_struct *cur_mm = NULL;
2951 mm_segment_t old_fs;
2952 DEFINE_WAIT(wait);
2953 unsigned inflight;
2954 unsigned long timeout;
2955
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002956 complete(&ctx->sqo_thread_started);
2957
Jens Axboe6c271ce2019-01-10 11:22:30 -07002958 old_fs = get_fs();
2959 set_fs(USER_DS);
2960
2961 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002962 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002963 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002964
2965 if (inflight) {
2966 unsigned nr_events = 0;
2967
2968 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002969 /*
2970 * inflight is the count of the maximum possible
2971 * entries we submitted, but it can be smaller
2972 * if we dropped some of them. If we don't have
2973 * poll entries available, then we know that we
2974 * have nothing left to poll for. Reset the
2975 * inflight count to zero in that case.
2976 */
2977 mutex_lock(&ctx->uring_lock);
2978 if (!list_empty(&ctx->poll_list))
2979 __io_iopoll_check(ctx, &nr_events, 0);
2980 else
2981 inflight = 0;
2982 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002983 } else {
2984 /*
2985 * Normal IO, just pretend everything completed.
2986 * We don't have to poll completions for that.
2987 */
2988 nr_events = inflight;
2989 }
2990
2991 inflight -= nr_events;
2992 if (!inflight)
2993 timeout = jiffies + ctx->sq_thread_idle;
2994 }
2995
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002996 to_submit = io_sqring_entries(ctx);
2997 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002998 /*
2999 * We're polling. If we're within the defined idle
3000 * period, then let us spin without work before going
3001 * to sleep.
3002 */
3003 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003004 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003005 continue;
3006 }
3007
3008 /*
3009 * Drop cur_mm before scheduling, we can't hold it for
3010 * long periods (or over schedule()). Do this before
3011 * adding ourselves to the waitqueue, as the unuse/drop
3012 * may sleep.
3013 */
3014 if (cur_mm) {
3015 unuse_mm(cur_mm);
3016 mmput(cur_mm);
3017 cur_mm = NULL;
3018 }
3019
3020 prepare_to_wait(&ctx->sqo_wait, &wait,
3021 TASK_INTERRUPTIBLE);
3022
3023 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003024 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003025 /* make sure to read SQ tail after writing flags */
3026 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003027
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003028 to_submit = io_sqring_entries(ctx);
3029 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003030 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003031 finish_wait(&ctx->sqo_wait, &wait);
3032 break;
3033 }
3034 if (signal_pending(current))
3035 flush_signals(current);
3036 schedule();
3037 finish_wait(&ctx->sqo_wait, &wait);
3038
Hristo Venev75b28af2019-08-26 17:23:46 +00003039 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003040 continue;
3041 }
3042 finish_wait(&ctx->sqo_wait, &wait);
3043
Hristo Venev75b28af2019-08-26 17:23:46 +00003044 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003045 }
3046
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003047 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003048 inflight += io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm,
3049 true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003050 }
3051
3052 set_fs(old_fs);
3053 if (cur_mm) {
3054 unuse_mm(cur_mm);
3055 mmput(cur_mm);
3056 }
Jens Axboe06058632019-04-13 09:26:03 -06003057
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003058 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003059
Jens Axboe6c271ce2019-01-10 11:22:30 -07003060 return 0;
3061}
3062
Jens Axboebda52162019-09-24 13:47:15 -06003063struct io_wait_queue {
3064 struct wait_queue_entry wq;
3065 struct io_ring_ctx *ctx;
3066 unsigned to_wait;
3067 unsigned nr_timeouts;
3068};
3069
3070static inline bool io_should_wake(struct io_wait_queue *iowq)
3071{
3072 struct io_ring_ctx *ctx = iowq->ctx;
3073
3074 /*
3075 * Wake up if we have enough events, or if a timeout occured since we
3076 * started waiting. For timeouts, we always want to return to userspace,
3077 * regardless of event count.
3078 */
3079 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
3080 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3081}
3082
3083static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3084 int wake_flags, void *key)
3085{
3086 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3087 wq);
3088
3089 if (!io_should_wake(iowq))
3090 return -1;
3091
3092 return autoremove_wake_function(curr, mode, wake_flags, key);
3093}
3094
Jens Axboe2b188cc2019-01-07 10:46:33 -07003095/*
3096 * Wait until events become available, if we don't already have some. The
3097 * application must reap them itself, as they reside on the shared cq ring.
3098 */
3099static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3100 const sigset_t __user *sig, size_t sigsz)
3101{
Jens Axboebda52162019-09-24 13:47:15 -06003102 struct io_wait_queue iowq = {
3103 .wq = {
3104 .private = current,
3105 .func = io_wake_function,
3106 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3107 },
3108 .ctx = ctx,
3109 .to_wait = min_events,
3110 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003111 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003112 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003113
Hristo Venev75b28af2019-08-26 17:23:46 +00003114 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003115 return 0;
3116
3117 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003118#ifdef CONFIG_COMPAT
3119 if (in_compat_syscall())
3120 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003121 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003122 else
3123#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003124 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003125
Jens Axboe2b188cc2019-01-07 10:46:33 -07003126 if (ret)
3127 return ret;
3128 }
3129
Jens Axboebda52162019-09-24 13:47:15 -06003130 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003131 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003132 do {
3133 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3134 TASK_INTERRUPTIBLE);
3135 if (io_should_wake(&iowq))
3136 break;
3137 schedule();
3138 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003139 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003140 break;
3141 }
3142 } while (1);
3143 finish_wait(&ctx->wait, &iowq.wq);
3144
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003145 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003146
Hristo Venev75b28af2019-08-26 17:23:46 +00003147 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148}
3149
Jens Axboe6b063142019-01-10 22:13:58 -07003150static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3151{
3152#if defined(CONFIG_UNIX)
3153 if (ctx->ring_sock) {
3154 struct sock *sock = ctx->ring_sock->sk;
3155 struct sk_buff *skb;
3156
3157 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3158 kfree_skb(skb);
3159 }
3160#else
3161 int i;
3162
Jens Axboe65e19f52019-10-26 07:20:21 -06003163 for (i = 0; i < ctx->nr_user_files; i++) {
3164 struct file *file;
3165
3166 file = io_file_from_index(ctx, i);
3167 if (file)
3168 fput(file);
3169 }
Jens Axboe6b063142019-01-10 22:13:58 -07003170#endif
3171}
3172
3173static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3174{
Jens Axboe65e19f52019-10-26 07:20:21 -06003175 unsigned nr_tables, i;
3176
3177 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003178 return -ENXIO;
3179
3180 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003181 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3182 for (i = 0; i < nr_tables; i++)
3183 kfree(ctx->file_table[i].files);
3184 kfree(ctx->file_table);
3185 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003186 ctx->nr_user_files = 0;
3187 return 0;
3188}
3189
Jens Axboe6c271ce2019-01-10 11:22:30 -07003190static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3191{
3192 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003193 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003194 /*
3195 * The park is a bit of a work-around, without it we get
3196 * warning spews on shutdown with SQPOLL set and affinity
3197 * set to a single CPU.
3198 */
Jens Axboe06058632019-04-13 09:26:03 -06003199 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003200 kthread_stop(ctx->sqo_thread);
3201 ctx->sqo_thread = NULL;
3202 }
3203}
3204
Jens Axboe6b063142019-01-10 22:13:58 -07003205static void io_finish_async(struct io_ring_ctx *ctx)
3206{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003207 io_sq_thread_stop(ctx);
3208
Jens Axboe561fb042019-10-24 07:25:42 -06003209 if (ctx->io_wq) {
3210 io_wq_destroy(ctx->io_wq);
3211 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003212 }
3213}
3214
3215#if defined(CONFIG_UNIX)
3216static void io_destruct_skb(struct sk_buff *skb)
3217{
3218 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3219
Jens Axboe561fb042019-10-24 07:25:42 -06003220 if (ctx->io_wq)
3221 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003222
Jens Axboe6b063142019-01-10 22:13:58 -07003223 unix_destruct_scm(skb);
3224}
3225
3226/*
3227 * Ensure the UNIX gc is aware of our file set, so we are certain that
3228 * the io_uring can be safely unregistered on process exit, even if we have
3229 * loops in the file referencing.
3230 */
3231static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3232{
3233 struct sock *sk = ctx->ring_sock->sk;
3234 struct scm_fp_list *fpl;
3235 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003236 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003237
3238 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3239 unsigned long inflight = ctx->user->unix_inflight + nr;
3240
3241 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3242 return -EMFILE;
3243 }
3244
3245 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3246 if (!fpl)
3247 return -ENOMEM;
3248
3249 skb = alloc_skb(0, GFP_KERNEL);
3250 if (!skb) {
3251 kfree(fpl);
3252 return -ENOMEM;
3253 }
3254
3255 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003256
Jens Axboe08a45172019-10-03 08:11:03 -06003257 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003258 fpl->user = get_uid(ctx->user);
3259 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003260 struct file *file = io_file_from_index(ctx, i + offset);
3261
3262 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003263 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003264 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003265 unix_inflight(fpl->user, fpl->fp[nr_files]);
3266 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003267 }
3268
Jens Axboe08a45172019-10-03 08:11:03 -06003269 if (nr_files) {
3270 fpl->max = SCM_MAX_FD;
3271 fpl->count = nr_files;
3272 UNIXCB(skb).fp = fpl;
3273 skb->destructor = io_destruct_skb;
3274 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3275 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003276
Jens Axboe08a45172019-10-03 08:11:03 -06003277 for (i = 0; i < nr_files; i++)
3278 fput(fpl->fp[i]);
3279 } else {
3280 kfree_skb(skb);
3281 kfree(fpl);
3282 }
Jens Axboe6b063142019-01-10 22:13:58 -07003283
3284 return 0;
3285}
3286
3287/*
3288 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3289 * causes regular reference counting to break down. We rely on the UNIX
3290 * garbage collection to take care of this problem for us.
3291 */
3292static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3293{
3294 unsigned left, total;
3295 int ret = 0;
3296
3297 total = 0;
3298 left = ctx->nr_user_files;
3299 while (left) {
3300 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003301
3302 ret = __io_sqe_files_scm(ctx, this_files, total);
3303 if (ret)
3304 break;
3305 left -= this_files;
3306 total += this_files;
3307 }
3308
3309 if (!ret)
3310 return 0;
3311
3312 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003313 struct file *file = io_file_from_index(ctx, total);
3314
3315 if (file)
3316 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003317 total++;
3318 }
3319
3320 return ret;
3321}
3322#else
3323static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3324{
3325 return 0;
3326}
3327#endif
3328
Jens Axboe65e19f52019-10-26 07:20:21 -06003329static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3330 unsigned nr_files)
3331{
3332 int i;
3333
3334 for (i = 0; i < nr_tables; i++) {
3335 struct fixed_file_table *table = &ctx->file_table[i];
3336 unsigned this_files;
3337
3338 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3339 table->files = kcalloc(this_files, sizeof(struct file *),
3340 GFP_KERNEL);
3341 if (!table->files)
3342 break;
3343 nr_files -= this_files;
3344 }
3345
3346 if (i == nr_tables)
3347 return 0;
3348
3349 for (i = 0; i < nr_tables; i++) {
3350 struct fixed_file_table *table = &ctx->file_table[i];
3351 kfree(table->files);
3352 }
3353 return 1;
3354}
3355
Jens Axboe6b063142019-01-10 22:13:58 -07003356static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3357 unsigned nr_args)
3358{
3359 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003360 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003361 int fd, ret = 0;
3362 unsigned i;
3363
Jens Axboe65e19f52019-10-26 07:20:21 -06003364 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003365 return -EBUSY;
3366 if (!nr_args)
3367 return -EINVAL;
3368 if (nr_args > IORING_MAX_FIXED_FILES)
3369 return -EMFILE;
3370
Jens Axboe65e19f52019-10-26 07:20:21 -06003371 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3372 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3373 GFP_KERNEL);
3374 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003375 return -ENOMEM;
3376
Jens Axboe65e19f52019-10-26 07:20:21 -06003377 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3378 kfree(ctx->file_table);
3379 return -ENOMEM;
3380 }
3381
Jens Axboe08a45172019-10-03 08:11:03 -06003382 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003383 struct fixed_file_table *table;
3384 unsigned index;
3385
Jens Axboe6b063142019-01-10 22:13:58 -07003386 ret = -EFAULT;
3387 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3388 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003389 /* allow sparse sets */
3390 if (fd == -1) {
3391 ret = 0;
3392 continue;
3393 }
Jens Axboe6b063142019-01-10 22:13:58 -07003394
Jens Axboe65e19f52019-10-26 07:20:21 -06003395 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3396 index = i & IORING_FILE_TABLE_MASK;
3397 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003398
3399 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003400 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003401 break;
3402 /*
3403 * Don't allow io_uring instances to be registered. If UNIX
3404 * isn't enabled, then this causes a reference cycle and this
3405 * instance can never get freed. If UNIX is enabled we'll
3406 * handle it just fine, but there's still no point in allowing
3407 * a ring fd as it doesn't support regular read/write anyway.
3408 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003409 if (table->files[index]->f_op == &io_uring_fops) {
3410 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003411 break;
3412 }
Jens Axboe6b063142019-01-10 22:13:58 -07003413 ret = 0;
3414 }
3415
3416 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003417 for (i = 0; i < ctx->nr_user_files; i++) {
3418 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003419
Jens Axboe65e19f52019-10-26 07:20:21 -06003420 file = io_file_from_index(ctx, i);
3421 if (file)
3422 fput(file);
3423 }
3424 for (i = 0; i < nr_tables; i++)
3425 kfree(ctx->file_table[i].files);
3426
3427 kfree(ctx->file_table);
3428 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003429 ctx->nr_user_files = 0;
3430 return ret;
3431 }
3432
3433 ret = io_sqe_files_scm(ctx);
3434 if (ret)
3435 io_sqe_files_unregister(ctx);
3436
3437 return ret;
3438}
3439
Jens Axboec3a31e62019-10-03 13:59:56 -06003440static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3441{
3442#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003443 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003444 struct sock *sock = ctx->ring_sock->sk;
3445 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3446 struct sk_buff *skb;
3447 int i;
3448
3449 __skb_queue_head_init(&list);
3450
3451 /*
3452 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3453 * remove this entry and rearrange the file array.
3454 */
3455 skb = skb_dequeue(head);
3456 while (skb) {
3457 struct scm_fp_list *fp;
3458
3459 fp = UNIXCB(skb).fp;
3460 for (i = 0; i < fp->count; i++) {
3461 int left;
3462
3463 if (fp->fp[i] != file)
3464 continue;
3465
3466 unix_notinflight(fp->user, fp->fp[i]);
3467 left = fp->count - 1 - i;
3468 if (left) {
3469 memmove(&fp->fp[i], &fp->fp[i + 1],
3470 left * sizeof(struct file *));
3471 }
3472 fp->count--;
3473 if (!fp->count) {
3474 kfree_skb(skb);
3475 skb = NULL;
3476 } else {
3477 __skb_queue_tail(&list, skb);
3478 }
3479 fput(file);
3480 file = NULL;
3481 break;
3482 }
3483
3484 if (!file)
3485 break;
3486
3487 __skb_queue_tail(&list, skb);
3488
3489 skb = skb_dequeue(head);
3490 }
3491
3492 if (skb_peek(&list)) {
3493 spin_lock_irq(&head->lock);
3494 while ((skb = __skb_dequeue(&list)) != NULL)
3495 __skb_queue_tail(head, skb);
3496 spin_unlock_irq(&head->lock);
3497 }
3498#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003499 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003500#endif
3501}
3502
3503static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3504 int index)
3505{
3506#if defined(CONFIG_UNIX)
3507 struct sock *sock = ctx->ring_sock->sk;
3508 struct sk_buff_head *head = &sock->sk_receive_queue;
3509 struct sk_buff *skb;
3510
3511 /*
3512 * See if we can merge this file into an existing skb SCM_RIGHTS
3513 * file set. If there's no room, fall back to allocating a new skb
3514 * and filling it in.
3515 */
3516 spin_lock_irq(&head->lock);
3517 skb = skb_peek(head);
3518 if (skb) {
3519 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3520
3521 if (fpl->count < SCM_MAX_FD) {
3522 __skb_unlink(skb, head);
3523 spin_unlock_irq(&head->lock);
3524 fpl->fp[fpl->count] = get_file(file);
3525 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3526 fpl->count++;
3527 spin_lock_irq(&head->lock);
3528 __skb_queue_head(head, skb);
3529 } else {
3530 skb = NULL;
3531 }
3532 }
3533 spin_unlock_irq(&head->lock);
3534
3535 if (skb) {
3536 fput(file);
3537 return 0;
3538 }
3539
3540 return __io_sqe_files_scm(ctx, 1, index);
3541#else
3542 return 0;
3543#endif
3544}
3545
3546static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3547 unsigned nr_args)
3548{
3549 struct io_uring_files_update up;
3550 __s32 __user *fds;
3551 int fd, i, err;
3552 __u32 done;
3553
Jens Axboe65e19f52019-10-26 07:20:21 -06003554 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003555 return -ENXIO;
3556 if (!nr_args)
3557 return -EINVAL;
3558 if (copy_from_user(&up, arg, sizeof(up)))
3559 return -EFAULT;
3560 if (check_add_overflow(up.offset, nr_args, &done))
3561 return -EOVERFLOW;
3562 if (done > ctx->nr_user_files)
3563 return -EINVAL;
3564
3565 done = 0;
3566 fds = (__s32 __user *) up.fds;
3567 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003568 struct fixed_file_table *table;
3569 unsigned index;
3570
Jens Axboec3a31e62019-10-03 13:59:56 -06003571 err = 0;
3572 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3573 err = -EFAULT;
3574 break;
3575 }
3576 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003577 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3578 index = i & IORING_FILE_TABLE_MASK;
3579 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003580 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003581 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003582 }
3583 if (fd != -1) {
3584 struct file *file;
3585
3586 file = fget(fd);
3587 if (!file) {
3588 err = -EBADF;
3589 break;
3590 }
3591 /*
3592 * Don't allow io_uring instances to be registered. If
3593 * UNIX isn't enabled, then this causes a reference
3594 * cycle and this instance can never get freed. If UNIX
3595 * is enabled we'll handle it just fine, but there's
3596 * still no point in allowing a ring fd as it doesn't
3597 * support regular read/write anyway.
3598 */
3599 if (file->f_op == &io_uring_fops) {
3600 fput(file);
3601 err = -EBADF;
3602 break;
3603 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003604 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003605 err = io_sqe_file_register(ctx, file, i);
3606 if (err)
3607 break;
3608 }
3609 nr_args--;
3610 done++;
3611 up.offset++;
3612 }
3613
3614 return done ? done : err;
3615}
3616
Jens Axboe6c271ce2019-01-10 11:22:30 -07003617static int io_sq_offload_start(struct io_ring_ctx *ctx,
3618 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003619{
Jens Axboe561fb042019-10-24 07:25:42 -06003620 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003621 int ret;
3622
Jens Axboe6c271ce2019-01-10 11:22:30 -07003623 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003624 mmgrab(current->mm);
3625 ctx->sqo_mm = current->mm;
3626
Jens Axboe6c271ce2019-01-10 11:22:30 -07003627 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003628 ret = -EPERM;
3629 if (!capable(CAP_SYS_ADMIN))
3630 goto err;
3631
Jens Axboe917257d2019-04-13 09:28:55 -06003632 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3633 if (!ctx->sq_thread_idle)
3634 ctx->sq_thread_idle = HZ;
3635
Jens Axboe6c271ce2019-01-10 11:22:30 -07003636 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003637 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003638
Jens Axboe917257d2019-04-13 09:28:55 -06003639 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003640 if (cpu >= nr_cpu_ids)
3641 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003642 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003643 goto err;
3644
Jens Axboe6c271ce2019-01-10 11:22:30 -07003645 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3646 ctx, cpu,
3647 "io_uring-sq");
3648 } else {
3649 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3650 "io_uring-sq");
3651 }
3652 if (IS_ERR(ctx->sqo_thread)) {
3653 ret = PTR_ERR(ctx->sqo_thread);
3654 ctx->sqo_thread = NULL;
3655 goto err;
3656 }
3657 wake_up_process(ctx->sqo_thread);
3658 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3659 /* Can't have SQ_AFF without SQPOLL */
3660 ret = -EINVAL;
3661 goto err;
3662 }
3663
Jens Axboe561fb042019-10-24 07:25:42 -06003664 /* Do QD, or 4 * CPUS, whatever is smallest */
3665 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3666 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
Jens Axboe975c99a52019-10-30 08:42:56 -06003667 if (IS_ERR(ctx->io_wq)) {
3668 ret = PTR_ERR(ctx->io_wq);
3669 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003670 goto err;
3671 }
3672
3673 return 0;
3674err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003675 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003676 mmdrop(ctx->sqo_mm);
3677 ctx->sqo_mm = NULL;
3678 return ret;
3679}
3680
3681static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3682{
3683 atomic_long_sub(nr_pages, &user->locked_vm);
3684}
3685
3686static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3687{
3688 unsigned long page_limit, cur_pages, new_pages;
3689
3690 /* Don't allow more pages than we can safely lock */
3691 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3692
3693 do {
3694 cur_pages = atomic_long_read(&user->locked_vm);
3695 new_pages = cur_pages + nr_pages;
3696 if (new_pages > page_limit)
3697 return -ENOMEM;
3698 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3699 new_pages) != cur_pages);
3700
3701 return 0;
3702}
3703
3704static void io_mem_free(void *ptr)
3705{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003706 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003707
Mark Rutland52e04ef2019-04-30 17:30:21 +01003708 if (!ptr)
3709 return;
3710
3711 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003712 if (put_page_testzero(page))
3713 free_compound_page(page);
3714}
3715
3716static void *io_mem_alloc(size_t size)
3717{
3718 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3719 __GFP_NORETRY;
3720
3721 return (void *) __get_free_pages(gfp_flags, get_order(size));
3722}
3723
Hristo Venev75b28af2019-08-26 17:23:46 +00003724static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3725 size_t *sq_offset)
3726{
3727 struct io_rings *rings;
3728 size_t off, sq_array_size;
3729
3730 off = struct_size(rings, cqes, cq_entries);
3731 if (off == SIZE_MAX)
3732 return SIZE_MAX;
3733
3734#ifdef CONFIG_SMP
3735 off = ALIGN(off, SMP_CACHE_BYTES);
3736 if (off == 0)
3737 return SIZE_MAX;
3738#endif
3739
3740 sq_array_size = array_size(sizeof(u32), sq_entries);
3741 if (sq_array_size == SIZE_MAX)
3742 return SIZE_MAX;
3743
3744 if (check_add_overflow(off, sq_array_size, &off))
3745 return SIZE_MAX;
3746
3747 if (sq_offset)
3748 *sq_offset = off;
3749
3750 return off;
3751}
3752
Jens Axboe2b188cc2019-01-07 10:46:33 -07003753static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3754{
Hristo Venev75b28af2019-08-26 17:23:46 +00003755 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003756
Hristo Venev75b28af2019-08-26 17:23:46 +00003757 pages = (size_t)1 << get_order(
3758 rings_size(sq_entries, cq_entries, NULL));
3759 pages += (size_t)1 << get_order(
3760 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003761
Hristo Venev75b28af2019-08-26 17:23:46 +00003762 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003763}
3764
Jens Axboeedafcce2019-01-09 09:16:05 -07003765static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3766{
3767 int i, j;
3768
3769 if (!ctx->user_bufs)
3770 return -ENXIO;
3771
3772 for (i = 0; i < ctx->nr_user_bufs; i++) {
3773 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3774
3775 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003776 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003777
3778 if (ctx->account_mem)
3779 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003780 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003781 imu->nr_bvecs = 0;
3782 }
3783
3784 kfree(ctx->user_bufs);
3785 ctx->user_bufs = NULL;
3786 ctx->nr_user_bufs = 0;
3787 return 0;
3788}
3789
3790static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3791 void __user *arg, unsigned index)
3792{
3793 struct iovec __user *src;
3794
3795#ifdef CONFIG_COMPAT
3796 if (ctx->compat) {
3797 struct compat_iovec __user *ciovs;
3798 struct compat_iovec ciov;
3799
3800 ciovs = (struct compat_iovec __user *) arg;
3801 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3802 return -EFAULT;
3803
3804 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3805 dst->iov_len = ciov.iov_len;
3806 return 0;
3807 }
3808#endif
3809 src = (struct iovec __user *) arg;
3810 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3811 return -EFAULT;
3812 return 0;
3813}
3814
3815static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3816 unsigned nr_args)
3817{
3818 struct vm_area_struct **vmas = NULL;
3819 struct page **pages = NULL;
3820 int i, j, got_pages = 0;
3821 int ret = -EINVAL;
3822
3823 if (ctx->user_bufs)
3824 return -EBUSY;
3825 if (!nr_args || nr_args > UIO_MAXIOV)
3826 return -EINVAL;
3827
3828 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3829 GFP_KERNEL);
3830 if (!ctx->user_bufs)
3831 return -ENOMEM;
3832
3833 for (i = 0; i < nr_args; i++) {
3834 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3835 unsigned long off, start, end, ubuf;
3836 int pret, nr_pages;
3837 struct iovec iov;
3838 size_t size;
3839
3840 ret = io_copy_iov(ctx, &iov, arg, i);
3841 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003842 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003843
3844 /*
3845 * Don't impose further limits on the size and buffer
3846 * constraints here, we'll -EINVAL later when IO is
3847 * submitted if they are wrong.
3848 */
3849 ret = -EFAULT;
3850 if (!iov.iov_base || !iov.iov_len)
3851 goto err;
3852
3853 /* arbitrary limit, but we need something */
3854 if (iov.iov_len > SZ_1G)
3855 goto err;
3856
3857 ubuf = (unsigned long) iov.iov_base;
3858 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3859 start = ubuf >> PAGE_SHIFT;
3860 nr_pages = end - start;
3861
3862 if (ctx->account_mem) {
3863 ret = io_account_mem(ctx->user, nr_pages);
3864 if (ret)
3865 goto err;
3866 }
3867
3868 ret = 0;
3869 if (!pages || nr_pages > got_pages) {
3870 kfree(vmas);
3871 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003872 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003873 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003874 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003875 sizeof(struct vm_area_struct *),
3876 GFP_KERNEL);
3877 if (!pages || !vmas) {
3878 ret = -ENOMEM;
3879 if (ctx->account_mem)
3880 io_unaccount_mem(ctx->user, nr_pages);
3881 goto err;
3882 }
3883 got_pages = nr_pages;
3884 }
3885
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003886 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003887 GFP_KERNEL);
3888 ret = -ENOMEM;
3889 if (!imu->bvec) {
3890 if (ctx->account_mem)
3891 io_unaccount_mem(ctx->user, nr_pages);
3892 goto err;
3893 }
3894
3895 ret = 0;
3896 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003897 pret = get_user_pages(ubuf, nr_pages,
3898 FOLL_WRITE | FOLL_LONGTERM,
3899 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003900 if (pret == nr_pages) {
3901 /* don't support file backed memory */
3902 for (j = 0; j < nr_pages; j++) {
3903 struct vm_area_struct *vma = vmas[j];
3904
3905 if (vma->vm_file &&
3906 !is_file_hugepages(vma->vm_file)) {
3907 ret = -EOPNOTSUPP;
3908 break;
3909 }
3910 }
3911 } else {
3912 ret = pret < 0 ? pret : -EFAULT;
3913 }
3914 up_read(&current->mm->mmap_sem);
3915 if (ret) {
3916 /*
3917 * if we did partial map, or found file backed vmas,
3918 * release any pages we did get
3919 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003920 if (pret > 0)
3921 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003922 if (ctx->account_mem)
3923 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003924 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003925 goto err;
3926 }
3927
3928 off = ubuf & ~PAGE_MASK;
3929 size = iov.iov_len;
3930 for (j = 0; j < nr_pages; j++) {
3931 size_t vec_len;
3932
3933 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3934 imu->bvec[j].bv_page = pages[j];
3935 imu->bvec[j].bv_len = vec_len;
3936 imu->bvec[j].bv_offset = off;
3937 off = 0;
3938 size -= vec_len;
3939 }
3940 /* store original address for later verification */
3941 imu->ubuf = ubuf;
3942 imu->len = iov.iov_len;
3943 imu->nr_bvecs = nr_pages;
3944
3945 ctx->nr_user_bufs++;
3946 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003947 kvfree(pages);
3948 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003949 return 0;
3950err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003951 kvfree(pages);
3952 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003953 io_sqe_buffer_unregister(ctx);
3954 return ret;
3955}
3956
Jens Axboe9b402842019-04-11 11:45:41 -06003957static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3958{
3959 __s32 __user *fds = arg;
3960 int fd;
3961
3962 if (ctx->cq_ev_fd)
3963 return -EBUSY;
3964
3965 if (copy_from_user(&fd, fds, sizeof(*fds)))
3966 return -EFAULT;
3967
3968 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3969 if (IS_ERR(ctx->cq_ev_fd)) {
3970 int ret = PTR_ERR(ctx->cq_ev_fd);
3971 ctx->cq_ev_fd = NULL;
3972 return ret;
3973 }
3974
3975 return 0;
3976}
3977
3978static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3979{
3980 if (ctx->cq_ev_fd) {
3981 eventfd_ctx_put(ctx->cq_ev_fd);
3982 ctx->cq_ev_fd = NULL;
3983 return 0;
3984 }
3985
3986 return -ENXIO;
3987}
3988
Jens Axboe2b188cc2019-01-07 10:46:33 -07003989static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3990{
Jens Axboe6b063142019-01-10 22:13:58 -07003991 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003992 if (ctx->sqo_mm)
3993 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003994
3995 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003996 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003997 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003998 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003999
Jens Axboe2b188cc2019-01-07 10:46:33 -07004000#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004001 if (ctx->ring_sock) {
4002 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004003 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004004 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004005#endif
4006
Hristo Venev75b28af2019-08-26 17:23:46 +00004007 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004008 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004009
4010 percpu_ref_exit(&ctx->refs);
4011 if (ctx->account_mem)
4012 io_unaccount_mem(ctx->user,
4013 ring_pages(ctx->sq_entries, ctx->cq_entries));
4014 free_uid(ctx->user);
4015 kfree(ctx);
4016}
4017
4018static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4019{
4020 struct io_ring_ctx *ctx = file->private_data;
4021 __poll_t mask = 0;
4022
4023 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004024 /*
4025 * synchronizes with barrier from wq_has_sleeper call in
4026 * io_commit_cqring
4027 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004028 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004029 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4030 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004031 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004032 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004033 mask |= EPOLLIN | EPOLLRDNORM;
4034
4035 return mask;
4036}
4037
4038static int io_uring_fasync(int fd, struct file *file, int on)
4039{
4040 struct io_ring_ctx *ctx = file->private_data;
4041
4042 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4043}
4044
4045static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4046{
4047 mutex_lock(&ctx->uring_lock);
4048 percpu_ref_kill(&ctx->refs);
4049 mutex_unlock(&ctx->uring_lock);
4050
Jens Axboe5262f562019-09-17 12:26:57 -06004051 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004052 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004053
4054 if (ctx->io_wq)
4055 io_wq_cancel_all(ctx->io_wq);
4056
Jens Axboedef596e2019-01-09 08:59:42 -07004057 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004058 wait_for_completion(&ctx->ctx_done);
4059 io_ring_ctx_free(ctx);
4060}
4061
4062static int io_uring_release(struct inode *inode, struct file *file)
4063{
4064 struct io_ring_ctx *ctx = file->private_data;
4065
4066 file->private_data = NULL;
4067 io_ring_ctx_wait_and_kill(ctx);
4068 return 0;
4069}
4070
Jens Axboefcb323c2019-10-24 12:39:47 -06004071static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4072 struct files_struct *files)
4073{
4074 struct io_kiocb *req;
4075 DEFINE_WAIT(wait);
4076
4077 while (!list_empty_careful(&ctx->inflight_list)) {
4078 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
4079
4080 spin_lock_irq(&ctx->inflight_lock);
4081 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4082 if (req->work.files == files) {
4083 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
4084 break;
4085 }
4086 }
4087 if (ret == IO_WQ_CANCEL_RUNNING)
4088 prepare_to_wait(&ctx->inflight_wait, &wait,
4089 TASK_UNINTERRUPTIBLE);
4090
4091 spin_unlock_irq(&ctx->inflight_lock);
4092
4093 /*
4094 * We need to keep going until we get NOTFOUND. We only cancel
4095 * one work at the time.
4096 *
4097 * If we get CANCEL_RUNNING, then wait for a work to complete
4098 * before continuing.
4099 */
4100 if (ret == IO_WQ_CANCEL_OK)
4101 continue;
4102 else if (ret != IO_WQ_CANCEL_RUNNING)
4103 break;
4104 schedule();
4105 }
4106}
4107
4108static int io_uring_flush(struct file *file, void *data)
4109{
4110 struct io_ring_ctx *ctx = file->private_data;
4111
4112 io_uring_cancel_files(ctx, data);
4113 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
4114 io_wq_cancel_all(ctx->io_wq);
4115 return 0;
4116}
4117
Jens Axboe2b188cc2019-01-07 10:46:33 -07004118static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4119{
4120 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4121 unsigned long sz = vma->vm_end - vma->vm_start;
4122 struct io_ring_ctx *ctx = file->private_data;
4123 unsigned long pfn;
4124 struct page *page;
4125 void *ptr;
4126
4127 switch (offset) {
4128 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004129 case IORING_OFF_CQ_RING:
4130 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004131 break;
4132 case IORING_OFF_SQES:
4133 ptr = ctx->sq_sqes;
4134 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004135 default:
4136 return -EINVAL;
4137 }
4138
4139 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004140 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004141 return -EINVAL;
4142
4143 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4144 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4145}
4146
4147SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4148 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4149 size_t, sigsz)
4150{
4151 struct io_ring_ctx *ctx;
4152 long ret = -EBADF;
4153 int submitted = 0;
4154 struct fd f;
4155
Jens Axboe6c271ce2019-01-10 11:22:30 -07004156 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004157 return -EINVAL;
4158
4159 f = fdget(fd);
4160 if (!f.file)
4161 return -EBADF;
4162
4163 ret = -EOPNOTSUPP;
4164 if (f.file->f_op != &io_uring_fops)
4165 goto out_fput;
4166
4167 ret = -ENXIO;
4168 ctx = f.file->private_data;
4169 if (!percpu_ref_tryget(&ctx->refs))
4170 goto out_fput;
4171
Jens Axboe6c271ce2019-01-10 11:22:30 -07004172 /*
4173 * For SQ polling, the thread will do all submissions and completions.
4174 * Just return the requested submit count, and wake the thread if
4175 * we were asked to.
4176 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004177 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004178 if (ctx->flags & IORING_SETUP_SQPOLL) {
4179 if (flags & IORING_ENTER_SQ_WAKEUP)
4180 wake_up(&ctx->sqo_wait);
4181 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004182 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004183 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004184
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004185 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004186 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004187 /* already have mm, so io_submit_sqes() won't try to grab it */
4188 cur_mm = ctx->sqo_mm;
4189 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4190 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004191 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004192 }
4193 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004194 unsigned nr_events = 0;
4195
Jens Axboe2b188cc2019-01-07 10:46:33 -07004196 min_complete = min(min_complete, ctx->cq_entries);
4197
Jens Axboedef596e2019-01-09 08:59:42 -07004198 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004199 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004200 } else {
4201 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4202 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004203 }
4204
Pavel Begunkov6805b322019-10-08 02:18:42 +03004205 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004206out_fput:
4207 fdput(f);
4208 return submitted ? submitted : ret;
4209}
4210
4211static const struct file_operations io_uring_fops = {
4212 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004213 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004214 .mmap = io_uring_mmap,
4215 .poll = io_uring_poll,
4216 .fasync = io_uring_fasync,
4217};
4218
4219static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4220 struct io_uring_params *p)
4221{
Hristo Venev75b28af2019-08-26 17:23:46 +00004222 struct io_rings *rings;
4223 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004224
Hristo Venev75b28af2019-08-26 17:23:46 +00004225 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4226 if (size == SIZE_MAX)
4227 return -EOVERFLOW;
4228
4229 rings = io_mem_alloc(size);
4230 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004231 return -ENOMEM;
4232
Hristo Venev75b28af2019-08-26 17:23:46 +00004233 ctx->rings = rings;
4234 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4235 rings->sq_ring_mask = p->sq_entries - 1;
4236 rings->cq_ring_mask = p->cq_entries - 1;
4237 rings->sq_ring_entries = p->sq_entries;
4238 rings->cq_ring_entries = p->cq_entries;
4239 ctx->sq_mask = rings->sq_ring_mask;
4240 ctx->cq_mask = rings->cq_ring_mask;
4241 ctx->sq_entries = rings->sq_ring_entries;
4242 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004243
4244 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4245 if (size == SIZE_MAX)
4246 return -EOVERFLOW;
4247
4248 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004249 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004250 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004251
Jens Axboe2b188cc2019-01-07 10:46:33 -07004252 return 0;
4253}
4254
4255/*
4256 * Allocate an anonymous fd, this is what constitutes the application
4257 * visible backing of an io_uring instance. The application mmaps this
4258 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4259 * we have to tie this fd to a socket for file garbage collection purposes.
4260 */
4261static int io_uring_get_fd(struct io_ring_ctx *ctx)
4262{
4263 struct file *file;
4264 int ret;
4265
4266#if defined(CONFIG_UNIX)
4267 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4268 &ctx->ring_sock);
4269 if (ret)
4270 return ret;
4271#endif
4272
4273 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4274 if (ret < 0)
4275 goto err;
4276
4277 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4278 O_RDWR | O_CLOEXEC);
4279 if (IS_ERR(file)) {
4280 put_unused_fd(ret);
4281 ret = PTR_ERR(file);
4282 goto err;
4283 }
4284
4285#if defined(CONFIG_UNIX)
4286 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004287 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004288#endif
4289 fd_install(ret, file);
4290 return ret;
4291err:
4292#if defined(CONFIG_UNIX)
4293 sock_release(ctx->ring_sock);
4294 ctx->ring_sock = NULL;
4295#endif
4296 return ret;
4297}
4298
4299static int io_uring_create(unsigned entries, struct io_uring_params *p)
4300{
4301 struct user_struct *user = NULL;
4302 struct io_ring_ctx *ctx;
4303 bool account_mem;
4304 int ret;
4305
4306 if (!entries || entries > IORING_MAX_ENTRIES)
4307 return -EINVAL;
4308
4309 /*
4310 * Use twice as many entries for the CQ ring. It's possible for the
4311 * application to drive a higher depth than the size of the SQ ring,
4312 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004313 * some flexibility in overcommitting a bit. If the application has
4314 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4315 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004316 */
4317 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004318 if (p->flags & IORING_SETUP_CQSIZE) {
4319 /*
4320 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4321 * to a power-of-two, if it isn't already. We do NOT impose
4322 * any cq vs sq ring sizing.
4323 */
4324 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4325 return -EINVAL;
4326 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4327 } else {
4328 p->cq_entries = 2 * p->sq_entries;
4329 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004330
4331 user = get_uid(current_user());
4332 account_mem = !capable(CAP_IPC_LOCK);
4333
4334 if (account_mem) {
4335 ret = io_account_mem(user,
4336 ring_pages(p->sq_entries, p->cq_entries));
4337 if (ret) {
4338 free_uid(user);
4339 return ret;
4340 }
4341 }
4342
4343 ctx = io_ring_ctx_alloc(p);
4344 if (!ctx) {
4345 if (account_mem)
4346 io_unaccount_mem(user, ring_pages(p->sq_entries,
4347 p->cq_entries));
4348 free_uid(user);
4349 return -ENOMEM;
4350 }
4351 ctx->compat = in_compat_syscall();
4352 ctx->account_mem = account_mem;
4353 ctx->user = user;
4354
4355 ret = io_allocate_scq_urings(ctx, p);
4356 if (ret)
4357 goto err;
4358
Jens Axboe6c271ce2019-01-10 11:22:30 -07004359 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004360 if (ret)
4361 goto err;
4362
Jens Axboe2b188cc2019-01-07 10:46:33 -07004363 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004364 p->sq_off.head = offsetof(struct io_rings, sq.head);
4365 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4366 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4367 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4368 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4369 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4370 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004371
4372 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004373 p->cq_off.head = offsetof(struct io_rings, cq.head);
4374 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4375 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4376 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4377 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4378 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004379
Jens Axboe044c1ab2019-10-28 09:15:33 -06004380 /*
4381 * Install ring fd as the very last thing, so we don't risk someone
4382 * having closed it before we finish setup
4383 */
4384 ret = io_uring_get_fd(ctx);
4385 if (ret < 0)
4386 goto err;
4387
Jens Axboeac90f242019-09-06 10:26:21 -06004388 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004389 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004390 return ret;
4391err:
4392 io_ring_ctx_wait_and_kill(ctx);
4393 return ret;
4394}
4395
4396/*
4397 * Sets up an aio uring context, and returns the fd. Applications asks for a
4398 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4399 * params structure passed in.
4400 */
4401static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4402{
4403 struct io_uring_params p;
4404 long ret;
4405 int i;
4406
4407 if (copy_from_user(&p, params, sizeof(p)))
4408 return -EFAULT;
4409 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4410 if (p.resv[i])
4411 return -EINVAL;
4412 }
4413
Jens Axboe6c271ce2019-01-10 11:22:30 -07004414 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004415 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004416 return -EINVAL;
4417
4418 ret = io_uring_create(entries, &p);
4419 if (ret < 0)
4420 return ret;
4421
4422 if (copy_to_user(params, &p, sizeof(p)))
4423 return -EFAULT;
4424
4425 return ret;
4426}
4427
4428SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4429 struct io_uring_params __user *, params)
4430{
4431 return io_uring_setup(entries, params);
4432}
4433
Jens Axboeedafcce2019-01-09 09:16:05 -07004434static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4435 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004436 __releases(ctx->uring_lock)
4437 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004438{
4439 int ret;
4440
Jens Axboe35fa71a2019-04-22 10:23:23 -06004441 /*
4442 * We're inside the ring mutex, if the ref is already dying, then
4443 * someone else killed the ctx or is already going through
4444 * io_uring_register().
4445 */
4446 if (percpu_ref_is_dying(&ctx->refs))
4447 return -ENXIO;
4448
Jens Axboeedafcce2019-01-09 09:16:05 -07004449 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004450
4451 /*
4452 * Drop uring mutex before waiting for references to exit. If another
4453 * thread is currently inside io_uring_enter() it might need to grab
4454 * the uring_lock to make progress. If we hold it here across the drain
4455 * wait, then we can deadlock. It's safe to drop the mutex here, since
4456 * no new references will come in after we've killed the percpu ref.
4457 */
4458 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004459 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004460 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004461
4462 switch (opcode) {
4463 case IORING_REGISTER_BUFFERS:
4464 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4465 break;
4466 case IORING_UNREGISTER_BUFFERS:
4467 ret = -EINVAL;
4468 if (arg || nr_args)
4469 break;
4470 ret = io_sqe_buffer_unregister(ctx);
4471 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004472 case IORING_REGISTER_FILES:
4473 ret = io_sqe_files_register(ctx, arg, nr_args);
4474 break;
4475 case IORING_UNREGISTER_FILES:
4476 ret = -EINVAL;
4477 if (arg || nr_args)
4478 break;
4479 ret = io_sqe_files_unregister(ctx);
4480 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004481 case IORING_REGISTER_FILES_UPDATE:
4482 ret = io_sqe_files_update(ctx, arg, nr_args);
4483 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004484 case IORING_REGISTER_EVENTFD:
4485 ret = -EINVAL;
4486 if (nr_args != 1)
4487 break;
4488 ret = io_eventfd_register(ctx, arg);
4489 break;
4490 case IORING_UNREGISTER_EVENTFD:
4491 ret = -EINVAL;
4492 if (arg || nr_args)
4493 break;
4494 ret = io_eventfd_unregister(ctx);
4495 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004496 default:
4497 ret = -EINVAL;
4498 break;
4499 }
4500
4501 /* bring the ctx back to life */
4502 reinit_completion(&ctx->ctx_done);
4503 percpu_ref_reinit(&ctx->refs);
4504 return ret;
4505}
4506
4507SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4508 void __user *, arg, unsigned int, nr_args)
4509{
4510 struct io_ring_ctx *ctx;
4511 long ret = -EBADF;
4512 struct fd f;
4513
4514 f = fdget(fd);
4515 if (!f.file)
4516 return -EBADF;
4517
4518 ret = -EOPNOTSUPP;
4519 if (f.file->f_op != &io_uring_fops)
4520 goto out_fput;
4521
4522 ctx = f.file->private_data;
4523
4524 mutex_lock(&ctx->uring_lock);
4525 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4526 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004527 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4528 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004529out_fput:
4530 fdput(f);
4531 return ret;
4532}
4533
Jens Axboe2b188cc2019-01-07 10:46:33 -07004534static int __init io_uring_init(void)
4535{
4536 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4537 return 0;
4538};
4539__initcall(io_uring_init);