blob: 2fc6809bc3a983cd739d1f7e64d27206e1fe82c6 [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>
59#include <linux/workqueue.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
74#include <uapi/linux/io_uring.h>
75
76#include "internal.h"
77
Daniel Xu5277dea2019-09-14 14:23:45 -070078#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060079#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe6b063142019-01-10 22:13:58 -070080#define IORING_MAX_FIXED_FILES 1024
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
82struct io_uring {
83 u32 head ____cacheline_aligned_in_smp;
84 u32 tail ____cacheline_aligned_in_smp;
85};
86
Stefan Bühler1e84b972019-04-24 23:54:16 +020087/*
Hristo Venev75b28af2019-08-26 17:23:46 +000088 * This data is shared with the application through the mmap at offsets
89 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +020090 *
91 * The offsets to the member fields are published through struct
92 * io_sqring_offsets when calling io_uring_setup.
93 */
Hristo Venev75b28af2019-08-26 17:23:46 +000094struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +020095 /*
96 * Head and tail offsets into the ring; the offsets need to be
97 * masked to get valid indices.
98 *
Hristo Venev75b28af2019-08-26 17:23:46 +000099 * The kernel controls head of the sq ring and the tail of the cq ring,
100 * and the application controls tail of the sq ring and the head of the
101 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200102 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000103 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000105 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 * ring_entries - 1)
107 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 u32 sq_ring_mask, cq_ring_mask;
109 /* Ring sizes (constant, power of 2) */
110 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200111 /*
112 * Number of invalid entries dropped by the kernel due to
113 * invalid index stored in array
114 *
115 * Written by the kernel, shouldn't be modified by the
116 * application (i.e. get number of "new events" by comparing to
117 * cached value).
118 *
119 * After a new SQ head value was read by the application this
120 * counter includes all submissions that were dropped reaching
121 * the new SQ head (and possibly more).
122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
125 * Runtime flags
126 *
127 * Written by the kernel, shouldn't be modified by the
128 * application.
129 *
130 * The application needs a full memory barrier before checking
131 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Number of completion events lost because the queue was full;
136 * this should be avoided by the application by making sure
137 * there are not more requests pending thatn there is space in
138 * the completion queue.
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application (i.e. get number of "new events" by comparing to
142 * cached value).
143 *
144 * As completion events come in out of order this counter is not
145 * ordered with any other data.
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
149 * Ring buffer of completion events.
150 *
151 * The kernel writes completion events fresh every time they are
152 * produced, so the application is allowed to modify pending
153 * entries.
154 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000155 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700156};
157
Jens Axboeedafcce2019-01-09 09:16:05 -0700158struct io_mapped_ubuf {
159 u64 ubuf;
160 size_t len;
161 struct bio_vec *bvec;
162 unsigned int nr_bvecs;
163};
164
Jens Axboe31b51512019-01-18 22:56:34 -0700165struct async_list {
166 spinlock_t lock;
167 atomic_t cnt;
168 struct list_head list;
169
170 struct file *file;
Jens Axboe6d5d5ac2019-09-11 10:16:13 -0600171 off_t io_start;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +0800172 size_t io_len;
Jens Axboe31b51512019-01-18 22:56:34 -0700173};
174
Jens Axboe2b188cc2019-01-07 10:46:33 -0700175struct io_ring_ctx {
176 struct {
177 struct percpu_ref refs;
178 } ____cacheline_aligned_in_smp;
179
180 struct {
181 unsigned int flags;
182 bool compat;
183 bool account_mem;
184
Hristo Venev75b28af2019-08-26 17:23:46 +0000185 /*
186 * Ring buffer of indices into array of io_uring_sqe, which is
187 * mmapped by the application using the IORING_OFF_SQES offset.
188 *
189 * This indirection could e.g. be used to assign fixed
190 * io_uring_sqe entries to operations and only submit them to
191 * the queue when needed.
192 *
193 * The kernel modifies neither the indices array nor the entries
194 * array.
195 */
196 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700197 unsigned cached_sq_head;
198 unsigned sq_entries;
199 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700200 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600201 unsigned cached_sq_dropped;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700202 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600203
204 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600205 struct list_head timeout_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700206 } ____cacheline_aligned_in_smp;
207
208 /* IO offload */
Jens Axboe54a91f32019-09-10 09:15:04 -0600209 struct workqueue_struct *sqo_wq[2];
Jens Axboe6c271ce2019-01-10 11:22:30 -0700210 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700211 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700212 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800213 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700214
215 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700216 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600217 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700218 unsigned cq_entries;
219 unsigned cq_mask;
220 struct wait_queue_head cq_wait;
221 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600222 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600223 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224 } ____cacheline_aligned_in_smp;
225
Hristo Venev75b28af2019-08-26 17:23:46 +0000226 struct io_rings *rings;
227
Jens Axboe6b063142019-01-10 22:13:58 -0700228 /*
229 * If used, fixed file set. Writers must ensure that ->refs is dead,
230 * readers must ensure that ->refs is alive as long as the file* is
231 * used. Only updated through io_uring_register(2).
232 */
233 struct file **user_files;
234 unsigned nr_user_files;
235
Jens Axboeedafcce2019-01-09 09:16:05 -0700236 /* if used, fixed mapped user buffers */
237 unsigned nr_user_bufs;
238 struct io_mapped_ubuf *user_bufs;
239
Jens Axboe2b188cc2019-01-07 10:46:33 -0700240 struct user_struct *user;
241
242 struct completion ctx_done;
243
244 struct {
245 struct mutex uring_lock;
246 wait_queue_head_t wait;
247 } ____cacheline_aligned_in_smp;
248
249 struct {
250 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700251 bool poll_multi_file;
252 /*
253 * ->poll_list is protected by the ctx->uring_lock for
254 * io_uring instances that don't use IORING_SETUP_SQPOLL.
255 * For SQPOLL, only the single threaded io_sq_thread() will
256 * manipulate the list, hence no extra locking is needed there.
257 */
258 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700259 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700260 } ____cacheline_aligned_in_smp;
261
Jens Axboe31b51512019-01-18 22:56:34 -0700262 struct async_list pending_async[2];
263
Jens Axboe2b188cc2019-01-07 10:46:33 -0700264#if defined(CONFIG_UNIX)
265 struct socket *ring_sock;
266#endif
267};
268
269struct sqe_submit {
270 const struct io_uring_sqe *sqe;
271 unsigned short index;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800272 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700273 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800274 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700275 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276};
277
Jens Axboe09bb8392019-03-13 12:39:28 -0600278/*
279 * First field must be the file pointer in all the
280 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
281 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700282struct io_poll_iocb {
283 struct file *file;
284 struct wait_queue_head *head;
285 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600286 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700287 bool canceled;
288 struct wait_queue_entry wait;
289};
290
Jens Axboe5262f562019-09-17 12:26:57 -0600291struct io_timeout {
292 struct file *file;
293 struct hrtimer timer;
294};
295
Jens Axboe09bb8392019-03-13 12:39:28 -0600296/*
297 * NOTE! Each of the iocb union members has the file pointer
298 * as the first entry in their struct definition. So you can
299 * access the file pointer through any of the sub-structs,
300 * or directly as just 'ki_filp' in this struct.
301 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700302struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700303 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600304 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700305 struct kiocb rw;
306 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600307 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700308 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309
310 struct sqe_submit submit;
311
312 struct io_ring_ctx *ctx;
313 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600314 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700315 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700316 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200317#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700318#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700319#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700320#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200321#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
322#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600323#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800324#define REQ_F_LINK_DONE 128 /* linked sqes done */
325#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800326#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600327#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600328#define REQ_F_ISREG 2048 /* regular file */
329#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600331 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600332 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333
334 struct work_struct work;
335};
336
337#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700338#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339
Jens Axboe9a56a232019-01-09 09:06:50 -0700340struct io_submit_state {
341 struct blk_plug plug;
342
343 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700344 * io_kiocb alloc cache
345 */
346 void *reqs[IO_IOPOLL_BATCH];
347 unsigned int free_reqs;
348 unsigned int cur_req;
349
350 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700351 * File reference cache
352 */
353 struct file *file;
354 unsigned int fd;
355 unsigned int has_refs;
356 unsigned int used_refs;
357 unsigned int ios_left;
358};
359
Jens Axboede0617e2019-04-06 21:51:27 -0600360static void io_sq_wq_submit_work(struct work_struct *work);
Jens Axboe5262f562019-09-17 12:26:57 -0600361static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
362 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800363static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600364
Jens Axboe2b188cc2019-01-07 10:46:33 -0700365static struct kmem_cache *req_cachep;
366
367static const struct file_operations io_uring_fops;
368
369struct sock *io_uring_get_socket(struct file *file)
370{
371#if defined(CONFIG_UNIX)
372 if (file->f_op == &io_uring_fops) {
373 struct io_ring_ctx *ctx = file->private_data;
374
375 return ctx->ring_sock->sk;
376 }
377#endif
378 return NULL;
379}
380EXPORT_SYMBOL(io_uring_get_socket);
381
382static void io_ring_ctx_ref_free(struct percpu_ref *ref)
383{
384 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
385
386 complete(&ctx->ctx_done);
387}
388
389static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
390{
391 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700392 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700393
394 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
395 if (!ctx)
396 return NULL;
397
Roman Gushchin21482892019-05-07 10:01:48 -0700398 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
399 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700400 kfree(ctx);
401 return NULL;
402 }
403
404 ctx->flags = p->flags;
405 init_waitqueue_head(&ctx->cq_wait);
406 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800407 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408 mutex_init(&ctx->uring_lock);
409 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700410 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
411 spin_lock_init(&ctx->pending_async[i].lock);
412 INIT_LIST_HEAD(&ctx->pending_async[i].list);
413 atomic_set(&ctx->pending_async[i].cnt, 0);
414 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700415 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700416 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700417 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600418 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600419 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700420 return ctx;
421}
422
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600423static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
424 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600425{
Jens Axboe498ccd92019-10-25 10:04:25 -0600426 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
427 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600428}
429
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600430static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
431 struct io_kiocb *req)
432{
433 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
434 return false;
435
436 return __io_sequence_defer(ctx, req);
437}
438
439static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600440{
441 struct io_kiocb *req;
442
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600443 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
444 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600445 list_del_init(&req->list);
446 return req;
447 }
448
449 return NULL;
450}
451
Jens Axboe5262f562019-09-17 12:26:57 -0600452static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
453{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600454 struct io_kiocb *req;
455
456 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
457 if (req && !__io_sequence_defer(ctx, req)) {
458 list_del_init(&req->list);
459 return req;
460 }
461
462 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600463}
464
Jens Axboede0617e2019-04-06 21:51:27 -0600465static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700466{
Hristo Venev75b28af2019-08-26 17:23:46 +0000467 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700468
Hristo Venev75b28af2019-08-26 17:23:46 +0000469 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700470 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000471 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700472
Jens Axboe2b188cc2019-01-07 10:46:33 -0700473 if (wq_has_sleeper(&ctx->cq_wait)) {
474 wake_up_interruptible(&ctx->cq_wait);
475 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
476 }
477 }
478}
479
Jens Axboe18d9be12019-09-10 09:13:05 -0600480static inline void io_queue_async_work(struct io_ring_ctx *ctx,
481 struct io_kiocb *req)
482{
Jens Axboe6cc47d12019-09-18 11:18:23 -0600483 int rw = 0;
Jens Axboe54a91f32019-09-10 09:15:04 -0600484
Jens Axboe6cc47d12019-09-18 11:18:23 -0600485 if (req->submit.sqe) {
486 switch (req->submit.sqe->opcode) {
487 case IORING_OP_WRITEV:
488 case IORING_OP_WRITE_FIXED:
489 rw = !(req->rw.ki_flags & IOCB_DIRECT);
490 break;
491 }
Jens Axboe54a91f32019-09-10 09:15:04 -0600492 }
493
494 queue_work(ctx->sqo_wq[rw], &req->work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600495}
496
Jens Axboe5262f562019-09-17 12:26:57 -0600497static void io_kill_timeout(struct io_kiocb *req)
498{
499 int ret;
500
501 ret = hrtimer_try_to_cancel(&req->timeout.timer);
502 if (ret != -1) {
503 atomic_inc(&req->ctx->cq_timeouts);
504 list_del(&req->list);
505 io_cqring_fill_event(req->ctx, req->user_data, 0);
506 __io_free_req(req);
507 }
508}
509
510static void io_kill_timeouts(struct io_ring_ctx *ctx)
511{
512 struct io_kiocb *req, *tmp;
513
514 spin_lock_irq(&ctx->completion_lock);
515 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
516 io_kill_timeout(req);
517 spin_unlock_irq(&ctx->completion_lock);
518}
519
Jens Axboede0617e2019-04-06 21:51:27 -0600520static void io_commit_cqring(struct io_ring_ctx *ctx)
521{
522 struct io_kiocb *req;
523
Jens Axboe5262f562019-09-17 12:26:57 -0600524 while ((req = io_get_timeout_req(ctx)) != NULL)
525 io_kill_timeout(req);
526
Jens Axboede0617e2019-04-06 21:51:27 -0600527 __io_commit_cqring(ctx);
528
529 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800530 if (req->flags & REQ_F_SHADOW_DRAIN) {
531 /* Just for drain, free it. */
532 __io_free_req(req);
533 continue;
534 }
Jens Axboede0617e2019-04-06 21:51:27 -0600535 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600536 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600537 }
538}
539
Jens Axboe2b188cc2019-01-07 10:46:33 -0700540static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
541{
Hristo Venev75b28af2019-08-26 17:23:46 +0000542 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700543 unsigned tail;
544
545 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200546 /*
547 * writes to the cq entry need to come after reading head; the
548 * control dependency is enough as we're using WRITE_ONCE to
549 * fill the cq entry
550 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000551 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700552 return NULL;
553
554 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000555 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700556}
557
558static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600559 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700560{
561 struct io_uring_cqe *cqe;
562
563 /*
564 * If we can't get a cq entry, userspace overflowed the
565 * submission (by quite a lot). Increment the overflow count in
566 * the ring.
567 */
568 cqe = io_get_cqring(ctx);
569 if (cqe) {
570 WRITE_ONCE(cqe->user_data, ki_user_data);
571 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600572 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600574 WRITE_ONCE(ctx->rings->cq_overflow,
575 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576 }
577}
578
Jens Axboe8c838782019-03-12 15:48:16 -0600579static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
580{
581 if (waitqueue_active(&ctx->wait))
582 wake_up(&ctx->wait);
583 if (waitqueue_active(&ctx->sqo_wait))
584 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600585 if (ctx->cq_ev_fd)
586 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600587}
588
589static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600590 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700591{
592 unsigned long flags;
593
594 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600595 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596 io_commit_cqring(ctx);
597 spin_unlock_irqrestore(&ctx->completion_lock, flags);
598
Jens Axboe8c838782019-03-12 15:48:16 -0600599 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700600}
601
Jens Axboe2579f912019-01-09 09:10:43 -0700602static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
603 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700604{
Jens Axboefd6fab22019-03-14 16:30:06 -0600605 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700606 struct io_kiocb *req;
607
608 if (!percpu_ref_tryget(&ctx->refs))
609 return NULL;
610
Jens Axboe2579f912019-01-09 09:10:43 -0700611 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600612 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700613 if (unlikely(!req))
614 goto out;
615 } else if (!state->free_reqs) {
616 size_t sz;
617 int ret;
618
619 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600620 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
621
622 /*
623 * Bulk alloc is all-or-nothing. If we fail to get a batch,
624 * retry single alloc to be on the safe side.
625 */
626 if (unlikely(ret <= 0)) {
627 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
628 if (!state->reqs[0])
629 goto out;
630 ret = 1;
631 }
Jens Axboe2579f912019-01-09 09:10:43 -0700632 state->free_reqs = ret - 1;
633 state->cur_req = 1;
634 req = state->reqs[0];
635 } else {
636 req = state->reqs[state->cur_req];
637 state->free_reqs--;
638 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700639 }
640
Jens Axboe60c112b2019-06-21 10:20:18 -0600641 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700642 req->ctx = ctx;
643 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600644 /* one is dropped after submission, the other at completion */
645 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600646 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700647 return req;
648out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300649 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700650 return NULL;
651}
652
Jens Axboedef596e2019-01-09 08:59:42 -0700653static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
654{
655 if (*nr) {
656 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300657 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700658 *nr = 0;
659 }
660}
661
Jens Axboe9e645e112019-05-10 16:07:28 -0600662static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700663{
Jens Axboe09bb8392019-03-13 12:39:28 -0600664 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
665 fput(req->file);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300666 percpu_ref_put(&req->ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600667 kmem_cache_free(req_cachep, req);
668}
669
Jens Axboeba816ad2019-09-28 11:36:45 -0600670static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600671{
672 struct io_kiocb *nxt;
673
674 /*
675 * The list should never be empty when we are called here. But could
676 * potentially happen if the chain is messed up, check to be on the
677 * safe side.
678 */
679 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
680 if (nxt) {
681 list_del(&nxt->list);
682 if (!list_empty(&req->link_list)) {
683 INIT_LIST_HEAD(&nxt->link_list);
684 list_splice(&req->link_list, &nxt->link_list);
685 nxt->flags |= REQ_F_LINK;
686 }
687
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800688 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboeba816ad2019-09-28 11:36:45 -0600689 /*
690 * If we're in async work, we can continue processing the chain
691 * in this context instead of having to queue up new async work.
692 */
693 if (nxtptr && current_work()) {
694 *nxtptr = nxt;
695 } else {
696 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
697 io_queue_async_work(req->ctx, nxt);
698 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600699 }
700}
701
702/*
703 * Called if REQ_F_LINK is set, and we fail the head request
704 */
705static void io_fail_links(struct io_kiocb *req)
706{
707 struct io_kiocb *link;
708
709 while (!list_empty(&req->link_list)) {
710 link = list_first_entry(&req->link_list, struct io_kiocb, list);
711 list_del(&link->list);
712
713 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
714 __io_free_req(link);
715 }
716}
717
Jens Axboeba816ad2019-09-28 11:36:45 -0600718static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600719{
720 /*
721 * If LINK is set, we have dependent requests in this chain. If we
722 * didn't fail this request, queue the first one up, moving any other
723 * dependencies to the next request. In case of failure, fail the rest
724 * of the chain.
725 */
726 if (req->flags & REQ_F_LINK) {
727 if (req->flags & REQ_F_FAIL_LINK)
728 io_fail_links(req);
729 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600730 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600731 }
732
733 __io_free_req(req);
734}
735
Jens Axboeba816ad2019-09-28 11:36:45 -0600736/*
737 * Drop reference to request, return next in chain (if there is one) if this
738 * was the last reference to this request.
739 */
740static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600741{
Jens Axboeba816ad2019-09-28 11:36:45 -0600742 struct io_kiocb *nxt = NULL;
743
Jens Axboee65ef562019-03-12 10:16:44 -0600744 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600745 io_free_req(req, &nxt);
746
747 return nxt;
748}
749
750static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
751{
752 struct io_kiocb *nxt;
753
754 nxt = io_put_req_find_next(req);
755 if (nxt) {
756 if (nxtptr) {
757 *nxtptr = nxt;
758 } else {
759 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
760 io_queue_async_work(nxt->ctx, nxt);
761 }
762 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700763}
764
Hristo Venev75b28af2019-08-26 17:23:46 +0000765static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600766{
767 /* See comment at the top of this file */
768 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000769 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600770}
771
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300772static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
773{
774 struct io_rings *rings = ctx->rings;
775
776 /* make sure SQ entry isn't read before tail */
777 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
778}
779
Jens Axboedef596e2019-01-09 08:59:42 -0700780/*
781 * Find and free completed poll iocbs
782 */
783static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
784 struct list_head *done)
785{
786 void *reqs[IO_IOPOLL_BATCH];
787 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600788 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700789
Jens Axboe09bb8392019-03-13 12:39:28 -0600790 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700791 while (!list_empty(done)) {
792 req = list_first_entry(done, struct io_kiocb, list);
793 list_del(&req->list);
794
Jens Axboe9e645e112019-05-10 16:07:28 -0600795 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700796 (*nr_events)++;
797
Jens Axboe09bb8392019-03-13 12:39:28 -0600798 if (refcount_dec_and_test(&req->refs)) {
799 /* If we're not using fixed files, we have to pair the
800 * completion part with the file put. Use regular
801 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600802 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600803 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600804 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
805 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600806 reqs[to_free++] = req;
807 if (to_free == ARRAY_SIZE(reqs))
808 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700809 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600810 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700811 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700812 }
Jens Axboedef596e2019-01-09 08:59:42 -0700813 }
Jens Axboedef596e2019-01-09 08:59:42 -0700814
Jens Axboe09bb8392019-03-13 12:39:28 -0600815 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700816 io_free_req_many(ctx, reqs, &to_free);
817}
818
819static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
820 long min)
821{
822 struct io_kiocb *req, *tmp;
823 LIST_HEAD(done);
824 bool spin;
825 int ret;
826
827 /*
828 * Only spin for completions if we don't have multiple devices hanging
829 * off our complete list, and we're under the requested amount.
830 */
831 spin = !ctx->poll_multi_file && *nr_events < min;
832
833 ret = 0;
834 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
835 struct kiocb *kiocb = &req->rw;
836
837 /*
838 * Move completed entries to our local list. If we find a
839 * request that requires polling, break out and complete
840 * the done list first, if we have entries there.
841 */
842 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
843 list_move_tail(&req->list, &done);
844 continue;
845 }
846 if (!list_empty(&done))
847 break;
848
849 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
850 if (ret < 0)
851 break;
852
853 if (ret && spin)
854 spin = false;
855 ret = 0;
856 }
857
858 if (!list_empty(&done))
859 io_iopoll_complete(ctx, nr_events, &done);
860
861 return ret;
862}
863
864/*
865 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
866 * non-spinning poll check - we'll still enter the driver poll loop, but only
867 * as a non-spinning completion check.
868 */
869static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
870 long min)
871{
Jens Axboe08f54392019-08-21 22:19:11 -0600872 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700873 int ret;
874
875 ret = io_do_iopoll(ctx, nr_events, min);
876 if (ret < 0)
877 return ret;
878 if (!min || *nr_events >= min)
879 return 0;
880 }
881
882 return 1;
883}
884
885/*
886 * We can't just wait for polled events to come to us, we have to actively
887 * find and complete them.
888 */
889static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
890{
891 if (!(ctx->flags & IORING_SETUP_IOPOLL))
892 return;
893
894 mutex_lock(&ctx->uring_lock);
895 while (!list_empty(&ctx->poll_list)) {
896 unsigned int nr_events = 0;
897
898 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600899
900 /*
901 * Ensure we allow local-to-the-cpu processing to take place,
902 * in this case we need to ensure that we reap all events.
903 */
904 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700905 }
906 mutex_unlock(&ctx->uring_lock);
907}
908
Jens Axboe2b2ed972019-10-25 10:06:15 -0600909static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
910 long min)
Jens Axboedef596e2019-01-09 08:59:42 -0700911{
Jens Axboe2b2ed972019-10-25 10:06:15 -0600912 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700913
914 do {
915 int tmin = 0;
916
Jens Axboe500f9fb2019-08-19 12:15:59 -0600917 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600918 * Don't enter poll loop if we already have events pending.
919 * If we do, we can potentially be spinning for commands that
920 * already triggered a CQE (eg in error).
921 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000922 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600923 break;
924
925 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600926 * If a submit got punted to a workqueue, we can have the
927 * application entering polling for a command before it gets
928 * issued. That app will hold the uring_lock for the duration
929 * of the poll right here, so we need to take a breather every
930 * now and then to ensure that the issue has a chance to add
931 * the poll to the issued list. Otherwise we can spin here
932 * forever, while the workqueue is stuck trying to acquire the
933 * very same mutex.
934 */
935 if (!(++iters & 7)) {
936 mutex_unlock(&ctx->uring_lock);
937 mutex_lock(&ctx->uring_lock);
938 }
939
Jens Axboedef596e2019-01-09 08:59:42 -0700940 if (*nr_events < min)
941 tmin = min - *nr_events;
942
943 ret = io_iopoll_getevents(ctx, nr_events, tmin);
944 if (ret <= 0)
945 break;
946 ret = 0;
947 } while (min && !*nr_events && !need_resched());
948
Jens Axboe2b2ed972019-10-25 10:06:15 -0600949 return ret;
950}
951
952static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
953 long min)
954{
955 int ret;
956
957 /*
958 * We disallow the app entering submit/complete with polling, but we
959 * still need to lock the ring to prevent racing with polled issue
960 * that got punted to a workqueue.
961 */
962 mutex_lock(&ctx->uring_lock);
963 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -0600964 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700965 return ret;
966}
967
Jens Axboe491381ce2019-10-17 09:20:46 -0600968static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700969{
Jens Axboe491381ce2019-10-17 09:20:46 -0600970 /*
971 * Tell lockdep we inherited freeze protection from submission
972 * thread.
973 */
974 if (req->flags & REQ_F_ISREG) {
975 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700976
Jens Axboe491381ce2019-10-17 09:20:46 -0600977 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700978 }
Jens Axboe491381ce2019-10-17 09:20:46 -0600979 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700980}
981
Jens Axboeba816ad2019-09-28 11:36:45 -0600982static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700983{
984 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
985
Jens Axboe491381ce2019-10-17 09:20:46 -0600986 if (kiocb->ki_flags & IOCB_WRITE)
987 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700988
Jens Axboe9e645e112019-05-10 16:07:28 -0600989 if ((req->flags & REQ_F_LINK) && res != req->result)
990 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600991 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -0600992}
993
994static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
995{
996 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
997
998 io_complete_rw_common(kiocb, res);
999 io_put_req(req, NULL);
1000}
1001
1002static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1003{
1004 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1005
1006 io_complete_rw_common(kiocb, res);
1007 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008}
1009
Jens Axboedef596e2019-01-09 08:59:42 -07001010static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1011{
1012 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1013
Jens Axboe491381ce2019-10-17 09:20:46 -06001014 if (kiocb->ki_flags & IOCB_WRITE)
1015 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001016
Jens Axboe9e645e112019-05-10 16:07:28 -06001017 if ((req->flags & REQ_F_LINK) && res != req->result)
1018 req->flags |= REQ_F_FAIL_LINK;
1019 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001020 if (res != -EAGAIN)
1021 req->flags |= REQ_F_IOPOLL_COMPLETED;
1022}
1023
1024/*
1025 * After the iocb has been issued, it's safe to be found on the poll list.
1026 * Adding the kiocb to the list AFTER submission ensures that we don't
1027 * find it from a io_iopoll_getevents() thread before the issuer is done
1028 * accessing the kiocb cookie.
1029 */
1030static void io_iopoll_req_issued(struct io_kiocb *req)
1031{
1032 struct io_ring_ctx *ctx = req->ctx;
1033
1034 /*
1035 * Track whether we have multiple files in our lists. This will impact
1036 * how we do polling eventually, not spinning if we're on potentially
1037 * different devices.
1038 */
1039 if (list_empty(&ctx->poll_list)) {
1040 ctx->poll_multi_file = false;
1041 } else if (!ctx->poll_multi_file) {
1042 struct io_kiocb *list_req;
1043
1044 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1045 list);
1046 if (list_req->rw.ki_filp != req->rw.ki_filp)
1047 ctx->poll_multi_file = true;
1048 }
1049
1050 /*
1051 * For fast devices, IO may have already completed. If it has, add
1052 * it to the front so we find it first.
1053 */
1054 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1055 list_add(&req->list, &ctx->poll_list);
1056 else
1057 list_add_tail(&req->list, &ctx->poll_list);
1058}
1059
Jens Axboe3d6770f2019-04-13 11:50:54 -06001060static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001061{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001062 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001063 int diff = state->has_refs - state->used_refs;
1064
1065 if (diff)
1066 fput_many(state->file, diff);
1067 state->file = NULL;
1068 }
1069}
1070
1071/*
1072 * Get as many references to a file as we have IOs left in this submission,
1073 * assuming most submissions are for one file, or at least that each file
1074 * has more than one submission.
1075 */
1076static struct file *io_file_get(struct io_submit_state *state, int fd)
1077{
1078 if (!state)
1079 return fget(fd);
1080
1081 if (state->file) {
1082 if (state->fd == fd) {
1083 state->used_refs++;
1084 state->ios_left--;
1085 return state->file;
1086 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001087 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001088 }
1089 state->file = fget_many(fd, state->ios_left);
1090 if (!state->file)
1091 return NULL;
1092
1093 state->fd = fd;
1094 state->has_refs = state->ios_left;
1095 state->used_refs = 1;
1096 state->ios_left--;
1097 return state->file;
1098}
1099
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100/*
1101 * If we tracked the file through the SCM inflight mechanism, we could support
1102 * any file. For now, just ensure that anything potentially problematic is done
1103 * inline.
1104 */
1105static bool io_file_supports_async(struct file *file)
1106{
1107 umode_t mode = file_inode(file)->i_mode;
1108
1109 if (S_ISBLK(mode) || S_ISCHR(mode))
1110 return true;
1111 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1112 return true;
1113
1114 return false;
1115}
1116
Jens Axboe6c271ce2019-01-10 11:22:30 -07001117static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001118 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001120 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001121 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001122 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001123 unsigned ioprio;
1124 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125
Jens Axboe09bb8392019-03-13 12:39:28 -06001126 if (!req->file)
1127 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001128
Jens Axboe491381ce2019-10-17 09:20:46 -06001129 if (S_ISREG(file_inode(req->file)->i_mode))
1130 req->flags |= REQ_F_ISREG;
1131
1132 /*
1133 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1134 * we know to async punt it even if it was opened O_NONBLOCK
1135 */
1136 if (force_nonblock && !io_file_supports_async(req->file)) {
1137 req->flags |= REQ_F_MUST_PUNT;
1138 return -EAGAIN;
1139 }
Jens Axboe6b063142019-01-10 22:13:58 -07001140
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141 kiocb->ki_pos = READ_ONCE(sqe->off);
1142 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1143 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1144
1145 ioprio = READ_ONCE(sqe->ioprio);
1146 if (ioprio) {
1147 ret = ioprio_check_cap(ioprio);
1148 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001149 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150
1151 kiocb->ki_ioprio = ioprio;
1152 } else
1153 kiocb->ki_ioprio = get_current_ioprio();
1154
1155 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1156 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001157 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001158
1159 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001160 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1161 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001162 req->flags |= REQ_F_NOWAIT;
1163
1164 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001166
Jens Axboedef596e2019-01-09 08:59:42 -07001167 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001168 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1169 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001170 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171
Jens Axboedef596e2019-01-09 08:59:42 -07001172 kiocb->ki_flags |= IOCB_HIPRI;
1173 kiocb->ki_complete = io_complete_rw_iopoll;
1174 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001175 if (kiocb->ki_flags & IOCB_HIPRI)
1176 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001177 kiocb->ki_complete = io_complete_rw;
1178 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001179 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180}
1181
1182static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1183{
1184 switch (ret) {
1185 case -EIOCBQUEUED:
1186 break;
1187 case -ERESTARTSYS:
1188 case -ERESTARTNOINTR:
1189 case -ERESTARTNOHAND:
1190 case -ERESTART_RESTARTBLOCK:
1191 /*
1192 * We can't just restart the syscall, since previously
1193 * submitted sqes may already be in progress. Just fail this
1194 * IO with EINTR.
1195 */
1196 ret = -EINTR;
1197 /* fall through */
1198 default:
1199 kiocb->ki_complete(kiocb, ret, 0);
1200 }
1201}
1202
Jens Axboeba816ad2019-09-28 11:36:45 -06001203static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1204 bool in_async)
1205{
1206 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1207 *nxt = __io_complete_rw(kiocb, ret);
1208 else
1209 io_rw_done(kiocb, ret);
1210}
1211
Jens Axboeedafcce2019-01-09 09:16:05 -07001212static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1213 const struct io_uring_sqe *sqe,
1214 struct iov_iter *iter)
1215{
1216 size_t len = READ_ONCE(sqe->len);
1217 struct io_mapped_ubuf *imu;
1218 unsigned index, buf_index;
1219 size_t offset;
1220 u64 buf_addr;
1221
1222 /* attempt to use fixed buffers without having provided iovecs */
1223 if (unlikely(!ctx->user_bufs))
1224 return -EFAULT;
1225
1226 buf_index = READ_ONCE(sqe->buf_index);
1227 if (unlikely(buf_index >= ctx->nr_user_bufs))
1228 return -EFAULT;
1229
1230 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1231 imu = &ctx->user_bufs[index];
1232 buf_addr = READ_ONCE(sqe->addr);
1233
1234 /* overflow */
1235 if (buf_addr + len < buf_addr)
1236 return -EFAULT;
1237 /* not inside the mapped region */
1238 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1239 return -EFAULT;
1240
1241 /*
1242 * May not be a start of buffer, set size appropriately
1243 * and advance us to the beginning.
1244 */
1245 offset = buf_addr - imu->ubuf;
1246 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001247
1248 if (offset) {
1249 /*
1250 * Don't use iov_iter_advance() here, as it's really slow for
1251 * using the latter parts of a big fixed buffer - it iterates
1252 * over each segment manually. We can cheat a bit here, because
1253 * we know that:
1254 *
1255 * 1) it's a BVEC iter, we set it up
1256 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1257 * first and last bvec
1258 *
1259 * So just find our index, and adjust the iterator afterwards.
1260 * If the offset is within the first bvec (or the whole first
1261 * bvec, just use iov_iter_advance(). This makes it easier
1262 * since we can just skip the first segment, which may not
1263 * be PAGE_SIZE aligned.
1264 */
1265 const struct bio_vec *bvec = imu->bvec;
1266
1267 if (offset <= bvec->bv_len) {
1268 iov_iter_advance(iter, offset);
1269 } else {
1270 unsigned long seg_skip;
1271
1272 /* skip first vec */
1273 offset -= bvec->bv_len;
1274 seg_skip = 1 + (offset >> PAGE_SHIFT);
1275
1276 iter->bvec = bvec + seg_skip;
1277 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001278 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001279 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001280 }
1281 }
1282
Jens Axboeedafcce2019-01-09 09:16:05 -07001283 return 0;
1284}
1285
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001286static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1287 const struct sqe_submit *s, struct iovec **iovec,
1288 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001289{
1290 const struct io_uring_sqe *sqe = s->sqe;
1291 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1292 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001293 u8 opcode;
1294
1295 /*
1296 * We're reading ->opcode for the second time, but the first read
1297 * doesn't care whether it's _FIXED or not, so it doesn't matter
1298 * whether ->opcode changes concurrently. The first read does care
1299 * about whether it is a READ or a WRITE, so we don't trust this read
1300 * for that purpose and instead let the caller pass in the read/write
1301 * flag.
1302 */
1303 opcode = READ_ONCE(sqe->opcode);
1304 if (opcode == IORING_OP_READ_FIXED ||
1305 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001306 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001307 *iovec = NULL;
1308 return ret;
1309 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001310
1311 if (!s->has_user)
1312 return -EFAULT;
1313
1314#ifdef CONFIG_COMPAT
1315 if (ctx->compat)
1316 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1317 iovec, iter);
1318#endif
1319
1320 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1321}
1322
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001323static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1324{
1325 if (al->file == kiocb->ki_filp) {
1326 off_t start, end;
1327
1328 /*
1329 * Allow merging if we're anywhere in the range of the same
1330 * page. Generally this happens for sub-page reads or writes,
1331 * and it's beneficial to allow the first worker to bring the
1332 * page in and the piggy backed work can then work on the
1333 * cached page.
1334 */
1335 start = al->io_start & PAGE_MASK;
1336 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1337 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1338 return true;
1339 }
1340
1341 al->file = NULL;
1342 return false;
1343}
1344
Jens Axboe31b51512019-01-18 22:56:34 -07001345/*
1346 * Make a note of the last file/offset/direction we punted to async
1347 * context. We'll use this information to see if we can piggy back a
1348 * sequential request onto the previous one, if it's still hasn't been
1349 * completed by the async worker.
1350 */
1351static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1352{
1353 struct async_list *async_list = &req->ctx->pending_async[rw];
1354 struct kiocb *kiocb = &req->rw;
1355 struct file *filp = kiocb->ki_filp;
Jens Axboe31b51512019-01-18 22:56:34 -07001356
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001357 if (io_should_merge(async_list, kiocb)) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001358 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001359
1360 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001361 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1362 if (!max_bytes)
1363 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001364
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001365 /* If max len are exceeded, reset the state */
1366 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001367 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001368 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001369 } else {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001370 async_list->file = NULL;
Jens Axboe31b51512019-01-18 22:56:34 -07001371 }
1372 }
1373
1374 /* New file? Reset state. */
1375 if (async_list->file != filp) {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001376 async_list->io_start = kiocb->ki_pos;
1377 async_list->io_len = len;
Jens Axboe31b51512019-01-18 22:56:34 -07001378 async_list->file = filp;
1379 }
Jens Axboe31b51512019-01-18 22:56:34 -07001380}
1381
Jens Axboe32960612019-09-23 11:05:34 -06001382/*
1383 * For files that don't have ->read_iter() and ->write_iter(), handle them
1384 * by looping over ->read() or ->write() manually.
1385 */
1386static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1387 struct iov_iter *iter)
1388{
1389 ssize_t ret = 0;
1390
1391 /*
1392 * Don't support polled IO through this interface, and we can't
1393 * support non-blocking either. For the latter, this just causes
1394 * the kiocb to be handled from an async context.
1395 */
1396 if (kiocb->ki_flags & IOCB_HIPRI)
1397 return -EOPNOTSUPP;
1398 if (kiocb->ki_flags & IOCB_NOWAIT)
1399 return -EAGAIN;
1400
1401 while (iov_iter_count(iter)) {
1402 struct iovec iovec = iov_iter_iovec(iter);
1403 ssize_t nr;
1404
1405 if (rw == READ) {
1406 nr = file->f_op->read(file, iovec.iov_base,
1407 iovec.iov_len, &kiocb->ki_pos);
1408 } else {
1409 nr = file->f_op->write(file, iovec.iov_base,
1410 iovec.iov_len, &kiocb->ki_pos);
1411 }
1412
1413 if (nr < 0) {
1414 if (!ret)
1415 ret = nr;
1416 break;
1417 }
1418 ret += nr;
1419 if (nr != iovec.iov_len)
1420 break;
1421 iov_iter_advance(iter, nr);
1422 }
1423
1424 return ret;
1425}
1426
Jens Axboee0c5c572019-03-12 10:18:47 -06001427static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001428 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429{
1430 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1431 struct kiocb *kiocb = &req->rw;
1432 struct iov_iter iter;
1433 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001434 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001435 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436
Jens Axboe8358e3a2019-04-23 08:17:58 -06001437 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001438 if (ret)
1439 return ret;
1440 file = kiocb->ki_filp;
1441
Jens Axboe2b188cc2019-01-07 10:46:33 -07001442 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001443 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001444
1445 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001446 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001447 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001448
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001449 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001450 if (req->flags & REQ_F_LINK)
1451 req->result = read_size;
1452
Jens Axboe31b51512019-01-18 22:56:34 -07001453 iov_count = iov_iter_count(&iter);
1454 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001455 if (!ret) {
1456 ssize_t ret2;
1457
Jens Axboe32960612019-09-23 11:05:34 -06001458 if (file->f_op->read_iter)
1459 ret2 = call_read_iter(file, kiocb, &iter);
1460 else
1461 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1462
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001463 /*
1464 * In case of a short read, punt to async. This can happen
1465 * if we have data partially cached. Alternatively we can
1466 * return the short read, in which case the application will
1467 * need to issue another SQE and wait for it. That SQE will
1468 * need async punt anyway, so it's more efficient to do it
1469 * here.
1470 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001471 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1472 (req->flags & REQ_F_ISREG) &&
1473 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001474 ret2 = -EAGAIN;
1475 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001476 if (!force_nonblock || ret2 != -EAGAIN) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001477 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe31b51512019-01-18 22:56:34 -07001478 } else {
Jackie Liuba5290c2019-10-09 09:19:59 +08001479 if (!s->in_async)
Jens Axboe31b51512019-01-18 22:56:34 -07001480 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001481 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001482 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001483 }
1484 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001485 return ret;
1486}
1487
Jens Axboee0c5c572019-03-12 10:18:47 -06001488static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001489 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490{
1491 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1492 struct kiocb *kiocb = &req->rw;
1493 struct iov_iter iter;
1494 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001495 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001496 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001497
Jens Axboe8358e3a2019-04-23 08:17:58 -06001498 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499 if (ret)
1500 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501
Jens Axboe2b188cc2019-01-07 10:46:33 -07001502 file = kiocb->ki_filp;
1503 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001504 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001505
1506 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001507 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001508 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001509
Jens Axboe9e645e112019-05-10 16:07:28 -06001510 if (req->flags & REQ_F_LINK)
1511 req->result = ret;
1512
Jens Axboe31b51512019-01-18 22:56:34 -07001513 iov_count = iov_iter_count(&iter);
1514
1515 ret = -EAGAIN;
1516 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001517 if (!s->in_async)
Jens Axboe31b51512019-01-18 22:56:34 -07001518 io_async_list_note(WRITE, req, iov_count);
1519 goto out_free;
1520 }
1521
1522 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001523 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001524 ssize_t ret2;
1525
Jens Axboe2b188cc2019-01-07 10:46:33 -07001526 /*
1527 * Open-code file_start_write here to grab freeze protection,
1528 * which will be released by another thread in
1529 * io_complete_rw(). Fool lockdep by telling it the lock got
1530 * released so that it doesn't complain about the held lock when
1531 * we return to userspace.
1532 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001533 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001534 __sb_start_write(file_inode(file)->i_sb,
1535 SB_FREEZE_WRITE, true);
1536 __sb_writers_release(file_inode(file)->i_sb,
1537 SB_FREEZE_WRITE);
1538 }
1539 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001540
Jens Axboe32960612019-09-23 11:05:34 -06001541 if (file->f_op->write_iter)
1542 ret2 = call_write_iter(file, kiocb, &iter);
1543 else
1544 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001545 if (!force_nonblock || ret2 != -EAGAIN) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001546 kiocb_done(kiocb, ret2, nxt, s->in_async);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001547 } else {
Jackie Liuba5290c2019-10-09 09:19:59 +08001548 if (!s->in_async)
Roman Penyaev9bf79332019-03-25 20:09:24 +01001549 io_async_list_note(WRITE, req, iov_count);
1550 ret = -EAGAIN;
1551 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001552 }
Jens Axboe31b51512019-01-18 22:56:34 -07001553out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001554 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001555 return ret;
1556}
1557
1558/*
1559 * IORING_OP_NOP just posts a completion event, nothing else.
1560 */
1561static int io_nop(struct io_kiocb *req, u64 user_data)
1562{
1563 struct io_ring_ctx *ctx = req->ctx;
1564 long err = 0;
1565
Jens Axboedef596e2019-01-09 08:59:42 -07001566 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1567 return -EINVAL;
1568
Jens Axboec71ffb62019-05-13 20:58:29 -06001569 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001570 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001571 return 0;
1572}
1573
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001574static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1575{
Jens Axboe6b063142019-01-10 22:13:58 -07001576 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001577
Jens Axboe09bb8392019-03-13 12:39:28 -06001578 if (!req->file)
1579 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001580
Jens Axboe6b063142019-01-10 22:13:58 -07001581 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001582 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001583 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001584 return -EINVAL;
1585
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001586 return 0;
1587}
1588
1589static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001590 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001591{
1592 loff_t sqe_off = READ_ONCE(sqe->off);
1593 loff_t sqe_len = READ_ONCE(sqe->len);
1594 loff_t end = sqe_off + sqe_len;
1595 unsigned fsync_flags;
1596 int ret;
1597
1598 fsync_flags = READ_ONCE(sqe->fsync_flags);
1599 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1600 return -EINVAL;
1601
1602 ret = io_prep_fsync(req, sqe);
1603 if (ret)
1604 return ret;
1605
1606 /* fsync always requires a blocking context */
1607 if (force_nonblock)
1608 return -EAGAIN;
1609
1610 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1611 end > 0 ? end : LLONG_MAX,
1612 fsync_flags & IORING_FSYNC_DATASYNC);
1613
Jens Axboe9e645e112019-05-10 16:07:28 -06001614 if (ret < 0 && (req->flags & REQ_F_LINK))
1615 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001616 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001617 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001618 return 0;
1619}
1620
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001621static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1622{
1623 struct io_ring_ctx *ctx = req->ctx;
1624 int ret = 0;
1625
1626 if (!req->file)
1627 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001628
1629 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1630 return -EINVAL;
1631 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1632 return -EINVAL;
1633
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001634 return ret;
1635}
1636
1637static int io_sync_file_range(struct io_kiocb *req,
1638 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001639 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001640 bool force_nonblock)
1641{
1642 loff_t sqe_off;
1643 loff_t sqe_len;
1644 unsigned flags;
1645 int ret;
1646
1647 ret = io_prep_sfr(req, sqe);
1648 if (ret)
1649 return ret;
1650
1651 /* sync_file_range always requires a blocking context */
1652 if (force_nonblock)
1653 return -EAGAIN;
1654
1655 sqe_off = READ_ONCE(sqe->off);
1656 sqe_len = READ_ONCE(sqe->len);
1657 flags = READ_ONCE(sqe->sync_range_flags);
1658
1659 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1660
Jens Axboe9e645e112019-05-10 16:07:28 -06001661 if (ret < 0 && (req->flags & REQ_F_LINK))
1662 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001663 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001664 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001665 return 0;
1666}
1667
Jens Axboe0fa03c62019-04-19 13:34:07 -06001668#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001669static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001670 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001671 long (*fn)(struct socket *, struct user_msghdr __user *,
1672 unsigned int))
1673{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001674 struct socket *sock;
1675 int ret;
1676
1677 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1678 return -EINVAL;
1679
1680 sock = sock_from_file(req->file, &ret);
1681 if (sock) {
1682 struct user_msghdr __user *msg;
1683 unsigned flags;
1684
1685 flags = READ_ONCE(sqe->msg_flags);
1686 if (flags & MSG_DONTWAIT)
1687 req->flags |= REQ_F_NOWAIT;
1688 else if (force_nonblock)
1689 flags |= MSG_DONTWAIT;
1690
1691 msg = (struct user_msghdr __user *) (unsigned long)
1692 READ_ONCE(sqe->addr);
1693
Jens Axboeaa1fa282019-04-19 13:38:09 -06001694 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001695 if (force_nonblock && ret == -EAGAIN)
1696 return ret;
1697 }
1698
1699 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001700 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001701 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001702}
1703#endif
1704
1705static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001706 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001707{
1708#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001709 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1710 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001711#else
1712 return -EOPNOTSUPP;
1713#endif
1714}
1715
1716static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001717 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001718{
1719#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001720 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1721 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001722#else
1723 return -EOPNOTSUPP;
1724#endif
1725}
1726
Jens Axboe221c5eb2019-01-17 09:41:58 -07001727static void io_poll_remove_one(struct io_kiocb *req)
1728{
1729 struct io_poll_iocb *poll = &req->poll;
1730
1731 spin_lock(&poll->head->lock);
1732 WRITE_ONCE(poll->canceled, true);
1733 if (!list_empty(&poll->wait.entry)) {
1734 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001735 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001736 }
1737 spin_unlock(&poll->head->lock);
1738
1739 list_del_init(&req->list);
1740}
1741
1742static void io_poll_remove_all(struct io_ring_ctx *ctx)
1743{
1744 struct io_kiocb *req;
1745
1746 spin_lock_irq(&ctx->completion_lock);
1747 while (!list_empty(&ctx->cancel_list)) {
1748 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1749 io_poll_remove_one(req);
1750 }
1751 spin_unlock_irq(&ctx->completion_lock);
1752}
1753
1754/*
1755 * Find a running poll command that matches one specified in sqe->addr,
1756 * and remove it if found.
1757 */
1758static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1759{
1760 struct io_ring_ctx *ctx = req->ctx;
1761 struct io_kiocb *poll_req, *next;
1762 int ret = -ENOENT;
1763
1764 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1765 return -EINVAL;
1766 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1767 sqe->poll_events)
1768 return -EINVAL;
1769
1770 spin_lock_irq(&ctx->completion_lock);
1771 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1772 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1773 io_poll_remove_one(poll_req);
1774 ret = 0;
1775 break;
1776 }
1777 }
1778 spin_unlock_irq(&ctx->completion_lock);
1779
Jens Axboec71ffb62019-05-13 20:58:29 -06001780 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001781 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001782 return 0;
1783}
1784
Jens Axboe8c838782019-03-12 15:48:16 -06001785static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1786 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001787{
Jens Axboe8c838782019-03-12 15:48:16 -06001788 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001789 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001790 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001791}
1792
1793static void io_poll_complete_work(struct work_struct *work)
1794{
1795 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1796 struct io_poll_iocb *poll = &req->poll;
1797 struct poll_table_struct pt = { ._key = poll->events };
1798 struct io_ring_ctx *ctx = req->ctx;
1799 __poll_t mask = 0;
1800
1801 if (!READ_ONCE(poll->canceled))
1802 mask = vfs_poll(poll->file, &pt) & poll->events;
1803
1804 /*
1805 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1806 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1807 * synchronize with them. In the cancellation case the list_del_init
1808 * itself is not actually needed, but harmless so we keep it in to
1809 * avoid further branches in the fast path.
1810 */
1811 spin_lock_irq(&ctx->completion_lock);
1812 if (!mask && !READ_ONCE(poll->canceled)) {
1813 add_wait_queue(poll->head, &poll->wait);
1814 spin_unlock_irq(&ctx->completion_lock);
1815 return;
1816 }
1817 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001818 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001819 spin_unlock_irq(&ctx->completion_lock);
1820
Jens Axboe8c838782019-03-12 15:48:16 -06001821 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001822 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001823}
1824
1825static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1826 void *key)
1827{
1828 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1829 wait);
1830 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1831 struct io_ring_ctx *ctx = req->ctx;
1832 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001833 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001834
1835 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001836 if (mask && !(mask & poll->events))
1837 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001838
1839 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001840
1841 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1842 list_del(&req->list);
1843 io_poll_complete(ctx, req, mask);
1844 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1845
1846 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001847 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001848 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001849 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001850 }
1851
Jens Axboe221c5eb2019-01-17 09:41:58 -07001852 return 1;
1853}
1854
1855struct io_poll_table {
1856 struct poll_table_struct pt;
1857 struct io_kiocb *req;
1858 int error;
1859};
1860
1861static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1862 struct poll_table_struct *p)
1863{
1864 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1865
1866 if (unlikely(pt->req->poll.head)) {
1867 pt->error = -EINVAL;
1868 return;
1869 }
1870
1871 pt->error = 0;
1872 pt->req->poll.head = head;
1873 add_wait_queue(head, &pt->req->poll.wait);
1874}
1875
1876static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1877{
1878 struct io_poll_iocb *poll = &req->poll;
1879 struct io_ring_ctx *ctx = req->ctx;
1880 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001881 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001882 __poll_t mask;
1883 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001884
1885 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1886 return -EINVAL;
1887 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1888 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001889 if (!poll->file)
1890 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001891
Jens Axboe6cc47d12019-09-18 11:18:23 -06001892 req->submit.sqe = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001893 INIT_WORK(&req->work, io_poll_complete_work);
1894 events = READ_ONCE(sqe->poll_events);
1895 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1896
Jens Axboe221c5eb2019-01-17 09:41:58 -07001897 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001898 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001899 poll->canceled = false;
1900
1901 ipt.pt._qproc = io_poll_queue_proc;
1902 ipt.pt._key = poll->events;
1903 ipt.req = req;
1904 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1905
1906 /* initialized the list so that we can do list_empty checks */
1907 INIT_LIST_HEAD(&poll->wait.entry);
1908 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1909
Jens Axboe36703242019-07-25 10:20:18 -06001910 INIT_LIST_HEAD(&req->list);
1911
Jens Axboe221c5eb2019-01-17 09:41:58 -07001912 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001913
1914 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001915 if (likely(poll->head)) {
1916 spin_lock(&poll->head->lock);
1917 if (unlikely(list_empty(&poll->wait.entry))) {
1918 if (ipt.error)
1919 cancel = true;
1920 ipt.error = 0;
1921 mask = 0;
1922 }
1923 if (mask || ipt.error)
1924 list_del_init(&poll->wait.entry);
1925 else if (cancel)
1926 WRITE_ONCE(poll->canceled, true);
1927 else if (!poll->done) /* actually waiting for an event */
1928 list_add_tail(&req->list, &ctx->cancel_list);
1929 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001930 }
Jens Axboe8c838782019-03-12 15:48:16 -06001931 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001932 ipt.error = 0;
1933 io_poll_complete(ctx, req, mask);
1934 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001935 spin_unlock_irq(&ctx->completion_lock);
1936
Jens Axboe8c838782019-03-12 15:48:16 -06001937 if (mask) {
1938 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001939 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001940 }
Jens Axboe8c838782019-03-12 15:48:16 -06001941 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001942}
1943
Jens Axboe5262f562019-09-17 12:26:57 -06001944static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1945{
1946 struct io_ring_ctx *ctx;
zhangyi (F)ef036812019-10-23 15:10:08 +08001947 struct io_kiocb *req, *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001948 unsigned long flags;
1949
1950 req = container_of(timer, struct io_kiocb, timeout.timer);
1951 ctx = req->ctx;
1952 atomic_inc(&ctx->cq_timeouts);
1953
1954 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001955 /*
1956 * Adjust the reqs sequence before the current one because it
1957 * will consume a slot in the cq_ring and the the cq_tail pointer
1958 * will be increased, otherwise other timeout reqs may return in
1959 * advance without waiting for enough wait_nr.
1960 */
1961 prev = req;
1962 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1963 prev->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06001964 list_del(&req->list);
1965
1966 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1967 io_commit_cqring(ctx);
1968 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1969
1970 io_cqring_ev_posted(ctx);
1971
Jens Axboeba816ad2019-09-28 11:36:45 -06001972 io_put_req(req, NULL);
Jens Axboe5262f562019-09-17 12:26:57 -06001973 return HRTIMER_NORESTART;
1974}
1975
1976static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1977{
yangerkun5da0fb12019-10-15 21:59:29 +08001978 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06001979 struct io_ring_ctx *ctx = req->ctx;
1980 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06001981 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001982 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001983 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06001984 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06001985
1986 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1987 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06001988 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
1989 return -EINVAL;
1990 flags = READ_ONCE(sqe->timeout_flags);
1991 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06001992 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001993
1994 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06001995 return -EFAULT;
1996
1997 /*
1998 * sqe->off holds how many events that need to occur for this
1999 * timeout event to be satisfied.
2000 */
2001 count = READ_ONCE(sqe->off);
2002 if (!count)
2003 count = 1;
2004
2005 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002006 /* reuse it to store the count */
2007 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002008 req->flags |= REQ_F_TIMEOUT;
2009
2010 /*
2011 * Insertion sort, ensuring the first entry in the list is always
2012 * the one we need first.
2013 */
Jens Axboe5262f562019-09-17 12:26:57 -06002014 spin_lock_irq(&ctx->completion_lock);
2015 list_for_each_prev(entry, &ctx->timeout_list) {
2016 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002017 unsigned nxt_sq_head;
2018 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002019
yangerkun5da0fb12019-10-15 21:59:29 +08002020 /*
2021 * Since cached_sq_head + count - 1 can overflow, use type long
2022 * long to store it.
2023 */
2024 tmp = (long long)ctx->cached_sq_head + count - 1;
2025 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2026 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2027
2028 /*
2029 * cached_sq_head may overflow, and it will never overflow twice
2030 * once there is some timeout req still be valid.
2031 */
2032 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002033 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002034
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002035 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002036 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002037
2038 /*
2039 * Sequence of reqs after the insert one and itself should
2040 * be adjusted because each timeout req consumes a slot.
2041 */
2042 span++;
2043 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002044 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002045 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002046 list_add(&req->list, entry);
2047 spin_unlock_irq(&ctx->completion_lock);
2048
Jens Axboea41525a2019-10-15 16:48:15 -06002049 if (flags & IORING_TIMEOUT_ABS)
2050 mode = HRTIMER_MODE_ABS;
2051 else
2052 mode = HRTIMER_MODE_REL;
2053 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
Jens Axboe5262f562019-09-17 12:26:57 -06002054 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002055 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe5262f562019-09-17 12:26:57 -06002056 return 0;
2057}
2058
Jens Axboede0617e2019-04-06 21:51:27 -06002059static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2060 const struct io_uring_sqe *sqe)
2061{
2062 struct io_uring_sqe *sqe_copy;
2063
2064 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2065 return 0;
2066
2067 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2068 if (!sqe_copy)
2069 return -EAGAIN;
2070
2071 spin_lock_irq(&ctx->completion_lock);
2072 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2073 spin_unlock_irq(&ctx->completion_lock);
2074 kfree(sqe_copy);
2075 return 0;
2076 }
2077
2078 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2079 req->submit.sqe = sqe_copy;
2080
2081 INIT_WORK(&req->work, io_sq_wq_submit_work);
2082 list_add_tail(&req->list, &ctx->defer_list);
2083 spin_unlock_irq(&ctx->completion_lock);
2084 return -EIOCBQUEUED;
2085}
2086
Jens Axboe2b188cc2019-01-07 10:46:33 -07002087static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboeba816ad2019-09-28 11:36:45 -06002088 const struct sqe_submit *s, struct io_kiocb **nxt,
2089 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002090{
Jens Axboee0c5c572019-03-12 10:18:47 -06002091 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002092
Jens Axboe9e645e112019-05-10 16:07:28 -06002093 req->user_data = READ_ONCE(s->sqe->user_data);
2094
Jens Axboe2b188cc2019-01-07 10:46:33 -07002095 if (unlikely(s->index >= ctx->sq_entries))
2096 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002097
2098 opcode = READ_ONCE(s->sqe->opcode);
2099 switch (opcode) {
2100 case IORING_OP_NOP:
2101 ret = io_nop(req, req->user_data);
2102 break;
2103 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002104 if (unlikely(s->sqe->buf_index))
2105 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002106 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002107 break;
2108 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002109 if (unlikely(s->sqe->buf_index))
2110 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002111 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002112 break;
2113 case IORING_OP_READ_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002114 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002115 break;
2116 case IORING_OP_WRITE_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002117 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002118 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002119 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002120 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002121 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002122 case IORING_OP_POLL_ADD:
2123 ret = io_poll_add(req, s->sqe);
2124 break;
2125 case IORING_OP_POLL_REMOVE:
2126 ret = io_poll_remove(req, s->sqe);
2127 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002128 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002129 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002130 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002131 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002132 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002133 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002134 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002135 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002136 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002137 case IORING_OP_TIMEOUT:
2138 ret = io_timeout(req, s->sqe);
2139 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002140 default:
2141 ret = -EINVAL;
2142 break;
2143 }
2144
Jens Axboedef596e2019-01-09 08:59:42 -07002145 if (ret)
2146 return ret;
2147
2148 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002149 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002150 return -EAGAIN;
2151
2152 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002153 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002154 mutex_lock(&ctx->uring_lock);
2155 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002156 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002157 mutex_unlock(&ctx->uring_lock);
2158 }
2159
2160 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002161}
2162
Jens Axboe31b51512019-01-18 22:56:34 -07002163static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
2164 const struct io_uring_sqe *sqe)
2165{
2166 switch (sqe->opcode) {
2167 case IORING_OP_READV:
2168 case IORING_OP_READ_FIXED:
2169 return &ctx->pending_async[READ];
2170 case IORING_OP_WRITEV:
2171 case IORING_OP_WRITE_FIXED:
2172 return &ctx->pending_async[WRITE];
2173 default:
2174 return NULL;
2175 }
2176}
2177
Jens Axboeedafcce2019-01-09 09:16:05 -07002178static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
2179{
2180 u8 opcode = READ_ONCE(sqe->opcode);
2181
2182 return !(opcode == IORING_OP_READ_FIXED ||
2183 opcode == IORING_OP_WRITE_FIXED);
2184}
2185
Jens Axboe2b188cc2019-01-07 10:46:33 -07002186static void io_sq_wq_submit_work(struct work_struct *work)
2187{
2188 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002189 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07002190 struct mm_struct *cur_mm = NULL;
2191 struct async_list *async_list;
2192 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07002193 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002194 int ret;
2195
Jens Axboe31b51512019-01-18 22:56:34 -07002196 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
2197restart:
2198 do {
2199 struct sqe_submit *s = &req->submit;
2200 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08002201 unsigned int flags = req->flags;
Jens Axboeba816ad2019-09-28 11:36:45 -06002202 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002203
Stefan Bühler8449eed2019-04-27 20:34:19 +02002204 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07002205 req->rw.ki_flags &= ~IOCB_NOWAIT;
2206
2207 ret = 0;
2208 if (io_sqe_needs_user(sqe) && !cur_mm) {
2209 if (!mmget_not_zero(ctx->sqo_mm)) {
2210 ret = -EFAULT;
2211 } else {
2212 cur_mm = ctx->sqo_mm;
2213 use_mm(cur_mm);
2214 old_fs = get_fs();
2215 set_fs(USER_DS);
2216 }
2217 }
2218
2219 if (!ret) {
2220 s->has_user = cur_mm != NULL;
Jackie Liuba5290c2019-10-09 09:19:59 +08002221 s->in_async = true;
Jens Axboe31b51512019-01-18 22:56:34 -07002222 do {
Jens Axboeba816ad2019-09-28 11:36:45 -06002223 ret = __io_submit_sqe(ctx, req, s, &nxt, false);
Jens Axboe31b51512019-01-18 22:56:34 -07002224 /*
2225 * We can get EAGAIN for polled IO even though
2226 * we're forcing a sync submission from here,
2227 * since we can't wait for request slots on the
2228 * block side.
2229 */
2230 if (ret != -EAGAIN)
2231 break;
2232 cond_resched();
2233 } while (1);
2234 }
Jens Axboe817869d2019-04-30 14:44:05 -06002235
2236 /* drop submission reference */
Jens Axboeba816ad2019-09-28 11:36:45 -06002237 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002238
Jens Axboe31b51512019-01-18 22:56:34 -07002239 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06002240 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002241 io_put_req(req, NULL);
Jens Axboe31b51512019-01-18 22:56:34 -07002242 }
2243
2244 /* async context always use a copy of the sqe */
2245 kfree(sqe);
2246
Jens Axboeba816ad2019-09-28 11:36:45 -06002247 /* if a dependent link is ready, do that as the next one */
2248 if (!ret && nxt) {
2249 req = nxt;
2250 continue;
2251 }
2252
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002253 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08002254 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002255 goto out;
2256
Jens Axboe31b51512019-01-18 22:56:34 -07002257 if (!async_list)
2258 break;
2259 if (!list_empty(&req_list)) {
2260 req = list_first_entry(&req_list, struct io_kiocb,
2261 list);
2262 list_del(&req->list);
2263 continue;
2264 }
2265 if (list_empty(&async_list->list))
2266 break;
2267
2268 req = NULL;
2269 spin_lock(&async_list->lock);
2270 if (list_empty(&async_list->list)) {
2271 spin_unlock(&async_list->lock);
2272 break;
2273 }
2274 list_splice_init(&async_list->list, &req_list);
2275 spin_unlock(&async_list->lock);
2276
2277 req = list_first_entry(&req_list, struct io_kiocb, list);
2278 list_del(&req->list);
2279 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002280
2281 /*
Jens Axboe31b51512019-01-18 22:56:34 -07002282 * Rare case of racing with a submitter. If we find the count has
2283 * dropped to zero AND we have pending work items, then restart
2284 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07002285 */
Jens Axboe31b51512019-01-18 22:56:34 -07002286 if (async_list) {
2287 ret = atomic_dec_return(&async_list->cnt);
2288 while (!ret && !list_empty(&async_list->list)) {
2289 spin_lock(&async_list->lock);
2290 atomic_inc(&async_list->cnt);
2291 list_splice_init(&async_list->list, &req_list);
2292 spin_unlock(&async_list->lock);
2293
2294 if (!list_empty(&req_list)) {
2295 req = list_first_entry(&req_list,
2296 struct io_kiocb, list);
2297 list_del(&req->list);
2298 goto restart;
2299 }
2300 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07002301 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002302 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002304out:
Jens Axboe31b51512019-01-18 22:56:34 -07002305 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002306 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07002307 unuse_mm(cur_mm);
2308 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07002309 }
Jens Axboe31b51512019-01-18 22:56:34 -07002310}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002311
Jens Axboe31b51512019-01-18 22:56:34 -07002312/*
2313 * See if we can piggy back onto previously submitted work, that is still
2314 * running. We currently only allow this if the new request is sequential
2315 * to the previous one we punted.
2316 */
2317static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2318{
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06002319 bool ret;
Jens Axboe31b51512019-01-18 22:56:34 -07002320
2321 if (!list)
2322 return false;
2323 if (!(req->flags & REQ_F_SEQ_PREV))
2324 return false;
2325 if (!atomic_read(&list->cnt))
2326 return false;
2327
2328 ret = true;
2329 spin_lock(&list->lock);
2330 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08002331 /*
2332 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
2333 */
2334 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002335 if (!atomic_read(&list->cnt)) {
2336 list_del_init(&req->list);
2337 ret = false;
2338 }
2339 spin_unlock(&list->lock);
2340 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002341}
2342
Jens Axboe09bb8392019-03-13 12:39:28 -06002343static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2344{
2345 int op = READ_ONCE(sqe->opcode);
2346
2347 switch (op) {
2348 case IORING_OP_NOP:
2349 case IORING_OP_POLL_REMOVE:
2350 return false;
2351 default:
2352 return true;
2353 }
2354}
2355
2356static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2357 struct io_submit_state *state, struct io_kiocb *req)
2358{
2359 unsigned flags;
2360 int fd;
2361
2362 flags = READ_ONCE(s->sqe->flags);
2363 fd = READ_ONCE(s->sqe->fd);
2364
Jackie Liu4fe2c962019-09-09 20:50:40 +08002365 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002366 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002367 /*
2368 * All io need record the previous position, if LINK vs DARIN,
2369 * it can be used to mark the position of the first IO in the
2370 * link list.
2371 */
2372 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002373
Jens Axboe60c112b2019-06-21 10:20:18 -06002374 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002375 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002376
2377 if (flags & IOSQE_FIXED_FILE) {
2378 if (unlikely(!ctx->user_files ||
2379 (unsigned) fd >= ctx->nr_user_files))
2380 return -EBADF;
Jens Axboe08a45172019-10-03 08:11:03 -06002381 if (!ctx->user_files[fd])
2382 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002383 req->file = ctx->user_files[fd];
2384 req->flags |= REQ_F_FIXED_FILE;
2385 } else {
2386 if (s->needs_fixed_file)
2387 return -EBADF;
2388 req->file = io_file_get(state, fd);
2389 if (unlikely(!req->file))
2390 return -EBADF;
2391 }
2392
2393 return 0;
2394}
2395
Jackie Liu4fe2c962019-09-09 20:50:40 +08002396static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002397 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002398{
Jens Axboee0c5c572019-03-12 10:18:47 -06002399 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400
Jens Axboeba816ad2019-09-28 11:36:45 -06002401 ret = __io_submit_sqe(ctx, req, s, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002402
2403 /*
2404 * We async punt it if the file wasn't marked NOWAIT, or if the file
2405 * doesn't support non-blocking read/write attempts
2406 */
2407 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2408 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002409 struct io_uring_sqe *sqe_copy;
2410
Jackie Liu954dab12019-09-18 10:37:52 +08002411 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002412 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002413 struct async_list *list;
2414
Jens Axboe2b188cc2019-01-07 10:46:33 -07002415 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002416 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002417 list = io_async_list_from_sqe(ctx, s->sqe);
2418 if (!io_add_to_prev_work(list, req)) {
2419 if (list)
2420 atomic_inc(&list->cnt);
2421 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002422 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002423 }
Jens Axboee65ef562019-03-12 10:16:44 -06002424
2425 /*
2426 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002427 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002428 */
2429 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002430 }
2431 }
Jens Axboee65ef562019-03-12 10:16:44 -06002432
2433 /* drop submission reference */
Jens Axboeba816ad2019-09-28 11:36:45 -06002434 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002435
2436 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002437 if (ret) {
2438 io_cqring_add_event(ctx, req->user_data, ret);
2439 if (req->flags & REQ_F_LINK)
2440 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002441 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002442 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002443
2444 return ret;
2445}
2446
Jackie Liu4fe2c962019-09-09 20:50:40 +08002447static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002448 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002449{
2450 int ret;
2451
2452 ret = io_req_defer(ctx, req, s->sqe);
2453 if (ret) {
2454 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002455 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002456 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2457 }
2458 return 0;
2459 }
2460
Jens Axboebc808bc2019-10-22 13:14:37 -06002461 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002462}
2463
2464static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002465 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002466{
2467 int ret;
2468 int need_submit = false;
2469
2470 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002471 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002472
2473 /*
2474 * Mark the first IO in link list as DRAIN, let all the following
2475 * IOs enter the defer list. all IO needs to be completed before link
2476 * list.
2477 */
2478 req->flags |= REQ_F_IO_DRAIN;
2479 ret = io_req_defer(ctx, req, s->sqe);
2480 if (ret) {
2481 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002482 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002483 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002484 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2485 return 0;
2486 }
2487 } else {
2488 /*
2489 * If ret == 0 means that all IOs in front of link io are
2490 * running done. let's queue link head.
2491 */
2492 need_submit = true;
2493 }
2494
2495 /* Insert shadow req to defer_list, blocking next IOs */
2496 spin_lock_irq(&ctx->completion_lock);
2497 list_add_tail(&shadow->list, &ctx->defer_list);
2498 spin_unlock_irq(&ctx->completion_lock);
2499
2500 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002501 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002502
2503 return 0;
2504}
2505
Jens Axboe9e645e112019-05-10 16:07:28 -06002506#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2507
2508static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002509 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002510{
2511 struct io_uring_sqe *sqe_copy;
2512 struct io_kiocb *req;
2513 int ret;
2514
2515 /* enforce forwards compatibility on users */
2516 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2517 ret = -EINVAL;
2518 goto err;
2519 }
2520
2521 req = io_get_req(ctx, state);
2522 if (unlikely(!req)) {
2523 ret = -EAGAIN;
2524 goto err;
2525 }
2526
2527 ret = io_req_set_file(ctx, s, state, req);
2528 if (unlikely(ret)) {
2529err_req:
Jens Axboeba816ad2019-09-28 11:36:45 -06002530 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002531err:
2532 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2533 return;
2534 }
2535
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002536 req->user_data = s->sqe->user_data;
2537
Jens Axboe9e645e112019-05-10 16:07:28 -06002538 /*
2539 * If we already have a head request, queue this one for async
2540 * submittal once the head completes. If we don't have a head but
2541 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2542 * submitted sync once the chain is complete. If none of those
2543 * conditions are true (normal request), then just queue it.
2544 */
2545 if (*link) {
2546 struct io_kiocb *prev = *link;
2547
2548 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2549 if (!sqe_copy) {
2550 ret = -EAGAIN;
2551 goto err_req;
2552 }
2553
2554 s->sqe = sqe_copy;
2555 memcpy(&req->submit, s, sizeof(*s));
2556 list_add_tail(&req->list, &prev->link_list);
2557 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2558 req->flags |= REQ_F_LINK;
2559
2560 memcpy(&req->submit, s, sizeof(*s));
2561 INIT_LIST_HEAD(&req->link_list);
2562 *link = req;
2563 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002564 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002565 }
2566}
2567
Jens Axboe9a56a232019-01-09 09:06:50 -07002568/*
2569 * Batched submission is done, ensure local IO is flushed out.
2570 */
2571static void io_submit_state_end(struct io_submit_state *state)
2572{
2573 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002574 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002575 if (state->free_reqs)
2576 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2577 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002578}
2579
2580/*
2581 * Start submission side cache.
2582 */
2583static void io_submit_state_start(struct io_submit_state *state,
2584 struct io_ring_ctx *ctx, unsigned max_ios)
2585{
2586 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002587 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002588 state->file = NULL;
2589 state->ios_left = max_ios;
2590}
2591
Jens Axboe2b188cc2019-01-07 10:46:33 -07002592static void io_commit_sqring(struct io_ring_ctx *ctx)
2593{
Hristo Venev75b28af2019-08-26 17:23:46 +00002594 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002595
Hristo Venev75b28af2019-08-26 17:23:46 +00002596 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002597 /*
2598 * Ensure any loads from the SQEs are done at this point,
2599 * since once we write the new head, the application could
2600 * write new data to them.
2601 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002602 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002603 }
2604}
2605
2606/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002607 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2608 * that is mapped by userspace. This means that care needs to be taken to
2609 * ensure that reads are stable, as we cannot rely on userspace always
2610 * being a good citizen. If members of the sqe are validated and then later
2611 * used, it's important that those reads are done through READ_ONCE() to
2612 * prevent a re-load down the line.
2613 */
2614static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2615{
Hristo Venev75b28af2019-08-26 17:23:46 +00002616 struct io_rings *rings = ctx->rings;
2617 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002618 unsigned head;
2619
2620 /*
2621 * The cached sq head (or cq tail) serves two purposes:
2622 *
2623 * 1) allows us to batch the cost of updating the user visible
2624 * head updates.
2625 * 2) allows the kernel side to track the head on its own, even
2626 * though the application is the one updating it.
2627 */
2628 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002629 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002630 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631 return false;
2632
Hristo Venev75b28af2019-08-26 17:23:46 +00002633 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634 if (head < ctx->sq_entries) {
2635 s->index = head;
2636 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002637 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002638 ctx->cached_sq_head++;
2639 return true;
2640 }
2641
2642 /* drop invalid entries */
2643 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002644 ctx->cached_sq_dropped++;
2645 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646 return false;
2647}
2648
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002649static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
2650 bool has_user, bool mm_fault)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002651{
2652 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002653 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002654 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002655 bool prev_was_link = false;
2656 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002657
2658 if (nr > IO_PLUG_THRESHOLD) {
2659 io_submit_state_start(&state, ctx, nr);
2660 statep = &state;
2661 }
2662
2663 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002664 struct sqe_submit s;
2665
2666 if (!io_get_sqring(ctx, &s))
2667 break;
2668
Jens Axboe9e645e112019-05-10 16:07:28 -06002669 /*
2670 * If previous wasn't linked and we have a linked command,
2671 * that's the end of the chain. Submit the previous link.
2672 */
2673 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002674 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002675 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002676 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002677 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002678 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002679
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002680 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002681 if (!shadow_req) {
2682 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002683 if (unlikely(!shadow_req))
2684 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002685 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2686 refcount_dec(&shadow_req->refs);
2687 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002688 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002689 }
2690
Jackie Liua1041c22019-09-18 17:25:52 +08002691out:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002692 if (unlikely(mm_fault)) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002693 io_cqring_add_event(ctx, s.sqe->user_data,
Jens Axboe9e645e112019-05-10 16:07:28 -06002694 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002695 } else {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002696 s.has_user = has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +08002697 s.in_async = true;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002698 s.needs_fixed_file = true;
2699 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002700 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002701 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002702 }
2703
Jens Axboe9e645e112019-05-10 16:07:28 -06002704 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002705 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002706 if (statep)
2707 io_submit_state_end(&state);
2708
2709 return submitted;
2710}
2711
2712static int io_sq_thread(void *data)
2713{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002714 struct io_ring_ctx *ctx = data;
2715 struct mm_struct *cur_mm = NULL;
2716 mm_segment_t old_fs;
2717 DEFINE_WAIT(wait);
2718 unsigned inflight;
2719 unsigned long timeout;
2720
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002721 complete(&ctx->sqo_thread_started);
2722
Jens Axboe6c271ce2019-01-10 11:22:30 -07002723 old_fs = get_fs();
2724 set_fs(USER_DS);
2725
2726 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002727 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002728 bool mm_fault = false;
2729 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002730
2731 if (inflight) {
2732 unsigned nr_events = 0;
2733
2734 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002735 /*
2736 * inflight is the count of the maximum possible
2737 * entries we submitted, but it can be smaller
2738 * if we dropped some of them. If we don't have
2739 * poll entries available, then we know that we
2740 * have nothing left to poll for. Reset the
2741 * inflight count to zero in that case.
2742 */
2743 mutex_lock(&ctx->uring_lock);
2744 if (!list_empty(&ctx->poll_list))
2745 __io_iopoll_check(ctx, &nr_events, 0);
2746 else
2747 inflight = 0;
2748 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002749 } else {
2750 /*
2751 * Normal IO, just pretend everything completed.
2752 * We don't have to poll completions for that.
2753 */
2754 nr_events = inflight;
2755 }
2756
2757 inflight -= nr_events;
2758 if (!inflight)
2759 timeout = jiffies + ctx->sq_thread_idle;
2760 }
2761
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002762 to_submit = io_sqring_entries(ctx);
2763 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002764 /*
2765 * We're polling. If we're within the defined idle
2766 * period, then let us spin without work before going
2767 * to sleep.
2768 */
2769 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002770 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002771 continue;
2772 }
2773
2774 /*
2775 * Drop cur_mm before scheduling, we can't hold it for
2776 * long periods (or over schedule()). Do this before
2777 * adding ourselves to the waitqueue, as the unuse/drop
2778 * may sleep.
2779 */
2780 if (cur_mm) {
2781 unuse_mm(cur_mm);
2782 mmput(cur_mm);
2783 cur_mm = NULL;
2784 }
2785
2786 prepare_to_wait(&ctx->sqo_wait, &wait,
2787 TASK_INTERRUPTIBLE);
2788
2789 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002790 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002791 /* make sure to read SQ tail after writing flags */
2792 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002793
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002794 to_submit = io_sqring_entries(ctx);
2795 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002796 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002797 finish_wait(&ctx->sqo_wait, &wait);
2798 break;
2799 }
2800 if (signal_pending(current))
2801 flush_signals(current);
2802 schedule();
2803 finish_wait(&ctx->sqo_wait, &wait);
2804
Hristo Venev75b28af2019-08-26 17:23:46 +00002805 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002806 continue;
2807 }
2808 finish_wait(&ctx->sqo_wait, &wait);
2809
Hristo Venev75b28af2019-08-26 17:23:46 +00002810 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002811 }
2812
Jens Axboe6c271ce2019-01-10 11:22:30 -07002813 /* Unless all new commands are FIXED regions, grab mm */
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002814 if (!cur_mm) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002815 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2816 if (!mm_fault) {
2817 use_mm(ctx->sqo_mm);
2818 cur_mm = ctx->sqo_mm;
2819 }
2820 }
2821
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002822 to_submit = min(to_submit, ctx->sq_entries);
2823 inflight += io_submit_sqes(ctx, to_submit, cur_mm != NULL,
2824 mm_fault);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002825
2826 /* Commit SQ ring head once we've consumed all SQEs */
2827 io_commit_sqring(ctx);
2828 }
2829
2830 set_fs(old_fs);
2831 if (cur_mm) {
2832 unuse_mm(cur_mm);
2833 mmput(cur_mm);
2834 }
Jens Axboe06058632019-04-13 09:26:03 -06002835
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002836 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002837
Jens Axboe6c271ce2019-01-10 11:22:30 -07002838 return 0;
2839}
2840
Jens Axboebc808bc2019-10-22 13:14:37 -06002841static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002842{
Jens Axboe9a56a232019-01-09 09:06:50 -07002843 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002844 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002845 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002846 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002847 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848
Jens Axboe9a56a232019-01-09 09:06:50 -07002849 if (to_submit > IO_PLUG_THRESHOLD) {
2850 io_submit_state_start(&state, ctx, to_submit);
2851 statep = &state;
2852 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002853
2854 for (i = 0; i < to_submit; i++) {
2855 struct sqe_submit s;
2856
2857 if (!io_get_sqring(ctx, &s))
2858 break;
2859
Jens Axboe9e645e112019-05-10 16:07:28 -06002860 /*
2861 * If previous wasn't linked and we have a linked command,
2862 * that's the end of the chain. Submit the previous link.
2863 */
2864 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002865 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002866 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002867 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002868 }
2869 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2870
Jackie Liu4fe2c962019-09-09 20:50:40 +08002871 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2872 if (!shadow_req) {
2873 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002874 if (unlikely(!shadow_req))
2875 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002876 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2877 refcount_dec(&shadow_req->refs);
2878 }
2879 shadow_req->sequence = s.sequence;
2880 }
2881
Jackie Liua1041c22019-09-18 17:25:52 +08002882out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002883 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002884 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002885 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002886 submit++;
Jens Axboebc808bc2019-10-22 13:14:37 -06002887 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002888 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002889
Jens Axboe9e645e112019-05-10 16:07:28 -06002890 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002891 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002892 if (statep)
2893 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002894
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002895 io_commit_sqring(ctx);
2896
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002897 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002898}
2899
Jens Axboebda52162019-09-24 13:47:15 -06002900struct io_wait_queue {
2901 struct wait_queue_entry wq;
2902 struct io_ring_ctx *ctx;
2903 unsigned to_wait;
2904 unsigned nr_timeouts;
2905};
2906
2907static inline bool io_should_wake(struct io_wait_queue *iowq)
2908{
2909 struct io_ring_ctx *ctx = iowq->ctx;
2910
2911 /*
2912 * Wake up if we have enough events, or if a timeout occured since we
2913 * started waiting. For timeouts, we always want to return to userspace,
2914 * regardless of event count.
2915 */
2916 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2917 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2918}
2919
2920static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2921 int wake_flags, void *key)
2922{
2923 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2924 wq);
2925
2926 if (!io_should_wake(iowq))
2927 return -1;
2928
2929 return autoremove_wake_function(curr, mode, wake_flags, key);
2930}
2931
Jens Axboe2b188cc2019-01-07 10:46:33 -07002932/*
2933 * Wait until events become available, if we don't already have some. The
2934 * application must reap them itself, as they reside on the shared cq ring.
2935 */
2936static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2937 const sigset_t __user *sig, size_t sigsz)
2938{
Jens Axboebda52162019-09-24 13:47:15 -06002939 struct io_wait_queue iowq = {
2940 .wq = {
2941 .private = current,
2942 .func = io_wake_function,
2943 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2944 },
2945 .ctx = ctx,
2946 .to_wait = min_events,
2947 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002948 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002949 int ret;
2950
Hristo Venev75b28af2019-08-26 17:23:46 +00002951 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952 return 0;
2953
2954 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002955#ifdef CONFIG_COMPAT
2956 if (in_compat_syscall())
2957 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002958 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002959 else
2960#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002961 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002962
Jens Axboe2b188cc2019-01-07 10:46:33 -07002963 if (ret)
2964 return ret;
2965 }
2966
Jens Axboebda52162019-09-24 13:47:15 -06002967 ret = 0;
2968 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
2969 do {
2970 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2971 TASK_INTERRUPTIBLE);
2972 if (io_should_wake(&iowq))
2973 break;
2974 schedule();
2975 if (signal_pending(current)) {
2976 ret = -ERESTARTSYS;
2977 break;
2978 }
2979 } while (1);
2980 finish_wait(&ctx->wait, &iowq.wq);
2981
Oleg Nesterovb7724342019-07-16 16:29:53 -07002982 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002983 if (ret == -ERESTARTSYS)
2984 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002985
Hristo Venev75b28af2019-08-26 17:23:46 +00002986 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002987}
2988
Jens Axboe6b063142019-01-10 22:13:58 -07002989static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2990{
2991#if defined(CONFIG_UNIX)
2992 if (ctx->ring_sock) {
2993 struct sock *sock = ctx->ring_sock->sk;
2994 struct sk_buff *skb;
2995
2996 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2997 kfree_skb(skb);
2998 }
2999#else
3000 int i;
3001
3002 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003003 if (ctx->user_files[i])
3004 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003005#endif
3006}
3007
3008static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3009{
3010 if (!ctx->user_files)
3011 return -ENXIO;
3012
3013 __io_sqe_files_unregister(ctx);
3014 kfree(ctx->user_files);
3015 ctx->user_files = NULL;
3016 ctx->nr_user_files = 0;
3017 return 0;
3018}
3019
Jens Axboe6c271ce2019-01-10 11:22:30 -07003020static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3021{
3022 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003023 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003024 /*
3025 * The park is a bit of a work-around, without it we get
3026 * warning spews on shutdown with SQPOLL set and affinity
3027 * set to a single CPU.
3028 */
Jens Axboe06058632019-04-13 09:26:03 -06003029 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003030 kthread_stop(ctx->sqo_thread);
3031 ctx->sqo_thread = NULL;
3032 }
3033}
3034
Jens Axboe6b063142019-01-10 22:13:58 -07003035static void io_finish_async(struct io_ring_ctx *ctx)
3036{
Jens Axboe54a91f32019-09-10 09:15:04 -06003037 int i;
3038
Jens Axboe6c271ce2019-01-10 11:22:30 -07003039 io_sq_thread_stop(ctx);
3040
Jens Axboe54a91f32019-09-10 09:15:04 -06003041 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
3042 if (ctx->sqo_wq[i]) {
3043 destroy_workqueue(ctx->sqo_wq[i]);
3044 ctx->sqo_wq[i] = NULL;
3045 }
Jens Axboe6b063142019-01-10 22:13:58 -07003046 }
3047}
3048
3049#if defined(CONFIG_UNIX)
3050static void io_destruct_skb(struct sk_buff *skb)
3051{
3052 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
Jens Axboe8a997342019-10-09 14:40:13 -06003053 int i;
Jens Axboe6b063142019-01-10 22:13:58 -07003054
Jens Axboe8a997342019-10-09 14:40:13 -06003055 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++)
3056 if (ctx->sqo_wq[i])
3057 flush_workqueue(ctx->sqo_wq[i]);
3058
Jens Axboe6b063142019-01-10 22:13:58 -07003059 unix_destruct_scm(skb);
3060}
3061
3062/*
3063 * Ensure the UNIX gc is aware of our file set, so we are certain that
3064 * the io_uring can be safely unregistered on process exit, even if we have
3065 * loops in the file referencing.
3066 */
3067static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3068{
3069 struct sock *sk = ctx->ring_sock->sk;
3070 struct scm_fp_list *fpl;
3071 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003072 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003073
3074 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3075 unsigned long inflight = ctx->user->unix_inflight + nr;
3076
3077 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3078 return -EMFILE;
3079 }
3080
3081 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3082 if (!fpl)
3083 return -ENOMEM;
3084
3085 skb = alloc_skb(0, GFP_KERNEL);
3086 if (!skb) {
3087 kfree(fpl);
3088 return -ENOMEM;
3089 }
3090
3091 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003092
Jens Axboe08a45172019-10-03 08:11:03 -06003093 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003094 fpl->user = get_uid(ctx->user);
3095 for (i = 0; i < nr; i++) {
Jens Axboe08a45172019-10-03 08:11:03 -06003096 if (!ctx->user_files[i + offset])
3097 continue;
3098 fpl->fp[nr_files] = get_file(ctx->user_files[i + offset]);
3099 unix_inflight(fpl->user, fpl->fp[nr_files]);
3100 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003101 }
3102
Jens Axboe08a45172019-10-03 08:11:03 -06003103 if (nr_files) {
3104 fpl->max = SCM_MAX_FD;
3105 fpl->count = nr_files;
3106 UNIXCB(skb).fp = fpl;
3107 skb->destructor = io_destruct_skb;
3108 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3109 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003110
Jens Axboe08a45172019-10-03 08:11:03 -06003111 for (i = 0; i < nr_files; i++)
3112 fput(fpl->fp[i]);
3113 } else {
3114 kfree_skb(skb);
3115 kfree(fpl);
3116 }
Jens Axboe6b063142019-01-10 22:13:58 -07003117
3118 return 0;
3119}
3120
3121/*
3122 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3123 * causes regular reference counting to break down. We rely on the UNIX
3124 * garbage collection to take care of this problem for us.
3125 */
3126static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3127{
3128 unsigned left, total;
3129 int ret = 0;
3130
3131 total = 0;
3132 left = ctx->nr_user_files;
3133 while (left) {
3134 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003135
3136 ret = __io_sqe_files_scm(ctx, this_files, total);
3137 if (ret)
3138 break;
3139 left -= this_files;
3140 total += this_files;
3141 }
3142
3143 if (!ret)
3144 return 0;
3145
3146 while (total < ctx->nr_user_files) {
Jens Axboe08a45172019-10-03 08:11:03 -06003147 if (ctx->user_files[total])
3148 fput(ctx->user_files[total]);
Jens Axboe6b063142019-01-10 22:13:58 -07003149 total++;
3150 }
3151
3152 return ret;
3153}
3154#else
3155static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3156{
3157 return 0;
3158}
3159#endif
3160
3161static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3162 unsigned nr_args)
3163{
3164 __s32 __user *fds = (__s32 __user *) arg;
3165 int fd, ret = 0;
3166 unsigned i;
3167
3168 if (ctx->user_files)
3169 return -EBUSY;
3170 if (!nr_args)
3171 return -EINVAL;
3172 if (nr_args > IORING_MAX_FIXED_FILES)
3173 return -EMFILE;
3174
3175 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3176 if (!ctx->user_files)
3177 return -ENOMEM;
3178
Jens Axboe08a45172019-10-03 08:11:03 -06003179 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe6b063142019-01-10 22:13:58 -07003180 ret = -EFAULT;
3181 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3182 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003183 /* allow sparse sets */
3184 if (fd == -1) {
3185 ret = 0;
3186 continue;
3187 }
Jens Axboe6b063142019-01-10 22:13:58 -07003188
3189 ctx->user_files[i] = fget(fd);
3190
3191 ret = -EBADF;
3192 if (!ctx->user_files[i])
3193 break;
3194 /*
3195 * Don't allow io_uring instances to be registered. If UNIX
3196 * isn't enabled, then this causes a reference cycle and this
3197 * instance can never get freed. If UNIX is enabled we'll
3198 * handle it just fine, but there's still no point in allowing
3199 * a ring fd as it doesn't support regular read/write anyway.
3200 */
3201 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3202 fput(ctx->user_files[i]);
3203 break;
3204 }
Jens Axboe6b063142019-01-10 22:13:58 -07003205 ret = 0;
3206 }
3207
3208 if (ret) {
3209 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003210 if (ctx->user_files[i])
3211 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003212
3213 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003214 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003215 ctx->nr_user_files = 0;
3216 return ret;
3217 }
3218
3219 ret = io_sqe_files_scm(ctx);
3220 if (ret)
3221 io_sqe_files_unregister(ctx);
3222
3223 return ret;
3224}
3225
Jens Axboec3a31e62019-10-03 13:59:56 -06003226static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3227{
3228#if defined(CONFIG_UNIX)
3229 struct file *file = ctx->user_files[index];
3230 struct sock *sock = ctx->ring_sock->sk;
3231 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3232 struct sk_buff *skb;
3233 int i;
3234
3235 __skb_queue_head_init(&list);
3236
3237 /*
3238 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3239 * remove this entry and rearrange the file array.
3240 */
3241 skb = skb_dequeue(head);
3242 while (skb) {
3243 struct scm_fp_list *fp;
3244
3245 fp = UNIXCB(skb).fp;
3246 for (i = 0; i < fp->count; i++) {
3247 int left;
3248
3249 if (fp->fp[i] != file)
3250 continue;
3251
3252 unix_notinflight(fp->user, fp->fp[i]);
3253 left = fp->count - 1 - i;
3254 if (left) {
3255 memmove(&fp->fp[i], &fp->fp[i + 1],
3256 left * sizeof(struct file *));
3257 }
3258 fp->count--;
3259 if (!fp->count) {
3260 kfree_skb(skb);
3261 skb = NULL;
3262 } else {
3263 __skb_queue_tail(&list, skb);
3264 }
3265 fput(file);
3266 file = NULL;
3267 break;
3268 }
3269
3270 if (!file)
3271 break;
3272
3273 __skb_queue_tail(&list, skb);
3274
3275 skb = skb_dequeue(head);
3276 }
3277
3278 if (skb_peek(&list)) {
3279 spin_lock_irq(&head->lock);
3280 while ((skb = __skb_dequeue(&list)) != NULL)
3281 __skb_queue_tail(head, skb);
3282 spin_unlock_irq(&head->lock);
3283 }
3284#else
3285 fput(ctx->user_files[index]);
3286#endif
3287}
3288
3289static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3290 int index)
3291{
3292#if defined(CONFIG_UNIX)
3293 struct sock *sock = ctx->ring_sock->sk;
3294 struct sk_buff_head *head = &sock->sk_receive_queue;
3295 struct sk_buff *skb;
3296
3297 /*
3298 * See if we can merge this file into an existing skb SCM_RIGHTS
3299 * file set. If there's no room, fall back to allocating a new skb
3300 * and filling it in.
3301 */
3302 spin_lock_irq(&head->lock);
3303 skb = skb_peek(head);
3304 if (skb) {
3305 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3306
3307 if (fpl->count < SCM_MAX_FD) {
3308 __skb_unlink(skb, head);
3309 spin_unlock_irq(&head->lock);
3310 fpl->fp[fpl->count] = get_file(file);
3311 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3312 fpl->count++;
3313 spin_lock_irq(&head->lock);
3314 __skb_queue_head(head, skb);
3315 } else {
3316 skb = NULL;
3317 }
3318 }
3319 spin_unlock_irq(&head->lock);
3320
3321 if (skb) {
3322 fput(file);
3323 return 0;
3324 }
3325
3326 return __io_sqe_files_scm(ctx, 1, index);
3327#else
3328 return 0;
3329#endif
3330}
3331
3332static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3333 unsigned nr_args)
3334{
3335 struct io_uring_files_update up;
3336 __s32 __user *fds;
3337 int fd, i, err;
3338 __u32 done;
3339
3340 if (!ctx->user_files)
3341 return -ENXIO;
3342 if (!nr_args)
3343 return -EINVAL;
3344 if (copy_from_user(&up, arg, sizeof(up)))
3345 return -EFAULT;
3346 if (check_add_overflow(up.offset, nr_args, &done))
3347 return -EOVERFLOW;
3348 if (done > ctx->nr_user_files)
3349 return -EINVAL;
3350
3351 done = 0;
3352 fds = (__s32 __user *) up.fds;
3353 while (nr_args) {
3354 err = 0;
3355 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3356 err = -EFAULT;
3357 break;
3358 }
3359 i = array_index_nospec(up.offset, ctx->nr_user_files);
3360 if (ctx->user_files[i]) {
3361 io_sqe_file_unregister(ctx, i);
3362 ctx->user_files[i] = NULL;
3363 }
3364 if (fd != -1) {
3365 struct file *file;
3366
3367 file = fget(fd);
3368 if (!file) {
3369 err = -EBADF;
3370 break;
3371 }
3372 /*
3373 * Don't allow io_uring instances to be registered. If
3374 * UNIX isn't enabled, then this causes a reference
3375 * cycle and this instance can never get freed. If UNIX
3376 * is enabled we'll handle it just fine, but there's
3377 * still no point in allowing a ring fd as it doesn't
3378 * support regular read/write anyway.
3379 */
3380 if (file->f_op == &io_uring_fops) {
3381 fput(file);
3382 err = -EBADF;
3383 break;
3384 }
3385 ctx->user_files[i] = file;
3386 err = io_sqe_file_register(ctx, file, i);
3387 if (err)
3388 break;
3389 }
3390 nr_args--;
3391 done++;
3392 up.offset++;
3393 }
3394
3395 return done ? done : err;
3396}
3397
Jens Axboe6c271ce2019-01-10 11:22:30 -07003398static int io_sq_offload_start(struct io_ring_ctx *ctx,
3399 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003400{
3401 int ret;
3402
Jens Axboe6c271ce2019-01-10 11:22:30 -07003403 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003404 mmgrab(current->mm);
3405 ctx->sqo_mm = current->mm;
3406
Jens Axboe6c271ce2019-01-10 11:22:30 -07003407 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003408 ret = -EPERM;
3409 if (!capable(CAP_SYS_ADMIN))
3410 goto err;
3411
Jens Axboe917257d2019-04-13 09:28:55 -06003412 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3413 if (!ctx->sq_thread_idle)
3414 ctx->sq_thread_idle = HZ;
3415
Jens Axboe6c271ce2019-01-10 11:22:30 -07003416 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003417 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003418
Jens Axboe917257d2019-04-13 09:28:55 -06003419 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003420 if (cpu >= nr_cpu_ids)
3421 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003422 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003423 goto err;
3424
Jens Axboe6c271ce2019-01-10 11:22:30 -07003425 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3426 ctx, cpu,
3427 "io_uring-sq");
3428 } else {
3429 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3430 "io_uring-sq");
3431 }
3432 if (IS_ERR(ctx->sqo_thread)) {
3433 ret = PTR_ERR(ctx->sqo_thread);
3434 ctx->sqo_thread = NULL;
3435 goto err;
3436 }
3437 wake_up_process(ctx->sqo_thread);
3438 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3439 /* Can't have SQ_AFF without SQPOLL */
3440 ret = -EINVAL;
3441 goto err;
3442 }
3443
Jens Axboe2b188cc2019-01-07 10:46:33 -07003444 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06003445 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3446 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003447 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06003448 if (!ctx->sqo_wq[0]) {
3449 ret = -ENOMEM;
3450 goto err;
3451 }
3452
3453 /*
3454 * This is for buffered writes, where we want to limit the parallelism
3455 * due to file locking in file systems. As "normal" buffered writes
3456 * should parellelize on writeout quite nicely, limit us to having 2
3457 * pending. This avoids massive contention on the inode when doing
3458 * buffered async writes.
3459 */
3460 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3461 WQ_UNBOUND | WQ_FREEZABLE, 2);
3462 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003463 ret = -ENOMEM;
3464 goto err;
3465 }
3466
3467 return 0;
3468err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003469 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003470 mmdrop(ctx->sqo_mm);
3471 ctx->sqo_mm = NULL;
3472 return ret;
3473}
3474
3475static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3476{
3477 atomic_long_sub(nr_pages, &user->locked_vm);
3478}
3479
3480static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3481{
3482 unsigned long page_limit, cur_pages, new_pages;
3483
3484 /* Don't allow more pages than we can safely lock */
3485 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3486
3487 do {
3488 cur_pages = atomic_long_read(&user->locked_vm);
3489 new_pages = cur_pages + nr_pages;
3490 if (new_pages > page_limit)
3491 return -ENOMEM;
3492 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3493 new_pages) != cur_pages);
3494
3495 return 0;
3496}
3497
3498static void io_mem_free(void *ptr)
3499{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003500 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003501
Mark Rutland52e04ef2019-04-30 17:30:21 +01003502 if (!ptr)
3503 return;
3504
3505 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003506 if (put_page_testzero(page))
3507 free_compound_page(page);
3508}
3509
3510static void *io_mem_alloc(size_t size)
3511{
3512 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3513 __GFP_NORETRY;
3514
3515 return (void *) __get_free_pages(gfp_flags, get_order(size));
3516}
3517
Hristo Venev75b28af2019-08-26 17:23:46 +00003518static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3519 size_t *sq_offset)
3520{
3521 struct io_rings *rings;
3522 size_t off, sq_array_size;
3523
3524 off = struct_size(rings, cqes, cq_entries);
3525 if (off == SIZE_MAX)
3526 return SIZE_MAX;
3527
3528#ifdef CONFIG_SMP
3529 off = ALIGN(off, SMP_CACHE_BYTES);
3530 if (off == 0)
3531 return SIZE_MAX;
3532#endif
3533
3534 sq_array_size = array_size(sizeof(u32), sq_entries);
3535 if (sq_array_size == SIZE_MAX)
3536 return SIZE_MAX;
3537
3538 if (check_add_overflow(off, sq_array_size, &off))
3539 return SIZE_MAX;
3540
3541 if (sq_offset)
3542 *sq_offset = off;
3543
3544 return off;
3545}
3546
Jens Axboe2b188cc2019-01-07 10:46:33 -07003547static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3548{
Hristo Venev75b28af2019-08-26 17:23:46 +00003549 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003550
Hristo Venev75b28af2019-08-26 17:23:46 +00003551 pages = (size_t)1 << get_order(
3552 rings_size(sq_entries, cq_entries, NULL));
3553 pages += (size_t)1 << get_order(
3554 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003555
Hristo Venev75b28af2019-08-26 17:23:46 +00003556 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003557}
3558
Jens Axboeedafcce2019-01-09 09:16:05 -07003559static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3560{
3561 int i, j;
3562
3563 if (!ctx->user_bufs)
3564 return -ENXIO;
3565
3566 for (i = 0; i < ctx->nr_user_bufs; i++) {
3567 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3568
3569 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003570 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003571
3572 if (ctx->account_mem)
3573 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003574 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003575 imu->nr_bvecs = 0;
3576 }
3577
3578 kfree(ctx->user_bufs);
3579 ctx->user_bufs = NULL;
3580 ctx->nr_user_bufs = 0;
3581 return 0;
3582}
3583
3584static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3585 void __user *arg, unsigned index)
3586{
3587 struct iovec __user *src;
3588
3589#ifdef CONFIG_COMPAT
3590 if (ctx->compat) {
3591 struct compat_iovec __user *ciovs;
3592 struct compat_iovec ciov;
3593
3594 ciovs = (struct compat_iovec __user *) arg;
3595 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3596 return -EFAULT;
3597
3598 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3599 dst->iov_len = ciov.iov_len;
3600 return 0;
3601 }
3602#endif
3603 src = (struct iovec __user *) arg;
3604 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3605 return -EFAULT;
3606 return 0;
3607}
3608
3609static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3610 unsigned nr_args)
3611{
3612 struct vm_area_struct **vmas = NULL;
3613 struct page **pages = NULL;
3614 int i, j, got_pages = 0;
3615 int ret = -EINVAL;
3616
3617 if (ctx->user_bufs)
3618 return -EBUSY;
3619 if (!nr_args || nr_args > UIO_MAXIOV)
3620 return -EINVAL;
3621
3622 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3623 GFP_KERNEL);
3624 if (!ctx->user_bufs)
3625 return -ENOMEM;
3626
3627 for (i = 0; i < nr_args; i++) {
3628 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3629 unsigned long off, start, end, ubuf;
3630 int pret, nr_pages;
3631 struct iovec iov;
3632 size_t size;
3633
3634 ret = io_copy_iov(ctx, &iov, arg, i);
3635 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003636 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003637
3638 /*
3639 * Don't impose further limits on the size and buffer
3640 * constraints here, we'll -EINVAL later when IO is
3641 * submitted if they are wrong.
3642 */
3643 ret = -EFAULT;
3644 if (!iov.iov_base || !iov.iov_len)
3645 goto err;
3646
3647 /* arbitrary limit, but we need something */
3648 if (iov.iov_len > SZ_1G)
3649 goto err;
3650
3651 ubuf = (unsigned long) iov.iov_base;
3652 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3653 start = ubuf >> PAGE_SHIFT;
3654 nr_pages = end - start;
3655
3656 if (ctx->account_mem) {
3657 ret = io_account_mem(ctx->user, nr_pages);
3658 if (ret)
3659 goto err;
3660 }
3661
3662 ret = 0;
3663 if (!pages || nr_pages > got_pages) {
3664 kfree(vmas);
3665 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003666 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003667 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003668 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003669 sizeof(struct vm_area_struct *),
3670 GFP_KERNEL);
3671 if (!pages || !vmas) {
3672 ret = -ENOMEM;
3673 if (ctx->account_mem)
3674 io_unaccount_mem(ctx->user, nr_pages);
3675 goto err;
3676 }
3677 got_pages = nr_pages;
3678 }
3679
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003680 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003681 GFP_KERNEL);
3682 ret = -ENOMEM;
3683 if (!imu->bvec) {
3684 if (ctx->account_mem)
3685 io_unaccount_mem(ctx->user, nr_pages);
3686 goto err;
3687 }
3688
3689 ret = 0;
3690 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003691 pret = get_user_pages(ubuf, nr_pages,
3692 FOLL_WRITE | FOLL_LONGTERM,
3693 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003694 if (pret == nr_pages) {
3695 /* don't support file backed memory */
3696 for (j = 0; j < nr_pages; j++) {
3697 struct vm_area_struct *vma = vmas[j];
3698
3699 if (vma->vm_file &&
3700 !is_file_hugepages(vma->vm_file)) {
3701 ret = -EOPNOTSUPP;
3702 break;
3703 }
3704 }
3705 } else {
3706 ret = pret < 0 ? pret : -EFAULT;
3707 }
3708 up_read(&current->mm->mmap_sem);
3709 if (ret) {
3710 /*
3711 * if we did partial map, or found file backed vmas,
3712 * release any pages we did get
3713 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003714 if (pret > 0)
3715 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003716 if (ctx->account_mem)
3717 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003718 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003719 goto err;
3720 }
3721
3722 off = ubuf & ~PAGE_MASK;
3723 size = iov.iov_len;
3724 for (j = 0; j < nr_pages; j++) {
3725 size_t vec_len;
3726
3727 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3728 imu->bvec[j].bv_page = pages[j];
3729 imu->bvec[j].bv_len = vec_len;
3730 imu->bvec[j].bv_offset = off;
3731 off = 0;
3732 size -= vec_len;
3733 }
3734 /* store original address for later verification */
3735 imu->ubuf = ubuf;
3736 imu->len = iov.iov_len;
3737 imu->nr_bvecs = nr_pages;
3738
3739 ctx->nr_user_bufs++;
3740 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003741 kvfree(pages);
3742 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003743 return 0;
3744err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003745 kvfree(pages);
3746 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003747 io_sqe_buffer_unregister(ctx);
3748 return ret;
3749}
3750
Jens Axboe9b402842019-04-11 11:45:41 -06003751static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3752{
3753 __s32 __user *fds = arg;
3754 int fd;
3755
3756 if (ctx->cq_ev_fd)
3757 return -EBUSY;
3758
3759 if (copy_from_user(&fd, fds, sizeof(*fds)))
3760 return -EFAULT;
3761
3762 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3763 if (IS_ERR(ctx->cq_ev_fd)) {
3764 int ret = PTR_ERR(ctx->cq_ev_fd);
3765 ctx->cq_ev_fd = NULL;
3766 return ret;
3767 }
3768
3769 return 0;
3770}
3771
3772static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3773{
3774 if (ctx->cq_ev_fd) {
3775 eventfd_ctx_put(ctx->cq_ev_fd);
3776 ctx->cq_ev_fd = NULL;
3777 return 0;
3778 }
3779
3780 return -ENXIO;
3781}
3782
Jens Axboe2b188cc2019-01-07 10:46:33 -07003783static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3784{
Jens Axboe6b063142019-01-10 22:13:58 -07003785 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003786 if (ctx->sqo_mm)
3787 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003788
3789 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003790 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003791 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003792 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003793
Jens Axboe2b188cc2019-01-07 10:46:33 -07003794#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003795 if (ctx->ring_sock) {
3796 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003797 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003798 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003799#endif
3800
Hristo Venev75b28af2019-08-26 17:23:46 +00003801 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003802 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003803
3804 percpu_ref_exit(&ctx->refs);
3805 if (ctx->account_mem)
3806 io_unaccount_mem(ctx->user,
3807 ring_pages(ctx->sq_entries, ctx->cq_entries));
3808 free_uid(ctx->user);
3809 kfree(ctx);
3810}
3811
3812static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3813{
3814 struct io_ring_ctx *ctx = file->private_data;
3815 __poll_t mask = 0;
3816
3817 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003818 /*
3819 * synchronizes with barrier from wq_has_sleeper call in
3820 * io_commit_cqring
3821 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003822 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003823 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3824 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003825 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003826 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003827 mask |= EPOLLIN | EPOLLRDNORM;
3828
3829 return mask;
3830}
3831
3832static int io_uring_fasync(int fd, struct file *file, int on)
3833{
3834 struct io_ring_ctx *ctx = file->private_data;
3835
3836 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3837}
3838
3839static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3840{
3841 mutex_lock(&ctx->uring_lock);
3842 percpu_ref_kill(&ctx->refs);
3843 mutex_unlock(&ctx->uring_lock);
3844
Jens Axboe5262f562019-09-17 12:26:57 -06003845 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003846 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003847 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003848 wait_for_completion(&ctx->ctx_done);
3849 io_ring_ctx_free(ctx);
3850}
3851
3852static int io_uring_release(struct inode *inode, struct file *file)
3853{
3854 struct io_ring_ctx *ctx = file->private_data;
3855
3856 file->private_data = NULL;
3857 io_ring_ctx_wait_and_kill(ctx);
3858 return 0;
3859}
3860
3861static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3862{
3863 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3864 unsigned long sz = vma->vm_end - vma->vm_start;
3865 struct io_ring_ctx *ctx = file->private_data;
3866 unsigned long pfn;
3867 struct page *page;
3868 void *ptr;
3869
3870 switch (offset) {
3871 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003872 case IORING_OFF_CQ_RING:
3873 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003874 break;
3875 case IORING_OFF_SQES:
3876 ptr = ctx->sq_sqes;
3877 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003878 default:
3879 return -EINVAL;
3880 }
3881
3882 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003883 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884 return -EINVAL;
3885
3886 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3887 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3888}
3889
3890SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3891 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3892 size_t, sigsz)
3893{
3894 struct io_ring_ctx *ctx;
3895 long ret = -EBADF;
3896 int submitted = 0;
3897 struct fd f;
3898
Jens Axboe6c271ce2019-01-10 11:22:30 -07003899 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003900 return -EINVAL;
3901
3902 f = fdget(fd);
3903 if (!f.file)
3904 return -EBADF;
3905
3906 ret = -EOPNOTSUPP;
3907 if (f.file->f_op != &io_uring_fops)
3908 goto out_fput;
3909
3910 ret = -ENXIO;
3911 ctx = f.file->private_data;
3912 if (!percpu_ref_tryget(&ctx->refs))
3913 goto out_fput;
3914
Jens Axboe6c271ce2019-01-10 11:22:30 -07003915 /*
3916 * For SQ polling, the thread will do all submissions and completions.
3917 * Just return the requested submit count, and wake the thread if
3918 * we were asked to.
3919 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003920 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003921 if (ctx->flags & IORING_SETUP_SQPOLL) {
3922 if (flags & IORING_ENTER_SQ_WAKEUP)
3923 wake_up(&ctx->sqo_wait);
3924 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003925 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003926 to_submit = min(to_submit, ctx->sq_entries);
3927
3928 mutex_lock(&ctx->uring_lock);
Jens Axboebc808bc2019-10-22 13:14:37 -06003929 submitted = io_ring_submit(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003930 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003931 }
3932 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003933 unsigned nr_events = 0;
3934
Jens Axboe2b188cc2019-01-07 10:46:33 -07003935 min_complete = min(min_complete, ctx->cq_entries);
3936
Jens Axboedef596e2019-01-09 08:59:42 -07003937 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003938 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003939 } else {
3940 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3941 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003942 }
3943
Pavel Begunkov6805b322019-10-08 02:18:42 +03003944 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003945out_fput:
3946 fdput(f);
3947 return submitted ? submitted : ret;
3948}
3949
3950static const struct file_operations io_uring_fops = {
3951 .release = io_uring_release,
3952 .mmap = io_uring_mmap,
3953 .poll = io_uring_poll,
3954 .fasync = io_uring_fasync,
3955};
3956
3957static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3958 struct io_uring_params *p)
3959{
Hristo Venev75b28af2019-08-26 17:23:46 +00003960 struct io_rings *rings;
3961 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003962
Hristo Venev75b28af2019-08-26 17:23:46 +00003963 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3964 if (size == SIZE_MAX)
3965 return -EOVERFLOW;
3966
3967 rings = io_mem_alloc(size);
3968 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003969 return -ENOMEM;
3970
Hristo Venev75b28af2019-08-26 17:23:46 +00003971 ctx->rings = rings;
3972 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3973 rings->sq_ring_mask = p->sq_entries - 1;
3974 rings->cq_ring_mask = p->cq_entries - 1;
3975 rings->sq_ring_entries = p->sq_entries;
3976 rings->cq_ring_entries = p->cq_entries;
3977 ctx->sq_mask = rings->sq_ring_mask;
3978 ctx->cq_mask = rings->cq_ring_mask;
3979 ctx->sq_entries = rings->sq_ring_entries;
3980 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003981
3982 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3983 if (size == SIZE_MAX)
3984 return -EOVERFLOW;
3985
3986 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003987 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003988 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003989
Jens Axboe2b188cc2019-01-07 10:46:33 -07003990 return 0;
3991}
3992
3993/*
3994 * Allocate an anonymous fd, this is what constitutes the application
3995 * visible backing of an io_uring instance. The application mmaps this
3996 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3997 * we have to tie this fd to a socket for file garbage collection purposes.
3998 */
3999static int io_uring_get_fd(struct io_ring_ctx *ctx)
4000{
4001 struct file *file;
4002 int ret;
4003
4004#if defined(CONFIG_UNIX)
4005 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4006 &ctx->ring_sock);
4007 if (ret)
4008 return ret;
4009#endif
4010
4011 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4012 if (ret < 0)
4013 goto err;
4014
4015 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4016 O_RDWR | O_CLOEXEC);
4017 if (IS_ERR(file)) {
4018 put_unused_fd(ret);
4019 ret = PTR_ERR(file);
4020 goto err;
4021 }
4022
4023#if defined(CONFIG_UNIX)
4024 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004025 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004026#endif
4027 fd_install(ret, file);
4028 return ret;
4029err:
4030#if defined(CONFIG_UNIX)
4031 sock_release(ctx->ring_sock);
4032 ctx->ring_sock = NULL;
4033#endif
4034 return ret;
4035}
4036
4037static int io_uring_create(unsigned entries, struct io_uring_params *p)
4038{
4039 struct user_struct *user = NULL;
4040 struct io_ring_ctx *ctx;
4041 bool account_mem;
4042 int ret;
4043
4044 if (!entries || entries > IORING_MAX_ENTRIES)
4045 return -EINVAL;
4046
4047 /*
4048 * Use twice as many entries for the CQ ring. It's possible for the
4049 * application to drive a higher depth than the size of the SQ ring,
4050 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004051 * some flexibility in overcommitting a bit. If the application has
4052 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4053 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004054 */
4055 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004056 if (p->flags & IORING_SETUP_CQSIZE) {
4057 /*
4058 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4059 * to a power-of-two, if it isn't already. We do NOT impose
4060 * any cq vs sq ring sizing.
4061 */
4062 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4063 return -EINVAL;
4064 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4065 } else {
4066 p->cq_entries = 2 * p->sq_entries;
4067 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004068
4069 user = get_uid(current_user());
4070 account_mem = !capable(CAP_IPC_LOCK);
4071
4072 if (account_mem) {
4073 ret = io_account_mem(user,
4074 ring_pages(p->sq_entries, p->cq_entries));
4075 if (ret) {
4076 free_uid(user);
4077 return ret;
4078 }
4079 }
4080
4081 ctx = io_ring_ctx_alloc(p);
4082 if (!ctx) {
4083 if (account_mem)
4084 io_unaccount_mem(user, ring_pages(p->sq_entries,
4085 p->cq_entries));
4086 free_uid(user);
4087 return -ENOMEM;
4088 }
4089 ctx->compat = in_compat_syscall();
4090 ctx->account_mem = account_mem;
4091 ctx->user = user;
4092
4093 ret = io_allocate_scq_urings(ctx, p);
4094 if (ret)
4095 goto err;
4096
Jens Axboe6c271ce2019-01-10 11:22:30 -07004097 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004098 if (ret)
4099 goto err;
4100
Jens Axboe2b188cc2019-01-07 10:46:33 -07004101 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004102 p->sq_off.head = offsetof(struct io_rings, sq.head);
4103 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4104 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4105 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4106 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4107 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4108 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004109
4110 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004111 p->cq_off.head = offsetof(struct io_rings, cq.head);
4112 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4113 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4114 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4115 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4116 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004117
Jens Axboe044c1ab2019-10-28 09:15:33 -06004118 /*
4119 * Install ring fd as the very last thing, so we don't risk someone
4120 * having closed it before we finish setup
4121 */
4122 ret = io_uring_get_fd(ctx);
4123 if (ret < 0)
4124 goto err;
4125
Jens Axboeac90f242019-09-06 10:26:21 -06004126 p->features = IORING_FEAT_SINGLE_MMAP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004127 return ret;
4128err:
4129 io_ring_ctx_wait_and_kill(ctx);
4130 return ret;
4131}
4132
4133/*
4134 * Sets up an aio uring context, and returns the fd. Applications asks for a
4135 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4136 * params structure passed in.
4137 */
4138static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4139{
4140 struct io_uring_params p;
4141 long ret;
4142 int i;
4143
4144 if (copy_from_user(&p, params, sizeof(p)))
4145 return -EFAULT;
4146 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4147 if (p.resv[i])
4148 return -EINVAL;
4149 }
4150
Jens Axboe6c271ce2019-01-10 11:22:30 -07004151 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004152 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004153 return -EINVAL;
4154
4155 ret = io_uring_create(entries, &p);
4156 if (ret < 0)
4157 return ret;
4158
4159 if (copy_to_user(params, &p, sizeof(p)))
4160 return -EFAULT;
4161
4162 return ret;
4163}
4164
4165SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4166 struct io_uring_params __user *, params)
4167{
4168 return io_uring_setup(entries, params);
4169}
4170
Jens Axboeedafcce2019-01-09 09:16:05 -07004171static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4172 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004173 __releases(ctx->uring_lock)
4174 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004175{
4176 int ret;
4177
Jens Axboe35fa71a2019-04-22 10:23:23 -06004178 /*
4179 * We're inside the ring mutex, if the ref is already dying, then
4180 * someone else killed the ctx or is already going through
4181 * io_uring_register().
4182 */
4183 if (percpu_ref_is_dying(&ctx->refs))
4184 return -ENXIO;
4185
Jens Axboeedafcce2019-01-09 09:16:05 -07004186 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004187
4188 /*
4189 * Drop uring mutex before waiting for references to exit. If another
4190 * thread is currently inside io_uring_enter() it might need to grab
4191 * the uring_lock to make progress. If we hold it here across the drain
4192 * wait, then we can deadlock. It's safe to drop the mutex here, since
4193 * no new references will come in after we've killed the percpu ref.
4194 */
4195 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004196 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004197 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004198
4199 switch (opcode) {
4200 case IORING_REGISTER_BUFFERS:
4201 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4202 break;
4203 case IORING_UNREGISTER_BUFFERS:
4204 ret = -EINVAL;
4205 if (arg || nr_args)
4206 break;
4207 ret = io_sqe_buffer_unregister(ctx);
4208 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004209 case IORING_REGISTER_FILES:
4210 ret = io_sqe_files_register(ctx, arg, nr_args);
4211 break;
4212 case IORING_UNREGISTER_FILES:
4213 ret = -EINVAL;
4214 if (arg || nr_args)
4215 break;
4216 ret = io_sqe_files_unregister(ctx);
4217 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004218 case IORING_REGISTER_FILES_UPDATE:
4219 ret = io_sqe_files_update(ctx, arg, nr_args);
4220 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004221 case IORING_REGISTER_EVENTFD:
4222 ret = -EINVAL;
4223 if (nr_args != 1)
4224 break;
4225 ret = io_eventfd_register(ctx, arg);
4226 break;
4227 case IORING_UNREGISTER_EVENTFD:
4228 ret = -EINVAL;
4229 if (arg || nr_args)
4230 break;
4231 ret = io_eventfd_unregister(ctx);
4232 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004233 default:
4234 ret = -EINVAL;
4235 break;
4236 }
4237
4238 /* bring the ctx back to life */
4239 reinit_completion(&ctx->ctx_done);
4240 percpu_ref_reinit(&ctx->refs);
4241 return ret;
4242}
4243
4244SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4245 void __user *, arg, unsigned int, nr_args)
4246{
4247 struct io_ring_ctx *ctx;
4248 long ret = -EBADF;
4249 struct fd f;
4250
4251 f = fdget(fd);
4252 if (!f.file)
4253 return -EBADF;
4254
4255 ret = -EOPNOTSUPP;
4256 if (f.file->f_op != &io_uring_fops)
4257 goto out_fput;
4258
4259 ctx = f.file->private_data;
4260
4261 mutex_lock(&ctx->uring_lock);
4262 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4263 mutex_unlock(&ctx->uring_lock);
4264out_fput:
4265 fdput(f);
4266 return ret;
4267}
4268
Jens Axboe2b188cc2019-01-07 10:46:33 -07004269static int __init io_uring_init(void)
4270{
4271 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4272 return 0;
4273};
4274__initcall(io_uring_init);