blob: d94bd4e3a60eb675fac9f447fd9eb5e28505058e [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 Axboe6b063142019-01-10 22:13:58 -070083#define IORING_MAX_FIXED_FILES 1024
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
85struct io_uring {
86 u32 head ____cacheline_aligned_in_smp;
87 u32 tail ____cacheline_aligned_in_smp;
88};
89
Stefan Bühler1e84b972019-04-24 23:54:16 +020090/*
Hristo Venev75b28af2019-08-26 17:23:46 +000091 * This data is shared with the application through the mmap at offsets
92 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +020093 *
94 * The offsets to the member fields are published through struct
95 * io_sqring_offsets when calling io_uring_setup.
96 */
Hristo Venev75b28af2019-08-26 17:23:46 +000097struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +020098 /*
99 * Head and tail offsets into the ring; the offsets need to be
100 * masked to get valid indices.
101 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 * The kernel controls head of the sq ring and the tail of the cq ring,
103 * and the application controls tail of the sq ring and the head of the
104 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000106 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 * ring_entries - 1)
110 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000111 u32 sq_ring_mask, cq_ring_mask;
112 /* Ring sizes (constant, power of 2) */
113 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
115 * Number of invalid entries dropped by the kernel due to
116 * invalid index stored in array
117 *
118 * Written by the kernel, shouldn't be modified by the
119 * application (i.e. get number of "new events" by comparing to
120 * cached value).
121 *
122 * After a new SQ head value was read by the application this
123 * counter includes all submissions that were dropped reaching
124 * the new SQ head (and possibly more).
125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
128 * Runtime flags
129 *
130 * Written by the kernel, shouldn't be modified by the
131 * application.
132 *
133 * The application needs a full memory barrier before checking
134 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200137 /*
138 * Number of completion events lost because the queue was full;
139 * this should be avoided by the application by making sure
140 * there are not more requests pending thatn there is space in
141 * the completion queue.
142 *
143 * Written by the kernel, shouldn't be modified by the
144 * application (i.e. get number of "new events" by comparing to
145 * cached value).
146 *
147 * As completion events come in out of order this counter is not
148 * ordered with any other data.
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
152 * Ring buffer of completion events.
153 *
154 * The kernel writes completion events fresh every time they are
155 * produced, so the application is allowed to modify pending
156 * entries.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700159};
160
Jens Axboeedafcce2019-01-09 09:16:05 -0700161struct io_mapped_ubuf {
162 u64 ubuf;
163 size_t len;
164 struct bio_vec *bvec;
165 unsigned int nr_bvecs;
166};
167
Jens Axboe2b188cc2019-01-07 10:46:33 -0700168struct io_ring_ctx {
169 struct {
170 struct percpu_ref refs;
171 } ____cacheline_aligned_in_smp;
172
173 struct {
174 unsigned int flags;
175 bool compat;
176 bool account_mem;
177
Hristo Venev75b28af2019-08-26 17:23:46 +0000178 /*
179 * Ring buffer of indices into array of io_uring_sqe, which is
180 * mmapped by the application using the IORING_OFF_SQES offset.
181 *
182 * This indirection could e.g. be used to assign fixed
183 * io_uring_sqe entries to operations and only submit them to
184 * the queue when needed.
185 *
186 * The kernel modifies neither the indices array nor the entries
187 * array.
188 */
189 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700190 unsigned cached_sq_head;
191 unsigned sq_entries;
192 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700193 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600194 unsigned cached_sq_dropped;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700195 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600196
197 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600198 struct list_head timeout_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700199 } ____cacheline_aligned_in_smp;
200
201 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600202 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700203 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700204 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700205 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800206 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700207
208 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600210 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700211 unsigned cq_entries;
212 unsigned cq_mask;
213 struct wait_queue_head cq_wait;
214 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600215 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600216 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 } ____cacheline_aligned_in_smp;
218
Hristo Venev75b28af2019-08-26 17:23:46 +0000219 struct io_rings *rings;
220
Jens Axboe6b063142019-01-10 22:13:58 -0700221 /*
222 * If used, fixed file set. Writers must ensure that ->refs is dead,
223 * readers must ensure that ->refs is alive as long as the file* is
224 * used. Only updated through io_uring_register(2).
225 */
226 struct file **user_files;
227 unsigned nr_user_files;
228
Jens Axboeedafcce2019-01-09 09:16:05 -0700229 /* if used, fixed mapped user buffers */
230 unsigned nr_user_bufs;
231 struct io_mapped_ubuf *user_bufs;
232
Jens Axboe2b188cc2019-01-07 10:46:33 -0700233 struct user_struct *user;
234
235 struct completion ctx_done;
236
237 struct {
238 struct mutex uring_lock;
239 wait_queue_head_t wait;
240 } ____cacheline_aligned_in_smp;
241
242 struct {
243 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700244 bool poll_multi_file;
245 /*
246 * ->poll_list is protected by the ctx->uring_lock for
247 * io_uring instances that don't use IORING_SETUP_SQPOLL.
248 * For SQPOLL, only the single threaded io_sq_thread() will
249 * manipulate the list, hence no extra locking is needed there.
250 */
251 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700252 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700253 } ____cacheline_aligned_in_smp;
254
255#if defined(CONFIG_UNIX)
256 struct socket *ring_sock;
257#endif
258};
259
260struct sqe_submit {
261 const struct io_uring_sqe *sqe;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800262 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800264 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700265 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700266};
267
Jens Axboe09bb8392019-03-13 12:39:28 -0600268/*
269 * First field must be the file pointer in all the
270 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
271 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700272struct io_poll_iocb {
273 struct file *file;
274 struct wait_queue_head *head;
275 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600276 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700277 bool canceled;
278 struct wait_queue_entry wait;
279};
280
Jens Axboe5262f562019-09-17 12:26:57 -0600281struct io_timeout {
282 struct file *file;
283 struct hrtimer timer;
284};
285
Jens Axboe09bb8392019-03-13 12:39:28 -0600286/*
287 * NOTE! Each of the iocb union members has the file pointer
288 * as the first entry in their struct definition. So you can
289 * access the file pointer through any of the sub-structs,
290 * or directly as just 'ki_filp' in this struct.
291 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700293 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600294 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700295 struct kiocb rw;
296 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600297 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700298 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700299
300 struct sqe_submit submit;
301
302 struct io_ring_ctx *ctx;
303 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600304 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700305 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700306 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200307#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700308#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700309#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700310#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200311#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
312#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600313#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800314#define REQ_F_LINK_DONE 128 /* linked sqes done */
315#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800316#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600317#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600318#define REQ_F_ISREG 2048 /* regular file */
319#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700320 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600321 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600322 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700323
Jens Axboe561fb042019-10-24 07:25:42 -0600324 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700325};
326
327#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700328#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329
Jens Axboe9a56a232019-01-09 09:06:50 -0700330struct io_submit_state {
331 struct blk_plug plug;
332
333 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700334 * io_kiocb alloc cache
335 */
336 void *reqs[IO_IOPOLL_BATCH];
337 unsigned int free_reqs;
338 unsigned int cur_req;
339
340 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700341 * File reference cache
342 */
343 struct file *file;
344 unsigned int fd;
345 unsigned int has_refs;
346 unsigned int used_refs;
347 unsigned int ios_left;
348};
349
Jens Axboe561fb042019-10-24 07:25:42 -0600350static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe5262f562019-09-17 12:26:57 -0600351static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
352 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800353static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600354
Jens Axboe2b188cc2019-01-07 10:46:33 -0700355static struct kmem_cache *req_cachep;
356
357static const struct file_operations io_uring_fops;
358
359struct sock *io_uring_get_socket(struct file *file)
360{
361#if defined(CONFIG_UNIX)
362 if (file->f_op == &io_uring_fops) {
363 struct io_ring_ctx *ctx = file->private_data;
364
365 return ctx->ring_sock->sk;
366 }
367#endif
368 return NULL;
369}
370EXPORT_SYMBOL(io_uring_get_socket);
371
372static void io_ring_ctx_ref_free(struct percpu_ref *ref)
373{
374 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
375
376 complete(&ctx->ctx_done);
377}
378
379static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
380{
381 struct io_ring_ctx *ctx;
382
383 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
384 if (!ctx)
385 return NULL;
386
Roman Gushchin21482892019-05-07 10:01:48 -0700387 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
388 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700389 kfree(ctx);
390 return NULL;
391 }
392
393 ctx->flags = p->flags;
394 init_waitqueue_head(&ctx->cq_wait);
395 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800396 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700397 mutex_init(&ctx->uring_lock);
398 init_waitqueue_head(&ctx->wait);
399 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700400 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700401 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600402 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600403 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700404 return ctx;
405}
406
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600407static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
408 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600409{
Jens Axboe498ccd92019-10-25 10:04:25 -0600410 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
411 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600412}
413
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600414static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
415 struct io_kiocb *req)
416{
417 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
418 return false;
419
420 return __io_sequence_defer(ctx, req);
421}
422
423static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600424{
425 struct io_kiocb *req;
426
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600427 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
428 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600429 list_del_init(&req->list);
430 return req;
431 }
432
433 return NULL;
434}
435
Jens Axboe5262f562019-09-17 12:26:57 -0600436static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
437{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600438 struct io_kiocb *req;
439
440 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
441 if (req && !__io_sequence_defer(ctx, req)) {
442 list_del_init(&req->list);
443 return req;
444 }
445
446 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600447}
448
Jens Axboede0617e2019-04-06 21:51:27 -0600449static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700450{
Hristo Venev75b28af2019-08-26 17:23:46 +0000451 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700452
Hristo Venev75b28af2019-08-26 17:23:46 +0000453 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700454 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000455 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700456
Jens Axboe2b188cc2019-01-07 10:46:33 -0700457 if (wq_has_sleeper(&ctx->cq_wait)) {
458 wake_up_interruptible(&ctx->cq_wait);
459 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
460 }
461 }
462}
463
Jens Axboe561fb042019-10-24 07:25:42 -0600464static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600465{
Jens Axboe561fb042019-10-24 07:25:42 -0600466 u8 opcode = READ_ONCE(sqe->opcode);
467
468 return !(opcode == IORING_OP_READ_FIXED ||
469 opcode == IORING_OP_WRITE_FIXED);
470}
471
472static inline bool io_prep_async_work(struct io_kiocb *req)
473{
474 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600475
Jens Axboe6cc47d12019-09-18 11:18:23 -0600476 if (req->submit.sqe) {
477 switch (req->submit.sqe->opcode) {
478 case IORING_OP_WRITEV:
479 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600480 do_hashed = true;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600481 break;
482 }
Jens Axboe561fb042019-10-24 07:25:42 -0600483 if (io_sqe_needs_user(req->submit.sqe))
484 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600485 }
486
Jens Axboe561fb042019-10-24 07:25:42 -0600487 return do_hashed;
488}
489
490static inline void io_queue_async_work(struct io_ring_ctx *ctx,
491 struct io_kiocb *req)
492{
493 bool do_hashed = io_prep_async_work(req);
494
495 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
496 req->flags);
497 if (!do_hashed) {
498 io_wq_enqueue(ctx->io_wq, &req->work);
499 } else {
500 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
501 file_inode(req->file));
502 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600503}
504
Jens Axboe5262f562019-09-17 12:26:57 -0600505static void io_kill_timeout(struct io_kiocb *req)
506{
507 int ret;
508
509 ret = hrtimer_try_to_cancel(&req->timeout.timer);
510 if (ret != -1) {
511 atomic_inc(&req->ctx->cq_timeouts);
512 list_del(&req->list);
513 io_cqring_fill_event(req->ctx, req->user_data, 0);
514 __io_free_req(req);
515 }
516}
517
518static void io_kill_timeouts(struct io_ring_ctx *ctx)
519{
520 struct io_kiocb *req, *tmp;
521
522 spin_lock_irq(&ctx->completion_lock);
523 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
524 io_kill_timeout(req);
525 spin_unlock_irq(&ctx->completion_lock);
526}
527
Jens Axboede0617e2019-04-06 21:51:27 -0600528static void io_commit_cqring(struct io_ring_ctx *ctx)
529{
530 struct io_kiocb *req;
531
Jens Axboe5262f562019-09-17 12:26:57 -0600532 while ((req = io_get_timeout_req(ctx)) != NULL)
533 io_kill_timeout(req);
534
Jens Axboede0617e2019-04-06 21:51:27 -0600535 __io_commit_cqring(ctx);
536
537 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800538 if (req->flags & REQ_F_SHADOW_DRAIN) {
539 /* Just for drain, free it. */
540 __io_free_req(req);
541 continue;
542 }
Jens Axboede0617e2019-04-06 21:51:27 -0600543 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600544 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600545 }
546}
547
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
549{
Hristo Venev75b28af2019-08-26 17:23:46 +0000550 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700551 unsigned tail;
552
553 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200554 /*
555 * writes to the cq entry need to come after reading head; the
556 * control dependency is enough as we're using WRITE_ONCE to
557 * fill the cq entry
558 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000559 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700560 return NULL;
561
562 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000563 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700564}
565
566static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600567 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700568{
569 struct io_uring_cqe *cqe;
570
571 /*
572 * If we can't get a cq entry, userspace overflowed the
573 * submission (by quite a lot). Increment the overflow count in
574 * the ring.
575 */
576 cqe = io_get_cqring(ctx);
577 if (cqe) {
578 WRITE_ONCE(cqe->user_data, ki_user_data);
579 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600580 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700581 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600582 WRITE_ONCE(ctx->rings->cq_overflow,
583 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700584 }
585}
586
Jens Axboe8c838782019-03-12 15:48:16 -0600587static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
588{
589 if (waitqueue_active(&ctx->wait))
590 wake_up(&ctx->wait);
591 if (waitqueue_active(&ctx->sqo_wait))
592 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600593 if (ctx->cq_ev_fd)
594 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600595}
596
597static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600598 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599{
600 unsigned long flags;
601
602 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600603 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700604 io_commit_cqring(ctx);
605 spin_unlock_irqrestore(&ctx->completion_lock, flags);
606
Jens Axboe8c838782019-03-12 15:48:16 -0600607 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608}
609
Jens Axboe2579f912019-01-09 09:10:43 -0700610static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
611 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612{
Jens Axboefd6fab22019-03-14 16:30:06 -0600613 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700614 struct io_kiocb *req;
615
616 if (!percpu_ref_tryget(&ctx->refs))
617 return NULL;
618
Jens Axboe2579f912019-01-09 09:10:43 -0700619 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600620 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700621 if (unlikely(!req))
622 goto out;
623 } else if (!state->free_reqs) {
624 size_t sz;
625 int ret;
626
627 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600628 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
629
630 /*
631 * Bulk alloc is all-or-nothing. If we fail to get a batch,
632 * retry single alloc to be on the safe side.
633 */
634 if (unlikely(ret <= 0)) {
635 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
636 if (!state->reqs[0])
637 goto out;
638 ret = 1;
639 }
Jens Axboe2579f912019-01-09 09:10:43 -0700640 state->free_reqs = ret - 1;
641 state->cur_req = 1;
642 req = state->reqs[0];
643 } else {
644 req = state->reqs[state->cur_req];
645 state->free_reqs--;
646 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700647 }
648
Jens Axboe60c112b2019-06-21 10:20:18 -0600649 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700650 req->ctx = ctx;
651 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600652 /* one is dropped after submission, the other at completion */
653 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600654 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600655 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700656 return req;
657out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300658 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700659 return NULL;
660}
661
Jens Axboedef596e2019-01-09 08:59:42 -0700662static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
663{
664 if (*nr) {
665 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300666 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700667 *nr = 0;
668 }
669}
670
Jens Axboe9e645e112019-05-10 16:07:28 -0600671static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700672{
Jens Axboe09bb8392019-03-13 12:39:28 -0600673 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
674 fput(req->file);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300675 percpu_ref_put(&req->ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600676 kmem_cache_free(req_cachep, req);
677}
678
Jens Axboeba816ad2019-09-28 11:36:45 -0600679static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600680{
681 struct io_kiocb *nxt;
682
683 /*
684 * The list should never be empty when we are called here. But could
685 * potentially happen if the chain is messed up, check to be on the
686 * safe side.
687 */
688 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
689 if (nxt) {
690 list_del(&nxt->list);
691 if (!list_empty(&req->link_list)) {
692 INIT_LIST_HEAD(&nxt->link_list);
693 list_splice(&req->link_list, &nxt->link_list);
694 nxt->flags |= REQ_F_LINK;
695 }
696
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800697 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboeba816ad2019-09-28 11:36:45 -0600698 /*
699 * If we're in async work, we can continue processing the chain
700 * in this context instead of having to queue up new async work.
701 */
Jens Axboe561fb042019-10-24 07:25:42 -0600702 if (nxtptr && current_work())
Jens Axboeba816ad2019-09-28 11:36:45 -0600703 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600704 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600705 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600706 }
707}
708
709/*
710 * Called if REQ_F_LINK is set, and we fail the head request
711 */
712static void io_fail_links(struct io_kiocb *req)
713{
714 struct io_kiocb *link;
715
716 while (!list_empty(&req->link_list)) {
717 link = list_first_entry(&req->link_list, struct io_kiocb, list);
718 list_del(&link->list);
719
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200720 trace_io_uring_fail_link(req, link);
Jens Axboe9e645e112019-05-10 16:07:28 -0600721 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
722 __io_free_req(link);
723 }
724}
725
Jens Axboeba816ad2019-09-28 11:36:45 -0600726static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600727{
728 /*
729 * If LINK is set, we have dependent requests in this chain. If we
730 * didn't fail this request, queue the first one up, moving any other
731 * dependencies to the next request. In case of failure, fail the rest
732 * of the chain.
733 */
734 if (req->flags & REQ_F_LINK) {
735 if (req->flags & REQ_F_FAIL_LINK)
736 io_fail_links(req);
737 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600738 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600739 }
740
741 __io_free_req(req);
742}
743
Jens Axboeba816ad2019-09-28 11:36:45 -0600744/*
745 * Drop reference to request, return next in chain (if there is one) if this
746 * was the last reference to this request.
747 */
748static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600749{
Jens Axboeba816ad2019-09-28 11:36:45 -0600750 struct io_kiocb *nxt = NULL;
751
Jens Axboee65ef562019-03-12 10:16:44 -0600752 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600753 io_free_req(req, &nxt);
754
755 return nxt;
756}
757
758static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
759{
760 struct io_kiocb *nxt;
761
762 nxt = io_put_req_find_next(req);
763 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600764 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600765 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600766 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600767 io_queue_async_work(nxt->ctx, nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600768 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700769}
770
Hristo Venev75b28af2019-08-26 17:23:46 +0000771static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600772{
773 /* See comment at the top of this file */
774 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000775 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600776}
777
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300778static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
779{
780 struct io_rings *rings = ctx->rings;
781
782 /* make sure SQ entry isn't read before tail */
783 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
784}
785
Jens Axboedef596e2019-01-09 08:59:42 -0700786/*
787 * Find and free completed poll iocbs
788 */
789static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
790 struct list_head *done)
791{
792 void *reqs[IO_IOPOLL_BATCH];
793 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600794 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700795
Jens Axboe09bb8392019-03-13 12:39:28 -0600796 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700797 while (!list_empty(done)) {
798 req = list_first_entry(done, struct io_kiocb, list);
799 list_del(&req->list);
800
Jens Axboe9e645e112019-05-10 16:07:28 -0600801 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700802 (*nr_events)++;
803
Jens Axboe09bb8392019-03-13 12:39:28 -0600804 if (refcount_dec_and_test(&req->refs)) {
805 /* If we're not using fixed files, we have to pair the
806 * completion part with the file put. Use regular
807 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600808 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600809 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600810 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
811 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600812 reqs[to_free++] = req;
813 if (to_free == ARRAY_SIZE(reqs))
814 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700815 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600816 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700817 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700818 }
Jens Axboedef596e2019-01-09 08:59:42 -0700819 }
Jens Axboedef596e2019-01-09 08:59:42 -0700820
Jens Axboe09bb8392019-03-13 12:39:28 -0600821 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700822 io_free_req_many(ctx, reqs, &to_free);
823}
824
825static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
826 long min)
827{
828 struct io_kiocb *req, *tmp;
829 LIST_HEAD(done);
830 bool spin;
831 int ret;
832
833 /*
834 * Only spin for completions if we don't have multiple devices hanging
835 * off our complete list, and we're under the requested amount.
836 */
837 spin = !ctx->poll_multi_file && *nr_events < min;
838
839 ret = 0;
840 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
841 struct kiocb *kiocb = &req->rw;
842
843 /*
844 * Move completed entries to our local list. If we find a
845 * request that requires polling, break out and complete
846 * the done list first, if we have entries there.
847 */
848 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
849 list_move_tail(&req->list, &done);
850 continue;
851 }
852 if (!list_empty(&done))
853 break;
854
855 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
856 if (ret < 0)
857 break;
858
859 if (ret && spin)
860 spin = false;
861 ret = 0;
862 }
863
864 if (!list_empty(&done))
865 io_iopoll_complete(ctx, nr_events, &done);
866
867 return ret;
868}
869
870/*
871 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
872 * non-spinning poll check - we'll still enter the driver poll loop, but only
873 * as a non-spinning completion check.
874 */
875static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
876 long min)
877{
Jens Axboe08f54392019-08-21 22:19:11 -0600878 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700879 int ret;
880
881 ret = io_do_iopoll(ctx, nr_events, min);
882 if (ret < 0)
883 return ret;
884 if (!min || *nr_events >= min)
885 return 0;
886 }
887
888 return 1;
889}
890
891/*
892 * We can't just wait for polled events to come to us, we have to actively
893 * find and complete them.
894 */
895static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
896{
897 if (!(ctx->flags & IORING_SETUP_IOPOLL))
898 return;
899
900 mutex_lock(&ctx->uring_lock);
901 while (!list_empty(&ctx->poll_list)) {
902 unsigned int nr_events = 0;
903
904 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600905
906 /*
907 * Ensure we allow local-to-the-cpu processing to take place,
908 * in this case we need to ensure that we reap all events.
909 */
910 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700911 }
912 mutex_unlock(&ctx->uring_lock);
913}
914
Jens Axboe2b2ed972019-10-25 10:06:15 -0600915static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
916 long min)
Jens Axboedef596e2019-01-09 08:59:42 -0700917{
Jens Axboe2b2ed972019-10-25 10:06:15 -0600918 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700919
920 do {
921 int tmin = 0;
922
Jens Axboe500f9fb2019-08-19 12:15:59 -0600923 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600924 * Don't enter poll loop if we already have events pending.
925 * If we do, we can potentially be spinning for commands that
926 * already triggered a CQE (eg in error).
927 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000928 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600929 break;
930
931 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600932 * If a submit got punted to a workqueue, we can have the
933 * application entering polling for a command before it gets
934 * issued. That app will hold the uring_lock for the duration
935 * of the poll right here, so we need to take a breather every
936 * now and then to ensure that the issue has a chance to add
937 * the poll to the issued list. Otherwise we can spin here
938 * forever, while the workqueue is stuck trying to acquire the
939 * very same mutex.
940 */
941 if (!(++iters & 7)) {
942 mutex_unlock(&ctx->uring_lock);
943 mutex_lock(&ctx->uring_lock);
944 }
945
Jens Axboedef596e2019-01-09 08:59:42 -0700946 if (*nr_events < min)
947 tmin = min - *nr_events;
948
949 ret = io_iopoll_getevents(ctx, nr_events, tmin);
950 if (ret <= 0)
951 break;
952 ret = 0;
953 } while (min && !*nr_events && !need_resched());
954
Jens Axboe2b2ed972019-10-25 10:06:15 -0600955 return ret;
956}
957
958static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
959 long min)
960{
961 int ret;
962
963 /*
964 * We disallow the app entering submit/complete with polling, but we
965 * still need to lock the ring to prevent racing with polled issue
966 * that got punted to a workqueue.
967 */
968 mutex_lock(&ctx->uring_lock);
969 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -0600970 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700971 return ret;
972}
973
Jens Axboe491381ce2019-10-17 09:20:46 -0600974static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700975{
Jens Axboe491381ce2019-10-17 09:20:46 -0600976 /*
977 * Tell lockdep we inherited freeze protection from submission
978 * thread.
979 */
980 if (req->flags & REQ_F_ISREG) {
981 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700982
Jens Axboe491381ce2019-10-17 09:20:46 -0600983 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700984 }
Jens Axboe491381ce2019-10-17 09:20:46 -0600985 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700986}
987
Jens Axboeba816ad2019-09-28 11:36:45 -0600988static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989{
990 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
991
Jens Axboe491381ce2019-10-17 09:20:46 -0600992 if (kiocb->ki_flags & IOCB_WRITE)
993 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700994
Jens Axboe9e645e112019-05-10 16:07:28 -0600995 if ((req->flags & REQ_F_LINK) && res != req->result)
996 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600997 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -0600998}
999
1000static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1001{
1002 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1003
1004 io_complete_rw_common(kiocb, res);
1005 io_put_req(req, NULL);
1006}
1007
1008static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1009{
1010 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1011
1012 io_complete_rw_common(kiocb, res);
1013 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001014}
1015
Jens Axboedef596e2019-01-09 08:59:42 -07001016static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1017{
1018 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1019
Jens Axboe491381ce2019-10-17 09:20:46 -06001020 if (kiocb->ki_flags & IOCB_WRITE)
1021 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001022
Jens Axboe9e645e112019-05-10 16:07:28 -06001023 if ((req->flags & REQ_F_LINK) && res != req->result)
1024 req->flags |= REQ_F_FAIL_LINK;
1025 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001026 if (res != -EAGAIN)
1027 req->flags |= REQ_F_IOPOLL_COMPLETED;
1028}
1029
1030/*
1031 * After the iocb has been issued, it's safe to be found on the poll list.
1032 * Adding the kiocb to the list AFTER submission ensures that we don't
1033 * find it from a io_iopoll_getevents() thread before the issuer is done
1034 * accessing the kiocb cookie.
1035 */
1036static void io_iopoll_req_issued(struct io_kiocb *req)
1037{
1038 struct io_ring_ctx *ctx = req->ctx;
1039
1040 /*
1041 * Track whether we have multiple files in our lists. This will impact
1042 * how we do polling eventually, not spinning if we're on potentially
1043 * different devices.
1044 */
1045 if (list_empty(&ctx->poll_list)) {
1046 ctx->poll_multi_file = false;
1047 } else if (!ctx->poll_multi_file) {
1048 struct io_kiocb *list_req;
1049
1050 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1051 list);
1052 if (list_req->rw.ki_filp != req->rw.ki_filp)
1053 ctx->poll_multi_file = true;
1054 }
1055
1056 /*
1057 * For fast devices, IO may have already completed. If it has, add
1058 * it to the front so we find it first.
1059 */
1060 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1061 list_add(&req->list, &ctx->poll_list);
1062 else
1063 list_add_tail(&req->list, &ctx->poll_list);
1064}
1065
Jens Axboe3d6770f2019-04-13 11:50:54 -06001066static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001067{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001068 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001069 int diff = state->has_refs - state->used_refs;
1070
1071 if (diff)
1072 fput_many(state->file, diff);
1073 state->file = NULL;
1074 }
1075}
1076
1077/*
1078 * Get as many references to a file as we have IOs left in this submission,
1079 * assuming most submissions are for one file, or at least that each file
1080 * has more than one submission.
1081 */
1082static struct file *io_file_get(struct io_submit_state *state, int fd)
1083{
1084 if (!state)
1085 return fget(fd);
1086
1087 if (state->file) {
1088 if (state->fd == fd) {
1089 state->used_refs++;
1090 state->ios_left--;
1091 return state->file;
1092 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001093 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001094 }
1095 state->file = fget_many(fd, state->ios_left);
1096 if (!state->file)
1097 return NULL;
1098
1099 state->fd = fd;
1100 state->has_refs = state->ios_left;
1101 state->used_refs = 1;
1102 state->ios_left--;
1103 return state->file;
1104}
1105
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106/*
1107 * If we tracked the file through the SCM inflight mechanism, we could support
1108 * any file. For now, just ensure that anything potentially problematic is done
1109 * inline.
1110 */
1111static bool io_file_supports_async(struct file *file)
1112{
1113 umode_t mode = file_inode(file)->i_mode;
1114
1115 if (S_ISBLK(mode) || S_ISCHR(mode))
1116 return true;
1117 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1118 return true;
1119
1120 return false;
1121}
1122
Jens Axboe6c271ce2019-01-10 11:22:30 -07001123static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001124 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001126 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001127 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001128 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001129 unsigned ioprio;
1130 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001131
Jens Axboe09bb8392019-03-13 12:39:28 -06001132 if (!req->file)
1133 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134
Jens Axboe491381ce2019-10-17 09:20:46 -06001135 if (S_ISREG(file_inode(req->file)->i_mode))
1136 req->flags |= REQ_F_ISREG;
1137
1138 /*
1139 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1140 * we know to async punt it even if it was opened O_NONBLOCK
1141 */
1142 if (force_nonblock && !io_file_supports_async(req->file)) {
1143 req->flags |= REQ_F_MUST_PUNT;
1144 return -EAGAIN;
1145 }
Jens Axboe6b063142019-01-10 22:13:58 -07001146
Jens Axboe2b188cc2019-01-07 10:46:33 -07001147 kiocb->ki_pos = READ_ONCE(sqe->off);
1148 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1149 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1150
1151 ioprio = READ_ONCE(sqe->ioprio);
1152 if (ioprio) {
1153 ret = ioprio_check_cap(ioprio);
1154 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001155 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001156
1157 kiocb->ki_ioprio = ioprio;
1158 } else
1159 kiocb->ki_ioprio = get_current_ioprio();
1160
1161 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1162 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001163 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001164
1165 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001166 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1167 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001168 req->flags |= REQ_F_NOWAIT;
1169
1170 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001172
Jens Axboedef596e2019-01-09 08:59:42 -07001173 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001174 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1175 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001176 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177
Jens Axboedef596e2019-01-09 08:59:42 -07001178 kiocb->ki_flags |= IOCB_HIPRI;
1179 kiocb->ki_complete = io_complete_rw_iopoll;
1180 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001181 if (kiocb->ki_flags & IOCB_HIPRI)
1182 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001183 kiocb->ki_complete = io_complete_rw;
1184 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001185 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001186}
1187
1188static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1189{
1190 switch (ret) {
1191 case -EIOCBQUEUED:
1192 break;
1193 case -ERESTARTSYS:
1194 case -ERESTARTNOINTR:
1195 case -ERESTARTNOHAND:
1196 case -ERESTART_RESTARTBLOCK:
1197 /*
1198 * We can't just restart the syscall, since previously
1199 * submitted sqes may already be in progress. Just fail this
1200 * IO with EINTR.
1201 */
1202 ret = -EINTR;
1203 /* fall through */
1204 default:
1205 kiocb->ki_complete(kiocb, ret, 0);
1206 }
1207}
1208
Jens Axboeba816ad2019-09-28 11:36:45 -06001209static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1210 bool in_async)
1211{
1212 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1213 *nxt = __io_complete_rw(kiocb, ret);
1214 else
1215 io_rw_done(kiocb, ret);
1216}
1217
Jens Axboeedafcce2019-01-09 09:16:05 -07001218static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1219 const struct io_uring_sqe *sqe,
1220 struct iov_iter *iter)
1221{
1222 size_t len = READ_ONCE(sqe->len);
1223 struct io_mapped_ubuf *imu;
1224 unsigned index, buf_index;
1225 size_t offset;
1226 u64 buf_addr;
1227
1228 /* attempt to use fixed buffers without having provided iovecs */
1229 if (unlikely(!ctx->user_bufs))
1230 return -EFAULT;
1231
1232 buf_index = READ_ONCE(sqe->buf_index);
1233 if (unlikely(buf_index >= ctx->nr_user_bufs))
1234 return -EFAULT;
1235
1236 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1237 imu = &ctx->user_bufs[index];
1238 buf_addr = READ_ONCE(sqe->addr);
1239
1240 /* overflow */
1241 if (buf_addr + len < buf_addr)
1242 return -EFAULT;
1243 /* not inside the mapped region */
1244 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1245 return -EFAULT;
1246
1247 /*
1248 * May not be a start of buffer, set size appropriately
1249 * and advance us to the beginning.
1250 */
1251 offset = buf_addr - imu->ubuf;
1252 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001253
1254 if (offset) {
1255 /*
1256 * Don't use iov_iter_advance() here, as it's really slow for
1257 * using the latter parts of a big fixed buffer - it iterates
1258 * over each segment manually. We can cheat a bit here, because
1259 * we know that:
1260 *
1261 * 1) it's a BVEC iter, we set it up
1262 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1263 * first and last bvec
1264 *
1265 * So just find our index, and adjust the iterator afterwards.
1266 * If the offset is within the first bvec (or the whole first
1267 * bvec, just use iov_iter_advance(). This makes it easier
1268 * since we can just skip the first segment, which may not
1269 * be PAGE_SIZE aligned.
1270 */
1271 const struct bio_vec *bvec = imu->bvec;
1272
1273 if (offset <= bvec->bv_len) {
1274 iov_iter_advance(iter, offset);
1275 } else {
1276 unsigned long seg_skip;
1277
1278 /* skip first vec */
1279 offset -= bvec->bv_len;
1280 seg_skip = 1 + (offset >> PAGE_SHIFT);
1281
1282 iter->bvec = bvec + seg_skip;
1283 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001284 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001285 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001286 }
1287 }
1288
Jens Axboeedafcce2019-01-09 09:16:05 -07001289 return 0;
1290}
1291
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001292static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1293 const struct sqe_submit *s, struct iovec **iovec,
1294 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295{
1296 const struct io_uring_sqe *sqe = s->sqe;
1297 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1298 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001299 u8 opcode;
1300
1301 /*
1302 * We're reading ->opcode for the second time, but the first read
1303 * doesn't care whether it's _FIXED or not, so it doesn't matter
1304 * whether ->opcode changes concurrently. The first read does care
1305 * about whether it is a READ or a WRITE, so we don't trust this read
1306 * for that purpose and instead let the caller pass in the read/write
1307 * flag.
1308 */
1309 opcode = READ_ONCE(sqe->opcode);
1310 if (opcode == IORING_OP_READ_FIXED ||
1311 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001312 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001313 *iovec = NULL;
1314 return ret;
1315 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001316
1317 if (!s->has_user)
1318 return -EFAULT;
1319
1320#ifdef CONFIG_COMPAT
1321 if (ctx->compat)
1322 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1323 iovec, iter);
1324#endif
1325
1326 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1327}
1328
Jens Axboe32960612019-09-23 11:05:34 -06001329/*
1330 * For files that don't have ->read_iter() and ->write_iter(), handle them
1331 * by looping over ->read() or ->write() manually.
1332 */
1333static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1334 struct iov_iter *iter)
1335{
1336 ssize_t ret = 0;
1337
1338 /*
1339 * Don't support polled IO through this interface, and we can't
1340 * support non-blocking either. For the latter, this just causes
1341 * the kiocb to be handled from an async context.
1342 */
1343 if (kiocb->ki_flags & IOCB_HIPRI)
1344 return -EOPNOTSUPP;
1345 if (kiocb->ki_flags & IOCB_NOWAIT)
1346 return -EAGAIN;
1347
1348 while (iov_iter_count(iter)) {
1349 struct iovec iovec = iov_iter_iovec(iter);
1350 ssize_t nr;
1351
1352 if (rw == READ) {
1353 nr = file->f_op->read(file, iovec.iov_base,
1354 iovec.iov_len, &kiocb->ki_pos);
1355 } else {
1356 nr = file->f_op->write(file, iovec.iov_base,
1357 iovec.iov_len, &kiocb->ki_pos);
1358 }
1359
1360 if (nr < 0) {
1361 if (!ret)
1362 ret = nr;
1363 break;
1364 }
1365 ret += nr;
1366 if (nr != iovec.iov_len)
1367 break;
1368 iov_iter_advance(iter, nr);
1369 }
1370
1371 return ret;
1372}
1373
Jens Axboee0c5c572019-03-12 10:18:47 -06001374static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001375 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001376{
1377 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1378 struct kiocb *kiocb = &req->rw;
1379 struct iov_iter iter;
1380 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001381 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001382 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001383
Jens Axboe8358e3a2019-04-23 08:17:58 -06001384 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001385 if (ret)
1386 return ret;
1387 file = kiocb->ki_filp;
1388
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001390 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001391
1392 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001393 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001394 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001395
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001396 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001397 if (req->flags & REQ_F_LINK)
1398 req->result = read_size;
1399
Jens Axboe31b51512019-01-18 22:56:34 -07001400 iov_count = iov_iter_count(&iter);
1401 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001402 if (!ret) {
1403 ssize_t ret2;
1404
Jens Axboe32960612019-09-23 11:05:34 -06001405 if (file->f_op->read_iter)
1406 ret2 = call_read_iter(file, kiocb, &iter);
1407 else
1408 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1409
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001410 /*
1411 * In case of a short read, punt to async. This can happen
1412 * if we have data partially cached. Alternatively we can
1413 * return the short read, in which case the application will
1414 * need to issue another SQE and wait for it. That SQE will
1415 * need async punt anyway, so it's more efficient to do it
1416 * here.
1417 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001418 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1419 (req->flags & REQ_F_ISREG) &&
1420 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001421 ret2 = -EAGAIN;
1422 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001423 if (!force_nonblock || ret2 != -EAGAIN)
Jackie Liuba5290c2019-10-09 09:19:59 +08001424 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001425 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426 ret = -EAGAIN;
1427 }
1428 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429 return ret;
1430}
1431
Jens Axboee0c5c572019-03-12 10:18:47 -06001432static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001433 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001434{
1435 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1436 struct kiocb *kiocb = &req->rw;
1437 struct iov_iter iter;
1438 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001439 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001440 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001441
Jens Axboe8358e3a2019-04-23 08:17:58 -06001442 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443 if (ret)
1444 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001445
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446 file = kiocb->ki_filp;
1447 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001448 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001449
1450 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001451 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001452 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001453
Jens Axboe9e645e112019-05-10 16:07:28 -06001454 if (req->flags & REQ_F_LINK)
1455 req->result = ret;
1456
Jens Axboe31b51512019-01-18 22:56:34 -07001457 iov_count = iov_iter_count(&iter);
1458
1459 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001460 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001461 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001462
1463 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001464 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001465 ssize_t ret2;
1466
Jens Axboe2b188cc2019-01-07 10:46:33 -07001467 /*
1468 * Open-code file_start_write here to grab freeze protection,
1469 * which will be released by another thread in
1470 * io_complete_rw(). Fool lockdep by telling it the lock got
1471 * released so that it doesn't complain about the held lock when
1472 * we return to userspace.
1473 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001474 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001475 __sb_start_write(file_inode(file)->i_sb,
1476 SB_FREEZE_WRITE, true);
1477 __sb_writers_release(file_inode(file)->i_sb,
1478 SB_FREEZE_WRITE);
1479 }
1480 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001481
Jens Axboe32960612019-09-23 11:05:34 -06001482 if (file->f_op->write_iter)
1483 ret2 = call_write_iter(file, kiocb, &iter);
1484 else
1485 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001486 if (!force_nonblock || ret2 != -EAGAIN)
Jackie Liuba5290c2019-10-09 09:19:59 +08001487 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001488 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001489 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490 }
Jens Axboe31b51512019-01-18 22:56:34 -07001491out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001492 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001493 return ret;
1494}
1495
1496/*
1497 * IORING_OP_NOP just posts a completion event, nothing else.
1498 */
1499static int io_nop(struct io_kiocb *req, u64 user_data)
1500{
1501 struct io_ring_ctx *ctx = req->ctx;
1502 long err = 0;
1503
Jens Axboedef596e2019-01-09 08:59:42 -07001504 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1505 return -EINVAL;
1506
Jens Axboec71ffb62019-05-13 20:58:29 -06001507 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001508 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001509 return 0;
1510}
1511
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001512static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1513{
Jens Axboe6b063142019-01-10 22:13:58 -07001514 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001515
Jens Axboe09bb8392019-03-13 12:39:28 -06001516 if (!req->file)
1517 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001518
Jens Axboe6b063142019-01-10 22:13:58 -07001519 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001520 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001521 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001522 return -EINVAL;
1523
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001524 return 0;
1525}
1526
1527static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001528 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001529{
1530 loff_t sqe_off = READ_ONCE(sqe->off);
1531 loff_t sqe_len = READ_ONCE(sqe->len);
1532 loff_t end = sqe_off + sqe_len;
1533 unsigned fsync_flags;
1534 int ret;
1535
1536 fsync_flags = READ_ONCE(sqe->fsync_flags);
1537 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1538 return -EINVAL;
1539
1540 ret = io_prep_fsync(req, sqe);
1541 if (ret)
1542 return ret;
1543
1544 /* fsync always requires a blocking context */
1545 if (force_nonblock)
1546 return -EAGAIN;
1547
1548 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1549 end > 0 ? end : LLONG_MAX,
1550 fsync_flags & IORING_FSYNC_DATASYNC);
1551
Jens Axboe9e645e112019-05-10 16:07:28 -06001552 if (ret < 0 && (req->flags & REQ_F_LINK))
1553 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001554 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001555 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001556 return 0;
1557}
1558
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001559static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1560{
1561 struct io_ring_ctx *ctx = req->ctx;
1562 int ret = 0;
1563
1564 if (!req->file)
1565 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001566
1567 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1568 return -EINVAL;
1569 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1570 return -EINVAL;
1571
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001572 return ret;
1573}
1574
1575static int io_sync_file_range(struct io_kiocb *req,
1576 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001577 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001578 bool force_nonblock)
1579{
1580 loff_t sqe_off;
1581 loff_t sqe_len;
1582 unsigned flags;
1583 int ret;
1584
1585 ret = io_prep_sfr(req, sqe);
1586 if (ret)
1587 return ret;
1588
1589 /* sync_file_range always requires a blocking context */
1590 if (force_nonblock)
1591 return -EAGAIN;
1592
1593 sqe_off = READ_ONCE(sqe->off);
1594 sqe_len = READ_ONCE(sqe->len);
1595 flags = READ_ONCE(sqe->sync_range_flags);
1596
1597 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1598
Jens Axboe9e645e112019-05-10 16:07:28 -06001599 if (ret < 0 && (req->flags & REQ_F_LINK))
1600 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001601 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001602 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001603 return 0;
1604}
1605
Jens Axboe0fa03c62019-04-19 13:34:07 -06001606#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001607static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001608 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001609 long (*fn)(struct socket *, struct user_msghdr __user *,
1610 unsigned int))
1611{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001612 struct socket *sock;
1613 int ret;
1614
1615 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1616 return -EINVAL;
1617
1618 sock = sock_from_file(req->file, &ret);
1619 if (sock) {
1620 struct user_msghdr __user *msg;
1621 unsigned flags;
1622
1623 flags = READ_ONCE(sqe->msg_flags);
1624 if (flags & MSG_DONTWAIT)
1625 req->flags |= REQ_F_NOWAIT;
1626 else if (force_nonblock)
1627 flags |= MSG_DONTWAIT;
1628
1629 msg = (struct user_msghdr __user *) (unsigned long)
1630 READ_ONCE(sqe->addr);
1631
Jens Axboeaa1fa282019-04-19 13:38:09 -06001632 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001633 if (force_nonblock && ret == -EAGAIN)
1634 return ret;
1635 }
1636
1637 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001638 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001639 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001640}
1641#endif
1642
1643static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001644 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001645{
1646#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001647 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1648 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001649#else
1650 return -EOPNOTSUPP;
1651#endif
1652}
1653
1654static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001655 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001656{
1657#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001658 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1659 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001660#else
1661 return -EOPNOTSUPP;
1662#endif
1663}
1664
Jens Axboe221c5eb2019-01-17 09:41:58 -07001665static void io_poll_remove_one(struct io_kiocb *req)
1666{
1667 struct io_poll_iocb *poll = &req->poll;
1668
1669 spin_lock(&poll->head->lock);
1670 WRITE_ONCE(poll->canceled, true);
1671 if (!list_empty(&poll->wait.entry)) {
1672 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001673 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001674 }
1675 spin_unlock(&poll->head->lock);
1676
1677 list_del_init(&req->list);
1678}
1679
1680static void io_poll_remove_all(struct io_ring_ctx *ctx)
1681{
1682 struct io_kiocb *req;
1683
1684 spin_lock_irq(&ctx->completion_lock);
1685 while (!list_empty(&ctx->cancel_list)) {
1686 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1687 io_poll_remove_one(req);
1688 }
1689 spin_unlock_irq(&ctx->completion_lock);
1690}
1691
1692/*
1693 * Find a running poll command that matches one specified in sqe->addr,
1694 * and remove it if found.
1695 */
1696static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1697{
1698 struct io_ring_ctx *ctx = req->ctx;
1699 struct io_kiocb *poll_req, *next;
1700 int ret = -ENOENT;
1701
1702 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1703 return -EINVAL;
1704 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1705 sqe->poll_events)
1706 return -EINVAL;
1707
1708 spin_lock_irq(&ctx->completion_lock);
1709 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1710 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1711 io_poll_remove_one(poll_req);
1712 ret = 0;
1713 break;
1714 }
1715 }
1716 spin_unlock_irq(&ctx->completion_lock);
1717
Jens Axboec71ffb62019-05-13 20:58:29 -06001718 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001719 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001720 return 0;
1721}
1722
Jens Axboe8c838782019-03-12 15:48:16 -06001723static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1724 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001725{
Jens Axboe8c838782019-03-12 15:48:16 -06001726 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001727 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001728 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001729}
1730
Jens Axboe561fb042019-10-24 07:25:42 -06001731static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001732{
Jens Axboe561fb042019-10-24 07:25:42 -06001733 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001734 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1735 struct io_poll_iocb *poll = &req->poll;
1736 struct poll_table_struct pt = { ._key = poll->events };
1737 struct io_ring_ctx *ctx = req->ctx;
1738 __poll_t mask = 0;
1739
Jens Axboe561fb042019-10-24 07:25:42 -06001740 if (work->flags & IO_WQ_WORK_CANCEL)
1741 WRITE_ONCE(poll->canceled, true);
1742
Jens Axboe221c5eb2019-01-17 09:41:58 -07001743 if (!READ_ONCE(poll->canceled))
1744 mask = vfs_poll(poll->file, &pt) & poll->events;
1745
1746 /*
1747 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1748 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1749 * synchronize with them. In the cancellation case the list_del_init
1750 * itself is not actually needed, but harmless so we keep it in to
1751 * avoid further branches in the fast path.
1752 */
1753 spin_lock_irq(&ctx->completion_lock);
1754 if (!mask && !READ_ONCE(poll->canceled)) {
1755 add_wait_queue(poll->head, &poll->wait);
1756 spin_unlock_irq(&ctx->completion_lock);
1757 return;
1758 }
1759 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001760 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001761 spin_unlock_irq(&ctx->completion_lock);
1762
Jens Axboe8c838782019-03-12 15:48:16 -06001763 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001764 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001765}
1766
1767static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1768 void *key)
1769{
1770 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1771 wait);
1772 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1773 struct io_ring_ctx *ctx = req->ctx;
1774 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001775 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001776
1777 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001778 if (mask && !(mask & poll->events))
1779 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001780
1781 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001782
1783 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1784 list_del(&req->list);
1785 io_poll_complete(ctx, req, mask);
1786 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1787
1788 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001789 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001790 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001791 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001792 }
1793
Jens Axboe221c5eb2019-01-17 09:41:58 -07001794 return 1;
1795}
1796
1797struct io_poll_table {
1798 struct poll_table_struct pt;
1799 struct io_kiocb *req;
1800 int error;
1801};
1802
1803static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1804 struct poll_table_struct *p)
1805{
1806 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1807
1808 if (unlikely(pt->req->poll.head)) {
1809 pt->error = -EINVAL;
1810 return;
1811 }
1812
1813 pt->error = 0;
1814 pt->req->poll.head = head;
1815 add_wait_queue(head, &pt->req->poll.wait);
1816}
1817
1818static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1819{
1820 struct io_poll_iocb *poll = &req->poll;
1821 struct io_ring_ctx *ctx = req->ctx;
1822 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001823 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001824 __poll_t mask;
1825 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001826
1827 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1828 return -EINVAL;
1829 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1830 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001831 if (!poll->file)
1832 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001833
Jens Axboe6cc47d12019-09-18 11:18:23 -06001834 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06001835 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001836 events = READ_ONCE(sqe->poll_events);
1837 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1838
Jens Axboe221c5eb2019-01-17 09:41:58 -07001839 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001840 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001841 poll->canceled = false;
1842
1843 ipt.pt._qproc = io_poll_queue_proc;
1844 ipt.pt._key = poll->events;
1845 ipt.req = req;
1846 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1847
1848 /* initialized the list so that we can do list_empty checks */
1849 INIT_LIST_HEAD(&poll->wait.entry);
1850 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1851
Jens Axboe36703242019-07-25 10:20:18 -06001852 INIT_LIST_HEAD(&req->list);
1853
Jens Axboe221c5eb2019-01-17 09:41:58 -07001854 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001855
1856 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001857 if (likely(poll->head)) {
1858 spin_lock(&poll->head->lock);
1859 if (unlikely(list_empty(&poll->wait.entry))) {
1860 if (ipt.error)
1861 cancel = true;
1862 ipt.error = 0;
1863 mask = 0;
1864 }
1865 if (mask || ipt.error)
1866 list_del_init(&poll->wait.entry);
1867 else if (cancel)
1868 WRITE_ONCE(poll->canceled, true);
1869 else if (!poll->done) /* actually waiting for an event */
1870 list_add_tail(&req->list, &ctx->cancel_list);
1871 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001872 }
Jens Axboe8c838782019-03-12 15:48:16 -06001873 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001874 ipt.error = 0;
1875 io_poll_complete(ctx, req, mask);
1876 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001877 spin_unlock_irq(&ctx->completion_lock);
1878
Jens Axboe8c838782019-03-12 15:48:16 -06001879 if (mask) {
1880 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001881 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001882 }
Jens Axboe8c838782019-03-12 15:48:16 -06001883 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001884}
1885
Jens Axboe5262f562019-09-17 12:26:57 -06001886static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1887{
1888 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06001889 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06001890 unsigned long flags;
Jens Axboe11365042019-10-16 09:08:32 -06001891 bool comp;
Jens Axboe5262f562019-09-17 12:26:57 -06001892
1893 req = container_of(timer, struct io_kiocb, timeout.timer);
1894 ctx = req->ctx;
1895 atomic_inc(&ctx->cq_timeouts);
1896
1897 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001898 /*
Jens Axboe11365042019-10-16 09:08:32 -06001899 * We could be racing with timeout deletion. If the list is empty,
1900 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08001901 */
Jens Axboe11365042019-10-16 09:08:32 -06001902 comp = !list_empty(&req->list);
1903 if (comp) {
1904 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001905
Jens Axboe11365042019-10-16 09:08:32 -06001906 /*
1907 * Adjust the reqs sequence before the current one because it
1908 * will consume a slot in the cq_ring and the the cq_tail
1909 * pointer will be increased, otherwise other timeout reqs may
1910 * return in advance without waiting for enough wait_nr.
1911 */
1912 prev = req;
1913 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1914 prev->sequence++;
1915
1916 list_del_init(&req->list);
1917 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1918 io_commit_cqring(ctx);
1919 }
Jens Axboe5262f562019-09-17 12:26:57 -06001920 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1921
Jens Axboe11365042019-10-16 09:08:32 -06001922 if (comp) {
1923 io_cqring_ev_posted(ctx);
1924 io_put_req(req, NULL);
1925 }
1926 return HRTIMER_NORESTART;
1927}
1928
1929/*
1930 * Remove or update an existing timeout command
1931 */
1932static int io_timeout_remove(struct io_kiocb *req,
1933 const struct io_uring_sqe *sqe)
1934{
1935 struct io_ring_ctx *ctx = req->ctx;
1936 struct io_kiocb *treq;
1937 int ret = -ENOENT;
1938 __u64 user_data;
1939 unsigned flags;
1940
1941 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1942 return -EINVAL;
1943 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
1944 return -EINVAL;
1945 flags = READ_ONCE(sqe->timeout_flags);
1946 if (flags)
1947 return -EINVAL;
1948
1949 user_data = READ_ONCE(sqe->addr);
1950 spin_lock_irq(&ctx->completion_lock);
1951 list_for_each_entry(treq, &ctx->timeout_list, list) {
1952 if (user_data == treq->user_data) {
1953 list_del_init(&treq->list);
1954 ret = 0;
1955 break;
1956 }
1957 }
1958
1959 /* didn't find timeout */
1960 if (ret) {
1961fill_ev:
1962 io_cqring_fill_event(ctx, req->user_data, ret);
1963 io_commit_cqring(ctx);
1964 spin_unlock_irq(&ctx->completion_lock);
1965 io_cqring_ev_posted(ctx);
1966 io_put_req(req, NULL);
1967 return 0;
1968 }
1969
1970 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
1971 if (ret == -1) {
1972 ret = -EBUSY;
1973 goto fill_ev;
1974 }
1975
1976 io_cqring_fill_event(ctx, req->user_data, 0);
1977 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
1978 io_commit_cqring(ctx);
1979 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06001980 io_cqring_ev_posted(ctx);
1981
Jens Axboe11365042019-10-16 09:08:32 -06001982 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06001983 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06001984 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001985}
1986
1987static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1988{
yangerkun5da0fb12019-10-15 21:59:29 +08001989 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06001990 struct io_ring_ctx *ctx = req->ctx;
1991 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06001992 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001993 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001994 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06001995 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06001996
1997 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1998 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06001999 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2000 return -EINVAL;
2001 flags = READ_ONCE(sqe->timeout_flags);
2002 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002003 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002004
2005 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002006 return -EFAULT;
2007
Jens Axboe11365042019-10-16 09:08:32 -06002008 if (flags & IORING_TIMEOUT_ABS)
2009 mode = HRTIMER_MODE_ABS;
2010 else
2011 mode = HRTIMER_MODE_REL;
2012
2013 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2014
Jens Axboe5262f562019-09-17 12:26:57 -06002015 /*
2016 * sqe->off holds how many events that need to occur for this
2017 * timeout event to be satisfied.
2018 */
2019 count = READ_ONCE(sqe->off);
2020 if (!count)
2021 count = 1;
2022
2023 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002024 /* reuse it to store the count */
2025 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002026 req->flags |= REQ_F_TIMEOUT;
2027
2028 /*
2029 * Insertion sort, ensuring the first entry in the list is always
2030 * the one we need first.
2031 */
Jens Axboe5262f562019-09-17 12:26:57 -06002032 spin_lock_irq(&ctx->completion_lock);
2033 list_for_each_prev(entry, &ctx->timeout_list) {
2034 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002035 unsigned nxt_sq_head;
2036 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002037
yangerkun5da0fb12019-10-15 21:59:29 +08002038 /*
2039 * Since cached_sq_head + count - 1 can overflow, use type long
2040 * long to store it.
2041 */
2042 tmp = (long long)ctx->cached_sq_head + count - 1;
2043 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2044 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2045
2046 /*
2047 * cached_sq_head may overflow, and it will never overflow twice
2048 * once there is some timeout req still be valid.
2049 */
2050 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002051 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002052
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002053 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002054 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002055
2056 /*
2057 * Sequence of reqs after the insert one and itself should
2058 * be adjusted because each timeout req consumes a slot.
2059 */
2060 span++;
2061 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002062 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002063 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002064 list_add(&req->list, entry);
2065 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002066 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002067 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe5262f562019-09-17 12:26:57 -06002068 return 0;
2069}
2070
Jens Axboede0617e2019-04-06 21:51:27 -06002071static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2072 const struct io_uring_sqe *sqe)
2073{
2074 struct io_uring_sqe *sqe_copy;
2075
2076 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2077 return 0;
2078
2079 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2080 if (!sqe_copy)
2081 return -EAGAIN;
2082
2083 spin_lock_irq(&ctx->completion_lock);
2084 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2085 spin_unlock_irq(&ctx->completion_lock);
2086 kfree(sqe_copy);
2087 return 0;
2088 }
2089
2090 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2091 req->submit.sqe = sqe_copy;
2092
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002093 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002094 list_add_tail(&req->list, &ctx->defer_list);
2095 spin_unlock_irq(&ctx->completion_lock);
2096 return -EIOCBQUEUED;
2097}
2098
Jens Axboe2b188cc2019-01-07 10:46:33 -07002099static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboeba816ad2019-09-28 11:36:45 -06002100 const struct sqe_submit *s, struct io_kiocb **nxt,
2101 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002102{
Jens Axboee0c5c572019-03-12 10:18:47 -06002103 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002104
Jens Axboe9e645e112019-05-10 16:07:28 -06002105 req->user_data = READ_ONCE(s->sqe->user_data);
2106
Jens Axboe2b188cc2019-01-07 10:46:33 -07002107 opcode = READ_ONCE(s->sqe->opcode);
2108 switch (opcode) {
2109 case IORING_OP_NOP:
2110 ret = io_nop(req, req->user_data);
2111 break;
2112 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002113 if (unlikely(s->sqe->buf_index))
2114 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002115 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002116 break;
2117 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002118 if (unlikely(s->sqe->buf_index))
2119 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002120 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002121 break;
2122 case IORING_OP_READ_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002123 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002124 break;
2125 case IORING_OP_WRITE_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002126 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002127 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002128 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002129 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002130 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002131 case IORING_OP_POLL_ADD:
2132 ret = io_poll_add(req, s->sqe);
2133 break;
2134 case IORING_OP_POLL_REMOVE:
2135 ret = io_poll_remove(req, s->sqe);
2136 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002137 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002138 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002139 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002140 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002141 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002142 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002143 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002144 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002145 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002146 case IORING_OP_TIMEOUT:
2147 ret = io_timeout(req, s->sqe);
2148 break;
Jens Axboe11365042019-10-16 09:08:32 -06002149 case IORING_OP_TIMEOUT_REMOVE:
2150 ret = io_timeout_remove(req, s->sqe);
2151 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002152 default:
2153 ret = -EINVAL;
2154 break;
2155 }
2156
Jens Axboedef596e2019-01-09 08:59:42 -07002157 if (ret)
2158 return ret;
2159
2160 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002161 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002162 return -EAGAIN;
2163
2164 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002165 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002166 mutex_lock(&ctx->uring_lock);
2167 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002168 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002169 mutex_unlock(&ctx->uring_lock);
2170 }
2171
2172 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002173}
2174
Jens Axboe561fb042019-10-24 07:25:42 -06002175static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002176{
Jens Axboe561fb042019-10-24 07:25:42 -06002177 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002178 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002179 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002180 struct sqe_submit *s = &req->submit;
2181 const struct io_uring_sqe *sqe = s->sqe;
2182 struct io_kiocb *nxt = NULL;
2183 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002184
Jens Axboe561fb042019-10-24 07:25:42 -06002185 /* Ensure we clear previously set non-block flag */
2186 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002187
Jens Axboe561fb042019-10-24 07:25:42 -06002188 if (work->flags & IO_WQ_WORK_CANCEL)
2189 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002190
Jens Axboe561fb042019-10-24 07:25:42 -06002191 if (!ret) {
2192 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2193 s->in_async = true;
2194 do {
2195 ret = __io_submit_sqe(ctx, req, s, &nxt, false);
2196 /*
2197 * We can get EAGAIN for polled IO even though we're
2198 * forcing a sync submission from here, since we can't
2199 * wait for request slots on the block side.
2200 */
2201 if (ret != -EAGAIN)
2202 break;
2203 cond_resched();
2204 } while (1);
2205 }
Jens Axboe31b51512019-01-18 22:56:34 -07002206
Jens Axboe561fb042019-10-24 07:25:42 -06002207 /* drop submission reference */
2208 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002209
Jens Axboe561fb042019-10-24 07:25:42 -06002210 if (ret) {
2211 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002212 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002213 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002214
Jens Axboe561fb042019-10-24 07:25:42 -06002215 /* async context always use a copy of the sqe */
2216 kfree(sqe);
2217
2218 /* if a dependent link is ready, pass it back */
2219 if (!ret && nxt) {
2220 io_prep_async_work(nxt);
2221 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002222 }
Jens Axboe31b51512019-01-18 22:56:34 -07002223}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002224
Jens Axboe09bb8392019-03-13 12:39:28 -06002225static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2226{
2227 int op = READ_ONCE(sqe->opcode);
2228
2229 switch (op) {
2230 case IORING_OP_NOP:
2231 case IORING_OP_POLL_REMOVE:
2232 return false;
2233 default:
2234 return true;
2235 }
2236}
2237
2238static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2239 struct io_submit_state *state, struct io_kiocb *req)
2240{
2241 unsigned flags;
2242 int fd;
2243
2244 flags = READ_ONCE(s->sqe->flags);
2245 fd = READ_ONCE(s->sqe->fd);
2246
Jackie Liu4fe2c962019-09-09 20:50:40 +08002247 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002248 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002249 /*
2250 * All io need record the previous position, if LINK vs DARIN,
2251 * it can be used to mark the position of the first IO in the
2252 * link list.
2253 */
2254 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002255
Jens Axboe60c112b2019-06-21 10:20:18 -06002256 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002257 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002258
2259 if (flags & IOSQE_FIXED_FILE) {
2260 if (unlikely(!ctx->user_files ||
2261 (unsigned) fd >= ctx->nr_user_files))
2262 return -EBADF;
Jens Axboe08a45172019-10-03 08:11:03 -06002263 if (!ctx->user_files[fd])
2264 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002265 req->file = ctx->user_files[fd];
2266 req->flags |= REQ_F_FIXED_FILE;
2267 } else {
2268 if (s->needs_fixed_file)
2269 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002270 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002271 req->file = io_file_get(state, fd);
2272 if (unlikely(!req->file))
2273 return -EBADF;
2274 }
2275
2276 return 0;
2277}
2278
Jackie Liu4fe2c962019-09-09 20:50:40 +08002279static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002280 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281{
Jens Axboee0c5c572019-03-12 10:18:47 -06002282 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002283
Jens Axboeba816ad2019-09-28 11:36:45 -06002284 ret = __io_submit_sqe(ctx, req, s, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002285
2286 /*
2287 * We async punt it if the file wasn't marked NOWAIT, or if the file
2288 * doesn't support non-blocking read/write attempts
2289 */
2290 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2291 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292 struct io_uring_sqe *sqe_copy;
2293
Jackie Liu954dab12019-09-18 10:37:52 +08002294 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002295 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002296 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002297 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe561fb042019-10-24 07:25:42 -06002298 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002299
2300 /*
2301 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002302 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002303 */
2304 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002305 }
2306 }
Jens Axboee65ef562019-03-12 10:16:44 -06002307
2308 /* drop submission reference */
Jens Axboeba816ad2019-09-28 11:36:45 -06002309 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002310
2311 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002312 if (ret) {
2313 io_cqring_add_event(ctx, req->user_data, ret);
2314 if (req->flags & REQ_F_LINK)
2315 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002316 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002317 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002318
2319 return ret;
2320}
2321
Jackie Liu4fe2c962019-09-09 20:50:40 +08002322static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002323 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002324{
2325 int ret;
2326
2327 ret = io_req_defer(ctx, req, s->sqe);
2328 if (ret) {
2329 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002330 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002331 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2332 }
2333 return 0;
2334 }
2335
Jens Axboebc808bc2019-10-22 13:14:37 -06002336 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002337}
2338
2339static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002340 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002341{
2342 int ret;
2343 int need_submit = false;
2344
2345 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002346 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002347
2348 /*
2349 * Mark the first IO in link list as DRAIN, let all the following
2350 * IOs enter the defer list. all IO needs to be completed before link
2351 * list.
2352 */
2353 req->flags |= REQ_F_IO_DRAIN;
2354 ret = io_req_defer(ctx, req, s->sqe);
2355 if (ret) {
2356 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002357 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002358 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002359 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2360 return 0;
2361 }
2362 } else {
2363 /*
2364 * If ret == 0 means that all IOs in front of link io are
2365 * running done. let's queue link head.
2366 */
2367 need_submit = true;
2368 }
2369
2370 /* Insert shadow req to defer_list, blocking next IOs */
2371 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002372 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002373 list_add_tail(&shadow->list, &ctx->defer_list);
2374 spin_unlock_irq(&ctx->completion_lock);
2375
2376 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002377 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002378
2379 return 0;
2380}
2381
Jens Axboe9e645e112019-05-10 16:07:28 -06002382#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2383
2384static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002385 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002386{
2387 struct io_uring_sqe *sqe_copy;
2388 struct io_kiocb *req;
2389 int ret;
2390
2391 /* enforce forwards compatibility on users */
2392 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2393 ret = -EINVAL;
2394 goto err;
2395 }
2396
2397 req = io_get_req(ctx, state);
2398 if (unlikely(!req)) {
2399 ret = -EAGAIN;
2400 goto err;
2401 }
2402
2403 ret = io_req_set_file(ctx, s, state, req);
2404 if (unlikely(ret)) {
2405err_req:
Jens Axboeba816ad2019-09-28 11:36:45 -06002406 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002407err:
2408 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2409 return;
2410 }
2411
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002412 req->user_data = s->sqe->user_data;
2413
Jens Axboe9e645e112019-05-10 16:07:28 -06002414 /*
2415 * If we already have a head request, queue this one for async
2416 * submittal once the head completes. If we don't have a head but
2417 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2418 * submitted sync once the chain is complete. If none of those
2419 * conditions are true (normal request), then just queue it.
2420 */
2421 if (*link) {
2422 struct io_kiocb *prev = *link;
2423
2424 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2425 if (!sqe_copy) {
2426 ret = -EAGAIN;
2427 goto err_req;
2428 }
2429
2430 s->sqe = sqe_copy;
2431 memcpy(&req->submit, s, sizeof(*s));
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002432 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002433 list_add_tail(&req->list, &prev->link_list);
2434 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2435 req->flags |= REQ_F_LINK;
2436
2437 memcpy(&req->submit, s, sizeof(*s));
2438 INIT_LIST_HEAD(&req->link_list);
2439 *link = req;
2440 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002441 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002442 }
2443}
2444
Jens Axboe9a56a232019-01-09 09:06:50 -07002445/*
2446 * Batched submission is done, ensure local IO is flushed out.
2447 */
2448static void io_submit_state_end(struct io_submit_state *state)
2449{
2450 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002451 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002452 if (state->free_reqs)
2453 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2454 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002455}
2456
2457/*
2458 * Start submission side cache.
2459 */
2460static void io_submit_state_start(struct io_submit_state *state,
2461 struct io_ring_ctx *ctx, unsigned max_ios)
2462{
2463 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002464 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002465 state->file = NULL;
2466 state->ios_left = max_ios;
2467}
2468
Jens Axboe2b188cc2019-01-07 10:46:33 -07002469static void io_commit_sqring(struct io_ring_ctx *ctx)
2470{
Hristo Venev75b28af2019-08-26 17:23:46 +00002471 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002472
Hristo Venev75b28af2019-08-26 17:23:46 +00002473 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002474 /*
2475 * Ensure any loads from the SQEs are done at this point,
2476 * since once we write the new head, the application could
2477 * write new data to them.
2478 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002479 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002480 }
2481}
2482
2483/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002484 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2485 * that is mapped by userspace. This means that care needs to be taken to
2486 * ensure that reads are stable, as we cannot rely on userspace always
2487 * being a good citizen. If members of the sqe are validated and then later
2488 * used, it's important that those reads are done through READ_ONCE() to
2489 * prevent a re-load down the line.
2490 */
2491static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2492{
Hristo Venev75b28af2019-08-26 17:23:46 +00002493 struct io_rings *rings = ctx->rings;
2494 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002495 unsigned head;
2496
2497 /*
2498 * The cached sq head (or cq tail) serves two purposes:
2499 *
2500 * 1) allows us to batch the cost of updating the user visible
2501 * head updates.
2502 * 2) allows the kernel side to track the head on its own, even
2503 * though the application is the one updating it.
2504 */
2505 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002506 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002507 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002508 return false;
2509
Hristo Venev75b28af2019-08-26 17:23:46 +00002510 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002511 if (head < ctx->sq_entries) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002512 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002513 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002514 ctx->cached_sq_head++;
2515 return true;
2516 }
2517
2518 /* drop invalid entries */
2519 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002520 ctx->cached_sq_dropped++;
2521 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002522 return false;
2523}
2524
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002525static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002526 struct mm_struct **mm)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002527{
2528 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002529 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002530 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002531 bool prev_was_link = false;
2532 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002533 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002534
2535 if (nr > IO_PLUG_THRESHOLD) {
2536 io_submit_state_start(&state, ctx, nr);
2537 statep = &state;
2538 }
2539
2540 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002541 struct sqe_submit s;
2542
2543 if (!io_get_sqring(ctx, &s))
2544 break;
2545
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002546 if (io_sqe_needs_user(s.sqe) && !*mm) {
2547 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2548 if (!mm_fault) {
2549 use_mm(ctx->sqo_mm);
2550 *mm = ctx->sqo_mm;
2551 }
2552 }
2553
Jens Axboe9e645e112019-05-10 16:07:28 -06002554 /*
2555 * If previous wasn't linked and we have a linked command,
2556 * that's the end of the chain. Submit the previous link.
2557 */
2558 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002559 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002560 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002561 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002562 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002563 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002564
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002565 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002566 if (!shadow_req) {
2567 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002568 if (unlikely(!shadow_req))
2569 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002570 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2571 refcount_dec(&shadow_req->refs);
2572 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002573 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002574 }
2575
Jackie Liua1041c22019-09-18 17:25:52 +08002576out:
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002577 s.has_user = *mm != NULL;
2578 s.in_async = true;
2579 s.needs_fixed_file = true;
2580 trace_io_uring_submit_sqe(ctx, true, true);
2581 io_submit_sqe(ctx, &s, statep, &link);
2582 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002583 }
2584
Jens Axboe9e645e112019-05-10 16:07:28 -06002585 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002586 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002587 if (statep)
2588 io_submit_state_end(&state);
2589
2590 return submitted;
2591}
2592
2593static int io_sq_thread(void *data)
2594{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002595 struct io_ring_ctx *ctx = data;
2596 struct mm_struct *cur_mm = NULL;
2597 mm_segment_t old_fs;
2598 DEFINE_WAIT(wait);
2599 unsigned inflight;
2600 unsigned long timeout;
2601
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002602 complete(&ctx->sqo_thread_started);
2603
Jens Axboe6c271ce2019-01-10 11:22:30 -07002604 old_fs = get_fs();
2605 set_fs(USER_DS);
2606
2607 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002608 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002609 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002610
2611 if (inflight) {
2612 unsigned nr_events = 0;
2613
2614 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002615 /*
2616 * inflight is the count of the maximum possible
2617 * entries we submitted, but it can be smaller
2618 * if we dropped some of them. If we don't have
2619 * poll entries available, then we know that we
2620 * have nothing left to poll for. Reset the
2621 * inflight count to zero in that case.
2622 */
2623 mutex_lock(&ctx->uring_lock);
2624 if (!list_empty(&ctx->poll_list))
2625 __io_iopoll_check(ctx, &nr_events, 0);
2626 else
2627 inflight = 0;
2628 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002629 } else {
2630 /*
2631 * Normal IO, just pretend everything completed.
2632 * We don't have to poll completions for that.
2633 */
2634 nr_events = inflight;
2635 }
2636
2637 inflight -= nr_events;
2638 if (!inflight)
2639 timeout = jiffies + ctx->sq_thread_idle;
2640 }
2641
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002642 to_submit = io_sqring_entries(ctx);
2643 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002644 /*
2645 * We're polling. If we're within the defined idle
2646 * period, then let us spin without work before going
2647 * to sleep.
2648 */
2649 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002650 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002651 continue;
2652 }
2653
2654 /*
2655 * Drop cur_mm before scheduling, we can't hold it for
2656 * long periods (or over schedule()). Do this before
2657 * adding ourselves to the waitqueue, as the unuse/drop
2658 * may sleep.
2659 */
2660 if (cur_mm) {
2661 unuse_mm(cur_mm);
2662 mmput(cur_mm);
2663 cur_mm = NULL;
2664 }
2665
2666 prepare_to_wait(&ctx->sqo_wait, &wait,
2667 TASK_INTERRUPTIBLE);
2668
2669 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002670 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002671 /* make sure to read SQ tail after writing flags */
2672 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002673
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002674 to_submit = io_sqring_entries(ctx);
2675 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002676 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002677 finish_wait(&ctx->sqo_wait, &wait);
2678 break;
2679 }
2680 if (signal_pending(current))
2681 flush_signals(current);
2682 schedule();
2683 finish_wait(&ctx->sqo_wait, &wait);
2684
Hristo Venev75b28af2019-08-26 17:23:46 +00002685 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002686 continue;
2687 }
2688 finish_wait(&ctx->sqo_wait, &wait);
2689
Hristo Venev75b28af2019-08-26 17:23:46 +00002690 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002691 }
2692
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002693 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002694 inflight += io_submit_sqes(ctx, to_submit, &cur_mm);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002695
2696 /* Commit SQ ring head once we've consumed all SQEs */
2697 io_commit_sqring(ctx);
2698 }
2699
2700 set_fs(old_fs);
2701 if (cur_mm) {
2702 unuse_mm(cur_mm);
2703 mmput(cur_mm);
2704 }
Jens Axboe06058632019-04-13 09:26:03 -06002705
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002706 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002707
Jens Axboe6c271ce2019-01-10 11:22:30 -07002708 return 0;
2709}
2710
Jens Axboebc808bc2019-10-22 13:14:37 -06002711static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002712{
Jens Axboe9a56a232019-01-09 09:06:50 -07002713 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002714 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002715 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002716 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002717 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002718
Jens Axboe9a56a232019-01-09 09:06:50 -07002719 if (to_submit > IO_PLUG_THRESHOLD) {
2720 io_submit_state_start(&state, ctx, to_submit);
2721 statep = &state;
2722 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002723
2724 for (i = 0; i < to_submit; i++) {
2725 struct sqe_submit s;
2726
2727 if (!io_get_sqring(ctx, &s))
2728 break;
2729
Jens Axboe9e645e112019-05-10 16:07:28 -06002730 /*
2731 * If previous wasn't linked and we have a linked command,
2732 * that's the end of the chain. Submit the previous link.
2733 */
2734 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002735 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002736 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002737 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002738 }
2739 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2740
Jackie Liu4fe2c962019-09-09 20:50:40 +08002741 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2742 if (!shadow_req) {
2743 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002744 if (unlikely(!shadow_req))
2745 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002746 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2747 refcount_dec(&shadow_req->refs);
2748 }
2749 shadow_req->sequence = s.sequence;
2750 }
2751
Jackie Liua1041c22019-09-18 17:25:52 +08002752out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002753 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002754 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002755 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002756 submit++;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002757 trace_io_uring_submit_sqe(ctx, true, false);
Jens Axboebc808bc2019-10-22 13:14:37 -06002758 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002759 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002760
Jens Axboe9e645e112019-05-10 16:07:28 -06002761 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002762 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002763 if (statep)
2764 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002765
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002766 io_commit_sqring(ctx);
2767
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002768 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002769}
2770
Jens Axboebda52162019-09-24 13:47:15 -06002771struct io_wait_queue {
2772 struct wait_queue_entry wq;
2773 struct io_ring_ctx *ctx;
2774 unsigned to_wait;
2775 unsigned nr_timeouts;
2776};
2777
2778static inline bool io_should_wake(struct io_wait_queue *iowq)
2779{
2780 struct io_ring_ctx *ctx = iowq->ctx;
2781
2782 /*
2783 * Wake up if we have enough events, or if a timeout occured since we
2784 * started waiting. For timeouts, we always want to return to userspace,
2785 * regardless of event count.
2786 */
2787 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2788 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2789}
2790
2791static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2792 int wake_flags, void *key)
2793{
2794 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2795 wq);
2796
2797 if (!io_should_wake(iowq))
2798 return -1;
2799
2800 return autoremove_wake_function(curr, mode, wake_flags, key);
2801}
2802
Jens Axboe2b188cc2019-01-07 10:46:33 -07002803/*
2804 * Wait until events become available, if we don't already have some. The
2805 * application must reap them itself, as they reside on the shared cq ring.
2806 */
2807static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2808 const sigset_t __user *sig, size_t sigsz)
2809{
Jens Axboebda52162019-09-24 13:47:15 -06002810 struct io_wait_queue iowq = {
2811 .wq = {
2812 .private = current,
2813 .func = io_wake_function,
2814 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2815 },
2816 .ctx = ctx,
2817 .to_wait = min_events,
2818 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002819 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002820 int ret;
2821
Hristo Venev75b28af2019-08-26 17:23:46 +00002822 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002823 return 0;
2824
2825 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002826#ifdef CONFIG_COMPAT
2827 if (in_compat_syscall())
2828 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002829 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002830 else
2831#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002832 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002833
Jens Axboe2b188cc2019-01-07 10:46:33 -07002834 if (ret)
2835 return ret;
2836 }
2837
Jens Axboebda52162019-09-24 13:47:15 -06002838 ret = 0;
2839 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002840 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06002841 do {
2842 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2843 TASK_INTERRUPTIBLE);
2844 if (io_should_wake(&iowq))
2845 break;
2846 schedule();
2847 if (signal_pending(current)) {
2848 ret = -ERESTARTSYS;
2849 break;
2850 }
2851 } while (1);
2852 finish_wait(&ctx->wait, &iowq.wq);
2853
Oleg Nesterovb7724342019-07-16 16:29:53 -07002854 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002855 if (ret == -ERESTARTSYS)
2856 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002857
Hristo Venev75b28af2019-08-26 17:23:46 +00002858 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002859}
2860
Jens Axboe6b063142019-01-10 22:13:58 -07002861static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2862{
2863#if defined(CONFIG_UNIX)
2864 if (ctx->ring_sock) {
2865 struct sock *sock = ctx->ring_sock->sk;
2866 struct sk_buff *skb;
2867
2868 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2869 kfree_skb(skb);
2870 }
2871#else
2872 int i;
2873
2874 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06002875 if (ctx->user_files[i])
2876 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07002877#endif
2878}
2879
2880static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2881{
2882 if (!ctx->user_files)
2883 return -ENXIO;
2884
2885 __io_sqe_files_unregister(ctx);
2886 kfree(ctx->user_files);
2887 ctx->user_files = NULL;
2888 ctx->nr_user_files = 0;
2889 return 0;
2890}
2891
Jens Axboe6c271ce2019-01-10 11:22:30 -07002892static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2893{
2894 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002895 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002896 /*
2897 * The park is a bit of a work-around, without it we get
2898 * warning spews on shutdown with SQPOLL set and affinity
2899 * set to a single CPU.
2900 */
Jens Axboe06058632019-04-13 09:26:03 -06002901 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002902 kthread_stop(ctx->sqo_thread);
2903 ctx->sqo_thread = NULL;
2904 }
2905}
2906
Jens Axboe6b063142019-01-10 22:13:58 -07002907static void io_finish_async(struct io_ring_ctx *ctx)
2908{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002909 io_sq_thread_stop(ctx);
2910
Jens Axboe561fb042019-10-24 07:25:42 -06002911 if (ctx->io_wq) {
2912 io_wq_destroy(ctx->io_wq);
2913 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002914 }
2915}
2916
2917#if defined(CONFIG_UNIX)
2918static void io_destruct_skb(struct sk_buff *skb)
2919{
2920 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2921
Jens Axboe561fb042019-10-24 07:25:42 -06002922 if (ctx->io_wq)
2923 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06002924
Jens Axboe6b063142019-01-10 22:13:58 -07002925 unix_destruct_scm(skb);
2926}
2927
2928/*
2929 * Ensure the UNIX gc is aware of our file set, so we are certain that
2930 * the io_uring can be safely unregistered on process exit, even if we have
2931 * loops in the file referencing.
2932 */
2933static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2934{
2935 struct sock *sk = ctx->ring_sock->sk;
2936 struct scm_fp_list *fpl;
2937 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06002938 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07002939
2940 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2941 unsigned long inflight = ctx->user->unix_inflight + nr;
2942
2943 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2944 return -EMFILE;
2945 }
2946
2947 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2948 if (!fpl)
2949 return -ENOMEM;
2950
2951 skb = alloc_skb(0, GFP_KERNEL);
2952 if (!skb) {
2953 kfree(fpl);
2954 return -ENOMEM;
2955 }
2956
2957 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07002958
Jens Axboe08a45172019-10-03 08:11:03 -06002959 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07002960 fpl->user = get_uid(ctx->user);
2961 for (i = 0; i < nr; i++) {
Jens Axboe08a45172019-10-03 08:11:03 -06002962 if (!ctx->user_files[i + offset])
2963 continue;
2964 fpl->fp[nr_files] = get_file(ctx->user_files[i + offset]);
2965 unix_inflight(fpl->user, fpl->fp[nr_files]);
2966 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07002967 }
2968
Jens Axboe08a45172019-10-03 08:11:03 -06002969 if (nr_files) {
2970 fpl->max = SCM_MAX_FD;
2971 fpl->count = nr_files;
2972 UNIXCB(skb).fp = fpl;
2973 skb->destructor = io_destruct_skb;
2974 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2975 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07002976
Jens Axboe08a45172019-10-03 08:11:03 -06002977 for (i = 0; i < nr_files; i++)
2978 fput(fpl->fp[i]);
2979 } else {
2980 kfree_skb(skb);
2981 kfree(fpl);
2982 }
Jens Axboe6b063142019-01-10 22:13:58 -07002983
2984 return 0;
2985}
2986
2987/*
2988 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2989 * causes regular reference counting to break down. We rely on the UNIX
2990 * garbage collection to take care of this problem for us.
2991 */
2992static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2993{
2994 unsigned left, total;
2995 int ret = 0;
2996
2997 total = 0;
2998 left = ctx->nr_user_files;
2999 while (left) {
3000 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003001
3002 ret = __io_sqe_files_scm(ctx, this_files, total);
3003 if (ret)
3004 break;
3005 left -= this_files;
3006 total += this_files;
3007 }
3008
3009 if (!ret)
3010 return 0;
3011
3012 while (total < ctx->nr_user_files) {
Jens Axboe08a45172019-10-03 08:11:03 -06003013 if (ctx->user_files[total])
3014 fput(ctx->user_files[total]);
Jens Axboe6b063142019-01-10 22:13:58 -07003015 total++;
3016 }
3017
3018 return ret;
3019}
3020#else
3021static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3022{
3023 return 0;
3024}
3025#endif
3026
3027static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3028 unsigned nr_args)
3029{
3030 __s32 __user *fds = (__s32 __user *) arg;
3031 int fd, ret = 0;
3032 unsigned i;
3033
3034 if (ctx->user_files)
3035 return -EBUSY;
3036 if (!nr_args)
3037 return -EINVAL;
3038 if (nr_args > IORING_MAX_FIXED_FILES)
3039 return -EMFILE;
3040
3041 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3042 if (!ctx->user_files)
3043 return -ENOMEM;
3044
Jens Axboe08a45172019-10-03 08:11:03 -06003045 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe6b063142019-01-10 22:13:58 -07003046 ret = -EFAULT;
3047 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3048 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003049 /* allow sparse sets */
3050 if (fd == -1) {
3051 ret = 0;
3052 continue;
3053 }
Jens Axboe6b063142019-01-10 22:13:58 -07003054
3055 ctx->user_files[i] = fget(fd);
3056
3057 ret = -EBADF;
3058 if (!ctx->user_files[i])
3059 break;
3060 /*
3061 * Don't allow io_uring instances to be registered. If UNIX
3062 * isn't enabled, then this causes a reference cycle and this
3063 * instance can never get freed. If UNIX is enabled we'll
3064 * handle it just fine, but there's still no point in allowing
3065 * a ring fd as it doesn't support regular read/write anyway.
3066 */
3067 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3068 fput(ctx->user_files[i]);
3069 break;
3070 }
Jens Axboe6b063142019-01-10 22:13:58 -07003071 ret = 0;
3072 }
3073
3074 if (ret) {
3075 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003076 if (ctx->user_files[i])
3077 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003078
3079 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003080 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003081 ctx->nr_user_files = 0;
3082 return ret;
3083 }
3084
3085 ret = io_sqe_files_scm(ctx);
3086 if (ret)
3087 io_sqe_files_unregister(ctx);
3088
3089 return ret;
3090}
3091
Jens Axboec3a31e62019-10-03 13:59:56 -06003092static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3093{
3094#if defined(CONFIG_UNIX)
3095 struct file *file = ctx->user_files[index];
3096 struct sock *sock = ctx->ring_sock->sk;
3097 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3098 struct sk_buff *skb;
3099 int i;
3100
3101 __skb_queue_head_init(&list);
3102
3103 /*
3104 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3105 * remove this entry and rearrange the file array.
3106 */
3107 skb = skb_dequeue(head);
3108 while (skb) {
3109 struct scm_fp_list *fp;
3110
3111 fp = UNIXCB(skb).fp;
3112 for (i = 0; i < fp->count; i++) {
3113 int left;
3114
3115 if (fp->fp[i] != file)
3116 continue;
3117
3118 unix_notinflight(fp->user, fp->fp[i]);
3119 left = fp->count - 1 - i;
3120 if (left) {
3121 memmove(&fp->fp[i], &fp->fp[i + 1],
3122 left * sizeof(struct file *));
3123 }
3124 fp->count--;
3125 if (!fp->count) {
3126 kfree_skb(skb);
3127 skb = NULL;
3128 } else {
3129 __skb_queue_tail(&list, skb);
3130 }
3131 fput(file);
3132 file = NULL;
3133 break;
3134 }
3135
3136 if (!file)
3137 break;
3138
3139 __skb_queue_tail(&list, skb);
3140
3141 skb = skb_dequeue(head);
3142 }
3143
3144 if (skb_peek(&list)) {
3145 spin_lock_irq(&head->lock);
3146 while ((skb = __skb_dequeue(&list)) != NULL)
3147 __skb_queue_tail(head, skb);
3148 spin_unlock_irq(&head->lock);
3149 }
3150#else
3151 fput(ctx->user_files[index]);
3152#endif
3153}
3154
3155static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3156 int index)
3157{
3158#if defined(CONFIG_UNIX)
3159 struct sock *sock = ctx->ring_sock->sk;
3160 struct sk_buff_head *head = &sock->sk_receive_queue;
3161 struct sk_buff *skb;
3162
3163 /*
3164 * See if we can merge this file into an existing skb SCM_RIGHTS
3165 * file set. If there's no room, fall back to allocating a new skb
3166 * and filling it in.
3167 */
3168 spin_lock_irq(&head->lock);
3169 skb = skb_peek(head);
3170 if (skb) {
3171 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3172
3173 if (fpl->count < SCM_MAX_FD) {
3174 __skb_unlink(skb, head);
3175 spin_unlock_irq(&head->lock);
3176 fpl->fp[fpl->count] = get_file(file);
3177 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3178 fpl->count++;
3179 spin_lock_irq(&head->lock);
3180 __skb_queue_head(head, skb);
3181 } else {
3182 skb = NULL;
3183 }
3184 }
3185 spin_unlock_irq(&head->lock);
3186
3187 if (skb) {
3188 fput(file);
3189 return 0;
3190 }
3191
3192 return __io_sqe_files_scm(ctx, 1, index);
3193#else
3194 return 0;
3195#endif
3196}
3197
3198static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3199 unsigned nr_args)
3200{
3201 struct io_uring_files_update up;
3202 __s32 __user *fds;
3203 int fd, i, err;
3204 __u32 done;
3205
3206 if (!ctx->user_files)
3207 return -ENXIO;
3208 if (!nr_args)
3209 return -EINVAL;
3210 if (copy_from_user(&up, arg, sizeof(up)))
3211 return -EFAULT;
3212 if (check_add_overflow(up.offset, nr_args, &done))
3213 return -EOVERFLOW;
3214 if (done > ctx->nr_user_files)
3215 return -EINVAL;
3216
3217 done = 0;
3218 fds = (__s32 __user *) up.fds;
3219 while (nr_args) {
3220 err = 0;
3221 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3222 err = -EFAULT;
3223 break;
3224 }
3225 i = array_index_nospec(up.offset, ctx->nr_user_files);
3226 if (ctx->user_files[i]) {
3227 io_sqe_file_unregister(ctx, i);
3228 ctx->user_files[i] = NULL;
3229 }
3230 if (fd != -1) {
3231 struct file *file;
3232
3233 file = fget(fd);
3234 if (!file) {
3235 err = -EBADF;
3236 break;
3237 }
3238 /*
3239 * Don't allow io_uring instances to be registered. If
3240 * UNIX isn't enabled, then this causes a reference
3241 * cycle and this instance can never get freed. If UNIX
3242 * is enabled we'll handle it just fine, but there's
3243 * still no point in allowing a ring fd as it doesn't
3244 * support regular read/write anyway.
3245 */
3246 if (file->f_op == &io_uring_fops) {
3247 fput(file);
3248 err = -EBADF;
3249 break;
3250 }
3251 ctx->user_files[i] = file;
3252 err = io_sqe_file_register(ctx, file, i);
3253 if (err)
3254 break;
3255 }
3256 nr_args--;
3257 done++;
3258 up.offset++;
3259 }
3260
3261 return done ? done : err;
3262}
3263
Jens Axboe6c271ce2019-01-10 11:22:30 -07003264static int io_sq_offload_start(struct io_ring_ctx *ctx,
3265 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266{
Jens Axboe561fb042019-10-24 07:25:42 -06003267 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003268 int ret;
3269
Jens Axboe6c271ce2019-01-10 11:22:30 -07003270 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003271 mmgrab(current->mm);
3272 ctx->sqo_mm = current->mm;
3273
Jens Axboe6c271ce2019-01-10 11:22:30 -07003274 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003275 ret = -EPERM;
3276 if (!capable(CAP_SYS_ADMIN))
3277 goto err;
3278
Jens Axboe917257d2019-04-13 09:28:55 -06003279 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3280 if (!ctx->sq_thread_idle)
3281 ctx->sq_thread_idle = HZ;
3282
Jens Axboe6c271ce2019-01-10 11:22:30 -07003283 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003284 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003285
Jens Axboe917257d2019-04-13 09:28:55 -06003286 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003287 if (cpu >= nr_cpu_ids)
3288 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003289 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003290 goto err;
3291
Jens Axboe6c271ce2019-01-10 11:22:30 -07003292 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3293 ctx, cpu,
3294 "io_uring-sq");
3295 } else {
3296 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3297 "io_uring-sq");
3298 }
3299 if (IS_ERR(ctx->sqo_thread)) {
3300 ret = PTR_ERR(ctx->sqo_thread);
3301 ctx->sqo_thread = NULL;
3302 goto err;
3303 }
3304 wake_up_process(ctx->sqo_thread);
3305 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3306 /* Can't have SQ_AFF without SQPOLL */
3307 ret = -EINVAL;
3308 goto err;
3309 }
3310
Jens Axboe561fb042019-10-24 07:25:42 -06003311 /* Do QD, or 4 * CPUS, whatever is smallest */
3312 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3313 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
3314 if (!ctx->io_wq) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003315 ret = -ENOMEM;
3316 goto err;
3317 }
3318
3319 return 0;
3320err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003321 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003322 mmdrop(ctx->sqo_mm);
3323 ctx->sqo_mm = NULL;
3324 return ret;
3325}
3326
3327static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3328{
3329 atomic_long_sub(nr_pages, &user->locked_vm);
3330}
3331
3332static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3333{
3334 unsigned long page_limit, cur_pages, new_pages;
3335
3336 /* Don't allow more pages than we can safely lock */
3337 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3338
3339 do {
3340 cur_pages = atomic_long_read(&user->locked_vm);
3341 new_pages = cur_pages + nr_pages;
3342 if (new_pages > page_limit)
3343 return -ENOMEM;
3344 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3345 new_pages) != cur_pages);
3346
3347 return 0;
3348}
3349
3350static void io_mem_free(void *ptr)
3351{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003352 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003353
Mark Rutland52e04ef2019-04-30 17:30:21 +01003354 if (!ptr)
3355 return;
3356
3357 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003358 if (put_page_testzero(page))
3359 free_compound_page(page);
3360}
3361
3362static void *io_mem_alloc(size_t size)
3363{
3364 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3365 __GFP_NORETRY;
3366
3367 return (void *) __get_free_pages(gfp_flags, get_order(size));
3368}
3369
Hristo Venev75b28af2019-08-26 17:23:46 +00003370static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3371 size_t *sq_offset)
3372{
3373 struct io_rings *rings;
3374 size_t off, sq_array_size;
3375
3376 off = struct_size(rings, cqes, cq_entries);
3377 if (off == SIZE_MAX)
3378 return SIZE_MAX;
3379
3380#ifdef CONFIG_SMP
3381 off = ALIGN(off, SMP_CACHE_BYTES);
3382 if (off == 0)
3383 return SIZE_MAX;
3384#endif
3385
3386 sq_array_size = array_size(sizeof(u32), sq_entries);
3387 if (sq_array_size == SIZE_MAX)
3388 return SIZE_MAX;
3389
3390 if (check_add_overflow(off, sq_array_size, &off))
3391 return SIZE_MAX;
3392
3393 if (sq_offset)
3394 *sq_offset = off;
3395
3396 return off;
3397}
3398
Jens Axboe2b188cc2019-01-07 10:46:33 -07003399static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3400{
Hristo Venev75b28af2019-08-26 17:23:46 +00003401 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003402
Hristo Venev75b28af2019-08-26 17:23:46 +00003403 pages = (size_t)1 << get_order(
3404 rings_size(sq_entries, cq_entries, NULL));
3405 pages += (size_t)1 << get_order(
3406 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003407
Hristo Venev75b28af2019-08-26 17:23:46 +00003408 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003409}
3410
Jens Axboeedafcce2019-01-09 09:16:05 -07003411static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3412{
3413 int i, j;
3414
3415 if (!ctx->user_bufs)
3416 return -ENXIO;
3417
3418 for (i = 0; i < ctx->nr_user_bufs; i++) {
3419 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3420
3421 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003422 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003423
3424 if (ctx->account_mem)
3425 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003426 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003427 imu->nr_bvecs = 0;
3428 }
3429
3430 kfree(ctx->user_bufs);
3431 ctx->user_bufs = NULL;
3432 ctx->nr_user_bufs = 0;
3433 return 0;
3434}
3435
3436static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3437 void __user *arg, unsigned index)
3438{
3439 struct iovec __user *src;
3440
3441#ifdef CONFIG_COMPAT
3442 if (ctx->compat) {
3443 struct compat_iovec __user *ciovs;
3444 struct compat_iovec ciov;
3445
3446 ciovs = (struct compat_iovec __user *) arg;
3447 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3448 return -EFAULT;
3449
3450 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3451 dst->iov_len = ciov.iov_len;
3452 return 0;
3453 }
3454#endif
3455 src = (struct iovec __user *) arg;
3456 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3457 return -EFAULT;
3458 return 0;
3459}
3460
3461static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3462 unsigned nr_args)
3463{
3464 struct vm_area_struct **vmas = NULL;
3465 struct page **pages = NULL;
3466 int i, j, got_pages = 0;
3467 int ret = -EINVAL;
3468
3469 if (ctx->user_bufs)
3470 return -EBUSY;
3471 if (!nr_args || nr_args > UIO_MAXIOV)
3472 return -EINVAL;
3473
3474 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3475 GFP_KERNEL);
3476 if (!ctx->user_bufs)
3477 return -ENOMEM;
3478
3479 for (i = 0; i < nr_args; i++) {
3480 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3481 unsigned long off, start, end, ubuf;
3482 int pret, nr_pages;
3483 struct iovec iov;
3484 size_t size;
3485
3486 ret = io_copy_iov(ctx, &iov, arg, i);
3487 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003488 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003489
3490 /*
3491 * Don't impose further limits on the size and buffer
3492 * constraints here, we'll -EINVAL later when IO is
3493 * submitted if they are wrong.
3494 */
3495 ret = -EFAULT;
3496 if (!iov.iov_base || !iov.iov_len)
3497 goto err;
3498
3499 /* arbitrary limit, but we need something */
3500 if (iov.iov_len > SZ_1G)
3501 goto err;
3502
3503 ubuf = (unsigned long) iov.iov_base;
3504 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3505 start = ubuf >> PAGE_SHIFT;
3506 nr_pages = end - start;
3507
3508 if (ctx->account_mem) {
3509 ret = io_account_mem(ctx->user, nr_pages);
3510 if (ret)
3511 goto err;
3512 }
3513
3514 ret = 0;
3515 if (!pages || nr_pages > got_pages) {
3516 kfree(vmas);
3517 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003518 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003519 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003520 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003521 sizeof(struct vm_area_struct *),
3522 GFP_KERNEL);
3523 if (!pages || !vmas) {
3524 ret = -ENOMEM;
3525 if (ctx->account_mem)
3526 io_unaccount_mem(ctx->user, nr_pages);
3527 goto err;
3528 }
3529 got_pages = nr_pages;
3530 }
3531
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003532 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003533 GFP_KERNEL);
3534 ret = -ENOMEM;
3535 if (!imu->bvec) {
3536 if (ctx->account_mem)
3537 io_unaccount_mem(ctx->user, nr_pages);
3538 goto err;
3539 }
3540
3541 ret = 0;
3542 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003543 pret = get_user_pages(ubuf, nr_pages,
3544 FOLL_WRITE | FOLL_LONGTERM,
3545 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003546 if (pret == nr_pages) {
3547 /* don't support file backed memory */
3548 for (j = 0; j < nr_pages; j++) {
3549 struct vm_area_struct *vma = vmas[j];
3550
3551 if (vma->vm_file &&
3552 !is_file_hugepages(vma->vm_file)) {
3553 ret = -EOPNOTSUPP;
3554 break;
3555 }
3556 }
3557 } else {
3558 ret = pret < 0 ? pret : -EFAULT;
3559 }
3560 up_read(&current->mm->mmap_sem);
3561 if (ret) {
3562 /*
3563 * if we did partial map, or found file backed vmas,
3564 * release any pages we did get
3565 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003566 if (pret > 0)
3567 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003568 if (ctx->account_mem)
3569 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003570 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003571 goto err;
3572 }
3573
3574 off = ubuf & ~PAGE_MASK;
3575 size = iov.iov_len;
3576 for (j = 0; j < nr_pages; j++) {
3577 size_t vec_len;
3578
3579 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3580 imu->bvec[j].bv_page = pages[j];
3581 imu->bvec[j].bv_len = vec_len;
3582 imu->bvec[j].bv_offset = off;
3583 off = 0;
3584 size -= vec_len;
3585 }
3586 /* store original address for later verification */
3587 imu->ubuf = ubuf;
3588 imu->len = iov.iov_len;
3589 imu->nr_bvecs = nr_pages;
3590
3591 ctx->nr_user_bufs++;
3592 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003593 kvfree(pages);
3594 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003595 return 0;
3596err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003597 kvfree(pages);
3598 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003599 io_sqe_buffer_unregister(ctx);
3600 return ret;
3601}
3602
Jens Axboe9b402842019-04-11 11:45:41 -06003603static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3604{
3605 __s32 __user *fds = arg;
3606 int fd;
3607
3608 if (ctx->cq_ev_fd)
3609 return -EBUSY;
3610
3611 if (copy_from_user(&fd, fds, sizeof(*fds)))
3612 return -EFAULT;
3613
3614 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3615 if (IS_ERR(ctx->cq_ev_fd)) {
3616 int ret = PTR_ERR(ctx->cq_ev_fd);
3617 ctx->cq_ev_fd = NULL;
3618 return ret;
3619 }
3620
3621 return 0;
3622}
3623
3624static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3625{
3626 if (ctx->cq_ev_fd) {
3627 eventfd_ctx_put(ctx->cq_ev_fd);
3628 ctx->cq_ev_fd = NULL;
3629 return 0;
3630 }
3631
3632 return -ENXIO;
3633}
3634
Jens Axboe2b188cc2019-01-07 10:46:33 -07003635static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3636{
Jens Axboe6b063142019-01-10 22:13:58 -07003637 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003638 if (ctx->sqo_mm)
3639 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003640
3641 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003642 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003643 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003644 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003645
Jens Axboe2b188cc2019-01-07 10:46:33 -07003646#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003647 if (ctx->ring_sock) {
3648 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003649 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003650 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003651#endif
3652
Hristo Venev75b28af2019-08-26 17:23:46 +00003653 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003654 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003655
3656 percpu_ref_exit(&ctx->refs);
3657 if (ctx->account_mem)
3658 io_unaccount_mem(ctx->user,
3659 ring_pages(ctx->sq_entries, ctx->cq_entries));
3660 free_uid(ctx->user);
3661 kfree(ctx);
3662}
3663
3664static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3665{
3666 struct io_ring_ctx *ctx = file->private_data;
3667 __poll_t mask = 0;
3668
3669 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003670 /*
3671 * synchronizes with barrier from wq_has_sleeper call in
3672 * io_commit_cqring
3673 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003674 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003675 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3676 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003677 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003678 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003679 mask |= EPOLLIN | EPOLLRDNORM;
3680
3681 return mask;
3682}
3683
3684static int io_uring_fasync(int fd, struct file *file, int on)
3685{
3686 struct io_ring_ctx *ctx = file->private_data;
3687
3688 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3689}
3690
3691static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3692{
3693 mutex_lock(&ctx->uring_lock);
3694 percpu_ref_kill(&ctx->refs);
3695 mutex_unlock(&ctx->uring_lock);
3696
Jens Axboe5262f562019-09-17 12:26:57 -06003697 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003698 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06003699
3700 if (ctx->io_wq)
3701 io_wq_cancel_all(ctx->io_wq);
3702
Jens Axboedef596e2019-01-09 08:59:42 -07003703 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003704 wait_for_completion(&ctx->ctx_done);
3705 io_ring_ctx_free(ctx);
3706}
3707
3708static int io_uring_release(struct inode *inode, struct file *file)
3709{
3710 struct io_ring_ctx *ctx = file->private_data;
3711
3712 file->private_data = NULL;
3713 io_ring_ctx_wait_and_kill(ctx);
3714 return 0;
3715}
3716
3717static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3718{
3719 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3720 unsigned long sz = vma->vm_end - vma->vm_start;
3721 struct io_ring_ctx *ctx = file->private_data;
3722 unsigned long pfn;
3723 struct page *page;
3724 void *ptr;
3725
3726 switch (offset) {
3727 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003728 case IORING_OFF_CQ_RING:
3729 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003730 break;
3731 case IORING_OFF_SQES:
3732 ptr = ctx->sq_sqes;
3733 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003734 default:
3735 return -EINVAL;
3736 }
3737
3738 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003739 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003740 return -EINVAL;
3741
3742 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3743 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3744}
3745
3746SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3747 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3748 size_t, sigsz)
3749{
3750 struct io_ring_ctx *ctx;
3751 long ret = -EBADF;
3752 int submitted = 0;
3753 struct fd f;
3754
Jens Axboe6c271ce2019-01-10 11:22:30 -07003755 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003756 return -EINVAL;
3757
3758 f = fdget(fd);
3759 if (!f.file)
3760 return -EBADF;
3761
3762 ret = -EOPNOTSUPP;
3763 if (f.file->f_op != &io_uring_fops)
3764 goto out_fput;
3765
3766 ret = -ENXIO;
3767 ctx = f.file->private_data;
3768 if (!percpu_ref_tryget(&ctx->refs))
3769 goto out_fput;
3770
Jens Axboe6c271ce2019-01-10 11:22:30 -07003771 /*
3772 * For SQ polling, the thread will do all submissions and completions.
3773 * Just return the requested submit count, and wake the thread if
3774 * we were asked to.
3775 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003776 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003777 if (ctx->flags & IORING_SETUP_SQPOLL) {
3778 if (flags & IORING_ENTER_SQ_WAKEUP)
3779 wake_up(&ctx->sqo_wait);
3780 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003781 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003782 to_submit = min(to_submit, ctx->sq_entries);
3783
3784 mutex_lock(&ctx->uring_lock);
Jens Axboebc808bc2019-10-22 13:14:37 -06003785 submitted = io_ring_submit(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003786 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003787 }
3788 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003789 unsigned nr_events = 0;
3790
Jens Axboe2b188cc2019-01-07 10:46:33 -07003791 min_complete = min(min_complete, ctx->cq_entries);
3792
Jens Axboedef596e2019-01-09 08:59:42 -07003793 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003794 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003795 } else {
3796 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3797 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003798 }
3799
Pavel Begunkov6805b322019-10-08 02:18:42 +03003800 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003801out_fput:
3802 fdput(f);
3803 return submitted ? submitted : ret;
3804}
3805
3806static const struct file_operations io_uring_fops = {
3807 .release = io_uring_release,
3808 .mmap = io_uring_mmap,
3809 .poll = io_uring_poll,
3810 .fasync = io_uring_fasync,
3811};
3812
3813static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3814 struct io_uring_params *p)
3815{
Hristo Venev75b28af2019-08-26 17:23:46 +00003816 struct io_rings *rings;
3817 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003818
Hristo Venev75b28af2019-08-26 17:23:46 +00003819 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3820 if (size == SIZE_MAX)
3821 return -EOVERFLOW;
3822
3823 rings = io_mem_alloc(size);
3824 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003825 return -ENOMEM;
3826
Hristo Venev75b28af2019-08-26 17:23:46 +00003827 ctx->rings = rings;
3828 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3829 rings->sq_ring_mask = p->sq_entries - 1;
3830 rings->cq_ring_mask = p->cq_entries - 1;
3831 rings->sq_ring_entries = p->sq_entries;
3832 rings->cq_ring_entries = p->cq_entries;
3833 ctx->sq_mask = rings->sq_ring_mask;
3834 ctx->cq_mask = rings->cq_ring_mask;
3835 ctx->sq_entries = rings->sq_ring_entries;
3836 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003837
3838 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3839 if (size == SIZE_MAX)
3840 return -EOVERFLOW;
3841
3842 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003843 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003844 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003845
Jens Axboe2b188cc2019-01-07 10:46:33 -07003846 return 0;
3847}
3848
3849/*
3850 * Allocate an anonymous fd, this is what constitutes the application
3851 * visible backing of an io_uring instance. The application mmaps this
3852 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3853 * we have to tie this fd to a socket for file garbage collection purposes.
3854 */
3855static int io_uring_get_fd(struct io_ring_ctx *ctx)
3856{
3857 struct file *file;
3858 int ret;
3859
3860#if defined(CONFIG_UNIX)
3861 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3862 &ctx->ring_sock);
3863 if (ret)
3864 return ret;
3865#endif
3866
3867 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3868 if (ret < 0)
3869 goto err;
3870
3871 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3872 O_RDWR | O_CLOEXEC);
3873 if (IS_ERR(file)) {
3874 put_unused_fd(ret);
3875 ret = PTR_ERR(file);
3876 goto err;
3877 }
3878
3879#if defined(CONFIG_UNIX)
3880 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003881 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003882#endif
3883 fd_install(ret, file);
3884 return ret;
3885err:
3886#if defined(CONFIG_UNIX)
3887 sock_release(ctx->ring_sock);
3888 ctx->ring_sock = NULL;
3889#endif
3890 return ret;
3891}
3892
3893static int io_uring_create(unsigned entries, struct io_uring_params *p)
3894{
3895 struct user_struct *user = NULL;
3896 struct io_ring_ctx *ctx;
3897 bool account_mem;
3898 int ret;
3899
3900 if (!entries || entries > IORING_MAX_ENTRIES)
3901 return -EINVAL;
3902
3903 /*
3904 * Use twice as many entries for the CQ ring. It's possible for the
3905 * application to drive a higher depth than the size of the SQ ring,
3906 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06003907 * some flexibility in overcommitting a bit. If the application has
3908 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
3909 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07003910 */
3911 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06003912 if (p->flags & IORING_SETUP_CQSIZE) {
3913 /*
3914 * If IORING_SETUP_CQSIZE is set, we do the same roundup
3915 * to a power-of-two, if it isn't already. We do NOT impose
3916 * any cq vs sq ring sizing.
3917 */
3918 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
3919 return -EINVAL;
3920 p->cq_entries = roundup_pow_of_two(p->cq_entries);
3921 } else {
3922 p->cq_entries = 2 * p->sq_entries;
3923 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003924
3925 user = get_uid(current_user());
3926 account_mem = !capable(CAP_IPC_LOCK);
3927
3928 if (account_mem) {
3929 ret = io_account_mem(user,
3930 ring_pages(p->sq_entries, p->cq_entries));
3931 if (ret) {
3932 free_uid(user);
3933 return ret;
3934 }
3935 }
3936
3937 ctx = io_ring_ctx_alloc(p);
3938 if (!ctx) {
3939 if (account_mem)
3940 io_unaccount_mem(user, ring_pages(p->sq_entries,
3941 p->cq_entries));
3942 free_uid(user);
3943 return -ENOMEM;
3944 }
3945 ctx->compat = in_compat_syscall();
3946 ctx->account_mem = account_mem;
3947 ctx->user = user;
3948
3949 ret = io_allocate_scq_urings(ctx, p);
3950 if (ret)
3951 goto err;
3952
Jens Axboe6c271ce2019-01-10 11:22:30 -07003953 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003954 if (ret)
3955 goto err;
3956
Jens Axboe2b188cc2019-01-07 10:46:33 -07003957 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003958 p->sq_off.head = offsetof(struct io_rings, sq.head);
3959 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3960 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3961 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3962 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3963 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3964 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003965
3966 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003967 p->cq_off.head = offsetof(struct io_rings, cq.head);
3968 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3969 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3970 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3971 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3972 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06003973
Jens Axboe044c1ab2019-10-28 09:15:33 -06003974 /*
3975 * Install ring fd as the very last thing, so we don't risk someone
3976 * having closed it before we finish setup
3977 */
3978 ret = io_uring_get_fd(ctx);
3979 if (ret < 0)
3980 goto err;
3981
Jens Axboeac90f242019-09-06 10:26:21 -06003982 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003983 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003984 return ret;
3985err:
3986 io_ring_ctx_wait_and_kill(ctx);
3987 return ret;
3988}
3989
3990/*
3991 * Sets up an aio uring context, and returns the fd. Applications asks for a
3992 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3993 * params structure passed in.
3994 */
3995static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3996{
3997 struct io_uring_params p;
3998 long ret;
3999 int i;
4000
4001 if (copy_from_user(&p, params, sizeof(p)))
4002 return -EFAULT;
4003 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4004 if (p.resv[i])
4005 return -EINVAL;
4006 }
4007
Jens Axboe6c271ce2019-01-10 11:22:30 -07004008 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004009 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004010 return -EINVAL;
4011
4012 ret = io_uring_create(entries, &p);
4013 if (ret < 0)
4014 return ret;
4015
4016 if (copy_to_user(params, &p, sizeof(p)))
4017 return -EFAULT;
4018
4019 return ret;
4020}
4021
4022SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4023 struct io_uring_params __user *, params)
4024{
4025 return io_uring_setup(entries, params);
4026}
4027
Jens Axboeedafcce2019-01-09 09:16:05 -07004028static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4029 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004030 __releases(ctx->uring_lock)
4031 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004032{
4033 int ret;
4034
Jens Axboe35fa71a2019-04-22 10:23:23 -06004035 /*
4036 * We're inside the ring mutex, if the ref is already dying, then
4037 * someone else killed the ctx or is already going through
4038 * io_uring_register().
4039 */
4040 if (percpu_ref_is_dying(&ctx->refs))
4041 return -ENXIO;
4042
Jens Axboeedafcce2019-01-09 09:16:05 -07004043 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004044
4045 /*
4046 * Drop uring mutex before waiting for references to exit. If another
4047 * thread is currently inside io_uring_enter() it might need to grab
4048 * the uring_lock to make progress. If we hold it here across the drain
4049 * wait, then we can deadlock. It's safe to drop the mutex here, since
4050 * no new references will come in after we've killed the percpu ref.
4051 */
4052 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004053 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004054 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004055
4056 switch (opcode) {
4057 case IORING_REGISTER_BUFFERS:
4058 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4059 break;
4060 case IORING_UNREGISTER_BUFFERS:
4061 ret = -EINVAL;
4062 if (arg || nr_args)
4063 break;
4064 ret = io_sqe_buffer_unregister(ctx);
4065 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004066 case IORING_REGISTER_FILES:
4067 ret = io_sqe_files_register(ctx, arg, nr_args);
4068 break;
4069 case IORING_UNREGISTER_FILES:
4070 ret = -EINVAL;
4071 if (arg || nr_args)
4072 break;
4073 ret = io_sqe_files_unregister(ctx);
4074 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004075 case IORING_REGISTER_FILES_UPDATE:
4076 ret = io_sqe_files_update(ctx, arg, nr_args);
4077 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004078 case IORING_REGISTER_EVENTFD:
4079 ret = -EINVAL;
4080 if (nr_args != 1)
4081 break;
4082 ret = io_eventfd_register(ctx, arg);
4083 break;
4084 case IORING_UNREGISTER_EVENTFD:
4085 ret = -EINVAL;
4086 if (arg || nr_args)
4087 break;
4088 ret = io_eventfd_unregister(ctx);
4089 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004090 default:
4091 ret = -EINVAL;
4092 break;
4093 }
4094
4095 /* bring the ctx back to life */
4096 reinit_completion(&ctx->ctx_done);
4097 percpu_ref_reinit(&ctx->refs);
4098 return ret;
4099}
4100
4101SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4102 void __user *, arg, unsigned int, nr_args)
4103{
4104 struct io_ring_ctx *ctx;
4105 long ret = -EBADF;
4106 struct fd f;
4107
4108 f = fdget(fd);
4109 if (!f.file)
4110 return -EBADF;
4111
4112 ret = -EOPNOTSUPP;
4113 if (f.file->f_op != &io_uring_fops)
4114 goto out_fput;
4115
4116 ctx = f.file->private_data;
4117
4118 mutex_lock(&ctx->uring_lock);
4119 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4120 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004121 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4122 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004123out_fput:
4124 fdput(f);
4125 return ret;
4126}
4127
Jens Axboe2b188cc2019-01-07 10:46:33 -07004128static int __init io_uring_init(void)
4129{
4130 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4131 return 0;
4132};
4133__initcall(io_uring_init);