blob: e92b88455e5ecefc481b1e7c5c1e8da40cec3fe6 [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>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.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 Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020082#define CREATE_TRACE_POINTS
83#include <trace/events/io_uring.h>
84
Jens Axboe2b188cc2019-01-07 10:46:33 -070085#include <uapi/linux/io_uring.h>
86
87#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060088#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070089
Daniel Xu5277dea2019-09-14 14:23:45 -070090#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060091#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060092
93/*
94 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
95 */
96#define IORING_FILE_TABLE_SHIFT 9
97#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
98#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
99#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700100
101struct io_uring {
102 u32 head ____cacheline_aligned_in_smp;
103 u32 tail ____cacheline_aligned_in_smp;
104};
105
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 * This data is shared with the application through the mmap at offsets
108 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 *
110 * The offsets to the member fields are published through struct
111 * io_sqring_offsets when calling io_uring_setup.
112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
115 * Head and tail offsets into the ring; the offsets need to be
116 * masked to get valid indices.
117 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * The kernel controls head of the sq ring and the tail of the cq ring,
119 * and the application controls tail of the sq ring and the head of the
120 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 * ring_entries - 1)
126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 u32 sq_ring_mask, cq_ring_mask;
128 /* Ring sizes (constant, power of 2) */
129 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 /*
131 * Number of invalid entries dropped by the kernel due to
132 * invalid index stored in array
133 *
134 * Written by the kernel, shouldn't be modified by the
135 * application (i.e. get number of "new events" by comparing to
136 * cached value).
137 *
138 * After a new SQ head value was read by the application this
139 * counter includes all submissions that were dropped reaching
140 * the new SQ head (and possibly more).
141 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000142 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
144 * Runtime flags
145 *
146 * Written by the kernel, shouldn't be modified by the
147 * application.
148 *
149 * The application needs a full memory barrier before checking
150 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
154 * Number of completion events lost because the queue was full;
155 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800156 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 * the completion queue.
158 *
159 * Written by the kernel, shouldn't be modified by the
160 * application (i.e. get number of "new events" by comparing to
161 * cached value).
162 *
163 * As completion events come in out of order this counter is not
164 * ordered with any other data.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 /*
168 * Ring buffer of completion events.
169 *
170 * The kernel writes completion events fresh every time they are
171 * produced, so the application is allowed to modify pending
172 * entries.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700175};
176
Jens Axboeedafcce2019-01-09 09:16:05 -0700177struct io_mapped_ubuf {
178 u64 ubuf;
179 size_t len;
180 struct bio_vec *bvec;
181 unsigned int nr_bvecs;
182};
183
Jens Axboe65e19f52019-10-26 07:20:21 -0600184struct fixed_file_table {
185 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700186};
187
Jens Axboe05f3fb32019-12-09 11:22:50 -0700188struct fixed_file_data {
189 struct fixed_file_table *table;
190 struct io_ring_ctx *ctx;
191
192 struct percpu_ref refs;
193 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe2b188cc2019-01-07 10:46:33 -0700198struct io_ring_ctx {
199 struct {
200 struct percpu_ref refs;
201 } ____cacheline_aligned_in_smp;
202
203 struct {
204 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800205 unsigned int compat: 1;
206 unsigned int account_mem: 1;
207 unsigned int cq_overflow_flushed: 1;
208 unsigned int drain_next: 1;
209 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210
Hristo Venev75b28af2019-08-26 17:23:46 +0000211 /*
212 * Ring buffer of indices into array of io_uring_sqe, which is
213 * mmapped by the application using the IORING_OFF_SQES offset.
214 *
215 * This indirection could e.g. be used to assign fixed
216 * io_uring_sqe entries to operations and only submit them to
217 * the queue when needed.
218 *
219 * The kernel modifies neither the indices array nor the entries
220 * array.
221 */
222 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 unsigned cached_sq_head;
224 unsigned sq_entries;
225 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700226 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600227 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700228 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700229 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600230
231 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600232 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700233 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Jens Axboefcb323c2019-10-24 12:39:47 -0600235 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700236 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 } ____cacheline_aligned_in_smp;
238
Hristo Venev75b28af2019-08-26 17:23:46 +0000239 struct io_rings *rings;
240
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600242 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 struct task_struct *sqo_thread; /* if using sq thread polling */
244 struct mm_struct *sqo_mm;
245 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246
Jens Axboe6b063142019-01-10 22:13:58 -0700247 /*
248 * If used, fixed file set. Writers must ensure that ->refs is dead,
249 * readers must ensure that ->refs is alive as long as the file* is
250 * used. Only updated through io_uring_register(2).
251 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700252 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700253 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300254 int ring_fd;
255 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700256
Jens Axboeedafcce2019-01-09 09:16:05 -0700257 /* if used, fixed mapped user buffers */
258 unsigned nr_user_bufs;
259 struct io_mapped_ubuf *user_bufs;
260
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 struct user_struct *user;
262
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700263 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700264
Jens Axboe206aefd2019-11-07 18:27:42 -0700265 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
266 struct completion *completions;
267
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700268 /* if all else fails... */
269 struct io_kiocb *fallback_req;
270
Jens Axboe206aefd2019-11-07 18:27:42 -0700271#if defined(CONFIG_UNIX)
272 struct socket *ring_sock;
273#endif
274
Jens Axboe071698e2020-01-28 10:04:42 -0700275 struct idr personality_idr;
276
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 struct {
278 unsigned cached_cq_tail;
279 unsigned cq_entries;
280 unsigned cq_mask;
281 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700282 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700283 struct wait_queue_head cq_wait;
284 struct fasync_struct *cq_fasync;
285 struct eventfd_ctx *cq_ev_fd;
286 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700287
288 struct {
289 struct mutex uring_lock;
290 wait_queue_head_t wait;
291 } ____cacheline_aligned_in_smp;
292
293 struct {
294 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700295
Jens Axboedef596e2019-01-09 08:59:42 -0700296 /*
297 * ->poll_list is protected by the ctx->uring_lock for
298 * io_uring instances that don't use IORING_SETUP_SQPOLL.
299 * For SQPOLL, only the single threaded io_sq_thread() will
300 * manipulate the list, hence no extra locking is needed there.
301 */
302 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700303 struct hlist_head *cancel_hash;
304 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700305 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600306
307 spinlock_t inflight_lock;
308 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700310};
311
Jens Axboe09bb8392019-03-13 12:39:28 -0600312/*
313 * First field must be the file pointer in all the
314 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
315 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316struct io_poll_iocb {
317 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700318 union {
319 struct wait_queue_head *head;
320 u64 addr;
321 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700322 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600323 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700324 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700325 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326};
327
Jens Axboeb5dba592019-12-11 14:02:38 -0700328struct io_close {
329 struct file *file;
330 struct file *put_file;
331 int fd;
332};
333
Jens Axboead8a48a2019-11-15 08:49:11 -0700334struct io_timeout_data {
335 struct io_kiocb *req;
336 struct hrtimer timer;
337 struct timespec64 ts;
338 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300339 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700340};
341
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700342struct io_accept {
343 struct file *file;
344 struct sockaddr __user *addr;
345 int __user *addr_len;
346 int flags;
347};
348
349struct io_sync {
350 struct file *file;
351 loff_t len;
352 loff_t off;
353 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700354 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700355};
356
Jens Axboefbf23842019-12-17 18:45:56 -0700357struct io_cancel {
358 struct file *file;
359 u64 addr;
360};
361
Jens Axboeb29472e2019-12-17 18:50:29 -0700362struct io_timeout {
363 struct file *file;
364 u64 addr;
365 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700366 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700367};
368
Jens Axboe9adbd452019-12-20 08:45:55 -0700369struct io_rw {
370 /* NOTE: kiocb has the file as the first member, so don't do it here */
371 struct kiocb kiocb;
372 u64 addr;
373 u64 len;
374};
375
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700376struct io_connect {
377 struct file *file;
378 struct sockaddr __user *addr;
379 int addr_len;
380};
381
Jens Axboee47293f2019-12-20 08:58:21 -0700382struct io_sr_msg {
383 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700384 union {
385 struct user_msghdr __user *msg;
386 void __user *buf;
387 };
Jens Axboee47293f2019-12-20 08:58:21 -0700388 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700389 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700390};
391
Jens Axboe15b71ab2019-12-11 11:20:36 -0700392struct io_open {
393 struct file *file;
394 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 unsigned mask;
397 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700400 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401};
402
Jens Axboe05f3fb32019-12-09 11:22:50 -0700403struct io_files_update {
404 struct file *file;
405 u64 arg;
406 u32 nr_args;
407 u32 offset;
408};
409
Jens Axboe4840e412019-12-25 22:03:45 -0700410struct io_fadvise {
411 struct file *file;
412 u64 offset;
413 u32 len;
414 u32 advice;
415};
416
Jens Axboec1ca7572019-12-25 22:18:28 -0700417struct io_madvise {
418 struct file *file;
419 u64 addr;
420 u32 len;
421 u32 advice;
422};
423
Jens Axboe3e4827b2020-01-08 15:18:09 -0700424struct io_epoll {
425 struct file *file;
426 int epfd;
427 int op;
428 int fd;
429 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430};
431
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300432struct io_splice {
433 struct file *file_out;
434 struct file *file_in;
435 loff_t off_out;
436 loff_t off_in;
437 u64 len;
438 unsigned int flags;
439};
440
Jens Axboef499a022019-12-02 16:28:46 -0700441struct io_async_connect {
442 struct sockaddr_storage address;
443};
444
Jens Axboe03b12302019-12-02 18:50:25 -0700445struct io_async_msghdr {
446 struct iovec fast_iov[UIO_FASTIOV];
447 struct iovec *iov;
448 struct sockaddr __user *uaddr;
449 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700450 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700451};
452
Jens Axboef67676d2019-12-02 11:03:47 -0700453struct io_async_rw {
454 struct iovec fast_iov[UIO_FASTIOV];
455 struct iovec *iov;
456 ssize_t nr_segs;
457 ssize_t size;
458};
459
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700460struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700461 union {
462 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700463 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700464 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700465 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700466 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700467};
468
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300469enum {
470 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
471 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
472 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
473 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
474 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
475
476 REQ_F_LINK_NEXT_BIT,
477 REQ_F_FAIL_LINK_BIT,
478 REQ_F_INFLIGHT_BIT,
479 REQ_F_CUR_POS_BIT,
480 REQ_F_NOWAIT_BIT,
481 REQ_F_IOPOLL_COMPLETED_BIT,
482 REQ_F_LINK_TIMEOUT_BIT,
483 REQ_F_TIMEOUT_BIT,
484 REQ_F_ISREG_BIT,
485 REQ_F_MUST_PUNT_BIT,
486 REQ_F_TIMEOUT_NOSEQ_BIT,
487 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300488 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700489 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700490 REQ_F_POLLED_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300491};
492
493enum {
494 /* ctx owns file */
495 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
496 /* drain existing IO first */
497 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
498 /* linked sqes */
499 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
500 /* doesn't sever on completion < 0 */
501 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
502 /* IOSQE_ASYNC */
503 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
504
505 /* already grabbed next link */
506 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
507 /* fail rest of links */
508 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
509 /* on inflight list */
510 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
511 /* read/write uses file position */
512 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
513 /* must not punt to workers */
514 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
515 /* polled IO has completed */
516 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
517 /* has linked timeout */
518 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
519 /* timeout request */
520 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
521 /* regular file */
522 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
523 /* must be punted even for NONBLOCK */
524 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
525 /* no timeout sequence */
526 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
527 /* completion under lock */
528 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300529 /* needs cleanup */
530 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700531 /* in overflow list */
532 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700533 /* already went through poll handler */
534 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
535};
536
537struct async_poll {
538 struct io_poll_iocb poll;
539 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300540};
541
Jens Axboe09bb8392019-03-13 12:39:28 -0600542/*
543 * NOTE! Each of the iocb union members has the file pointer
544 * as the first entry in their struct definition. So you can
545 * access the file pointer through any of the sub-structs,
546 * or directly as just 'ki_filp' in this struct.
547 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700549 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600550 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700551 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700552 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700553 struct io_accept accept;
554 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700555 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700556 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700557 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700558 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700559 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700560 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700561 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700562 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700563 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700564 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300565 struct io_splice splice;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700566 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700568 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300569 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700570 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571
572 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700573 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700575 refcount_t refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700576 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600578 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600579 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700580
Jens Axboed7718a92020-02-14 22:23:12 -0700581 struct list_head link_list;
582
Jens Axboefcb323c2019-10-24 12:39:47 -0600583 struct list_head inflight_entry;
584
Jens Axboeb41e9852020-02-17 09:52:41 -0700585 union {
586 /*
587 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700588 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
589 * async armed poll handlers for regular commands. The latter
590 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700591 */
592 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700593 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700594 struct hlist_node hash_node;
595 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700596 };
597 struct io_wq_work work;
598 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599};
600
601#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700602#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700603
Jens Axboe9a56a232019-01-09 09:06:50 -0700604struct io_submit_state {
605 struct blk_plug plug;
606
607 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700608 * io_kiocb alloc cache
609 */
610 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300611 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700612
613 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700614 * File reference cache
615 */
616 struct file *file;
617 unsigned int fd;
618 unsigned int has_refs;
619 unsigned int used_refs;
620 unsigned int ios_left;
621};
622
Jens Axboed3656342019-12-18 09:50:26 -0700623struct io_op_def {
624 /* needs req->io allocated for deferral/async */
625 unsigned async_ctx : 1;
626 /* needs current->mm setup, does mm access */
627 unsigned needs_mm : 1;
628 /* needs req->file assigned */
629 unsigned needs_file : 1;
630 /* needs req->file assigned IFF fd is >= 0 */
631 unsigned fd_non_neg : 1;
632 /* hash wq insertion if file is a regular file */
633 unsigned hash_reg_file : 1;
634 /* unbound wq insertion if file is a non-regular file */
635 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700636 /* opcode is not supported by this kernel */
637 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700638 /* needs file table */
639 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700640 /* needs ->fs */
641 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700642 /* set if opcode supports polled "wait" */
643 unsigned pollin : 1;
644 unsigned pollout : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700645};
646
647static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300648 [IORING_OP_NOP] = {},
649 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700650 .async_ctx = 1,
651 .needs_mm = 1,
652 .needs_file = 1,
653 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700654 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700655 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300656 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700657 .async_ctx = 1,
658 .needs_mm = 1,
659 .needs_file = 1,
660 .hash_reg_file = 1,
661 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700662 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700663 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300664 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700665 .needs_file = 1,
666 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300667 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700668 .needs_file = 1,
669 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700670 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700671 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300672 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700673 .needs_file = 1,
674 .hash_reg_file = 1,
675 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700676 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700677 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300678 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700679 .needs_file = 1,
680 .unbound_nonreg_file = 1,
681 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_POLL_REMOVE] = {},
683 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700684 .needs_file = 1,
685 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300686 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700687 .async_ctx = 1,
688 .needs_mm = 1,
689 .needs_file = 1,
690 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700691 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700692 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700695 .async_ctx = 1,
696 .needs_mm = 1,
697 .needs_file = 1,
698 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700699 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700700 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .async_ctx = 1,
704 .needs_mm = 1,
705 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300706 [IORING_OP_TIMEOUT_REMOVE] = {},
707 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_mm = 1,
709 .needs_file = 1,
710 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700711 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700712 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700713 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300714 [IORING_OP_ASYNC_CANCEL] = {},
715 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700716 .async_ctx = 1,
717 .needs_mm = 1,
718 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300719 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700720 .async_ctx = 1,
721 .needs_mm = 1,
722 .needs_file = 1,
723 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700724 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700727 .needs_file = 1,
728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700730 .needs_file = 1,
731 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700732 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700733 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700736 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700737 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700740 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700741 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700742 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300743 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700744 .needs_mm = 1,
745 .needs_file = 1,
746 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700747 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700748 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300749 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700750 .needs_mm = 1,
751 .needs_file = 1,
752 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700753 .pollin = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700756 .needs_mm = 1,
757 .needs_file = 1,
758 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700759 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700760 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300761 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700762 .needs_file = 1,
763 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300764 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700765 .needs_mm = 1,
766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700768 .needs_mm = 1,
769 .needs_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700771 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700774 .needs_mm = 1,
775 .needs_file = 1,
776 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700777 .pollin = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700778 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300779 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700780 .needs_file = 1,
781 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700782 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700783 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700784 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700785 [IORING_OP_EPOLL_CTL] = {
786 .unbound_nonreg_file = 1,
787 .file_table = 1,
788 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300789 [IORING_OP_SPLICE] = {
790 .needs_file = 1,
791 .hash_reg_file = 1,
792 .unbound_nonreg_file = 1,
793 }
Jens Axboed3656342019-12-18 09:50:26 -0700794};
795
Jens Axboe561fb042019-10-24 07:25:42 -0600796static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700797static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800798static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700799static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700800static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
801static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700802static int __io_sqe_files_update(struct io_ring_ctx *ctx,
803 struct io_uring_files_update *ip,
804 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700805static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700806static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300807static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700808static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
809 int fd, struct file **out_file, bool fixed);
810static void __io_queue_sqe(struct io_kiocb *req,
811 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600812
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813static struct kmem_cache *req_cachep;
814
815static const struct file_operations io_uring_fops;
816
817struct sock *io_uring_get_socket(struct file *file)
818{
819#if defined(CONFIG_UNIX)
820 if (file->f_op == &io_uring_fops) {
821 struct io_ring_ctx *ctx = file->private_data;
822
823 return ctx->ring_sock->sk;
824 }
825#endif
826 return NULL;
827}
828EXPORT_SYMBOL(io_uring_get_socket);
829
830static void io_ring_ctx_ref_free(struct percpu_ref *ref)
831{
832 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
833
Jens Axboe206aefd2019-11-07 18:27:42 -0700834 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700835}
836
837static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
838{
839 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700840 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841
842 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
843 if (!ctx)
844 return NULL;
845
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700846 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
847 if (!ctx->fallback_req)
848 goto err;
849
Jens Axboe206aefd2019-11-07 18:27:42 -0700850 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
851 if (!ctx->completions)
852 goto err;
853
Jens Axboe78076bb2019-12-04 19:56:40 -0700854 /*
855 * Use 5 bits less than the max cq entries, that should give us around
856 * 32 entries per hash list if totally full and uniformly spread.
857 */
858 hash_bits = ilog2(p->cq_entries);
859 hash_bits -= 5;
860 if (hash_bits <= 0)
861 hash_bits = 1;
862 ctx->cancel_hash_bits = hash_bits;
863 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
864 GFP_KERNEL);
865 if (!ctx->cancel_hash)
866 goto err;
867 __hash_init(ctx->cancel_hash, 1U << hash_bits);
868
Roman Gushchin21482892019-05-07 10:01:48 -0700869 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700870 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
871 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700872
873 ctx->flags = p->flags;
874 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700875 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700876 init_completion(&ctx->completions[0]);
877 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700878 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700879 mutex_init(&ctx->uring_lock);
880 init_waitqueue_head(&ctx->wait);
881 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700882 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600883 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600884 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600885 init_waitqueue_head(&ctx->inflight_wait);
886 spin_lock_init(&ctx->inflight_lock);
887 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700888 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700889err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700890 if (ctx->fallback_req)
891 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700892 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700893 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700894 kfree(ctx);
895 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896}
897
Bob Liu9d858b22019-11-13 18:06:25 +0800898static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600899{
Jackie Liua197f662019-11-08 08:09:12 -0700900 struct io_ring_ctx *ctx = req->ctx;
901
Jens Axboe498ccd92019-10-25 10:04:25 -0600902 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
903 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600904}
905
Bob Liu9d858b22019-11-13 18:06:25 +0800906static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600907{
Pavel Begunkov87987892020-01-18 01:22:30 +0300908 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800909 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600910
Bob Liu9d858b22019-11-13 18:06:25 +0800911 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600912}
913
914static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600915{
916 struct io_kiocb *req;
917
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600918 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800919 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600920 list_del_init(&req->list);
921 return req;
922 }
923
924 return NULL;
925}
926
Jens Axboe5262f562019-09-17 12:26:57 -0600927static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
928{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600929 struct io_kiocb *req;
930
931 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700932 if (req) {
933 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
934 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800935 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700936 list_del_init(&req->list);
937 return req;
938 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600939 }
940
941 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600942}
943
Jens Axboede0617e2019-04-06 21:51:27 -0600944static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700945{
Hristo Venev75b28af2019-08-26 17:23:46 +0000946 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700947
Pavel Begunkov07910152020-01-17 03:52:46 +0300948 /* order cqe stores with ring update */
949 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700950
Pavel Begunkov07910152020-01-17 03:52:46 +0300951 if (wq_has_sleeper(&ctx->cq_wait)) {
952 wake_up_interruptible(&ctx->cq_wait);
953 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700954 }
955}
956
Jens Axboecccf0ee2020-01-27 16:34:48 -0700957static inline void io_req_work_grab_env(struct io_kiocb *req,
958 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600959{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700960 if (!req->work.mm && def->needs_mm) {
961 mmgrab(current->mm);
962 req->work.mm = current->mm;
963 }
964 if (!req->work.creds)
965 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700966 if (!req->work.fs && def->needs_fs) {
967 spin_lock(&current->fs->lock);
968 if (!current->fs->in_exec) {
969 req->work.fs = current->fs;
970 req->work.fs->users++;
971 } else {
972 req->work.flags |= IO_WQ_WORK_CANCEL;
973 }
974 spin_unlock(&current->fs->lock);
975 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700976 if (!req->work.task_pid)
977 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700978}
979
980static inline void io_req_work_drop_env(struct io_kiocb *req)
981{
982 if (req->work.mm) {
983 mmdrop(req->work.mm);
984 req->work.mm = NULL;
985 }
986 if (req->work.creds) {
987 put_cred(req->work.creds);
988 req->work.creds = NULL;
989 }
Jens Axboeff002b32020-02-07 16:05:21 -0700990 if (req->work.fs) {
991 struct fs_struct *fs = req->work.fs;
992
993 spin_lock(&req->work.fs->lock);
994 if (--fs->users)
995 fs = NULL;
996 spin_unlock(&req->work.fs->lock);
997 if (fs)
998 free_fs_struct(fs);
999 }
Jens Axboe561fb042019-10-24 07:25:42 -06001000}
1001
Jens Axboe94ae5e72019-11-14 19:39:52 -07001002static inline bool io_prep_async_work(struct io_kiocb *req,
1003 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001004{
Jens Axboed3656342019-12-18 09:50:26 -07001005 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001006 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001007
Jens Axboed3656342019-12-18 09:50:26 -07001008 if (req->flags & REQ_F_ISREG) {
1009 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001010 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001011 } else {
1012 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001013 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001014 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001015
1016 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001017
Jens Axboe94ae5e72019-11-14 19:39:52 -07001018 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001019 return do_hashed;
1020}
1021
Jackie Liua197f662019-11-08 08:09:12 -07001022static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001023{
Jackie Liua197f662019-11-08 08:09:12 -07001024 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001025 struct io_kiocb *link;
1026 bool do_hashed;
1027
1028 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001029
1030 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1031 req->flags);
1032 if (!do_hashed) {
1033 io_wq_enqueue(ctx->io_wq, &req->work);
1034 } else {
1035 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1036 file_inode(req->file));
1037 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001038
1039 if (link)
1040 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001041}
1042
Jens Axboe5262f562019-09-17 12:26:57 -06001043static void io_kill_timeout(struct io_kiocb *req)
1044{
1045 int ret;
1046
Jens Axboe2d283902019-12-04 11:08:05 -07001047 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001048 if (ret != -1) {
1049 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001050 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001051 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001052 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001053 }
1054}
1055
1056static void io_kill_timeouts(struct io_ring_ctx *ctx)
1057{
1058 struct io_kiocb *req, *tmp;
1059
1060 spin_lock_irq(&ctx->completion_lock);
1061 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1062 io_kill_timeout(req);
1063 spin_unlock_irq(&ctx->completion_lock);
1064}
1065
Jens Axboede0617e2019-04-06 21:51:27 -06001066static void io_commit_cqring(struct io_ring_ctx *ctx)
1067{
1068 struct io_kiocb *req;
1069
Jens Axboe5262f562019-09-17 12:26:57 -06001070 while ((req = io_get_timeout_req(ctx)) != NULL)
1071 io_kill_timeout(req);
1072
Jens Axboede0617e2019-04-06 21:51:27 -06001073 __io_commit_cqring(ctx);
1074
Pavel Begunkov87987892020-01-18 01:22:30 +03001075 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001076 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001077}
1078
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1080{
Hristo Venev75b28af2019-08-26 17:23:46 +00001081 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082 unsigned tail;
1083
1084 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001085 /*
1086 * writes to the cq entry need to come after reading head; the
1087 * control dependency is enough as we're using WRITE_ONCE to
1088 * fill the cq entry
1089 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001090 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091 return NULL;
1092
1093 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001094 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095}
1096
Jens Axboef2842ab2020-01-08 11:04:00 -07001097static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1098{
Jens Axboef0b493e2020-02-01 21:30:11 -07001099 if (!ctx->cq_ev_fd)
1100 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001101 if (!ctx->eventfd_async)
1102 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001103 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001104}
1105
Jens Axboeb41e9852020-02-17 09:52:41 -07001106static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001107{
1108 if (waitqueue_active(&ctx->wait))
1109 wake_up(&ctx->wait);
1110 if (waitqueue_active(&ctx->sqo_wait))
1111 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001112 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001113 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001114}
1115
Jens Axboec4a2ed72019-11-21 21:01:26 -07001116/* Returns true if there are no backlogged entries after the flush */
1117static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001118{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001119 struct io_rings *rings = ctx->rings;
1120 struct io_uring_cqe *cqe;
1121 struct io_kiocb *req;
1122 unsigned long flags;
1123 LIST_HEAD(list);
1124
1125 if (!force) {
1126 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001127 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001128 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1129 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001130 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001131 }
1132
1133 spin_lock_irqsave(&ctx->completion_lock, flags);
1134
1135 /* if force is set, the ring is going away. always drop after that */
1136 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001137 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001138
Jens Axboec4a2ed72019-11-21 21:01:26 -07001139 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001140 while (!list_empty(&ctx->cq_overflow_list)) {
1141 cqe = io_get_cqring(ctx);
1142 if (!cqe && !force)
1143 break;
1144
1145 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1146 list);
1147 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001148 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001149 if (cqe) {
1150 WRITE_ONCE(cqe->user_data, req->user_data);
1151 WRITE_ONCE(cqe->res, req->result);
1152 WRITE_ONCE(cqe->flags, 0);
1153 } else {
1154 WRITE_ONCE(ctx->rings->cq_overflow,
1155 atomic_inc_return(&ctx->cached_cq_overflow));
1156 }
1157 }
1158
1159 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001160 if (cqe) {
1161 clear_bit(0, &ctx->sq_check_overflow);
1162 clear_bit(0, &ctx->cq_check_overflow);
1163 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001164 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1165 io_cqring_ev_posted(ctx);
1166
1167 while (!list_empty(&list)) {
1168 req = list_first_entry(&list, struct io_kiocb, list);
1169 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001170 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001171 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001172
1173 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001174}
1175
Jens Axboe78e19bb2019-11-06 15:21:34 -07001176static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001179 struct io_uring_cqe *cqe;
1180
Jens Axboe78e19bb2019-11-06 15:21:34 -07001181 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001182
Jens Axboe2b188cc2019-01-07 10:46:33 -07001183 /*
1184 * If we can't get a cq entry, userspace overflowed the
1185 * submission (by quite a lot). Increment the overflow count in
1186 * the ring.
1187 */
1188 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001189 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001190 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001191 WRITE_ONCE(cqe->res, res);
1192 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001193 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001194 WRITE_ONCE(ctx->rings->cq_overflow,
1195 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001196 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001197 if (list_empty(&ctx->cq_overflow_list)) {
1198 set_bit(0, &ctx->sq_check_overflow);
1199 set_bit(0, &ctx->cq_check_overflow);
1200 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001201 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001202 refcount_inc(&req->refs);
1203 req->result = res;
1204 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001205 }
1206}
1207
Jens Axboe78e19bb2019-11-06 15:21:34 -07001208static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001209{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001210 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001211 unsigned long flags;
1212
1213 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001214 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215 io_commit_cqring(ctx);
1216 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1217
Jens Axboe8c838782019-03-12 15:48:16 -06001218 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219}
1220
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001221static inline bool io_is_fallback_req(struct io_kiocb *req)
1222{
1223 return req == (struct io_kiocb *)
1224 ((unsigned long) req->ctx->fallback_req & ~1UL);
1225}
1226
1227static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1228{
1229 struct io_kiocb *req;
1230
1231 req = ctx->fallback_req;
1232 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1233 return req;
1234
1235 return NULL;
1236}
1237
Jens Axboe2579f912019-01-09 09:10:43 -07001238static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1239 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001240{
Jens Axboefd6fab22019-03-14 16:30:06 -06001241 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242 struct io_kiocb *req;
1243
Jens Axboe2579f912019-01-09 09:10:43 -07001244 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001245 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001246 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001247 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001248 } else if (!state->free_reqs) {
1249 size_t sz;
1250 int ret;
1251
1252 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001253 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1254
1255 /*
1256 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1257 * retry single alloc to be on the safe side.
1258 */
1259 if (unlikely(ret <= 0)) {
1260 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1261 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001262 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001263 ret = 1;
1264 }
Jens Axboe2579f912019-01-09 09:10:43 -07001265 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001266 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001267 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001268 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001269 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270 }
1271
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001272got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001273 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001274 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001275 req->ctx = ctx;
1276 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001277 /* one is dropped after submission, the other at completion */
1278 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001279 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001280 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001281 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001282fallback:
1283 req = io_get_fallback_req(ctx);
1284 if (req)
1285 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001286 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287 return NULL;
1288}
1289
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001290static inline void io_put_file(struct io_kiocb *req, struct file *file,
1291 bool fixed)
1292{
1293 if (fixed)
1294 percpu_ref_put(&req->ctx->file_data->refs);
1295 else
1296 fput(file);
1297}
1298
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001299static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001300{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001301 if (likely(!io_is_fallback_req(req)))
1302 kmem_cache_free(req_cachep, req);
1303 else
1304 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1305}
1306
Jens Axboec6ca97b302019-12-28 12:11:08 -07001307static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001309 if (req->flags & REQ_F_NEED_CLEANUP)
1310 io_cleanup_req(req);
1311
YueHaibing96fd84d2020-01-07 22:22:44 +08001312 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001313 if (req->file)
1314 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001315
1316 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001317}
1318
1319static void __io_free_req(struct io_kiocb *req)
1320{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001321 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001322
Jens Axboefcb323c2019-10-24 12:39:47 -06001323 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001324 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001325 unsigned long flags;
1326
1327 spin_lock_irqsave(&ctx->inflight_lock, flags);
1328 list_del(&req->inflight_entry);
1329 if (waitqueue_active(&ctx->inflight_wait))
1330 wake_up(&ctx->inflight_wait);
1331 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1332 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001333
1334 percpu_ref_put(&req->ctx->refs);
1335 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001336}
1337
Jens Axboec6ca97b302019-12-28 12:11:08 -07001338struct req_batch {
1339 void *reqs[IO_IOPOLL_BATCH];
1340 int to_free;
1341 int need_iter;
1342};
1343
1344static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1345{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001346 int fixed_refs = rb->to_free;
1347
Jens Axboec6ca97b302019-12-28 12:11:08 -07001348 if (!rb->to_free)
1349 return;
1350 if (rb->need_iter) {
1351 int i, inflight = 0;
1352 unsigned long flags;
1353
Jens Axboe10fef4b2020-01-09 07:52:28 -07001354 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001355 for (i = 0; i < rb->to_free; i++) {
1356 struct io_kiocb *req = rb->reqs[i];
1357
Jens Axboe10fef4b2020-01-09 07:52:28 -07001358 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001359 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001360 fixed_refs++;
1361 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001362 if (req->flags & REQ_F_INFLIGHT)
1363 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001364 __io_req_aux_free(req);
1365 }
1366 if (!inflight)
1367 goto do_free;
1368
1369 spin_lock_irqsave(&ctx->inflight_lock, flags);
1370 for (i = 0; i < rb->to_free; i++) {
1371 struct io_kiocb *req = rb->reqs[i];
1372
Jens Axboe10fef4b2020-01-09 07:52:28 -07001373 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001374 list_del(&req->inflight_entry);
1375 if (!--inflight)
1376 break;
1377 }
1378 }
1379 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1380
1381 if (waitqueue_active(&ctx->inflight_wait))
1382 wake_up(&ctx->inflight_wait);
1383 }
1384do_free:
1385 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001386 if (fixed_refs)
1387 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001388 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001389 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001390}
1391
Jackie Liua197f662019-11-08 08:09:12 -07001392static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001393{
Jackie Liua197f662019-11-08 08:09:12 -07001394 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001395 int ret;
1396
Jens Axboe2d283902019-12-04 11:08:05 -07001397 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001398 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001399 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001400 io_commit_cqring(ctx);
1401 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001402 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001403 return true;
1404 }
1405
1406 return false;
1407}
1408
Jens Axboeba816ad2019-09-28 11:36:45 -06001409static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001410{
Jens Axboe2665abf2019-11-05 12:40:47 -07001411 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001412 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001413
Jens Axboe4d7dd462019-11-20 13:03:52 -07001414 /* Already got next link */
1415 if (req->flags & REQ_F_LINK_NEXT)
1416 return;
1417
Jens Axboe9e645e112019-05-10 16:07:28 -06001418 /*
1419 * The list should never be empty when we are called here. But could
1420 * potentially happen if the chain is messed up, check to be on the
1421 * safe side.
1422 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001423 while (!list_empty(&req->link_list)) {
1424 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1425 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001426
Pavel Begunkov44932332019-12-05 16:16:35 +03001427 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1428 (nxt->flags & REQ_F_TIMEOUT))) {
1429 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001430 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001431 req->flags &= ~REQ_F_LINK_TIMEOUT;
1432 continue;
1433 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001434
Pavel Begunkov44932332019-12-05 16:16:35 +03001435 list_del_init(&req->link_list);
1436 if (!list_empty(&nxt->link_list))
1437 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001438 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001439 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001440 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001441
Jens Axboe4d7dd462019-11-20 13:03:52 -07001442 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001443 if (wake_ev)
1444 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001445}
1446
1447/*
1448 * Called if REQ_F_LINK is set, and we fail the head request
1449 */
1450static void io_fail_links(struct io_kiocb *req)
1451{
Jens Axboe2665abf2019-11-05 12:40:47 -07001452 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 unsigned long flags;
1454
1455 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001456
1457 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001458 struct io_kiocb *link = list_first_entry(&req->link_list,
1459 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001460
Pavel Begunkov44932332019-12-05 16:16:35 +03001461 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001462 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001463
1464 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001465 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001466 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001467 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001468 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001469 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001470 }
Jens Axboe5d960722019-11-19 15:31:28 -07001471 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001472 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001473
1474 io_commit_cqring(ctx);
1475 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1476 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001477}
1478
Jens Axboe4d7dd462019-11-20 13:03:52 -07001479static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001480{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001481 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001482 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001483
Jens Axboe9e645e112019-05-10 16:07:28 -06001484 /*
1485 * If LINK is set, we have dependent requests in this chain. If we
1486 * didn't fail this request, queue the first one up, moving any other
1487 * dependencies to the next request. In case of failure, fail the rest
1488 * of the chain.
1489 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001490 if (req->flags & REQ_F_FAIL_LINK) {
1491 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001492 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1493 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001494 struct io_ring_ctx *ctx = req->ctx;
1495 unsigned long flags;
1496
1497 /*
1498 * If this is a timeout link, we could be racing with the
1499 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001500 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001501 */
1502 spin_lock_irqsave(&ctx->completion_lock, flags);
1503 io_req_link_next(req, nxt);
1504 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1505 } else {
1506 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001507 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001508}
Jens Axboe9e645e112019-05-10 16:07:28 -06001509
Jackie Liuc69f8db2019-11-09 11:00:08 +08001510static void io_free_req(struct io_kiocb *req)
1511{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001512 struct io_kiocb *nxt = NULL;
1513
1514 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001515 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001516
1517 if (nxt)
1518 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001519}
1520
Jens Axboeba816ad2019-09-28 11:36:45 -06001521/*
1522 * Drop reference to request, return next in chain (if there is one) if this
1523 * was the last reference to this request.
1524 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001525__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001526static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001527{
Jens Axboe2a44f462020-02-25 13:25:41 -07001528 if (refcount_dec_and_test(&req->refs)) {
1529 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001530 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001531 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001532}
1533
Jens Axboe2b188cc2019-01-07 10:46:33 -07001534static void io_put_req(struct io_kiocb *req)
1535{
Jens Axboedef596e2019-01-09 08:59:42 -07001536 if (refcount_dec_and_test(&req->refs))
1537 io_free_req(req);
1538}
1539
Jens Axboe978db572019-11-14 22:39:04 -07001540/*
1541 * Must only be used if we don't need to care about links, usually from
1542 * within the completion handling itself.
1543 */
1544static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001545{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001546 /* drop both submit and complete references */
1547 if (refcount_sub_and_test(2, &req->refs))
1548 __io_free_req(req);
1549}
1550
Jens Axboe978db572019-11-14 22:39:04 -07001551static void io_double_put_req(struct io_kiocb *req)
1552{
1553 /* drop both submit and complete references */
1554 if (refcount_sub_and_test(2, &req->refs))
1555 io_free_req(req);
1556}
1557
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001558static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001559{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001560 struct io_rings *rings = ctx->rings;
1561
Jens Axboead3eb2c2019-12-18 17:12:20 -07001562 if (test_bit(0, &ctx->cq_check_overflow)) {
1563 /*
1564 * noflush == true is from the waitqueue handler, just ensure
1565 * we wake up the task, and the next invocation will flush the
1566 * entries. We cannot safely to it from here.
1567 */
1568 if (noflush && !list_empty(&ctx->cq_overflow_list))
1569 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001570
Jens Axboead3eb2c2019-12-18 17:12:20 -07001571 io_cqring_overflow_flush(ctx, false);
1572 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001573
Jens Axboea3a0e432019-08-20 11:03:11 -06001574 /* See comment at the top of this file */
1575 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001576 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001577}
1578
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001579static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1580{
1581 struct io_rings *rings = ctx->rings;
1582
1583 /* make sure SQ entry isn't read before tail */
1584 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1585}
1586
Jens Axboe8237e042019-12-28 10:48:22 -07001587static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001588{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001589 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1590 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001591
Jens Axboec6ca97b302019-12-28 12:11:08 -07001592 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1593 rb->need_iter++;
1594
1595 rb->reqs[rb->to_free++] = req;
1596 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1597 io_free_req_many(req->ctx, rb);
1598 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001599}
1600
Jens Axboedef596e2019-01-09 08:59:42 -07001601/*
1602 * Find and free completed poll iocbs
1603 */
1604static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1605 struct list_head *done)
1606{
Jens Axboe8237e042019-12-28 10:48:22 -07001607 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001608 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001609
Jens Axboec6ca97b302019-12-28 12:11:08 -07001610 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001611 while (!list_empty(done)) {
1612 req = list_first_entry(done, struct io_kiocb, list);
1613 list_del(&req->list);
1614
Jens Axboe78e19bb2019-11-06 15:21:34 -07001615 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001616 (*nr_events)++;
1617
Jens Axboe8237e042019-12-28 10:48:22 -07001618 if (refcount_dec_and_test(&req->refs) &&
1619 !io_req_multi_free(&rb, req))
1620 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001621 }
Jens Axboedef596e2019-01-09 08:59:42 -07001622
Jens Axboe09bb8392019-03-13 12:39:28 -06001623 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001624 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001625}
1626
1627static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1628 long min)
1629{
1630 struct io_kiocb *req, *tmp;
1631 LIST_HEAD(done);
1632 bool spin;
1633 int ret;
1634
1635 /*
1636 * Only spin for completions if we don't have multiple devices hanging
1637 * off our complete list, and we're under the requested amount.
1638 */
1639 spin = !ctx->poll_multi_file && *nr_events < min;
1640
1641 ret = 0;
1642 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001643 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001644
1645 /*
1646 * Move completed entries to our local list. If we find a
1647 * request that requires polling, break out and complete
1648 * the done list first, if we have entries there.
1649 */
1650 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1651 list_move_tail(&req->list, &done);
1652 continue;
1653 }
1654 if (!list_empty(&done))
1655 break;
1656
1657 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1658 if (ret < 0)
1659 break;
1660
1661 if (ret && spin)
1662 spin = false;
1663 ret = 0;
1664 }
1665
1666 if (!list_empty(&done))
1667 io_iopoll_complete(ctx, nr_events, &done);
1668
1669 return ret;
1670}
1671
1672/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001673 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001674 * non-spinning poll check - we'll still enter the driver poll loop, but only
1675 * as a non-spinning completion check.
1676 */
1677static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1678 long min)
1679{
Jens Axboe08f54392019-08-21 22:19:11 -06001680 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001681 int ret;
1682
1683 ret = io_do_iopoll(ctx, nr_events, min);
1684 if (ret < 0)
1685 return ret;
1686 if (!min || *nr_events >= min)
1687 return 0;
1688 }
1689
1690 return 1;
1691}
1692
1693/*
1694 * We can't just wait for polled events to come to us, we have to actively
1695 * find and complete them.
1696 */
1697static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1698{
1699 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1700 return;
1701
1702 mutex_lock(&ctx->uring_lock);
1703 while (!list_empty(&ctx->poll_list)) {
1704 unsigned int nr_events = 0;
1705
1706 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001707
1708 /*
1709 * Ensure we allow local-to-the-cpu processing to take place,
1710 * in this case we need to ensure that we reap all events.
1711 */
1712 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001713 }
1714 mutex_unlock(&ctx->uring_lock);
1715}
1716
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001717static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1718 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001719{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001720 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001721
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001722 /*
1723 * We disallow the app entering submit/complete with polling, but we
1724 * still need to lock the ring to prevent racing with polled issue
1725 * that got punted to a workqueue.
1726 */
1727 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001728 do {
1729 int tmin = 0;
1730
Jens Axboe500f9fb2019-08-19 12:15:59 -06001731 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001732 * Don't enter poll loop if we already have events pending.
1733 * If we do, we can potentially be spinning for commands that
1734 * already triggered a CQE (eg in error).
1735 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001736 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001737 break;
1738
1739 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001740 * If a submit got punted to a workqueue, we can have the
1741 * application entering polling for a command before it gets
1742 * issued. That app will hold the uring_lock for the duration
1743 * of the poll right here, so we need to take a breather every
1744 * now and then to ensure that the issue has a chance to add
1745 * the poll to the issued list. Otherwise we can spin here
1746 * forever, while the workqueue is stuck trying to acquire the
1747 * very same mutex.
1748 */
1749 if (!(++iters & 7)) {
1750 mutex_unlock(&ctx->uring_lock);
1751 mutex_lock(&ctx->uring_lock);
1752 }
1753
Jens Axboedef596e2019-01-09 08:59:42 -07001754 if (*nr_events < min)
1755 tmin = min - *nr_events;
1756
1757 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1758 if (ret <= 0)
1759 break;
1760 ret = 0;
1761 } while (min && !*nr_events && !need_resched());
1762
Jens Axboe500f9fb2019-08-19 12:15:59 -06001763 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001764 return ret;
1765}
1766
Jens Axboe491381ce2019-10-17 09:20:46 -06001767static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001768{
Jens Axboe491381ce2019-10-17 09:20:46 -06001769 /*
1770 * Tell lockdep we inherited freeze protection from submission
1771 * thread.
1772 */
1773 if (req->flags & REQ_F_ISREG) {
1774 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775
Jens Axboe491381ce2019-10-17 09:20:46 -06001776 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001778 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779}
1780
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001781static inline void req_set_fail_links(struct io_kiocb *req)
1782{
1783 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1784 req->flags |= REQ_F_FAIL_LINK;
1785}
1786
Jens Axboeba816ad2019-09-28 11:36:45 -06001787static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788{
Jens Axboe9adbd452019-12-20 08:45:55 -07001789 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790
Jens Axboe491381ce2019-10-17 09:20:46 -06001791 if (kiocb->ki_flags & IOCB_WRITE)
1792 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001794 if (res != req->result)
1795 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001796 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001797}
1798
1799static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1800{
Jens Axboe9adbd452019-12-20 08:45:55 -07001801 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001802
1803 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001804 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001805}
1806
Jens Axboeba816ad2019-09-28 11:36:45 -06001807static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1808{
Jens Axboe9adbd452019-12-20 08:45:55 -07001809 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001810 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001811
1812 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001813 io_put_req_find_next(req, &nxt);
1814
1815 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001816}
1817
Jens Axboedef596e2019-01-09 08:59:42 -07001818static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1819{
Jens Axboe9adbd452019-12-20 08:45:55 -07001820 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001821
Jens Axboe491381ce2019-10-17 09:20:46 -06001822 if (kiocb->ki_flags & IOCB_WRITE)
1823 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001824
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001825 if (res != req->result)
1826 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001827 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001828 if (res != -EAGAIN)
1829 req->flags |= REQ_F_IOPOLL_COMPLETED;
1830}
1831
1832/*
1833 * After the iocb has been issued, it's safe to be found on the poll list.
1834 * Adding the kiocb to the list AFTER submission ensures that we don't
1835 * find it from a io_iopoll_getevents() thread before the issuer is done
1836 * accessing the kiocb cookie.
1837 */
1838static void io_iopoll_req_issued(struct io_kiocb *req)
1839{
1840 struct io_ring_ctx *ctx = req->ctx;
1841
1842 /*
1843 * Track whether we have multiple files in our lists. This will impact
1844 * how we do polling eventually, not spinning if we're on potentially
1845 * different devices.
1846 */
1847 if (list_empty(&ctx->poll_list)) {
1848 ctx->poll_multi_file = false;
1849 } else if (!ctx->poll_multi_file) {
1850 struct io_kiocb *list_req;
1851
1852 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1853 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001854 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001855 ctx->poll_multi_file = true;
1856 }
1857
1858 /*
1859 * For fast devices, IO may have already completed. If it has, add
1860 * it to the front so we find it first.
1861 */
1862 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1863 list_add(&req->list, &ctx->poll_list);
1864 else
1865 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001866
1867 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1868 wq_has_sleeper(&ctx->sqo_wait))
1869 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001870}
1871
Jens Axboe3d6770f2019-04-13 11:50:54 -06001872static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001873{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001874 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001875 int diff = state->has_refs - state->used_refs;
1876
1877 if (diff)
1878 fput_many(state->file, diff);
1879 state->file = NULL;
1880 }
1881}
1882
1883/*
1884 * Get as many references to a file as we have IOs left in this submission,
1885 * assuming most submissions are for one file, or at least that each file
1886 * has more than one submission.
1887 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001888static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001889{
1890 if (!state)
1891 return fget(fd);
1892
1893 if (state->file) {
1894 if (state->fd == fd) {
1895 state->used_refs++;
1896 state->ios_left--;
1897 return state->file;
1898 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001899 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001900 }
1901 state->file = fget_many(fd, state->ios_left);
1902 if (!state->file)
1903 return NULL;
1904
1905 state->fd = fd;
1906 state->has_refs = state->ios_left;
1907 state->used_refs = 1;
1908 state->ios_left--;
1909 return state->file;
1910}
1911
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912/*
1913 * If we tracked the file through the SCM inflight mechanism, we could support
1914 * any file. For now, just ensure that anything potentially problematic is done
1915 * inline.
1916 */
1917static bool io_file_supports_async(struct file *file)
1918{
1919 umode_t mode = file_inode(file)->i_mode;
1920
Jens Axboe10d59342019-12-09 20:16:22 -07001921 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922 return true;
1923 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1924 return true;
1925
1926 return false;
1927}
1928
Jens Axboe3529d8c2019-12-19 18:24:38 -07001929static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1930 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001931{
Jens Axboedef596e2019-01-09 08:59:42 -07001932 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001933 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001934 unsigned ioprio;
1935 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001936
Jens Axboe491381ce2019-10-17 09:20:46 -06001937 if (S_ISREG(file_inode(req->file)->i_mode))
1938 req->flags |= REQ_F_ISREG;
1939
Jens Axboe2b188cc2019-01-07 10:46:33 -07001940 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001941 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1942 req->flags |= REQ_F_CUR_POS;
1943 kiocb->ki_pos = req->file->f_pos;
1944 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001945 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001946 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1947 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1948 if (unlikely(ret))
1949 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001950
1951 ioprio = READ_ONCE(sqe->ioprio);
1952 if (ioprio) {
1953 ret = ioprio_check_cap(ioprio);
1954 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001955 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956
1957 kiocb->ki_ioprio = ioprio;
1958 } else
1959 kiocb->ki_ioprio = get_current_ioprio();
1960
Stefan Bühler8449eed2019-04-27 20:34:19 +02001961 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001962 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1963 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001964 req->flags |= REQ_F_NOWAIT;
1965
1966 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001968
Jens Axboedef596e2019-01-09 08:59:42 -07001969 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001970 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1971 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001972 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001973
Jens Axboedef596e2019-01-09 08:59:42 -07001974 kiocb->ki_flags |= IOCB_HIPRI;
1975 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001976 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001977 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001978 if (kiocb->ki_flags & IOCB_HIPRI)
1979 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001980 kiocb->ki_complete = io_complete_rw;
1981 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001982
Jens Axboe3529d8c2019-12-19 18:24:38 -07001983 req->rw.addr = READ_ONCE(sqe->addr);
1984 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001985 /* we own ->private, reuse it for the buffer index */
1986 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001987 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001988 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001989}
1990
1991static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1992{
1993 switch (ret) {
1994 case -EIOCBQUEUED:
1995 break;
1996 case -ERESTARTSYS:
1997 case -ERESTARTNOINTR:
1998 case -ERESTARTNOHAND:
1999 case -ERESTART_RESTARTBLOCK:
2000 /*
2001 * We can't just restart the syscall, since previously
2002 * submitted sqes may already be in progress. Just fail this
2003 * IO with EINTR.
2004 */
2005 ret = -EINTR;
2006 /* fall through */
2007 default:
2008 kiocb->ki_complete(kiocb, ret, 0);
2009 }
2010}
2011
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002012static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt)
Jens Axboeba816ad2019-09-28 11:36:45 -06002013{
Jens Axboeba042912019-12-25 16:33:42 -07002014 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2015
2016 if (req->flags & REQ_F_CUR_POS)
2017 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002018 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06002019 *nxt = __io_complete_rw(kiocb, ret);
2020 else
2021 io_rw_done(kiocb, ret);
2022}
2023
Jens Axboe9adbd452019-12-20 08:45:55 -07002024static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002025 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002026{
Jens Axboe9adbd452019-12-20 08:45:55 -07002027 struct io_ring_ctx *ctx = req->ctx;
2028 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002029 struct io_mapped_ubuf *imu;
2030 unsigned index, buf_index;
2031 size_t offset;
2032 u64 buf_addr;
2033
2034 /* attempt to use fixed buffers without having provided iovecs */
2035 if (unlikely(!ctx->user_bufs))
2036 return -EFAULT;
2037
Jens Axboe9adbd452019-12-20 08:45:55 -07002038 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002039 if (unlikely(buf_index >= ctx->nr_user_bufs))
2040 return -EFAULT;
2041
2042 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2043 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002044 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002045
2046 /* overflow */
2047 if (buf_addr + len < buf_addr)
2048 return -EFAULT;
2049 /* not inside the mapped region */
2050 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2051 return -EFAULT;
2052
2053 /*
2054 * May not be a start of buffer, set size appropriately
2055 * and advance us to the beginning.
2056 */
2057 offset = buf_addr - imu->ubuf;
2058 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002059
2060 if (offset) {
2061 /*
2062 * Don't use iov_iter_advance() here, as it's really slow for
2063 * using the latter parts of a big fixed buffer - it iterates
2064 * over each segment manually. We can cheat a bit here, because
2065 * we know that:
2066 *
2067 * 1) it's a BVEC iter, we set it up
2068 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2069 * first and last bvec
2070 *
2071 * So just find our index, and adjust the iterator afterwards.
2072 * If the offset is within the first bvec (or the whole first
2073 * bvec, just use iov_iter_advance(). This makes it easier
2074 * since we can just skip the first segment, which may not
2075 * be PAGE_SIZE aligned.
2076 */
2077 const struct bio_vec *bvec = imu->bvec;
2078
2079 if (offset <= bvec->bv_len) {
2080 iov_iter_advance(iter, offset);
2081 } else {
2082 unsigned long seg_skip;
2083
2084 /* skip first vec */
2085 offset -= bvec->bv_len;
2086 seg_skip = 1 + (offset >> PAGE_SHIFT);
2087
2088 iter->bvec = bvec + seg_skip;
2089 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002090 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002091 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002092 }
2093 }
2094
Jens Axboe5e559562019-11-13 16:12:46 -07002095 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002096}
2097
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002098static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2099 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002100{
Jens Axboe9adbd452019-12-20 08:45:55 -07002101 void __user *buf = u64_to_user_ptr(req->rw.addr);
2102 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002103 u8 opcode;
2104
Jens Axboed625c6e2019-12-17 19:53:05 -07002105 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002106 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002107 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002108 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002109 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002110
Jens Axboe9adbd452019-12-20 08:45:55 -07002111 /* buffer index only valid with fixed read/write */
2112 if (req->rw.kiocb.private)
2113 return -EINVAL;
2114
Jens Axboe3a6820f2019-12-22 15:19:35 -07002115 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2116 ssize_t ret;
2117 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2118 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002119 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002120 }
2121
Jens Axboef67676d2019-12-02 11:03:47 -07002122 if (req->io) {
2123 struct io_async_rw *iorw = &req->io->rw;
2124
2125 *iovec = iorw->iov;
2126 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2127 if (iorw->iov == iorw->fast_iov)
2128 *iovec = NULL;
2129 return iorw->size;
2130 }
2131
Jens Axboe2b188cc2019-01-07 10:46:33 -07002132#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002133 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002134 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2135 iovec, iter);
2136#endif
2137
2138 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2139}
2140
Jens Axboe32960612019-09-23 11:05:34 -06002141/*
2142 * For files that don't have ->read_iter() and ->write_iter(), handle them
2143 * by looping over ->read() or ->write() manually.
2144 */
2145static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2146 struct iov_iter *iter)
2147{
2148 ssize_t ret = 0;
2149
2150 /*
2151 * Don't support polled IO through this interface, and we can't
2152 * support non-blocking either. For the latter, this just causes
2153 * the kiocb to be handled from an async context.
2154 */
2155 if (kiocb->ki_flags & IOCB_HIPRI)
2156 return -EOPNOTSUPP;
2157 if (kiocb->ki_flags & IOCB_NOWAIT)
2158 return -EAGAIN;
2159
2160 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002161 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002162 ssize_t nr;
2163
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002164 if (!iov_iter_is_bvec(iter)) {
2165 iovec = iov_iter_iovec(iter);
2166 } else {
2167 /* fixed buffers import bvec */
2168 iovec.iov_base = kmap(iter->bvec->bv_page)
2169 + iter->iov_offset;
2170 iovec.iov_len = min(iter->count,
2171 iter->bvec->bv_len - iter->iov_offset);
2172 }
2173
Jens Axboe32960612019-09-23 11:05:34 -06002174 if (rw == READ) {
2175 nr = file->f_op->read(file, iovec.iov_base,
2176 iovec.iov_len, &kiocb->ki_pos);
2177 } else {
2178 nr = file->f_op->write(file, iovec.iov_base,
2179 iovec.iov_len, &kiocb->ki_pos);
2180 }
2181
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002182 if (iov_iter_is_bvec(iter))
2183 kunmap(iter->bvec->bv_page);
2184
Jens Axboe32960612019-09-23 11:05:34 -06002185 if (nr < 0) {
2186 if (!ret)
2187 ret = nr;
2188 break;
2189 }
2190 ret += nr;
2191 if (nr != iovec.iov_len)
2192 break;
2193 iov_iter_advance(iter, nr);
2194 }
2195
2196 return ret;
2197}
2198
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002199static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002200 struct iovec *iovec, struct iovec *fast_iov,
2201 struct iov_iter *iter)
2202{
2203 req->io->rw.nr_segs = iter->nr_segs;
2204 req->io->rw.size = io_size;
2205 req->io->rw.iov = iovec;
2206 if (!req->io->rw.iov) {
2207 req->io->rw.iov = req->io->rw.fast_iov;
2208 memcpy(req->io->rw.iov, fast_iov,
2209 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002210 } else {
2211 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002212 }
2213}
2214
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002215static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002216{
Jens Axboed3656342019-12-18 09:50:26 -07002217 if (!io_op_defs[req->opcode].async_ctx)
2218 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002219 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002220 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002221}
2222
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002223static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2224 struct iovec *iovec, struct iovec *fast_iov,
2225 struct iov_iter *iter)
2226{
Jens Axboe980ad262020-01-24 23:08:54 -07002227 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002228 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002229 if (!req->io) {
2230 if (io_alloc_async_ctx(req))
2231 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002232
Jens Axboe5d204bc2020-01-31 12:06:52 -07002233 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2234 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002235 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002236}
2237
Jens Axboe3529d8c2019-12-19 18:24:38 -07002238static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2239 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002240{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002241 struct io_async_ctx *io;
2242 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002243 ssize_t ret;
2244
Jens Axboe3529d8c2019-12-19 18:24:38 -07002245 ret = io_prep_rw(req, sqe, force_nonblock);
2246 if (ret)
2247 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002248
Jens Axboe3529d8c2019-12-19 18:24:38 -07002249 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2250 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002251
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002252 /* either don't need iovec imported or already have it */
2253 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002254 return 0;
2255
2256 io = req->io;
2257 io->rw.iov = io->rw.fast_iov;
2258 req->io = NULL;
2259 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2260 req->io = io;
2261 if (ret < 0)
2262 return ret;
2263
2264 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2265 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002266}
2267
Pavel Begunkov267bc902019-11-07 01:41:08 +03002268static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002269 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002270{
2271 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002272 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002273 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002274 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002275 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002276
Jens Axboe3529d8c2019-12-19 18:24:38 -07002277 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002278 if (ret < 0)
2279 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002280
Jens Axboefd6c2e42019-12-18 12:19:41 -07002281 /* Ensure we clear previously set non-block flag */
2282 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002283 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002284
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002285 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002286 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002287 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002288 req->result = io_size;
2289
2290 /*
2291 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2292 * we know to async punt it even if it was opened O_NONBLOCK
2293 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002294 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002295 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002296
Jens Axboe31b51512019-01-18 22:56:34 -07002297 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002298 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002299 if (!ret) {
2300 ssize_t ret2;
2301
Jens Axboe9adbd452019-12-20 08:45:55 -07002302 if (req->file->f_op->read_iter)
2303 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002304 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002305 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002306
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002307 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002308 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002309 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002310 } else {
2311copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002312 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002313 inline_vecs, &iter);
2314 if (ret)
2315 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002316 /* any defer here is final, must blocking retry */
2317 if (!(req->flags & REQ_F_NOWAIT))
2318 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002319 return -EAGAIN;
2320 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321 }
Jens Axboef67676d2019-12-02 11:03:47 -07002322out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002323 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002324 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002325 return ret;
2326}
2327
Jens Axboe3529d8c2019-12-19 18:24:38 -07002328static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2329 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002330{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002331 struct io_async_ctx *io;
2332 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002333 ssize_t ret;
2334
Jens Axboe3529d8c2019-12-19 18:24:38 -07002335 ret = io_prep_rw(req, sqe, force_nonblock);
2336 if (ret)
2337 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002338
Jens Axboe3529d8c2019-12-19 18:24:38 -07002339 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2340 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002341
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002342 /* either don't need iovec imported or already have it */
2343 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002344 return 0;
2345
2346 io = req->io;
2347 io->rw.iov = io->rw.fast_iov;
2348 req->io = NULL;
2349 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2350 req->io = io;
2351 if (ret < 0)
2352 return ret;
2353
2354 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2355 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002356}
2357
Pavel Begunkov267bc902019-11-07 01:41:08 +03002358static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002359 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360{
2361 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002362 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002363 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002364 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002365 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002366
Jens Axboe3529d8c2019-12-19 18:24:38 -07002367 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002368 if (ret < 0)
2369 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002370
Jens Axboefd6c2e42019-12-18 12:19:41 -07002371 /* Ensure we clear previously set non-block flag */
2372 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002373 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002374
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002375 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002376 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002377 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002378 req->result = io_size;
2379
2380 /*
2381 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2382 * we know to async punt it even if it was opened O_NONBLOCK
2383 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002384 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002385 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002386
Jens Axboe10d59342019-12-09 20:16:22 -07002387 /* file path doesn't support NOWAIT for non-direct_IO */
2388 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2389 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002390 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002391
Jens Axboe31b51512019-01-18 22:56:34 -07002392 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002393 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002394 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002395 ssize_t ret2;
2396
Jens Axboe2b188cc2019-01-07 10:46:33 -07002397 /*
2398 * Open-code file_start_write here to grab freeze protection,
2399 * which will be released by another thread in
2400 * io_complete_rw(). Fool lockdep by telling it the lock got
2401 * released so that it doesn't complain about the held lock when
2402 * we return to userspace.
2403 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002404 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002405 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002406 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002407 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002408 SB_FREEZE_WRITE);
2409 }
2410 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002411
Jens Axboe9adbd452019-12-20 08:45:55 -07002412 if (req->file->f_op->write_iter)
2413 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002414 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002415 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002416 /*
2417 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2418 * retry them without IOCB_NOWAIT.
2419 */
2420 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2421 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002422 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002423 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002424 } else {
2425copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002426 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002427 inline_vecs, &iter);
2428 if (ret)
2429 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002430 /* any defer here is final, must blocking retry */
2431 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002432 return -EAGAIN;
2433 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002434 }
Jens Axboe31b51512019-01-18 22:56:34 -07002435out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002436 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002437 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002438 return ret;
2439}
2440
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002441static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2442{
2443 struct io_splice* sp = &req->splice;
2444 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2445 int ret;
2446
2447 if (req->flags & REQ_F_NEED_CLEANUP)
2448 return 0;
2449
2450 sp->file_in = NULL;
2451 sp->off_in = READ_ONCE(sqe->splice_off_in);
2452 sp->off_out = READ_ONCE(sqe->off);
2453 sp->len = READ_ONCE(sqe->len);
2454 sp->flags = READ_ONCE(sqe->splice_flags);
2455
2456 if (unlikely(sp->flags & ~valid_flags))
2457 return -EINVAL;
2458
2459 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2460 (sp->flags & SPLICE_F_FD_IN_FIXED));
2461 if (ret)
2462 return ret;
2463 req->flags |= REQ_F_NEED_CLEANUP;
2464
2465 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2466 req->work.flags |= IO_WQ_WORK_UNBOUND;
2467
2468 return 0;
2469}
2470
2471static bool io_splice_punt(struct file *file)
2472{
2473 if (get_pipe_info(file))
2474 return false;
2475 if (!io_file_supports_async(file))
2476 return true;
2477 return !(file->f_mode & O_NONBLOCK);
2478}
2479
2480static int io_splice(struct io_kiocb *req, struct io_kiocb **nxt,
2481 bool force_nonblock)
2482{
2483 struct io_splice *sp = &req->splice;
2484 struct file *in = sp->file_in;
2485 struct file *out = sp->file_out;
2486 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2487 loff_t *poff_in, *poff_out;
2488 long ret;
2489
2490 if (force_nonblock) {
2491 if (io_splice_punt(in) || io_splice_punt(out))
2492 return -EAGAIN;
2493 flags |= SPLICE_F_NONBLOCK;
2494 }
2495
2496 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2497 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2498 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2499 if (force_nonblock && ret == -EAGAIN)
2500 return -EAGAIN;
2501
2502 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2503 req->flags &= ~REQ_F_NEED_CLEANUP;
2504
2505 io_cqring_add_event(req, ret);
2506 if (ret != sp->len)
2507 req_set_fail_links(req);
2508 io_put_req_find_next(req, nxt);
2509 return 0;
2510}
2511
Jens Axboe2b188cc2019-01-07 10:46:33 -07002512/*
2513 * IORING_OP_NOP just posts a completion event, nothing else.
2514 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002515static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002516{
2517 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002518
Jens Axboedef596e2019-01-09 08:59:42 -07002519 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2520 return -EINVAL;
2521
Jens Axboe78e19bb2019-11-06 15:21:34 -07002522 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002523 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002524 return 0;
2525}
2526
Jens Axboe3529d8c2019-12-19 18:24:38 -07002527static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002528{
Jens Axboe6b063142019-01-10 22:13:58 -07002529 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002530
Jens Axboe09bb8392019-03-13 12:39:28 -06002531 if (!req->file)
2532 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002533
Jens Axboe6b063142019-01-10 22:13:58 -07002534 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002535 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002536 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002537 return -EINVAL;
2538
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002539 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2540 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2541 return -EINVAL;
2542
2543 req->sync.off = READ_ONCE(sqe->off);
2544 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002545 return 0;
2546}
2547
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002548static bool io_req_cancelled(struct io_kiocb *req)
2549{
2550 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2551 req_set_fail_links(req);
2552 io_cqring_add_event(req, -ECANCELED);
2553 io_put_req(req);
2554 return true;
2555 }
2556
2557 return false;
2558}
2559
Jens Axboe78912932020-01-14 22:09:06 -07002560static void io_link_work_cb(struct io_wq_work **workptr)
2561{
2562 struct io_wq_work *work = *workptr;
2563 struct io_kiocb *link = work->data;
2564
2565 io_queue_linked_timeout(link);
Pavel Begunkov5eae8612020-02-28 10:36:38 +03002566 io_wq_submit_work(workptr);
Jens Axboe78912932020-01-14 22:09:06 -07002567}
2568
2569static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2570{
2571 struct io_kiocb *link;
2572
Jens Axboe78912932020-01-14 22:09:06 -07002573 *workptr = &nxt->work;
Pavel Begunkov3b17cf52020-02-29 22:56:10 +03002574 link = io_prep_linked_timeout(nxt);
Jens Axboe78912932020-01-14 22:09:06 -07002575 if (link) {
Jens Axboe78912932020-01-14 22:09:06 -07002576 nxt->work.func = io_link_work_cb;
2577 nxt->work.data = link;
2578 }
2579}
2580
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002581static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002582{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002583 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002584 int ret;
2585
Jens Axboe9adbd452019-12-20 08:45:55 -07002586 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002587 end > 0 ? end : LLONG_MAX,
2588 req->sync.flags & IORING_FSYNC_DATASYNC);
2589 if (ret < 0)
2590 req_set_fail_links(req);
2591 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002592 io_put_req_find_next(req, nxt);
2593}
2594
2595static void io_fsync_finish(struct io_wq_work **workptr)
2596{
2597 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2598 struct io_kiocb *nxt = NULL;
2599
2600 if (io_req_cancelled(req))
2601 return;
2602 __io_fsync(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002603 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002604 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002605}
2606
Jens Axboefc4df992019-12-10 14:38:45 -07002607static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2608 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002609{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002610 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002611 if (force_nonblock) {
2612 io_put_req(req);
2613 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002614 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002615 }
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002616 __io_fsync(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002617 return 0;
2618}
2619
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002620static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboed63d1b52019-12-10 10:38:56 -07002621{
Jens Axboed63d1b52019-12-10 10:38:56 -07002622 int ret;
2623
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002624 if (io_req_cancelled(req))
2625 return;
2626
Jens Axboed63d1b52019-12-10 10:38:56 -07002627 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2628 req->sync.len);
2629 if (ret < 0)
2630 req_set_fail_links(req);
2631 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002632 io_put_req_find_next(req, nxt);
2633}
2634
2635static void io_fallocate_finish(struct io_wq_work **workptr)
2636{
2637 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2638 struct io_kiocb *nxt = NULL;
2639
2640 __io_fallocate(req, &nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002641 if (nxt)
2642 io_wq_assign_next(workptr, nxt);
2643}
2644
2645static int io_fallocate_prep(struct io_kiocb *req,
2646 const struct io_uring_sqe *sqe)
2647{
2648 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2649 return -EINVAL;
2650
2651 req->sync.off = READ_ONCE(sqe->off);
2652 req->sync.len = READ_ONCE(sqe->addr);
2653 req->sync.mode = READ_ONCE(sqe->len);
2654 return 0;
2655}
2656
2657static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2658 bool force_nonblock)
2659{
Jens Axboed63d1b52019-12-10 10:38:56 -07002660 /* fallocate always requiring blocking context */
2661 if (force_nonblock) {
2662 io_put_req(req);
2663 req->work.func = io_fallocate_finish;
2664 return -EAGAIN;
2665 }
2666
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002667 __io_fallocate(req, nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002668 return 0;
2669}
2670
Jens Axboe15b71ab2019-12-11 11:20:36 -07002671static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2672{
Jens Axboef8748882020-01-08 17:47:02 -07002673 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002674 int ret;
2675
2676 if (sqe->ioprio || sqe->buf_index)
2677 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002678 if (sqe->flags & IOSQE_FIXED_FILE)
2679 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002680 if (req->flags & REQ_F_NEED_CLEANUP)
2681 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002682
2683 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002684 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002685 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002686 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002687
Jens Axboef8748882020-01-08 17:47:02 -07002688 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002689 if (IS_ERR(req->open.filename)) {
2690 ret = PTR_ERR(req->open.filename);
2691 req->open.filename = NULL;
2692 return ret;
2693 }
2694
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002695 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002696 return 0;
2697}
2698
Jens Axboecebdb982020-01-08 17:59:24 -07002699static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2700{
2701 struct open_how __user *how;
2702 const char __user *fname;
2703 size_t len;
2704 int ret;
2705
2706 if (sqe->ioprio || sqe->buf_index)
2707 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002708 if (sqe->flags & IOSQE_FIXED_FILE)
2709 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002710 if (req->flags & REQ_F_NEED_CLEANUP)
2711 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002712
2713 req->open.dfd = READ_ONCE(sqe->fd);
2714 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2715 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2716 len = READ_ONCE(sqe->len);
2717
2718 if (len < OPEN_HOW_SIZE_VER0)
2719 return -EINVAL;
2720
2721 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2722 len);
2723 if (ret)
2724 return ret;
2725
2726 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2727 req->open.how.flags |= O_LARGEFILE;
2728
2729 req->open.filename = getname(fname);
2730 if (IS_ERR(req->open.filename)) {
2731 ret = PTR_ERR(req->open.filename);
2732 req->open.filename = NULL;
2733 return ret;
2734 }
2735
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002736 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002737 return 0;
2738}
2739
2740static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2741 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002742{
2743 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002744 struct file *file;
2745 int ret;
2746
Jens Axboef86cd202020-01-29 13:46:44 -07002747 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002748 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002749
Jens Axboecebdb982020-01-08 17:59:24 -07002750 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002751 if (ret)
2752 goto err;
2753
Jens Axboecebdb982020-01-08 17:59:24 -07002754 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002755 if (ret < 0)
2756 goto err;
2757
2758 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2759 if (IS_ERR(file)) {
2760 put_unused_fd(ret);
2761 ret = PTR_ERR(file);
2762 } else {
2763 fsnotify_open(file);
2764 fd_install(ret, file);
2765 }
2766err:
2767 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002768 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002769 if (ret < 0)
2770 req_set_fail_links(req);
2771 io_cqring_add_event(req, ret);
2772 io_put_req_find_next(req, nxt);
2773 return 0;
2774}
2775
Jens Axboecebdb982020-01-08 17:59:24 -07002776static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2777 bool force_nonblock)
2778{
2779 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2780 return io_openat2(req, nxt, force_nonblock);
2781}
2782
Jens Axboe3e4827b2020-01-08 15:18:09 -07002783static int io_epoll_ctl_prep(struct io_kiocb *req,
2784 const struct io_uring_sqe *sqe)
2785{
2786#if defined(CONFIG_EPOLL)
2787 if (sqe->ioprio || sqe->buf_index)
2788 return -EINVAL;
2789
2790 req->epoll.epfd = READ_ONCE(sqe->fd);
2791 req->epoll.op = READ_ONCE(sqe->len);
2792 req->epoll.fd = READ_ONCE(sqe->off);
2793
2794 if (ep_op_has_event(req->epoll.op)) {
2795 struct epoll_event __user *ev;
2796
2797 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2798 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2799 return -EFAULT;
2800 }
2801
2802 return 0;
2803#else
2804 return -EOPNOTSUPP;
2805#endif
2806}
2807
2808static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2809 bool force_nonblock)
2810{
2811#if defined(CONFIG_EPOLL)
2812 struct io_epoll *ie = &req->epoll;
2813 int ret;
2814
2815 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2816 if (force_nonblock && ret == -EAGAIN)
2817 return -EAGAIN;
2818
2819 if (ret < 0)
2820 req_set_fail_links(req);
2821 io_cqring_add_event(req, ret);
2822 io_put_req_find_next(req, nxt);
2823 return 0;
2824#else
2825 return -EOPNOTSUPP;
2826#endif
2827}
2828
Jens Axboec1ca7572019-12-25 22:18:28 -07002829static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2830{
2831#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2832 if (sqe->ioprio || sqe->buf_index || sqe->off)
2833 return -EINVAL;
2834
2835 req->madvise.addr = READ_ONCE(sqe->addr);
2836 req->madvise.len = READ_ONCE(sqe->len);
2837 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2838 return 0;
2839#else
2840 return -EOPNOTSUPP;
2841#endif
2842}
2843
2844static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2845 bool force_nonblock)
2846{
2847#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2848 struct io_madvise *ma = &req->madvise;
2849 int ret;
2850
2851 if (force_nonblock)
2852 return -EAGAIN;
2853
2854 ret = do_madvise(ma->addr, ma->len, ma->advice);
2855 if (ret < 0)
2856 req_set_fail_links(req);
2857 io_cqring_add_event(req, ret);
2858 io_put_req_find_next(req, nxt);
2859 return 0;
2860#else
2861 return -EOPNOTSUPP;
2862#endif
2863}
2864
Jens Axboe4840e412019-12-25 22:03:45 -07002865static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2866{
2867 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2868 return -EINVAL;
2869
2870 req->fadvise.offset = READ_ONCE(sqe->off);
2871 req->fadvise.len = READ_ONCE(sqe->len);
2872 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2873 return 0;
2874}
2875
2876static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2877 bool force_nonblock)
2878{
2879 struct io_fadvise *fa = &req->fadvise;
2880 int ret;
2881
Jens Axboe3e694262020-02-01 09:22:49 -07002882 if (force_nonblock) {
2883 switch (fa->advice) {
2884 case POSIX_FADV_NORMAL:
2885 case POSIX_FADV_RANDOM:
2886 case POSIX_FADV_SEQUENTIAL:
2887 break;
2888 default:
2889 return -EAGAIN;
2890 }
2891 }
Jens Axboe4840e412019-12-25 22:03:45 -07002892
2893 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2894 if (ret < 0)
2895 req_set_fail_links(req);
2896 io_cqring_add_event(req, ret);
2897 io_put_req_find_next(req, nxt);
2898 return 0;
2899}
2900
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002901static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2902{
Jens Axboef8748882020-01-08 17:47:02 -07002903 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002904 unsigned lookup_flags;
2905 int ret;
2906
2907 if (sqe->ioprio || sqe->buf_index)
2908 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002909 if (sqe->flags & IOSQE_FIXED_FILE)
2910 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002911 if (req->flags & REQ_F_NEED_CLEANUP)
2912 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002913
2914 req->open.dfd = READ_ONCE(sqe->fd);
2915 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002916 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002917 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002918 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002919
Jens Axboec12cedf2020-01-08 17:41:21 -07002920 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002921 return -EINVAL;
2922
Jens Axboef8748882020-01-08 17:47:02 -07002923 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002924 if (IS_ERR(req->open.filename)) {
2925 ret = PTR_ERR(req->open.filename);
2926 req->open.filename = NULL;
2927 return ret;
2928 }
2929
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002930 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002931 return 0;
2932}
2933
2934static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2935 bool force_nonblock)
2936{
2937 struct io_open *ctx = &req->open;
2938 unsigned lookup_flags;
2939 struct path path;
2940 struct kstat stat;
2941 int ret;
2942
2943 if (force_nonblock)
2944 return -EAGAIN;
2945
Jens Axboec12cedf2020-01-08 17:41:21 -07002946 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002947 return -EINVAL;
2948
2949retry:
2950 /* filename_lookup() drops it, keep a reference */
2951 ctx->filename->refcnt++;
2952
2953 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2954 NULL);
2955 if (ret)
2956 goto err;
2957
Jens Axboec12cedf2020-01-08 17:41:21 -07002958 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002959 path_put(&path);
2960 if (retry_estale(ret, lookup_flags)) {
2961 lookup_flags |= LOOKUP_REVAL;
2962 goto retry;
2963 }
2964 if (!ret)
2965 ret = cp_statx(&stat, ctx->buffer);
2966err:
2967 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002968 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002969 if (ret < 0)
2970 req_set_fail_links(req);
2971 io_cqring_add_event(req, ret);
2972 io_put_req_find_next(req, nxt);
2973 return 0;
2974}
2975
Jens Axboeb5dba592019-12-11 14:02:38 -07002976static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2977{
2978 /*
2979 * If we queue this for async, it must not be cancellable. That would
2980 * leave the 'file' in an undeterminate state.
2981 */
2982 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2983
2984 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2985 sqe->rw_flags || sqe->buf_index)
2986 return -EINVAL;
2987 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002988 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002989
2990 req->close.fd = READ_ONCE(sqe->fd);
2991 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002992 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002993 return -EBADF;
2994
2995 return 0;
2996}
2997
Pavel Begunkova93b3332020-02-08 14:04:34 +03002998/* only called when __close_fd_get_file() is done */
2999static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
3000{
3001 int ret;
3002
3003 ret = filp_close(req->close.put_file, req->work.files);
3004 if (ret < 0)
3005 req_set_fail_links(req);
3006 io_cqring_add_event(req, ret);
3007 fput(req->close.put_file);
3008 io_put_req_find_next(req, nxt);
3009}
3010
Jens Axboeb5dba592019-12-11 14:02:38 -07003011static void io_close_finish(struct io_wq_work **workptr)
3012{
3013 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3014 struct io_kiocb *nxt = NULL;
3015
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003016 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03003017 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07003018 if (nxt)
3019 io_wq_assign_next(workptr, nxt);
3020}
3021
3022static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
3023 bool force_nonblock)
3024{
3025 int ret;
3026
3027 req->close.put_file = NULL;
3028 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3029 if (ret < 0)
3030 return ret;
3031
3032 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07003033 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07003034 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07003035
3036 /*
3037 * No ->flush(), safely close from here and just punt the
3038 * fput() to async context.
3039 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03003040 __io_close_finish(req, nxt);
3041 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003042eagain:
3043 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07003044 /*
3045 * Do manual async queue here to avoid grabbing files - we don't
3046 * need the files, and it'll cause io_close_finish() to close
3047 * the file again and cause a double CQE entry for this request
3048 */
3049 io_queue_async_work(req);
3050 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003051}
3052
Jens Axboe3529d8c2019-12-19 18:24:38 -07003053static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003054{
3055 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003056
3057 if (!req->file)
3058 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003059
3060 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3061 return -EINVAL;
3062 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3063 return -EINVAL;
3064
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003065 req->sync.off = READ_ONCE(sqe->off);
3066 req->sync.len = READ_ONCE(sqe->len);
3067 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003068 return 0;
3069}
3070
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003071static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003072{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003073 int ret;
3074
Jens Axboe9adbd452019-12-20 08:45:55 -07003075 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003076 req->sync.flags);
3077 if (ret < 0)
3078 req_set_fail_links(req);
3079 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003080 io_put_req_find_next(req, nxt);
3081}
3082
3083
3084static void io_sync_file_range_finish(struct io_wq_work **workptr)
3085{
3086 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3087 struct io_kiocb *nxt = NULL;
3088
3089 if (io_req_cancelled(req))
3090 return;
3091 __io_sync_file_range(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003092 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003093 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003094}
3095
Jens Axboefc4df992019-12-10 14:38:45 -07003096static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003097 bool force_nonblock)
3098{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003099 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003100 if (force_nonblock) {
3101 io_put_req(req);
3102 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003103 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003104 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003105
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003106 __io_sync_file_range(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003107 return 0;
3108}
3109
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003110static int io_setup_async_msg(struct io_kiocb *req,
3111 struct io_async_msghdr *kmsg)
3112{
3113 if (req->io)
3114 return -EAGAIN;
3115 if (io_alloc_async_ctx(req)) {
3116 if (kmsg->iov != kmsg->fast_iov)
3117 kfree(kmsg->iov);
3118 return -ENOMEM;
3119 }
3120 req->flags |= REQ_F_NEED_CLEANUP;
3121 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3122 return -EAGAIN;
3123}
3124
Jens Axboe3529d8c2019-12-19 18:24:38 -07003125static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003126{
Jens Axboe03b12302019-12-02 18:50:25 -07003127#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003128 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003129 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003130 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003131
Jens Axboee47293f2019-12-20 08:58:21 -07003132 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3133 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003134 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003135
Jens Axboed8768362020-02-27 14:17:49 -07003136#ifdef CONFIG_COMPAT
3137 if (req->ctx->compat)
3138 sr->msg_flags |= MSG_CMSG_COMPAT;
3139#endif
3140
Jens Axboefddafac2020-01-04 20:19:44 -07003141 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003142 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003143 /* iovec is already imported */
3144 if (req->flags & REQ_F_NEED_CLEANUP)
3145 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003146
Jens Axboed9688562019-12-09 19:35:20 -07003147 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003148 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003149 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003150 if (!ret)
3151 req->flags |= REQ_F_NEED_CLEANUP;
3152 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003153#else
Jens Axboee47293f2019-12-20 08:58:21 -07003154 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003155#endif
3156}
3157
Jens Axboefc4df992019-12-10 14:38:45 -07003158static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3159 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003160{
3161#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003162 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003163 struct socket *sock;
3164 int ret;
3165
3166 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3167 return -EINVAL;
3168
3169 sock = sock_from_file(req->file, &ret);
3170 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003171 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003172 unsigned flags;
3173
Jens Axboe03b12302019-12-02 18:50:25 -07003174 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003175 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003176 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003177 /* if iov is set, it's allocated already */
3178 if (!kmsg->iov)
3179 kmsg->iov = kmsg->fast_iov;
3180 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003181 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003182 struct io_sr_msg *sr = &req->sr_msg;
3183
Jens Axboe0b416c32019-12-15 10:57:46 -07003184 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003185 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003186
3187 io.msg.iov = io.msg.fast_iov;
3188 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3189 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003190 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003191 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003192 }
3193
Jens Axboee47293f2019-12-20 08:58:21 -07003194 flags = req->sr_msg.msg_flags;
3195 if (flags & MSG_DONTWAIT)
3196 req->flags |= REQ_F_NOWAIT;
3197 else if (force_nonblock)
3198 flags |= MSG_DONTWAIT;
3199
Jens Axboe0b416c32019-12-15 10:57:46 -07003200 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003201 if (force_nonblock && ret == -EAGAIN)
3202 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003203 if (ret == -ERESTARTSYS)
3204 ret = -EINTR;
3205 }
3206
Pavel Begunkov1e950812020-02-06 19:51:16 +03003207 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003208 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003209 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003210 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003211 if (ret < 0)
3212 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003213 io_put_req_find_next(req, nxt);
3214 return 0;
3215#else
3216 return -EOPNOTSUPP;
3217#endif
3218}
3219
Jens Axboefddafac2020-01-04 20:19:44 -07003220static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3221 bool force_nonblock)
3222{
3223#if defined(CONFIG_NET)
3224 struct socket *sock;
3225 int ret;
3226
3227 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3228 return -EINVAL;
3229
3230 sock = sock_from_file(req->file, &ret);
3231 if (sock) {
3232 struct io_sr_msg *sr = &req->sr_msg;
3233 struct msghdr msg;
3234 struct iovec iov;
3235 unsigned flags;
3236
3237 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3238 &msg.msg_iter);
3239 if (ret)
3240 return ret;
3241
3242 msg.msg_name = NULL;
3243 msg.msg_control = NULL;
3244 msg.msg_controllen = 0;
3245 msg.msg_namelen = 0;
3246
3247 flags = req->sr_msg.msg_flags;
3248 if (flags & MSG_DONTWAIT)
3249 req->flags |= REQ_F_NOWAIT;
3250 else if (force_nonblock)
3251 flags |= MSG_DONTWAIT;
3252
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003253 msg.msg_flags = flags;
3254 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003255 if (force_nonblock && ret == -EAGAIN)
3256 return -EAGAIN;
3257 if (ret == -ERESTARTSYS)
3258 ret = -EINTR;
3259 }
3260
3261 io_cqring_add_event(req, ret);
3262 if (ret < 0)
3263 req_set_fail_links(req);
3264 io_put_req_find_next(req, nxt);
3265 return 0;
3266#else
3267 return -EOPNOTSUPP;
3268#endif
3269}
3270
Jens Axboe3529d8c2019-12-19 18:24:38 -07003271static int io_recvmsg_prep(struct io_kiocb *req,
3272 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003273{
3274#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003275 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003276 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003277 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003278
Jens Axboe3529d8c2019-12-19 18:24:38 -07003279 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3280 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003281 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003282
Jens Axboed8768362020-02-27 14:17:49 -07003283#ifdef CONFIG_COMPAT
3284 if (req->ctx->compat)
3285 sr->msg_flags |= MSG_CMSG_COMPAT;
3286#endif
3287
Jens Axboefddafac2020-01-04 20:19:44 -07003288 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003289 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003290 /* iovec is already imported */
3291 if (req->flags & REQ_F_NEED_CLEANUP)
3292 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003293
Jens Axboed9688562019-12-09 19:35:20 -07003294 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003295 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003296 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003297 if (!ret)
3298 req->flags |= REQ_F_NEED_CLEANUP;
3299 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003300#else
Jens Axboee47293f2019-12-20 08:58:21 -07003301 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003302#endif
3303}
3304
Jens Axboefc4df992019-12-10 14:38:45 -07003305static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3306 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003307{
3308#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003309 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003310 struct socket *sock;
3311 int ret;
3312
3313 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3314 return -EINVAL;
3315
3316 sock = sock_from_file(req->file, &ret);
3317 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003318 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003319 unsigned flags;
3320
Jens Axboe03b12302019-12-02 18:50:25 -07003321 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003322 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003323 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003324 /* if iov is set, it's allocated already */
3325 if (!kmsg->iov)
3326 kmsg->iov = kmsg->fast_iov;
3327 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003328 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003329 struct io_sr_msg *sr = &req->sr_msg;
3330
Jens Axboe0b416c32019-12-15 10:57:46 -07003331 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003332 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003333
3334 io.msg.iov = io.msg.fast_iov;
3335 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3336 sr->msg_flags, &io.msg.uaddr,
3337 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003338 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003339 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003340 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003341
Jens Axboee47293f2019-12-20 08:58:21 -07003342 flags = req->sr_msg.msg_flags;
3343 if (flags & MSG_DONTWAIT)
3344 req->flags |= REQ_F_NOWAIT;
3345 else if (force_nonblock)
3346 flags |= MSG_DONTWAIT;
3347
3348 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3349 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003350 if (force_nonblock && ret == -EAGAIN)
3351 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003352 if (ret == -ERESTARTSYS)
3353 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003354 }
3355
Pavel Begunkov1e950812020-02-06 19:51:16 +03003356 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003357 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003358 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003359 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003360 if (ret < 0)
3361 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003362 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003363 return 0;
3364#else
3365 return -EOPNOTSUPP;
3366#endif
3367}
3368
Jens Axboefddafac2020-01-04 20:19:44 -07003369static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3370 bool force_nonblock)
3371{
3372#if defined(CONFIG_NET)
3373 struct socket *sock;
3374 int ret;
3375
3376 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3377 return -EINVAL;
3378
3379 sock = sock_from_file(req->file, &ret);
3380 if (sock) {
3381 struct io_sr_msg *sr = &req->sr_msg;
3382 struct msghdr msg;
3383 struct iovec iov;
3384 unsigned flags;
3385
3386 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3387 &msg.msg_iter);
3388 if (ret)
3389 return ret;
3390
3391 msg.msg_name = NULL;
3392 msg.msg_control = NULL;
3393 msg.msg_controllen = 0;
3394 msg.msg_namelen = 0;
3395 msg.msg_iocb = NULL;
3396 msg.msg_flags = 0;
3397
3398 flags = req->sr_msg.msg_flags;
3399 if (flags & MSG_DONTWAIT)
3400 req->flags |= REQ_F_NOWAIT;
3401 else if (force_nonblock)
3402 flags |= MSG_DONTWAIT;
3403
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003404 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003405 if (force_nonblock && ret == -EAGAIN)
3406 return -EAGAIN;
3407 if (ret == -ERESTARTSYS)
3408 ret = -EINTR;
3409 }
3410
3411 io_cqring_add_event(req, ret);
3412 if (ret < 0)
3413 req_set_fail_links(req);
3414 io_put_req_find_next(req, nxt);
3415 return 0;
3416#else
3417 return -EOPNOTSUPP;
3418#endif
3419}
3420
3421
Jens Axboe3529d8c2019-12-19 18:24:38 -07003422static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003423{
3424#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003425 struct io_accept *accept = &req->accept;
3426
Jens Axboe17f2fe32019-10-17 14:42:58 -06003427 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3428 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003429 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003430 return -EINVAL;
3431
Jens Axboed55e5f52019-12-11 16:12:15 -07003432 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3433 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003434 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003435 return 0;
3436#else
3437 return -EOPNOTSUPP;
3438#endif
3439}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003440
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003441#if defined(CONFIG_NET)
3442static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3443 bool force_nonblock)
3444{
3445 struct io_accept *accept = &req->accept;
3446 unsigned file_flags;
3447 int ret;
3448
3449 file_flags = force_nonblock ? O_NONBLOCK : 0;
3450 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3451 accept->addr_len, accept->flags);
3452 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003453 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003454 if (ret == -ERESTARTSYS)
3455 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003456 if (ret < 0)
3457 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003458 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003459 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003460 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003461}
3462
3463static void io_accept_finish(struct io_wq_work **workptr)
3464{
3465 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3466 struct io_kiocb *nxt = NULL;
3467
Jens Axboee441d1c2020-02-20 09:59:02 -07003468 io_put_req(req);
3469
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003470 if (io_req_cancelled(req))
3471 return;
3472 __io_accept(req, &nxt, false);
3473 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003474 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003475}
3476#endif
3477
3478static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3479 bool force_nonblock)
3480{
3481#if defined(CONFIG_NET)
3482 int ret;
3483
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003484 ret = __io_accept(req, nxt, force_nonblock);
3485 if (ret == -EAGAIN && force_nonblock) {
3486 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003487 return -EAGAIN;
3488 }
3489 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003490#else
3491 return -EOPNOTSUPP;
3492#endif
3493}
3494
Jens Axboe3529d8c2019-12-19 18:24:38 -07003495static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003496{
3497#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003498 struct io_connect *conn = &req->connect;
3499 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003500
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003501 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3502 return -EINVAL;
3503 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3504 return -EINVAL;
3505
Jens Axboe3529d8c2019-12-19 18:24:38 -07003506 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3507 conn->addr_len = READ_ONCE(sqe->addr2);
3508
3509 if (!io)
3510 return 0;
3511
3512 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003513 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003514#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003515 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003516#endif
3517}
3518
Jens Axboefc4df992019-12-10 14:38:45 -07003519static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3520 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003521{
3522#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003523 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003524 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003525 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003526
Jens Axboef499a022019-12-02 16:28:46 -07003527 if (req->io) {
3528 io = req->io;
3529 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003530 ret = move_addr_to_kernel(req->connect.addr,
3531 req->connect.addr_len,
3532 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003533 if (ret)
3534 goto out;
3535 io = &__io;
3536 }
3537
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003538 file_flags = force_nonblock ? O_NONBLOCK : 0;
3539
3540 ret = __sys_connect_file(req->file, &io->connect.address,
3541 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003542 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003543 if (req->io)
3544 return -EAGAIN;
3545 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003546 ret = -ENOMEM;
3547 goto out;
3548 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003549 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003550 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003551 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003552 if (ret == -ERESTARTSYS)
3553 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003554out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003555 if (ret < 0)
3556 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003557 io_cqring_add_event(req, ret);
3558 io_put_req_find_next(req, nxt);
3559 return 0;
3560#else
3561 return -EOPNOTSUPP;
3562#endif
3563}
3564
Jens Axboed7718a92020-02-14 22:23:12 -07003565struct io_poll_table {
3566 struct poll_table_struct pt;
3567 struct io_kiocb *req;
3568 int error;
3569};
3570
3571static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
3572 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003573{
Jens Axboed7718a92020-02-14 22:23:12 -07003574 if (unlikely(poll->head)) {
3575 pt->error = -EINVAL;
3576 return;
3577 }
3578
3579 pt->error = 0;
3580 poll->head = head;
3581 add_wait_queue(head, &poll->wait);
3582}
3583
3584static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
3585 struct poll_table_struct *p)
3586{
3587 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3588
3589 __io_queue_proc(&pt->req->apoll->poll, pt, head);
3590}
3591
3592static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
3593 __poll_t mask, task_work_func_t func)
3594{
3595 struct task_struct *tsk;
3596
3597 /* for instances that support it check for an event match first: */
3598 if (mask && !(mask & poll->events))
3599 return 0;
3600
3601 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
3602
3603 list_del_init(&poll->wait.entry);
3604
3605 tsk = req->task;
3606 req->result = mask;
3607 init_task_work(&req->task_work, func);
3608 /*
3609 * If this fails, then the task is exiting. If that is the case, then
3610 * the exit check will ultimately cancel these work items. Hence we
3611 * don't need to check here and handle it specifically.
3612 */
3613 task_work_add(tsk, &req->task_work, true);
3614 wake_up_process(tsk);
3615 return 1;
3616}
3617
3618static void io_async_task_func(struct callback_head *cb)
3619{
3620 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3621 struct async_poll *apoll = req->apoll;
3622 struct io_ring_ctx *ctx = req->ctx;
3623
3624 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
3625
3626 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
3627
3628 if (hash_hashed(&req->hash_node)) {
3629 spin_lock_irq(&ctx->completion_lock);
3630 hash_del(&req->hash_node);
3631 spin_unlock_irq(&ctx->completion_lock);
3632 }
3633
3634 /* restore ->work in case we need to retry again */
3635 memcpy(&req->work, &apoll->work, sizeof(req->work));
3636
3637 __set_current_state(TASK_RUNNING);
3638 mutex_lock(&ctx->uring_lock);
3639 __io_queue_sqe(req, NULL);
3640 mutex_unlock(&ctx->uring_lock);
3641
3642 kfree(apoll);
3643}
3644
3645static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3646 void *key)
3647{
3648 struct io_kiocb *req = wait->private;
3649 struct io_poll_iocb *poll = &req->apoll->poll;
3650
3651 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
3652 key_to_poll(key));
3653
3654 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
3655}
3656
3657static void io_poll_req_insert(struct io_kiocb *req)
3658{
3659 struct io_ring_ctx *ctx = req->ctx;
3660 struct hlist_head *list;
3661
3662 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3663 hlist_add_head(&req->hash_node, list);
3664}
3665
3666static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
3667 struct io_poll_iocb *poll,
3668 struct io_poll_table *ipt, __poll_t mask,
3669 wait_queue_func_t wake_func)
3670 __acquires(&ctx->completion_lock)
3671{
3672 struct io_ring_ctx *ctx = req->ctx;
3673 bool cancel = false;
3674
3675 poll->file = req->file;
3676 poll->head = NULL;
3677 poll->done = poll->canceled = false;
3678 poll->events = mask;
3679
3680 ipt->pt._key = mask;
3681 ipt->req = req;
3682 ipt->error = -EINVAL;
3683
3684 INIT_LIST_HEAD(&poll->wait.entry);
3685 init_waitqueue_func_entry(&poll->wait, wake_func);
3686 poll->wait.private = req;
3687
3688 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
3689
3690 spin_lock_irq(&ctx->completion_lock);
3691 if (likely(poll->head)) {
3692 spin_lock(&poll->head->lock);
3693 if (unlikely(list_empty(&poll->wait.entry))) {
3694 if (ipt->error)
3695 cancel = true;
3696 ipt->error = 0;
3697 mask = 0;
3698 }
3699 if (mask || ipt->error)
3700 list_del_init(&poll->wait.entry);
3701 else if (cancel)
3702 WRITE_ONCE(poll->canceled, true);
3703 else if (!poll->done) /* actually waiting for an event */
3704 io_poll_req_insert(req);
3705 spin_unlock(&poll->head->lock);
3706 }
3707
3708 return mask;
3709}
3710
3711static bool io_arm_poll_handler(struct io_kiocb *req)
3712{
3713 const struct io_op_def *def = &io_op_defs[req->opcode];
3714 struct io_ring_ctx *ctx = req->ctx;
3715 struct async_poll *apoll;
3716 struct io_poll_table ipt;
3717 __poll_t mask, ret;
3718
3719 if (!req->file || !file_can_poll(req->file))
3720 return false;
3721 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
3722 return false;
3723 if (!def->pollin && !def->pollout)
3724 return false;
3725
3726 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
3727 if (unlikely(!apoll))
3728 return false;
3729
3730 req->flags |= REQ_F_POLLED;
3731 memcpy(&apoll->work, &req->work, sizeof(req->work));
3732
3733 /*
3734 * Don't need a reference here, as we're adding it to the task
3735 * task_works list. If the task exits, the list is pruned.
3736 */
3737 req->task = current;
3738 req->apoll = apoll;
3739 INIT_HLIST_NODE(&req->hash_node);
3740
Nathan Chancellor8755d972020-03-02 16:01:19 -07003741 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07003742 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07003743 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07003744 if (def->pollout)
3745 mask |= POLLOUT | POLLWRNORM;
3746 mask |= POLLERR | POLLPRI;
3747
3748 ipt.pt._qproc = io_async_queue_proc;
3749
3750 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
3751 io_async_wake);
3752 if (ret) {
3753 ipt.error = 0;
3754 apoll->poll.done = true;
3755 spin_unlock_irq(&ctx->completion_lock);
3756 memcpy(&req->work, &apoll->work, sizeof(req->work));
3757 kfree(apoll);
3758 return false;
3759 }
3760 spin_unlock_irq(&ctx->completion_lock);
3761 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
3762 apoll->poll.events);
3763 return true;
3764}
3765
3766static bool __io_poll_remove_one(struct io_kiocb *req,
3767 struct io_poll_iocb *poll)
3768{
Jens Axboeb41e9852020-02-17 09:52:41 -07003769 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003770
3771 spin_lock(&poll->head->lock);
3772 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003773 if (!list_empty(&poll->wait.entry)) {
3774 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07003775 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003776 }
3777 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07003778 return do_complete;
3779}
3780
3781static bool io_poll_remove_one(struct io_kiocb *req)
3782{
3783 bool do_complete;
3784
3785 if (req->opcode == IORING_OP_POLL_ADD) {
3786 do_complete = __io_poll_remove_one(req, &req->poll);
3787 } else {
3788 /* non-poll requests have submit ref still */
3789 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
3790 if (do_complete)
3791 io_put_req(req);
3792 }
3793
Jens Axboe78076bb2019-12-04 19:56:40 -07003794 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07003795
Jens Axboeb41e9852020-02-17 09:52:41 -07003796 if (do_complete) {
3797 io_cqring_fill_event(req, -ECANCELED);
3798 io_commit_cqring(req->ctx);
3799 req->flags |= REQ_F_COMP_LOCKED;
3800 io_put_req(req);
3801 }
3802
3803 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003804}
3805
3806static void io_poll_remove_all(struct io_ring_ctx *ctx)
3807{
Jens Axboe78076bb2019-12-04 19:56:40 -07003808 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003809 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003810 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003811
3812 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003813 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3814 struct hlist_head *list;
3815
3816 list = &ctx->cancel_hash[i];
3817 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3818 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003819 }
3820 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003821
3822 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003823}
3824
Jens Axboe47f46762019-11-09 17:43:02 -07003825static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3826{
Jens Axboe78076bb2019-12-04 19:56:40 -07003827 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003828 struct io_kiocb *req;
3829
Jens Axboe78076bb2019-12-04 19:56:40 -07003830 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3831 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07003832 if (sqe_addr != req->user_data)
3833 continue;
3834 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07003835 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07003836 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07003837 }
3838
3839 return -ENOENT;
3840}
3841
Jens Axboe3529d8c2019-12-19 18:24:38 -07003842static int io_poll_remove_prep(struct io_kiocb *req,
3843 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003844{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003845 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3846 return -EINVAL;
3847 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3848 sqe->poll_events)
3849 return -EINVAL;
3850
Jens Axboe0969e782019-12-17 18:40:57 -07003851 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003852 return 0;
3853}
3854
3855/*
3856 * Find a running poll command that matches one specified in sqe->addr,
3857 * and remove it if found.
3858 */
3859static int io_poll_remove(struct io_kiocb *req)
3860{
3861 struct io_ring_ctx *ctx = req->ctx;
3862 u64 addr;
3863 int ret;
3864
Jens Axboe0969e782019-12-17 18:40:57 -07003865 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003866 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003867 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003868 spin_unlock_irq(&ctx->completion_lock);
3869
Jens Axboe78e19bb2019-11-06 15:21:34 -07003870 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003871 if (ret < 0)
3872 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003873 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003874 return 0;
3875}
3876
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003877static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003878{
Jackie Liua197f662019-11-08 08:09:12 -07003879 struct io_ring_ctx *ctx = req->ctx;
3880
Jens Axboe8c838782019-03-12 15:48:16 -06003881 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03003882 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003883 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003884}
3885
Jens Axboeb41e9852020-02-17 09:52:41 -07003886static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003887{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003888 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003889
Jens Axboe221c5eb2019-01-17 09:41:58 -07003890 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003891 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07003892 io_poll_complete(req, req->result, 0);
3893 req->flags |= REQ_F_COMP_LOCKED;
3894 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003895 spin_unlock_irq(&ctx->completion_lock);
3896
Jens Axboe8c838782019-03-12 15:48:16 -06003897 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07003898}
Jens Axboe89723d02019-11-05 15:32:58 -07003899
Jens Axboeb41e9852020-02-17 09:52:41 -07003900static void io_poll_task_func(struct callback_head *cb)
3901{
3902 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3903 struct io_kiocb *nxt = NULL;
3904
3905 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07003906 if (nxt) {
3907 struct io_ring_ctx *ctx = nxt->ctx;
3908
3909 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003910 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07003911 mutex_unlock(&ctx->uring_lock);
3912 }
Jens Axboef0b493e2020-02-01 21:30:11 -07003913}
3914
Jens Axboe221c5eb2019-01-17 09:41:58 -07003915static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3916 void *key)
3917{
Jens Axboec2f2eb72020-02-10 09:07:05 -07003918 struct io_kiocb *req = wait->private;
3919 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003920
Jens Axboed7718a92020-02-14 22:23:12 -07003921 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003922}
3923
Jens Axboe221c5eb2019-01-17 09:41:58 -07003924static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3925 struct poll_table_struct *p)
3926{
3927 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3928
Jens Axboed7718a92020-02-14 22:23:12 -07003929 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07003930}
3931
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003933{
3934 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003935 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003936
3937 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3938 return -EINVAL;
3939 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3940 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003941 if (!poll->file)
3942 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003943
Jens Axboe221c5eb2019-01-17 09:41:58 -07003944 events = READ_ONCE(sqe->poll_events);
3945 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07003946
Jens Axboed7718a92020-02-14 22:23:12 -07003947 /*
3948 * Don't need a reference here, as we're adding it to the task
3949 * task_works list. If the task exits, the list is pruned.
3950 */
Jens Axboeb41e9852020-02-17 09:52:41 -07003951 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07003952 return 0;
3953}
3954
3955static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3956{
3957 struct io_poll_iocb *poll = &req->poll;
3958 struct io_ring_ctx *ctx = req->ctx;
3959 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07003960 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003961
Jens Axboe78076bb2019-12-04 19:56:40 -07003962 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06003963 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07003964 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06003965
Jens Axboed7718a92020-02-14 22:23:12 -07003966 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
3967 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003968
Jens Axboe8c838782019-03-12 15:48:16 -06003969 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003970 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003971 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003972 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003973 spin_unlock_irq(&ctx->completion_lock);
3974
Jens Axboe8c838782019-03-12 15:48:16 -06003975 if (mask) {
3976 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003977 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003978 }
Jens Axboe8c838782019-03-12 15:48:16 -06003979 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003980}
3981
Jens Axboe5262f562019-09-17 12:26:57 -06003982static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3983{
Jens Axboead8a48a2019-11-15 08:49:11 -07003984 struct io_timeout_data *data = container_of(timer,
3985 struct io_timeout_data, timer);
3986 struct io_kiocb *req = data->req;
3987 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003988 unsigned long flags;
3989
Jens Axboe5262f562019-09-17 12:26:57 -06003990 atomic_inc(&ctx->cq_timeouts);
3991
3992 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003993 /*
Jens Axboe11365042019-10-16 09:08:32 -06003994 * We could be racing with timeout deletion. If the list is empty,
3995 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003996 */
Jens Axboe842f9612019-10-29 12:34:10 -06003997 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003998 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003999
Jens Axboe11365042019-10-16 09:08:32 -06004000 /*
4001 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004002 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004003 * pointer will be increased, otherwise other timeout reqs may
4004 * return in advance without waiting for enough wait_nr.
4005 */
4006 prev = req;
4007 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4008 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004009 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004010 }
Jens Axboe842f9612019-10-29 12:34:10 -06004011
Jens Axboe78e19bb2019-11-06 15:21:34 -07004012 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004013 io_commit_cqring(ctx);
4014 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4015
4016 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004017 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004018 io_put_req(req);
4019 return HRTIMER_NORESTART;
4020}
4021
Jens Axboe47f46762019-11-09 17:43:02 -07004022static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4023{
4024 struct io_kiocb *req;
4025 int ret = -ENOENT;
4026
4027 list_for_each_entry(req, &ctx->timeout_list, list) {
4028 if (user_data == req->user_data) {
4029 list_del_init(&req->list);
4030 ret = 0;
4031 break;
4032 }
4033 }
4034
4035 if (ret == -ENOENT)
4036 return ret;
4037
Jens Axboe2d283902019-12-04 11:08:05 -07004038 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004039 if (ret == -1)
4040 return -EALREADY;
4041
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004042 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004043 io_cqring_fill_event(req, -ECANCELED);
4044 io_put_req(req);
4045 return 0;
4046}
4047
Jens Axboe3529d8c2019-12-19 18:24:38 -07004048static int io_timeout_remove_prep(struct io_kiocb *req,
4049 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004050{
Jens Axboeb29472e2019-12-17 18:50:29 -07004051 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4052 return -EINVAL;
4053 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4054 return -EINVAL;
4055
4056 req->timeout.addr = READ_ONCE(sqe->addr);
4057 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4058 if (req->timeout.flags)
4059 return -EINVAL;
4060
Jens Axboeb29472e2019-12-17 18:50:29 -07004061 return 0;
4062}
4063
Jens Axboe11365042019-10-16 09:08:32 -06004064/*
4065 * Remove or update an existing timeout command
4066 */
Jens Axboefc4df992019-12-10 14:38:45 -07004067static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004068{
4069 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004070 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004071
Jens Axboe11365042019-10-16 09:08:32 -06004072 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004073 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004074
Jens Axboe47f46762019-11-09 17:43:02 -07004075 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004076 io_commit_cqring(ctx);
4077 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004078 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004079 if (ret < 0)
4080 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004081 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004082 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004083}
4084
Jens Axboe3529d8c2019-12-19 18:24:38 -07004085static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004086 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004087{
Jens Axboead8a48a2019-11-15 08:49:11 -07004088 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004089 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004090
Jens Axboead8a48a2019-11-15 08:49:11 -07004091 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004092 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004093 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004094 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004095 if (sqe->off && is_timeout_link)
4096 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004097 flags = READ_ONCE(sqe->timeout_flags);
4098 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004099 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004100
Jens Axboe26a61672019-12-20 09:02:01 -07004101 req->timeout.count = READ_ONCE(sqe->off);
4102
Jens Axboe3529d8c2019-12-19 18:24:38 -07004103 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004104 return -ENOMEM;
4105
4106 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004107 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004108 req->flags |= REQ_F_TIMEOUT;
4109
4110 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004111 return -EFAULT;
4112
Jens Axboe11365042019-10-16 09:08:32 -06004113 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004114 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004115 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004116 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004117
Jens Axboead8a48a2019-11-15 08:49:11 -07004118 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4119 return 0;
4120}
4121
Jens Axboefc4df992019-12-10 14:38:45 -07004122static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004123{
4124 unsigned count;
4125 struct io_ring_ctx *ctx = req->ctx;
4126 struct io_timeout_data *data;
4127 struct list_head *entry;
4128 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004129
Jens Axboe2d283902019-12-04 11:08:05 -07004130 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004131
Jens Axboe5262f562019-09-17 12:26:57 -06004132 /*
4133 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004134 * timeout event to be satisfied. If it isn't set, then this is
4135 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004136 */
Jens Axboe26a61672019-12-20 09:02:01 -07004137 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004138 if (!count) {
4139 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4140 spin_lock_irq(&ctx->completion_lock);
4141 entry = ctx->timeout_list.prev;
4142 goto add;
4143 }
Jens Axboe5262f562019-09-17 12:26:57 -06004144
4145 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004146 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004147
4148 /*
4149 * Insertion sort, ensuring the first entry in the list is always
4150 * the one we need first.
4151 */
Jens Axboe5262f562019-09-17 12:26:57 -06004152 spin_lock_irq(&ctx->completion_lock);
4153 list_for_each_prev(entry, &ctx->timeout_list) {
4154 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004155 unsigned nxt_sq_head;
4156 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004157 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004158
Jens Axboe93bd25b2019-11-11 23:34:31 -07004159 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4160 continue;
4161
yangerkun5da0fb12019-10-15 21:59:29 +08004162 /*
4163 * Since cached_sq_head + count - 1 can overflow, use type long
4164 * long to store it.
4165 */
4166 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004167 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4168 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004169
4170 /*
4171 * cached_sq_head may overflow, and it will never overflow twice
4172 * once there is some timeout req still be valid.
4173 */
4174 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004175 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004176
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004177 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004178 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004179
4180 /*
4181 * Sequence of reqs after the insert one and itself should
4182 * be adjusted because each timeout req consumes a slot.
4183 */
4184 span++;
4185 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004186 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004187 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004188add:
Jens Axboe5262f562019-09-17 12:26:57 -06004189 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004190 data->timer.function = io_timeout_fn;
4191 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004192 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004193 return 0;
4194}
4195
Jens Axboe62755e32019-10-28 21:49:21 -06004196static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004197{
Jens Axboe62755e32019-10-28 21:49:21 -06004198 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004199
Jens Axboe62755e32019-10-28 21:49:21 -06004200 return req->user_data == (unsigned long) data;
4201}
4202
Jens Axboee977d6d2019-11-05 12:39:45 -07004203static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004204{
Jens Axboe62755e32019-10-28 21:49:21 -06004205 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004206 int ret = 0;
4207
Jens Axboe62755e32019-10-28 21:49:21 -06004208 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4209 switch (cancel_ret) {
4210 case IO_WQ_CANCEL_OK:
4211 ret = 0;
4212 break;
4213 case IO_WQ_CANCEL_RUNNING:
4214 ret = -EALREADY;
4215 break;
4216 case IO_WQ_CANCEL_NOTFOUND:
4217 ret = -ENOENT;
4218 break;
4219 }
4220
Jens Axboee977d6d2019-11-05 12:39:45 -07004221 return ret;
4222}
4223
Jens Axboe47f46762019-11-09 17:43:02 -07004224static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4225 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004226 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004227{
4228 unsigned long flags;
4229 int ret;
4230
4231 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4232 if (ret != -ENOENT) {
4233 spin_lock_irqsave(&ctx->completion_lock, flags);
4234 goto done;
4235 }
4236
4237 spin_lock_irqsave(&ctx->completion_lock, flags);
4238 ret = io_timeout_cancel(ctx, sqe_addr);
4239 if (ret != -ENOENT)
4240 goto done;
4241 ret = io_poll_cancel(ctx, sqe_addr);
4242done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004243 if (!ret)
4244 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004245 io_cqring_fill_event(req, ret);
4246 io_commit_cqring(ctx);
4247 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4248 io_cqring_ev_posted(ctx);
4249
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004250 if (ret < 0)
4251 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004252 io_put_req_find_next(req, nxt);
4253}
4254
Jens Axboe3529d8c2019-12-19 18:24:38 -07004255static int io_async_cancel_prep(struct io_kiocb *req,
4256 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004257{
Jens Axboefbf23842019-12-17 18:45:56 -07004258 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004259 return -EINVAL;
4260 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4261 sqe->cancel_flags)
4262 return -EINVAL;
4263
Jens Axboefbf23842019-12-17 18:45:56 -07004264 req->cancel.addr = READ_ONCE(sqe->addr);
4265 return 0;
4266}
4267
4268static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4269{
4270 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004271
4272 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004273 return 0;
4274}
4275
Jens Axboe05f3fb32019-12-09 11:22:50 -07004276static int io_files_update_prep(struct io_kiocb *req,
4277 const struct io_uring_sqe *sqe)
4278{
4279 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4280 return -EINVAL;
4281
4282 req->files_update.offset = READ_ONCE(sqe->off);
4283 req->files_update.nr_args = READ_ONCE(sqe->len);
4284 if (!req->files_update.nr_args)
4285 return -EINVAL;
4286 req->files_update.arg = READ_ONCE(sqe->addr);
4287 return 0;
4288}
4289
4290static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4291{
4292 struct io_ring_ctx *ctx = req->ctx;
4293 struct io_uring_files_update up;
4294 int ret;
4295
Jens Axboef86cd202020-01-29 13:46:44 -07004296 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004297 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004298
4299 up.offset = req->files_update.offset;
4300 up.fds = req->files_update.arg;
4301
4302 mutex_lock(&ctx->uring_lock);
4303 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4304 mutex_unlock(&ctx->uring_lock);
4305
4306 if (ret < 0)
4307 req_set_fail_links(req);
4308 io_cqring_add_event(req, ret);
4309 io_put_req(req);
4310 return 0;
4311}
4312
Jens Axboe3529d8c2019-12-19 18:24:38 -07004313static int io_req_defer_prep(struct io_kiocb *req,
4314 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004315{
Jens Axboee7815732019-12-17 19:45:06 -07004316 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004317
Jens Axboef86cd202020-01-29 13:46:44 -07004318 if (io_op_defs[req->opcode].file_table) {
4319 ret = io_grab_files(req);
4320 if (unlikely(ret))
4321 return ret;
4322 }
4323
Jens Axboecccf0ee2020-01-27 16:34:48 -07004324 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4325
Jens Axboed625c6e2019-12-17 19:53:05 -07004326 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004327 case IORING_OP_NOP:
4328 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004329 case IORING_OP_READV:
4330 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004331 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004332 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004333 break;
4334 case IORING_OP_WRITEV:
4335 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004336 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004337 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004338 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004339 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004340 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004341 break;
4342 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004343 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004344 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004345 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004346 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004347 break;
4348 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004349 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004350 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004351 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004352 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004353 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004354 break;
4355 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004356 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004358 break;
Jens Axboef499a022019-12-02 16:28:46 -07004359 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004360 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004361 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004362 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004364 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004365 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004366 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004367 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004368 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004369 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004370 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004371 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004372 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004373 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004374 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004375 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004376 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004377 case IORING_OP_FALLOCATE:
4378 ret = io_fallocate_prep(req, sqe);
4379 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004380 case IORING_OP_OPENAT:
4381 ret = io_openat_prep(req, sqe);
4382 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004383 case IORING_OP_CLOSE:
4384 ret = io_close_prep(req, sqe);
4385 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004386 case IORING_OP_FILES_UPDATE:
4387 ret = io_files_update_prep(req, sqe);
4388 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004389 case IORING_OP_STATX:
4390 ret = io_statx_prep(req, sqe);
4391 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004392 case IORING_OP_FADVISE:
4393 ret = io_fadvise_prep(req, sqe);
4394 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004395 case IORING_OP_MADVISE:
4396 ret = io_madvise_prep(req, sqe);
4397 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004398 case IORING_OP_OPENAT2:
4399 ret = io_openat2_prep(req, sqe);
4400 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004401 case IORING_OP_EPOLL_CTL:
4402 ret = io_epoll_ctl_prep(req, sqe);
4403 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004404 case IORING_OP_SPLICE:
4405 ret = io_splice_prep(req, sqe);
4406 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004407 default:
Jens Axboee7815732019-12-17 19:45:06 -07004408 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4409 req->opcode);
4410 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004411 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004412 }
4413
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004414 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004415}
4416
Jens Axboe3529d8c2019-12-19 18:24:38 -07004417static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004418{
Jackie Liua197f662019-11-08 08:09:12 -07004419 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004420 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004421
Bob Liu9d858b22019-11-13 18:06:25 +08004422 /* Still need defer if there is pending req in defer list. */
4423 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004424 return 0;
4425
Jens Axboe3529d8c2019-12-19 18:24:38 -07004426 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004427 return -EAGAIN;
4428
Jens Axboe3529d8c2019-12-19 18:24:38 -07004429 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004430 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004431 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004432
Jens Axboede0617e2019-04-06 21:51:27 -06004433 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004434 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004435 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004436 return 0;
4437 }
4438
Jens Axboe915967f2019-11-21 09:01:20 -07004439 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004440 list_add_tail(&req->list, &ctx->defer_list);
4441 spin_unlock_irq(&ctx->completion_lock);
4442 return -EIOCBQUEUED;
4443}
4444
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004445static void io_cleanup_req(struct io_kiocb *req)
4446{
4447 struct io_async_ctx *io = req->io;
4448
4449 switch (req->opcode) {
4450 case IORING_OP_READV:
4451 case IORING_OP_READ_FIXED:
4452 case IORING_OP_READ:
4453 case IORING_OP_WRITEV:
4454 case IORING_OP_WRITE_FIXED:
4455 case IORING_OP_WRITE:
4456 if (io->rw.iov != io->rw.fast_iov)
4457 kfree(io->rw.iov);
4458 break;
4459 case IORING_OP_SENDMSG:
4460 case IORING_OP_RECVMSG:
4461 if (io->msg.iov != io->msg.fast_iov)
4462 kfree(io->msg.iov);
4463 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004464 case IORING_OP_OPENAT:
4465 case IORING_OP_OPENAT2:
4466 case IORING_OP_STATX:
4467 putname(req->open.filename);
4468 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004469 case IORING_OP_SPLICE:
4470 io_put_file(req, req->splice.file_in,
4471 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4472 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004473 }
4474
4475 req->flags &= ~REQ_F_NEED_CLEANUP;
4476}
4477
Jens Axboe3529d8c2019-12-19 18:24:38 -07004478static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4479 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004480{
Jackie Liua197f662019-11-08 08:09:12 -07004481 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004482 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004483
Jens Axboed625c6e2019-12-17 19:53:05 -07004484 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004486 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004487 break;
4488 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004489 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004490 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004491 if (sqe) {
4492 ret = io_read_prep(req, sqe, force_nonblock);
4493 if (ret < 0)
4494 break;
4495 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004496 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004497 break;
4498 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004499 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004500 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004501 if (sqe) {
4502 ret = io_write_prep(req, sqe, force_nonblock);
4503 if (ret < 0)
4504 break;
4505 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004506 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004507 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004508 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004509 if (sqe) {
4510 ret = io_prep_fsync(req, sqe);
4511 if (ret < 0)
4512 break;
4513 }
Jens Axboefc4df992019-12-10 14:38:45 -07004514 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004515 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004516 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004517 if (sqe) {
4518 ret = io_poll_add_prep(req, sqe);
4519 if (ret)
4520 break;
4521 }
Jens Axboefc4df992019-12-10 14:38:45 -07004522 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004523 break;
4524 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004525 if (sqe) {
4526 ret = io_poll_remove_prep(req, sqe);
4527 if (ret < 0)
4528 break;
4529 }
Jens Axboefc4df992019-12-10 14:38:45 -07004530 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004531 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004532 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004533 if (sqe) {
4534 ret = io_prep_sfr(req, sqe);
4535 if (ret < 0)
4536 break;
4537 }
Jens Axboefc4df992019-12-10 14:38:45 -07004538 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004539 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004540 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004541 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004542 if (sqe) {
4543 ret = io_sendmsg_prep(req, sqe);
4544 if (ret < 0)
4545 break;
4546 }
Jens Axboefddafac2020-01-04 20:19:44 -07004547 if (req->opcode == IORING_OP_SENDMSG)
4548 ret = io_sendmsg(req, nxt, force_nonblock);
4549 else
4550 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004551 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004552 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004553 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004554 if (sqe) {
4555 ret = io_recvmsg_prep(req, sqe);
4556 if (ret)
4557 break;
4558 }
Jens Axboefddafac2020-01-04 20:19:44 -07004559 if (req->opcode == IORING_OP_RECVMSG)
4560 ret = io_recvmsg(req, nxt, force_nonblock);
4561 else
4562 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004563 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004564 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004565 if (sqe) {
4566 ret = io_timeout_prep(req, sqe, false);
4567 if (ret)
4568 break;
4569 }
Jens Axboefc4df992019-12-10 14:38:45 -07004570 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004571 break;
Jens Axboe11365042019-10-16 09:08:32 -06004572 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004573 if (sqe) {
4574 ret = io_timeout_remove_prep(req, sqe);
4575 if (ret)
4576 break;
4577 }
Jens Axboefc4df992019-12-10 14:38:45 -07004578 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004579 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004580 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004581 if (sqe) {
4582 ret = io_accept_prep(req, sqe);
4583 if (ret)
4584 break;
4585 }
Jens Axboefc4df992019-12-10 14:38:45 -07004586 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004587 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004588 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004589 if (sqe) {
4590 ret = io_connect_prep(req, sqe);
4591 if (ret)
4592 break;
4593 }
Jens Axboefc4df992019-12-10 14:38:45 -07004594 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004595 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004596 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004597 if (sqe) {
4598 ret = io_async_cancel_prep(req, sqe);
4599 if (ret)
4600 break;
4601 }
Jens Axboefc4df992019-12-10 14:38:45 -07004602 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004603 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004604 case IORING_OP_FALLOCATE:
4605 if (sqe) {
4606 ret = io_fallocate_prep(req, sqe);
4607 if (ret)
4608 break;
4609 }
4610 ret = io_fallocate(req, nxt, force_nonblock);
4611 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004612 case IORING_OP_OPENAT:
4613 if (sqe) {
4614 ret = io_openat_prep(req, sqe);
4615 if (ret)
4616 break;
4617 }
4618 ret = io_openat(req, nxt, force_nonblock);
4619 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004620 case IORING_OP_CLOSE:
4621 if (sqe) {
4622 ret = io_close_prep(req, sqe);
4623 if (ret)
4624 break;
4625 }
4626 ret = io_close(req, nxt, force_nonblock);
4627 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004628 case IORING_OP_FILES_UPDATE:
4629 if (sqe) {
4630 ret = io_files_update_prep(req, sqe);
4631 if (ret)
4632 break;
4633 }
4634 ret = io_files_update(req, force_nonblock);
4635 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004636 case IORING_OP_STATX:
4637 if (sqe) {
4638 ret = io_statx_prep(req, sqe);
4639 if (ret)
4640 break;
4641 }
4642 ret = io_statx(req, nxt, force_nonblock);
4643 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004644 case IORING_OP_FADVISE:
4645 if (sqe) {
4646 ret = io_fadvise_prep(req, sqe);
4647 if (ret)
4648 break;
4649 }
4650 ret = io_fadvise(req, nxt, force_nonblock);
4651 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004652 case IORING_OP_MADVISE:
4653 if (sqe) {
4654 ret = io_madvise_prep(req, sqe);
4655 if (ret)
4656 break;
4657 }
4658 ret = io_madvise(req, nxt, force_nonblock);
4659 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004660 case IORING_OP_OPENAT2:
4661 if (sqe) {
4662 ret = io_openat2_prep(req, sqe);
4663 if (ret)
4664 break;
4665 }
4666 ret = io_openat2(req, nxt, force_nonblock);
4667 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004668 case IORING_OP_EPOLL_CTL:
4669 if (sqe) {
4670 ret = io_epoll_ctl_prep(req, sqe);
4671 if (ret)
4672 break;
4673 }
4674 ret = io_epoll_ctl(req, nxt, force_nonblock);
4675 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004676 case IORING_OP_SPLICE:
4677 if (sqe) {
4678 ret = io_splice_prep(req, sqe);
4679 if (ret < 0)
4680 break;
4681 }
4682 ret = io_splice(req, nxt, force_nonblock);
4683 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004684 default:
4685 ret = -EINVAL;
4686 break;
4687 }
4688
Jens Axboedef596e2019-01-09 08:59:42 -07004689 if (ret)
4690 return ret;
4691
4692 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004693 const bool in_async = io_wq_current_is_worker();
4694
Jens Axboe9e645e112019-05-10 16:07:28 -06004695 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004696 return -EAGAIN;
4697
Jens Axboe11ba8202020-01-15 21:51:17 -07004698 /* workqueue context doesn't hold uring_lock, grab it now */
4699 if (in_async)
4700 mutex_lock(&ctx->uring_lock);
4701
Jens Axboedef596e2019-01-09 08:59:42 -07004702 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004703
4704 if (in_async)
4705 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004706 }
4707
4708 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004709}
4710
Jens Axboe561fb042019-10-24 07:25:42 -06004711static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004712{
Jens Axboe561fb042019-10-24 07:25:42 -06004713 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004714 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004715 struct io_kiocb *nxt = NULL;
4716 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004717
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004718 /* if NO_CANCEL is set, we must still run the work */
4719 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4720 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004721 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004722 }
Jens Axboe31b51512019-01-18 22:56:34 -07004723
Jens Axboe561fb042019-10-24 07:25:42 -06004724 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004725 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004726 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004727 /*
4728 * We can get EAGAIN for polled IO even though we're
4729 * forcing a sync submission from here, since we can't
4730 * wait for request slots on the block side.
4731 */
4732 if (ret != -EAGAIN)
4733 break;
4734 cond_resched();
4735 } while (1);
4736 }
Jens Axboe31b51512019-01-18 22:56:34 -07004737
Jens Axboe561fb042019-10-24 07:25:42 -06004738 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004739 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004740
Jens Axboe561fb042019-10-24 07:25:42 -06004741 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004742 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004743 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004744 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004745 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004746
Jens Axboe561fb042019-10-24 07:25:42 -06004747 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004748 if (!ret && nxt)
4749 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004750}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004751
Jens Axboe15b71ab2019-12-11 11:20:36 -07004752static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004753{
Jens Axboed3656342019-12-18 09:50:26 -07004754 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004755 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004756 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004757 return 0;
4758 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004759}
4760
Jens Axboe65e19f52019-10-26 07:20:21 -06004761static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4762 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004763{
Jens Axboe65e19f52019-10-26 07:20:21 -06004764 struct fixed_file_table *table;
4765
Jens Axboe05f3fb32019-12-09 11:22:50 -07004766 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4767 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004768}
4769
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004770static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
4771 int fd, struct file **out_file, bool fixed)
4772{
4773 struct io_ring_ctx *ctx = req->ctx;
4774 struct file *file;
4775
4776 if (fixed) {
4777 if (unlikely(!ctx->file_data ||
4778 (unsigned) fd >= ctx->nr_user_files))
4779 return -EBADF;
4780 fd = array_index_nospec(fd, ctx->nr_user_files);
4781 file = io_file_from_index(ctx, fd);
4782 if (!file)
4783 return -EBADF;
4784 percpu_ref_get(&ctx->file_data->refs);
4785 } else {
4786 trace_io_uring_file_get(ctx, fd);
4787 file = __io_file_get(state, fd);
4788 if (unlikely(!file))
4789 return -EBADF;
4790 }
4791
4792 *out_file = file;
4793 return 0;
4794}
4795
Jens Axboe3529d8c2019-12-19 18:24:38 -07004796static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4797 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004798{
4799 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004800 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004801 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06004802
Jens Axboe3529d8c2019-12-19 18:24:38 -07004803 flags = READ_ONCE(sqe->flags);
4804 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004805
Jens Axboed3656342019-12-18 09:50:26 -07004806 if (!io_req_needs_file(req, fd))
4807 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004808
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004809 fixed = (flags & IOSQE_FIXED_FILE);
4810 if (unlikely(!fixed && req->needs_fixed_file))
4811 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004812
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004813 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06004814}
4815
Jackie Liua197f662019-11-08 08:09:12 -07004816static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004817{
Jens Axboefcb323c2019-10-24 12:39:47 -06004818 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004819 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004820
Jens Axboef86cd202020-01-29 13:46:44 -07004821 if (req->work.files)
4822 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004823 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004824 return -EBADF;
4825
Jens Axboefcb323c2019-10-24 12:39:47 -06004826 rcu_read_lock();
4827 spin_lock_irq(&ctx->inflight_lock);
4828 /*
4829 * We use the f_ops->flush() handler to ensure that we can flush
4830 * out work accessing these files if the fd is closed. Check if
4831 * the fd has changed since we started down this path, and disallow
4832 * this operation if it has.
4833 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004834 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004835 list_add(&req->inflight_entry, &ctx->inflight_list);
4836 req->flags |= REQ_F_INFLIGHT;
4837 req->work.files = current->files;
4838 ret = 0;
4839 }
4840 spin_unlock_irq(&ctx->inflight_lock);
4841 rcu_read_unlock();
4842
4843 return ret;
4844}
4845
Jens Axboe2665abf2019-11-05 12:40:47 -07004846static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4847{
Jens Axboead8a48a2019-11-15 08:49:11 -07004848 struct io_timeout_data *data = container_of(timer,
4849 struct io_timeout_data, timer);
4850 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004851 struct io_ring_ctx *ctx = req->ctx;
4852 struct io_kiocb *prev = NULL;
4853 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004854
4855 spin_lock_irqsave(&ctx->completion_lock, flags);
4856
4857 /*
4858 * We don't expect the list to be empty, that will only happen if we
4859 * race with the completion of the linked work.
4860 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004861 if (!list_empty(&req->link_list)) {
4862 prev = list_entry(req->link_list.prev, struct io_kiocb,
4863 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004864 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004865 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004866 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4867 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004868 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004869 }
4870
4871 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4872
4873 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004874 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004875 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4876 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004877 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004878 } else {
4879 io_cqring_add_event(req, -ETIME);
4880 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004881 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004882 return HRTIMER_NORESTART;
4883}
4884
Jens Axboead8a48a2019-11-15 08:49:11 -07004885static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004886{
Jens Axboe76a46e02019-11-10 23:34:16 -07004887 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004888
Jens Axboe76a46e02019-11-10 23:34:16 -07004889 /*
4890 * If the list is now empty, then our linked request finished before
4891 * we got a chance to setup the timer
4892 */
4893 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004894 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004895 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004896
Jens Axboead8a48a2019-11-15 08:49:11 -07004897 data->timer.function = io_link_timeout_fn;
4898 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4899 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004900 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004901 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004902
Jens Axboe2665abf2019-11-05 12:40:47 -07004903 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004904 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004905}
4906
Jens Axboead8a48a2019-11-15 08:49:11 -07004907static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004908{
4909 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004910
Jens Axboe2665abf2019-11-05 12:40:47 -07004911 if (!(req->flags & REQ_F_LINK))
4912 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004913 /* for polled retry, if flag is set, we already went through here */
4914 if (req->flags & REQ_F_POLLED)
4915 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004916
Pavel Begunkov44932332019-12-05 16:16:35 +03004917 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4918 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004919 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004920 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004921
Jens Axboe76a46e02019-11-10 23:34:16 -07004922 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004923 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004924}
4925
Jens Axboe3529d8c2019-12-19 18:24:38 -07004926static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004927{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004928 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004929 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07004930 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004931 int ret;
4932
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004933again:
4934 linked_timeout = io_prep_linked_timeout(req);
4935
Jens Axboe193155c2020-02-22 23:22:19 -07004936 if (req->work.creds && req->work.creds != current_cred()) {
4937 if (old_creds)
4938 revert_creds(old_creds);
4939 if (old_creds == req->work.creds)
4940 old_creds = NULL; /* restored original creds */
4941 else
4942 old_creds = override_creds(req->work.creds);
4943 }
4944
Jens Axboe3529d8c2019-12-19 18:24:38 -07004945 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004946
4947 /*
4948 * We async punt it if the file wasn't marked NOWAIT, or if the file
4949 * doesn't support non-blocking read/write attempts
4950 */
4951 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4952 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07004953 if (io_arm_poll_handler(req)) {
4954 if (linked_timeout)
4955 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004956 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07004957 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004958punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004959 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004960 ret = io_grab_files(req);
4961 if (ret)
4962 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004963 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004964
4965 /*
4966 * Queued up for async execution, worker will release
4967 * submit reference when the iocb is actually submitted.
4968 */
4969 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004970 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004971 }
Jens Axboee65ef562019-03-12 10:16:44 -06004972
Jens Axboefcb323c2019-10-24 12:39:47 -06004973err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004974 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06004975 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004976 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004977
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004978 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004979 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004980 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004981 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004982 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004983 }
4984
Jens Axboee65ef562019-03-12 10:16:44 -06004985 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004986 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004987 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004988 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004989 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004990 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004991 if (nxt) {
4992 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004993
4994 if (req->flags & REQ_F_FORCE_ASYNC)
4995 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004996 goto again;
4997 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004998exit:
Jens Axboe193155c2020-02-22 23:22:19 -07004999 if (old_creds)
5000 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005001}
5002
Jens Axboe3529d8c2019-12-19 18:24:38 -07005003static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005004{
5005 int ret;
5006
Jens Axboe3529d8c2019-12-19 18:24:38 -07005007 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005008 if (ret) {
5009 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005010fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005011 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005012 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005013 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005014 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005015 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005016 ret = io_req_defer_prep(req, sqe);
5017 if (unlikely(ret < 0))
5018 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005019 /*
5020 * Never try inline submit of IOSQE_ASYNC is set, go straight
5021 * to async execution.
5022 */
5023 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5024 io_queue_async_work(req);
5025 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005026 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005027 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005028}
5029
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005030static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005031{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005032 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005033 io_cqring_add_event(req, -ECANCELED);
5034 io_double_put_req(req);
5035 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005036 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005037}
5038
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005039#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07005040 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06005041
Jens Axboe3529d8c2019-12-19 18:24:38 -07005042static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5043 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005044{
Jackie Liua197f662019-11-08 08:09:12 -07005045 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005046 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005047 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005048
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005049 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005050
5051 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005052 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005053 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005054 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005055 }
5056
Jens Axboe75c6a032020-01-28 10:15:23 -07005057 id = READ_ONCE(sqe->personality);
5058 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005059 req->work.creds = idr_find(&ctx->personality_idr, id);
5060 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005061 ret = -EINVAL;
5062 goto err_req;
5063 }
Jens Axboe193155c2020-02-22 23:22:19 -07005064 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005065 }
5066
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005067 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005068 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5069 IOSQE_ASYNC | IOSQE_FIXED_FILE);
Jens Axboe9e645e112019-05-10 16:07:28 -06005070
Jens Axboe3529d8c2019-12-19 18:24:38 -07005071 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005072 if (unlikely(ret)) {
5073err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005074 io_cqring_add_event(req, ret);
5075 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005076 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005077 }
5078
Jens Axboe9e645e112019-05-10 16:07:28 -06005079 /*
5080 * If we already have a head request, queue this one for async
5081 * submittal once the head completes. If we don't have a head but
5082 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5083 * submitted sync once the chain is complete. If none of those
5084 * conditions are true (normal request), then just queue it.
5085 */
5086 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005087 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005088
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005089 /*
5090 * Taking sequential execution of a link, draining both sides
5091 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5092 * requests in the link. So, it drains the head and the
5093 * next after the link request. The last one is done via
5094 * drain_next flag to persist the effect across calls.
5095 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005096 if (sqe_flags & IOSQE_IO_DRAIN) {
5097 head->flags |= REQ_F_IO_DRAIN;
5098 ctx->drain_next = 1;
5099 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005100 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005101 ret = -EAGAIN;
5102 goto err_req;
5103 }
5104
Jens Axboe3529d8c2019-12-19 18:24:38 -07005105 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005106 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005107 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005108 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005109 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005110 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005111 trace_io_uring_link(ctx, req, head);
5112 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005113
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005114 /* last request of a link, enqueue the link */
5115 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5116 io_queue_link_head(head);
5117 *link = NULL;
5118 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005119 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005120 if (unlikely(ctx->drain_next)) {
5121 req->flags |= REQ_F_IO_DRAIN;
5122 req->ctx->drain_next = 0;
5123 }
5124 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5125 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005126 INIT_LIST_HEAD(&req->link_list);
5127 ret = io_req_defer_prep(req, sqe);
5128 if (ret)
5129 req->flags |= REQ_F_FAIL_LINK;
5130 *link = req;
5131 } else {
5132 io_queue_sqe(req, sqe);
5133 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005134 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005135
5136 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005137}
5138
Jens Axboe9a56a232019-01-09 09:06:50 -07005139/*
5140 * Batched submission is done, ensure local IO is flushed out.
5141 */
5142static void io_submit_state_end(struct io_submit_state *state)
5143{
5144 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005145 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005146 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005147 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005148}
5149
5150/*
5151 * Start submission side cache.
5152 */
5153static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005154 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005155{
5156 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005157 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005158 state->file = NULL;
5159 state->ios_left = max_ios;
5160}
5161
Jens Axboe2b188cc2019-01-07 10:46:33 -07005162static void io_commit_sqring(struct io_ring_ctx *ctx)
5163{
Hristo Venev75b28af2019-08-26 17:23:46 +00005164 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005165
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005166 /*
5167 * Ensure any loads from the SQEs are done at this point,
5168 * since once we write the new head, the application could
5169 * write new data to them.
5170 */
5171 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005172}
5173
5174/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005175 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005176 * that is mapped by userspace. This means that care needs to be taken to
5177 * ensure that reads are stable, as we cannot rely on userspace always
5178 * being a good citizen. If members of the sqe are validated and then later
5179 * used, it's important that those reads are done through READ_ONCE() to
5180 * prevent a re-load down the line.
5181 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005182static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5183 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005184{
Hristo Venev75b28af2019-08-26 17:23:46 +00005185 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005186 unsigned head;
5187
5188 /*
5189 * The cached sq head (or cq tail) serves two purposes:
5190 *
5191 * 1) allows us to batch the cost of updating the user visible
5192 * head updates.
5193 * 2) allows the kernel side to track the head on its own, even
5194 * though the application is the one updating it.
5195 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005196 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005197 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005198 /*
5199 * All io need record the previous position, if LINK vs DARIN,
5200 * it can be used to mark the position of the first IO in the
5201 * link list.
5202 */
5203 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005204 *sqe_ptr = &ctx->sq_sqes[head];
5205 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5206 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005207 ctx->cached_sq_head++;
5208 return true;
5209 }
5210
5211 /* drop invalid entries */
5212 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005213 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005214 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005215 return false;
5216}
5217
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005218static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005219 struct file *ring_file, int ring_fd,
5220 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005221{
5222 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005223 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005224 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005225 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005226
Jens Axboec4a2ed72019-11-21 21:01:26 -07005227 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005228 if (test_bit(0, &ctx->sq_check_overflow)) {
5229 if (!list_empty(&ctx->cq_overflow_list) &&
5230 !io_cqring_overflow_flush(ctx, false))
5231 return -EBUSY;
5232 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005233
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005234 /* make sure SQ entry isn't read before tail */
5235 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005236
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005237 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5238 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005239
5240 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005241 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005242 statep = &state;
5243 }
5244
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005245 ctx->ring_fd = ring_fd;
5246 ctx->ring_file = ring_file;
5247
Jens Axboe6c271ce2019-01-10 11:22:30 -07005248 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005249 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005250 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005251 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005252
Pavel Begunkov196be952019-11-07 01:41:06 +03005253 req = io_get_req(ctx, statep);
5254 if (unlikely(!req)) {
5255 if (!submitted)
5256 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005257 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005258 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005259 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005260 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005261 break;
5262 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005263
Jens Axboed3656342019-12-18 09:50:26 -07005264 /* will complete beyond this point, count as submitted */
5265 submitted++;
5266
5267 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005268 err = -EINVAL;
5269fail_req:
5270 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005271 io_double_put_req(req);
5272 break;
5273 }
5274
5275 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005276 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005277 if (unlikely(mm_fault)) {
5278 err = -EFAULT;
5279 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005280 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005281 use_mm(ctx->sqo_mm);
5282 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005283 }
5284
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005285 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005286 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5287 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005288 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005289 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005290 }
5291
Pavel Begunkov9466f432020-01-25 22:34:01 +03005292 if (unlikely(submitted != nr)) {
5293 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5294
5295 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5296 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005297 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005298 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005299 if (statep)
5300 io_submit_state_end(&state);
5301
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005302 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5303 io_commit_sqring(ctx);
5304
Jens Axboe6c271ce2019-01-10 11:22:30 -07005305 return submitted;
5306}
5307
5308static int io_sq_thread(void *data)
5309{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005310 struct io_ring_ctx *ctx = data;
5311 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005312 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005313 mm_segment_t old_fs;
5314 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005315 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005316 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005317
Jens Axboe206aefd2019-11-07 18:27:42 -07005318 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005319
Jens Axboe6c271ce2019-01-10 11:22:30 -07005320 old_fs = get_fs();
5321 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005322 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005323
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005324 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005325 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005326 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005327
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005328 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005329 unsigned nr_events = 0;
5330
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005331 mutex_lock(&ctx->uring_lock);
5332 if (!list_empty(&ctx->poll_list))
5333 io_iopoll_getevents(ctx, &nr_events, 0);
5334 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005335 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005336 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005337 }
5338
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005339 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005340
5341 /*
5342 * If submit got -EBUSY, flag us as needing the application
5343 * to enter the kernel to reap and flush events.
5344 */
5345 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005346 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005347 * Drop cur_mm before scheduling, we can't hold it for
5348 * long periods (or over schedule()). Do this before
5349 * adding ourselves to the waitqueue, as the unuse/drop
5350 * may sleep.
5351 */
5352 if (cur_mm) {
5353 unuse_mm(cur_mm);
5354 mmput(cur_mm);
5355 cur_mm = NULL;
5356 }
5357
5358 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005359 * We're polling. If we're within the defined idle
5360 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005361 * to sleep. The exception is if we got EBUSY doing
5362 * more IO, we should wait for the application to
5363 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005364 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005365 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005366 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5367 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005368 if (current->task_works)
5369 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005370 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005371 continue;
5372 }
5373
Jens Axboe6c271ce2019-01-10 11:22:30 -07005374 prepare_to_wait(&ctx->sqo_wait, &wait,
5375 TASK_INTERRUPTIBLE);
5376
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005377 /*
5378 * While doing polled IO, before going to sleep, we need
5379 * to check if there are new reqs added to poll_list, it
5380 * is because reqs may have been punted to io worker and
5381 * will be added to poll_list later, hence check the
5382 * poll_list again.
5383 */
5384 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5385 !list_empty_careful(&ctx->poll_list)) {
5386 finish_wait(&ctx->sqo_wait, &wait);
5387 continue;
5388 }
5389
Jens Axboe6c271ce2019-01-10 11:22:30 -07005390 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005391 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005392 /* make sure to read SQ tail after writing flags */
5393 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005394
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005395 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005396 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005397 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005398 finish_wait(&ctx->sqo_wait, &wait);
5399 break;
5400 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005401 if (current->task_works) {
5402 task_work_run();
5403 continue;
5404 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005405 if (signal_pending(current))
5406 flush_signals(current);
5407 schedule();
5408 finish_wait(&ctx->sqo_wait, &wait);
5409
Hristo Venev75b28af2019-08-26 17:23:46 +00005410 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005411 continue;
5412 }
5413 finish_wait(&ctx->sqo_wait, &wait);
5414
Hristo Venev75b28af2019-08-26 17:23:46 +00005415 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005416 }
5417
Jens Axboe8a4955f2019-12-09 14:52:35 -07005418 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005419 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005420 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005421 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005422 }
5423
Jens Axboeb41e9852020-02-17 09:52:41 -07005424 if (current->task_works)
5425 task_work_run();
5426
Jens Axboe6c271ce2019-01-10 11:22:30 -07005427 set_fs(old_fs);
5428 if (cur_mm) {
5429 unuse_mm(cur_mm);
5430 mmput(cur_mm);
5431 }
Jens Axboe181e4482019-11-25 08:52:30 -07005432 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005433
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005434 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005435
Jens Axboe6c271ce2019-01-10 11:22:30 -07005436 return 0;
5437}
5438
Jens Axboebda52162019-09-24 13:47:15 -06005439struct io_wait_queue {
5440 struct wait_queue_entry wq;
5441 struct io_ring_ctx *ctx;
5442 unsigned to_wait;
5443 unsigned nr_timeouts;
5444};
5445
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005446static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005447{
5448 struct io_ring_ctx *ctx = iowq->ctx;
5449
5450 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005451 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005452 * started waiting. For timeouts, we always want to return to userspace,
5453 * regardless of event count.
5454 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005455 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005456 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5457}
5458
5459static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5460 int wake_flags, void *key)
5461{
5462 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5463 wq);
5464
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005465 /* use noflush == true, as we can't safely rely on locking context */
5466 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005467 return -1;
5468
5469 return autoremove_wake_function(curr, mode, wake_flags, key);
5470}
5471
Jens Axboe2b188cc2019-01-07 10:46:33 -07005472/*
5473 * Wait until events become available, if we don't already have some. The
5474 * application must reap them itself, as they reside on the shared cq ring.
5475 */
5476static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5477 const sigset_t __user *sig, size_t sigsz)
5478{
Jens Axboebda52162019-09-24 13:47:15 -06005479 struct io_wait_queue iowq = {
5480 .wq = {
5481 .private = current,
5482 .func = io_wake_function,
5483 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5484 },
5485 .ctx = ctx,
5486 .to_wait = min_events,
5487 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005488 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005489 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005490
Jens Axboeb41e9852020-02-17 09:52:41 -07005491 do {
5492 if (io_cqring_events(ctx, false) >= min_events)
5493 return 0;
5494 if (!current->task_works)
5495 break;
5496 task_work_run();
5497 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005498
5499 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005500#ifdef CONFIG_COMPAT
5501 if (in_compat_syscall())
5502 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005503 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005504 else
5505#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005506 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005507
Jens Axboe2b188cc2019-01-07 10:46:33 -07005508 if (ret)
5509 return ret;
5510 }
5511
Jens Axboebda52162019-09-24 13:47:15 -06005512 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005513 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005514 do {
5515 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5516 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005517 if (current->task_works)
5518 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005519 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005520 break;
5521 schedule();
5522 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005523 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005524 break;
5525 }
5526 } while (1);
5527 finish_wait(&ctx->wait, &iowq.wq);
5528
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005529 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005530
Hristo Venev75b28af2019-08-26 17:23:46 +00005531 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005532}
5533
Jens Axboe6b063142019-01-10 22:13:58 -07005534static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5535{
5536#if defined(CONFIG_UNIX)
5537 if (ctx->ring_sock) {
5538 struct sock *sock = ctx->ring_sock->sk;
5539 struct sk_buff *skb;
5540
5541 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5542 kfree_skb(skb);
5543 }
5544#else
5545 int i;
5546
Jens Axboe65e19f52019-10-26 07:20:21 -06005547 for (i = 0; i < ctx->nr_user_files; i++) {
5548 struct file *file;
5549
5550 file = io_file_from_index(ctx, i);
5551 if (file)
5552 fput(file);
5553 }
Jens Axboe6b063142019-01-10 22:13:58 -07005554#endif
5555}
5556
Jens Axboe05f3fb32019-12-09 11:22:50 -07005557static void io_file_ref_kill(struct percpu_ref *ref)
5558{
5559 struct fixed_file_data *data;
5560
5561 data = container_of(ref, struct fixed_file_data, refs);
5562 complete(&data->done);
5563}
5564
Jens Axboe6b063142019-01-10 22:13:58 -07005565static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5566{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005567 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005568 unsigned nr_tables, i;
5569
Jens Axboe05f3fb32019-12-09 11:22:50 -07005570 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005571 return -ENXIO;
5572
Jens Axboe05f3fb32019-12-09 11:22:50 -07005573 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005574 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005575 wait_for_completion(&data->done);
5576 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005577 percpu_ref_exit(&data->refs);
5578
Jens Axboe6b063142019-01-10 22:13:58 -07005579 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005580 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5581 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005582 kfree(data->table[i].files);
5583 kfree(data->table);
5584 kfree(data);
5585 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005586 ctx->nr_user_files = 0;
5587 return 0;
5588}
5589
Jens Axboe6c271ce2019-01-10 11:22:30 -07005590static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5591{
5592 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005593 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005594 /*
5595 * The park is a bit of a work-around, without it we get
5596 * warning spews on shutdown with SQPOLL set and affinity
5597 * set to a single CPU.
5598 */
Jens Axboe06058632019-04-13 09:26:03 -06005599 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005600 kthread_stop(ctx->sqo_thread);
5601 ctx->sqo_thread = NULL;
5602 }
5603}
5604
Jens Axboe6b063142019-01-10 22:13:58 -07005605static void io_finish_async(struct io_ring_ctx *ctx)
5606{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005607 io_sq_thread_stop(ctx);
5608
Jens Axboe561fb042019-10-24 07:25:42 -06005609 if (ctx->io_wq) {
5610 io_wq_destroy(ctx->io_wq);
5611 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005612 }
5613}
5614
5615#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005616/*
5617 * Ensure the UNIX gc is aware of our file set, so we are certain that
5618 * the io_uring can be safely unregistered on process exit, even if we have
5619 * loops in the file referencing.
5620 */
5621static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5622{
5623 struct sock *sk = ctx->ring_sock->sk;
5624 struct scm_fp_list *fpl;
5625 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005626 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005627
5628 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5629 unsigned long inflight = ctx->user->unix_inflight + nr;
5630
5631 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5632 return -EMFILE;
5633 }
5634
5635 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5636 if (!fpl)
5637 return -ENOMEM;
5638
5639 skb = alloc_skb(0, GFP_KERNEL);
5640 if (!skb) {
5641 kfree(fpl);
5642 return -ENOMEM;
5643 }
5644
5645 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005646
Jens Axboe08a45172019-10-03 08:11:03 -06005647 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005648 fpl->user = get_uid(ctx->user);
5649 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005650 struct file *file = io_file_from_index(ctx, i + offset);
5651
5652 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005653 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005654 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005655 unix_inflight(fpl->user, fpl->fp[nr_files]);
5656 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005657 }
5658
Jens Axboe08a45172019-10-03 08:11:03 -06005659 if (nr_files) {
5660 fpl->max = SCM_MAX_FD;
5661 fpl->count = nr_files;
5662 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005663 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005664 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5665 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005666
Jens Axboe08a45172019-10-03 08:11:03 -06005667 for (i = 0; i < nr_files; i++)
5668 fput(fpl->fp[i]);
5669 } else {
5670 kfree_skb(skb);
5671 kfree(fpl);
5672 }
Jens Axboe6b063142019-01-10 22:13:58 -07005673
5674 return 0;
5675}
5676
5677/*
5678 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5679 * causes regular reference counting to break down. We rely on the UNIX
5680 * garbage collection to take care of this problem for us.
5681 */
5682static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5683{
5684 unsigned left, total;
5685 int ret = 0;
5686
5687 total = 0;
5688 left = ctx->nr_user_files;
5689 while (left) {
5690 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005691
5692 ret = __io_sqe_files_scm(ctx, this_files, total);
5693 if (ret)
5694 break;
5695 left -= this_files;
5696 total += this_files;
5697 }
5698
5699 if (!ret)
5700 return 0;
5701
5702 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005703 struct file *file = io_file_from_index(ctx, total);
5704
5705 if (file)
5706 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005707 total++;
5708 }
5709
5710 return ret;
5711}
5712#else
5713static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5714{
5715 return 0;
5716}
5717#endif
5718
Jens Axboe65e19f52019-10-26 07:20:21 -06005719static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5720 unsigned nr_files)
5721{
5722 int i;
5723
5724 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005725 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005726 unsigned this_files;
5727
5728 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5729 table->files = kcalloc(this_files, sizeof(struct file *),
5730 GFP_KERNEL);
5731 if (!table->files)
5732 break;
5733 nr_files -= this_files;
5734 }
5735
5736 if (i == nr_tables)
5737 return 0;
5738
5739 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005740 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005741 kfree(table->files);
5742 }
5743 return 1;
5744}
5745
Jens Axboe05f3fb32019-12-09 11:22:50 -07005746static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005747{
5748#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005749 struct sock *sock = ctx->ring_sock->sk;
5750 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5751 struct sk_buff *skb;
5752 int i;
5753
5754 __skb_queue_head_init(&list);
5755
5756 /*
5757 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5758 * remove this entry and rearrange the file array.
5759 */
5760 skb = skb_dequeue(head);
5761 while (skb) {
5762 struct scm_fp_list *fp;
5763
5764 fp = UNIXCB(skb).fp;
5765 for (i = 0; i < fp->count; i++) {
5766 int left;
5767
5768 if (fp->fp[i] != file)
5769 continue;
5770
5771 unix_notinflight(fp->user, fp->fp[i]);
5772 left = fp->count - 1 - i;
5773 if (left) {
5774 memmove(&fp->fp[i], &fp->fp[i + 1],
5775 left * sizeof(struct file *));
5776 }
5777 fp->count--;
5778 if (!fp->count) {
5779 kfree_skb(skb);
5780 skb = NULL;
5781 } else {
5782 __skb_queue_tail(&list, skb);
5783 }
5784 fput(file);
5785 file = NULL;
5786 break;
5787 }
5788
5789 if (!file)
5790 break;
5791
5792 __skb_queue_tail(&list, skb);
5793
5794 skb = skb_dequeue(head);
5795 }
5796
5797 if (skb_peek(&list)) {
5798 spin_lock_irq(&head->lock);
5799 while ((skb = __skb_dequeue(&list)) != NULL)
5800 __skb_queue_tail(head, skb);
5801 spin_unlock_irq(&head->lock);
5802 }
5803#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005804 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005805#endif
5806}
5807
Jens Axboe05f3fb32019-12-09 11:22:50 -07005808struct io_file_put {
5809 struct llist_node llist;
5810 struct file *file;
5811 struct completion *done;
5812};
5813
Jens Axboe2faf8522020-02-04 19:54:55 -07005814static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005815{
5816 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005817 struct llist_node *node;
5818
Jens Axboe05f3fb32019-12-09 11:22:50 -07005819 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5820 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5821 io_ring_file_put(data->ctx, pfile->file);
5822 if (pfile->done)
5823 complete(pfile->done);
5824 else
5825 kfree(pfile);
5826 }
5827 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005828}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005829
Jens Axboe2faf8522020-02-04 19:54:55 -07005830static void io_ring_file_ref_switch(struct work_struct *work)
5831{
5832 struct fixed_file_data *data;
5833
5834 data = container_of(work, struct fixed_file_data, ref_work);
5835 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005836 percpu_ref_switch_to_percpu(&data->refs);
5837}
5838
5839static void io_file_data_ref_zero(struct percpu_ref *ref)
5840{
5841 struct fixed_file_data *data;
5842
5843 data = container_of(ref, struct fixed_file_data, refs);
5844
Jens Axboe2faf8522020-02-04 19:54:55 -07005845 /*
5846 * We can't safely switch from inside this context, punt to wq. If
5847 * the table ref is going away, the table is being unregistered.
5848 * Don't queue up the async work for that case, the caller will
5849 * handle it.
5850 */
5851 if (!percpu_ref_is_dying(&data->refs))
5852 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005853}
5854
5855static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5856 unsigned nr_args)
5857{
5858 __s32 __user *fds = (__s32 __user *) arg;
5859 unsigned nr_tables;
5860 struct file *file;
5861 int fd, ret = 0;
5862 unsigned i;
5863
5864 if (ctx->file_data)
5865 return -EBUSY;
5866 if (!nr_args)
5867 return -EINVAL;
5868 if (nr_args > IORING_MAX_FIXED_FILES)
5869 return -EMFILE;
5870
5871 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5872 if (!ctx->file_data)
5873 return -ENOMEM;
5874 ctx->file_data->ctx = ctx;
5875 init_completion(&ctx->file_data->done);
5876
5877 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5878 ctx->file_data->table = kcalloc(nr_tables,
5879 sizeof(struct fixed_file_table),
5880 GFP_KERNEL);
5881 if (!ctx->file_data->table) {
5882 kfree(ctx->file_data);
5883 ctx->file_data = NULL;
5884 return -ENOMEM;
5885 }
5886
5887 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5888 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5889 kfree(ctx->file_data->table);
5890 kfree(ctx->file_data);
5891 ctx->file_data = NULL;
5892 return -ENOMEM;
5893 }
5894 ctx->file_data->put_llist.first = NULL;
5895 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5896
5897 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5898 percpu_ref_exit(&ctx->file_data->refs);
5899 kfree(ctx->file_data->table);
5900 kfree(ctx->file_data);
5901 ctx->file_data = NULL;
5902 return -ENOMEM;
5903 }
5904
5905 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5906 struct fixed_file_table *table;
5907 unsigned index;
5908
5909 ret = -EFAULT;
5910 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5911 break;
5912 /* allow sparse sets */
5913 if (fd == -1) {
5914 ret = 0;
5915 continue;
5916 }
5917
5918 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5919 index = i & IORING_FILE_TABLE_MASK;
5920 file = fget(fd);
5921
5922 ret = -EBADF;
5923 if (!file)
5924 break;
5925
5926 /*
5927 * Don't allow io_uring instances to be registered. If UNIX
5928 * isn't enabled, then this causes a reference cycle and this
5929 * instance can never get freed. If UNIX is enabled we'll
5930 * handle it just fine, but there's still no point in allowing
5931 * a ring fd as it doesn't support regular read/write anyway.
5932 */
5933 if (file->f_op == &io_uring_fops) {
5934 fput(file);
5935 break;
5936 }
5937 ret = 0;
5938 table->files[index] = file;
5939 }
5940
5941 if (ret) {
5942 for (i = 0; i < ctx->nr_user_files; i++) {
5943 file = io_file_from_index(ctx, i);
5944 if (file)
5945 fput(file);
5946 }
5947 for (i = 0; i < nr_tables; i++)
5948 kfree(ctx->file_data->table[i].files);
5949
5950 kfree(ctx->file_data->table);
5951 kfree(ctx->file_data);
5952 ctx->file_data = NULL;
5953 ctx->nr_user_files = 0;
5954 return ret;
5955 }
5956
5957 ret = io_sqe_files_scm(ctx);
5958 if (ret)
5959 io_sqe_files_unregister(ctx);
5960
5961 return ret;
5962}
5963
Jens Axboec3a31e62019-10-03 13:59:56 -06005964static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5965 int index)
5966{
5967#if defined(CONFIG_UNIX)
5968 struct sock *sock = ctx->ring_sock->sk;
5969 struct sk_buff_head *head = &sock->sk_receive_queue;
5970 struct sk_buff *skb;
5971
5972 /*
5973 * See if we can merge this file into an existing skb SCM_RIGHTS
5974 * file set. If there's no room, fall back to allocating a new skb
5975 * and filling it in.
5976 */
5977 spin_lock_irq(&head->lock);
5978 skb = skb_peek(head);
5979 if (skb) {
5980 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5981
5982 if (fpl->count < SCM_MAX_FD) {
5983 __skb_unlink(skb, head);
5984 spin_unlock_irq(&head->lock);
5985 fpl->fp[fpl->count] = get_file(file);
5986 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5987 fpl->count++;
5988 spin_lock_irq(&head->lock);
5989 __skb_queue_head(head, skb);
5990 } else {
5991 skb = NULL;
5992 }
5993 }
5994 spin_unlock_irq(&head->lock);
5995
5996 if (skb) {
5997 fput(file);
5998 return 0;
5999 }
6000
6001 return __io_sqe_files_scm(ctx, 1, index);
6002#else
6003 return 0;
6004#endif
6005}
6006
Jens Axboe05f3fb32019-12-09 11:22:50 -07006007static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006008{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006009 struct fixed_file_data *data;
6010
Jens Axboedd3db2a2020-02-26 10:23:43 -07006011 /*
6012 * Juggle reference to ensure we hit zero, if needed, so we can
6013 * switch back to percpu mode
6014 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006015 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006016 percpu_ref_put(&data->refs);
6017 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006018}
6019
6020static bool io_queue_file_removal(struct fixed_file_data *data,
6021 struct file *file)
6022{
6023 struct io_file_put *pfile, pfile_stack;
6024 DECLARE_COMPLETION_ONSTACK(done);
6025
6026 /*
6027 * If we fail allocating the struct we need for doing async reomval
6028 * of this file, just punt to sync and wait for it.
6029 */
6030 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6031 if (!pfile) {
6032 pfile = &pfile_stack;
6033 pfile->done = &done;
6034 }
6035
6036 pfile->file = file;
6037 llist_add(&pfile->llist, &data->put_llist);
6038
6039 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006040 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006041 wait_for_completion(&done);
6042 flush_work(&data->ref_work);
6043 return false;
6044 }
6045
6046 return true;
6047}
6048
6049static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6050 struct io_uring_files_update *up,
6051 unsigned nr_args)
6052{
6053 struct fixed_file_data *data = ctx->file_data;
6054 bool ref_switch = false;
6055 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006056 __s32 __user *fds;
6057 int fd, i, err;
6058 __u32 done;
6059
Jens Axboe05f3fb32019-12-09 11:22:50 -07006060 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006061 return -EOVERFLOW;
6062 if (done > ctx->nr_user_files)
6063 return -EINVAL;
6064
6065 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006066 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006067 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006068 struct fixed_file_table *table;
6069 unsigned index;
6070
Jens Axboec3a31e62019-10-03 13:59:56 -06006071 err = 0;
6072 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6073 err = -EFAULT;
6074 break;
6075 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006076 i = array_index_nospec(up->offset, ctx->nr_user_files);
6077 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006078 index = i & IORING_FILE_TABLE_MASK;
6079 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006080 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006081 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006082 if (io_queue_file_removal(data, file))
6083 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006084 }
6085 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006086 file = fget(fd);
6087 if (!file) {
6088 err = -EBADF;
6089 break;
6090 }
6091 /*
6092 * Don't allow io_uring instances to be registered. If
6093 * UNIX isn't enabled, then this causes a reference
6094 * cycle and this instance can never get freed. If UNIX
6095 * is enabled we'll handle it just fine, but there's
6096 * still no point in allowing a ring fd as it doesn't
6097 * support regular read/write anyway.
6098 */
6099 if (file->f_op == &io_uring_fops) {
6100 fput(file);
6101 err = -EBADF;
6102 break;
6103 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006104 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006105 err = io_sqe_file_register(ctx, file, i);
6106 if (err)
6107 break;
6108 }
6109 nr_args--;
6110 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006111 up->offset++;
6112 }
6113
Jens Axboedd3db2a2020-02-26 10:23:43 -07006114 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006115 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006116
6117 return done ? done : err;
6118}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006119static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6120 unsigned nr_args)
6121{
6122 struct io_uring_files_update up;
6123
6124 if (!ctx->file_data)
6125 return -ENXIO;
6126 if (!nr_args)
6127 return -EINVAL;
6128 if (copy_from_user(&up, arg, sizeof(up)))
6129 return -EFAULT;
6130 if (up.resv)
6131 return -EINVAL;
6132
6133 return __io_sqe_files_update(ctx, &up, nr_args);
6134}
Jens Axboec3a31e62019-10-03 13:59:56 -06006135
Jens Axboe7d723062019-11-12 22:31:31 -07006136static void io_put_work(struct io_wq_work *work)
6137{
6138 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6139
6140 io_put_req(req);
6141}
6142
6143static void io_get_work(struct io_wq_work *work)
6144{
6145 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6146
6147 refcount_inc(&req->refs);
6148}
6149
Pavel Begunkov24369c22020-01-28 03:15:48 +03006150static int io_init_wq_offload(struct io_ring_ctx *ctx,
6151 struct io_uring_params *p)
6152{
6153 struct io_wq_data data;
6154 struct fd f;
6155 struct io_ring_ctx *ctx_attach;
6156 unsigned int concurrency;
6157 int ret = 0;
6158
6159 data.user = ctx->user;
6160 data.get_work = io_get_work;
6161 data.put_work = io_put_work;
6162
6163 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6164 /* Do QD, or 4 * CPUS, whatever is smallest */
6165 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6166
6167 ctx->io_wq = io_wq_create(concurrency, &data);
6168 if (IS_ERR(ctx->io_wq)) {
6169 ret = PTR_ERR(ctx->io_wq);
6170 ctx->io_wq = NULL;
6171 }
6172 return ret;
6173 }
6174
6175 f = fdget(p->wq_fd);
6176 if (!f.file)
6177 return -EBADF;
6178
6179 if (f.file->f_op != &io_uring_fops) {
6180 ret = -EINVAL;
6181 goto out_fput;
6182 }
6183
6184 ctx_attach = f.file->private_data;
6185 /* @io_wq is protected by holding the fd */
6186 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6187 ret = -EINVAL;
6188 goto out_fput;
6189 }
6190
6191 ctx->io_wq = ctx_attach->io_wq;
6192out_fput:
6193 fdput(f);
6194 return ret;
6195}
6196
Jens Axboe6c271ce2019-01-10 11:22:30 -07006197static int io_sq_offload_start(struct io_ring_ctx *ctx,
6198 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006199{
6200 int ret;
6201
Jens Axboe6c271ce2019-01-10 11:22:30 -07006202 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006203 mmgrab(current->mm);
6204 ctx->sqo_mm = current->mm;
6205
Jens Axboe6c271ce2019-01-10 11:22:30 -07006206 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006207 ret = -EPERM;
6208 if (!capable(CAP_SYS_ADMIN))
6209 goto err;
6210
Jens Axboe917257d2019-04-13 09:28:55 -06006211 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6212 if (!ctx->sq_thread_idle)
6213 ctx->sq_thread_idle = HZ;
6214
Jens Axboe6c271ce2019-01-10 11:22:30 -07006215 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006216 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006217
Jens Axboe917257d2019-04-13 09:28:55 -06006218 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006219 if (cpu >= nr_cpu_ids)
6220 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006221 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006222 goto err;
6223
Jens Axboe6c271ce2019-01-10 11:22:30 -07006224 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6225 ctx, cpu,
6226 "io_uring-sq");
6227 } else {
6228 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6229 "io_uring-sq");
6230 }
6231 if (IS_ERR(ctx->sqo_thread)) {
6232 ret = PTR_ERR(ctx->sqo_thread);
6233 ctx->sqo_thread = NULL;
6234 goto err;
6235 }
6236 wake_up_process(ctx->sqo_thread);
6237 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6238 /* Can't have SQ_AFF without SQPOLL */
6239 ret = -EINVAL;
6240 goto err;
6241 }
6242
Pavel Begunkov24369c22020-01-28 03:15:48 +03006243 ret = io_init_wq_offload(ctx, p);
6244 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006245 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006246
6247 return 0;
6248err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006249 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006250 mmdrop(ctx->sqo_mm);
6251 ctx->sqo_mm = NULL;
6252 return ret;
6253}
6254
6255static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6256{
6257 atomic_long_sub(nr_pages, &user->locked_vm);
6258}
6259
6260static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6261{
6262 unsigned long page_limit, cur_pages, new_pages;
6263
6264 /* Don't allow more pages than we can safely lock */
6265 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6266
6267 do {
6268 cur_pages = atomic_long_read(&user->locked_vm);
6269 new_pages = cur_pages + nr_pages;
6270 if (new_pages > page_limit)
6271 return -ENOMEM;
6272 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6273 new_pages) != cur_pages);
6274
6275 return 0;
6276}
6277
6278static void io_mem_free(void *ptr)
6279{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006280 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006281
Mark Rutland52e04ef2019-04-30 17:30:21 +01006282 if (!ptr)
6283 return;
6284
6285 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006286 if (put_page_testzero(page))
6287 free_compound_page(page);
6288}
6289
6290static void *io_mem_alloc(size_t size)
6291{
6292 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6293 __GFP_NORETRY;
6294
6295 return (void *) __get_free_pages(gfp_flags, get_order(size));
6296}
6297
Hristo Venev75b28af2019-08-26 17:23:46 +00006298static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6299 size_t *sq_offset)
6300{
6301 struct io_rings *rings;
6302 size_t off, sq_array_size;
6303
6304 off = struct_size(rings, cqes, cq_entries);
6305 if (off == SIZE_MAX)
6306 return SIZE_MAX;
6307
6308#ifdef CONFIG_SMP
6309 off = ALIGN(off, SMP_CACHE_BYTES);
6310 if (off == 0)
6311 return SIZE_MAX;
6312#endif
6313
6314 sq_array_size = array_size(sizeof(u32), sq_entries);
6315 if (sq_array_size == SIZE_MAX)
6316 return SIZE_MAX;
6317
6318 if (check_add_overflow(off, sq_array_size, &off))
6319 return SIZE_MAX;
6320
6321 if (sq_offset)
6322 *sq_offset = off;
6323
6324 return off;
6325}
6326
Jens Axboe2b188cc2019-01-07 10:46:33 -07006327static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6328{
Hristo Venev75b28af2019-08-26 17:23:46 +00006329 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006330
Hristo Venev75b28af2019-08-26 17:23:46 +00006331 pages = (size_t)1 << get_order(
6332 rings_size(sq_entries, cq_entries, NULL));
6333 pages += (size_t)1 << get_order(
6334 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006335
Hristo Venev75b28af2019-08-26 17:23:46 +00006336 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006337}
6338
Jens Axboeedafcce2019-01-09 09:16:05 -07006339static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6340{
6341 int i, j;
6342
6343 if (!ctx->user_bufs)
6344 return -ENXIO;
6345
6346 for (i = 0; i < ctx->nr_user_bufs; i++) {
6347 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6348
6349 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006350 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006351
6352 if (ctx->account_mem)
6353 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006354 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006355 imu->nr_bvecs = 0;
6356 }
6357
6358 kfree(ctx->user_bufs);
6359 ctx->user_bufs = NULL;
6360 ctx->nr_user_bufs = 0;
6361 return 0;
6362}
6363
6364static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6365 void __user *arg, unsigned index)
6366{
6367 struct iovec __user *src;
6368
6369#ifdef CONFIG_COMPAT
6370 if (ctx->compat) {
6371 struct compat_iovec __user *ciovs;
6372 struct compat_iovec ciov;
6373
6374 ciovs = (struct compat_iovec __user *) arg;
6375 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6376 return -EFAULT;
6377
Jens Axboed55e5f52019-12-11 16:12:15 -07006378 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006379 dst->iov_len = ciov.iov_len;
6380 return 0;
6381 }
6382#endif
6383 src = (struct iovec __user *) arg;
6384 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6385 return -EFAULT;
6386 return 0;
6387}
6388
6389static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6390 unsigned nr_args)
6391{
6392 struct vm_area_struct **vmas = NULL;
6393 struct page **pages = NULL;
6394 int i, j, got_pages = 0;
6395 int ret = -EINVAL;
6396
6397 if (ctx->user_bufs)
6398 return -EBUSY;
6399 if (!nr_args || nr_args > UIO_MAXIOV)
6400 return -EINVAL;
6401
6402 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6403 GFP_KERNEL);
6404 if (!ctx->user_bufs)
6405 return -ENOMEM;
6406
6407 for (i = 0; i < nr_args; i++) {
6408 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6409 unsigned long off, start, end, ubuf;
6410 int pret, nr_pages;
6411 struct iovec iov;
6412 size_t size;
6413
6414 ret = io_copy_iov(ctx, &iov, arg, i);
6415 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006416 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006417
6418 /*
6419 * Don't impose further limits on the size and buffer
6420 * constraints here, we'll -EINVAL later when IO is
6421 * submitted if they are wrong.
6422 */
6423 ret = -EFAULT;
6424 if (!iov.iov_base || !iov.iov_len)
6425 goto err;
6426
6427 /* arbitrary limit, but we need something */
6428 if (iov.iov_len > SZ_1G)
6429 goto err;
6430
6431 ubuf = (unsigned long) iov.iov_base;
6432 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6433 start = ubuf >> PAGE_SHIFT;
6434 nr_pages = end - start;
6435
6436 if (ctx->account_mem) {
6437 ret = io_account_mem(ctx->user, nr_pages);
6438 if (ret)
6439 goto err;
6440 }
6441
6442 ret = 0;
6443 if (!pages || nr_pages > got_pages) {
6444 kfree(vmas);
6445 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006446 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006447 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006448 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006449 sizeof(struct vm_area_struct *),
6450 GFP_KERNEL);
6451 if (!pages || !vmas) {
6452 ret = -ENOMEM;
6453 if (ctx->account_mem)
6454 io_unaccount_mem(ctx->user, nr_pages);
6455 goto err;
6456 }
6457 got_pages = nr_pages;
6458 }
6459
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006460 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006461 GFP_KERNEL);
6462 ret = -ENOMEM;
6463 if (!imu->bvec) {
6464 if (ctx->account_mem)
6465 io_unaccount_mem(ctx->user, nr_pages);
6466 goto err;
6467 }
6468
6469 ret = 0;
6470 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006471 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006472 FOLL_WRITE | FOLL_LONGTERM,
6473 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006474 if (pret == nr_pages) {
6475 /* don't support file backed memory */
6476 for (j = 0; j < nr_pages; j++) {
6477 struct vm_area_struct *vma = vmas[j];
6478
6479 if (vma->vm_file &&
6480 !is_file_hugepages(vma->vm_file)) {
6481 ret = -EOPNOTSUPP;
6482 break;
6483 }
6484 }
6485 } else {
6486 ret = pret < 0 ? pret : -EFAULT;
6487 }
6488 up_read(&current->mm->mmap_sem);
6489 if (ret) {
6490 /*
6491 * if we did partial map, or found file backed vmas,
6492 * release any pages we did get
6493 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006494 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006495 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006496 if (ctx->account_mem)
6497 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006498 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006499 goto err;
6500 }
6501
6502 off = ubuf & ~PAGE_MASK;
6503 size = iov.iov_len;
6504 for (j = 0; j < nr_pages; j++) {
6505 size_t vec_len;
6506
6507 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6508 imu->bvec[j].bv_page = pages[j];
6509 imu->bvec[j].bv_len = vec_len;
6510 imu->bvec[j].bv_offset = off;
6511 off = 0;
6512 size -= vec_len;
6513 }
6514 /* store original address for later verification */
6515 imu->ubuf = ubuf;
6516 imu->len = iov.iov_len;
6517 imu->nr_bvecs = nr_pages;
6518
6519 ctx->nr_user_bufs++;
6520 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006521 kvfree(pages);
6522 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006523 return 0;
6524err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006525 kvfree(pages);
6526 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006527 io_sqe_buffer_unregister(ctx);
6528 return ret;
6529}
6530
Jens Axboe9b402842019-04-11 11:45:41 -06006531static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6532{
6533 __s32 __user *fds = arg;
6534 int fd;
6535
6536 if (ctx->cq_ev_fd)
6537 return -EBUSY;
6538
6539 if (copy_from_user(&fd, fds, sizeof(*fds)))
6540 return -EFAULT;
6541
6542 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6543 if (IS_ERR(ctx->cq_ev_fd)) {
6544 int ret = PTR_ERR(ctx->cq_ev_fd);
6545 ctx->cq_ev_fd = NULL;
6546 return ret;
6547 }
6548
6549 return 0;
6550}
6551
6552static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6553{
6554 if (ctx->cq_ev_fd) {
6555 eventfd_ctx_put(ctx->cq_ev_fd);
6556 ctx->cq_ev_fd = NULL;
6557 return 0;
6558 }
6559
6560 return -ENXIO;
6561}
6562
Jens Axboe2b188cc2019-01-07 10:46:33 -07006563static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6564{
Jens Axboe6b063142019-01-10 22:13:58 -07006565 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006566 if (ctx->sqo_mm)
6567 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006568
6569 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006570 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006571 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006572 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006573 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006574
Jens Axboe2b188cc2019-01-07 10:46:33 -07006575#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006576 if (ctx->ring_sock) {
6577 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006578 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006579 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006580#endif
6581
Hristo Venev75b28af2019-08-26 17:23:46 +00006582 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006583 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006584
6585 percpu_ref_exit(&ctx->refs);
6586 if (ctx->account_mem)
6587 io_unaccount_mem(ctx->user,
6588 ring_pages(ctx->sq_entries, ctx->cq_entries));
6589 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006590 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006591 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006592 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006593 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006594 kfree(ctx);
6595}
6596
6597static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6598{
6599 struct io_ring_ctx *ctx = file->private_data;
6600 __poll_t mask = 0;
6601
6602 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006603 /*
6604 * synchronizes with barrier from wq_has_sleeper call in
6605 * io_commit_cqring
6606 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006607 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006608 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6609 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006610 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006611 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006612 mask |= EPOLLIN | EPOLLRDNORM;
6613
6614 return mask;
6615}
6616
6617static int io_uring_fasync(int fd, struct file *file, int on)
6618{
6619 struct io_ring_ctx *ctx = file->private_data;
6620
6621 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6622}
6623
Jens Axboe071698e2020-01-28 10:04:42 -07006624static int io_remove_personalities(int id, void *p, void *data)
6625{
6626 struct io_ring_ctx *ctx = data;
6627 const struct cred *cred;
6628
6629 cred = idr_remove(&ctx->personality_idr, id);
6630 if (cred)
6631 put_cred(cred);
6632 return 0;
6633}
6634
Jens Axboe2b188cc2019-01-07 10:46:33 -07006635static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6636{
6637 mutex_lock(&ctx->uring_lock);
6638 percpu_ref_kill(&ctx->refs);
6639 mutex_unlock(&ctx->uring_lock);
6640
Jens Axboedf069d82020-02-04 16:48:34 -07006641 /*
6642 * Wait for sq thread to idle, if we have one. It won't spin on new
6643 * work after we've killed the ctx ref above. This is important to do
6644 * before we cancel existing commands, as the thread could otherwise
6645 * be queueing new work post that. If that's work we need to cancel,
6646 * it could cause shutdown to hang.
6647 */
6648 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6649 cpu_relax();
6650
Jens Axboe5262f562019-09-17 12:26:57 -06006651 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006652 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006653
6654 if (ctx->io_wq)
6655 io_wq_cancel_all(ctx->io_wq);
6656
Jens Axboedef596e2019-01-09 08:59:42 -07006657 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006658 /* if we failed setting up the ctx, we might not have any rings */
6659 if (ctx->rings)
6660 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006661 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006662 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006663 io_ring_ctx_free(ctx);
6664}
6665
6666static int io_uring_release(struct inode *inode, struct file *file)
6667{
6668 struct io_ring_ctx *ctx = file->private_data;
6669
6670 file->private_data = NULL;
6671 io_ring_ctx_wait_and_kill(ctx);
6672 return 0;
6673}
6674
Jens Axboefcb323c2019-10-24 12:39:47 -06006675static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6676 struct files_struct *files)
6677{
6678 struct io_kiocb *req;
6679 DEFINE_WAIT(wait);
6680
6681 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006682 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006683
6684 spin_lock_irq(&ctx->inflight_lock);
6685 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006686 if (req->work.files != files)
6687 continue;
6688 /* req is being completed, ignore */
6689 if (!refcount_inc_not_zero(&req->refs))
6690 continue;
6691 cancel_req = req;
6692 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006693 }
Jens Axboe768134d2019-11-10 20:30:53 -07006694 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006695 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006696 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006697 spin_unlock_irq(&ctx->inflight_lock);
6698
Jens Axboe768134d2019-11-10 20:30:53 -07006699 /* We need to keep going until we don't find a matching req */
6700 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006701 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006702
Jens Axboe2ca10252020-02-13 17:17:35 -07006703 if (cancel_req->flags & REQ_F_OVERFLOW) {
6704 spin_lock_irq(&ctx->completion_lock);
6705 list_del(&cancel_req->list);
6706 cancel_req->flags &= ~REQ_F_OVERFLOW;
6707 if (list_empty(&ctx->cq_overflow_list)) {
6708 clear_bit(0, &ctx->sq_check_overflow);
6709 clear_bit(0, &ctx->cq_check_overflow);
6710 }
6711 spin_unlock_irq(&ctx->completion_lock);
6712
6713 WRITE_ONCE(ctx->rings->cq_overflow,
6714 atomic_inc_return(&ctx->cached_cq_overflow));
6715
6716 /*
6717 * Put inflight ref and overflow ref. If that's
6718 * all we had, then we're done with this request.
6719 */
6720 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6721 io_put_req(cancel_req);
6722 continue;
6723 }
6724 }
6725
Bob Liu2f6d9b92019-11-13 18:06:24 +08006726 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6727 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006728 schedule();
6729 }
Jens Axboe768134d2019-11-10 20:30:53 -07006730 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006731}
6732
6733static int io_uring_flush(struct file *file, void *data)
6734{
6735 struct io_ring_ctx *ctx = file->private_data;
6736
6737 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006738
6739 /*
6740 * If the task is going away, cancel work it may have pending
6741 */
6742 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6743 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6744
Jens Axboefcb323c2019-10-24 12:39:47 -06006745 return 0;
6746}
6747
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006748static void *io_uring_validate_mmap_request(struct file *file,
6749 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006750{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006751 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006752 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006753 struct page *page;
6754 void *ptr;
6755
6756 switch (offset) {
6757 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006758 case IORING_OFF_CQ_RING:
6759 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006760 break;
6761 case IORING_OFF_SQES:
6762 ptr = ctx->sq_sqes;
6763 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006764 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006765 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006766 }
6767
6768 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006769 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006770 return ERR_PTR(-EINVAL);
6771
6772 return ptr;
6773}
6774
6775#ifdef CONFIG_MMU
6776
6777static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6778{
6779 size_t sz = vma->vm_end - vma->vm_start;
6780 unsigned long pfn;
6781 void *ptr;
6782
6783 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6784 if (IS_ERR(ptr))
6785 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006786
6787 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6788 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6789}
6790
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006791#else /* !CONFIG_MMU */
6792
6793static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6794{
6795 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6796}
6797
6798static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6799{
6800 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6801}
6802
6803static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6804 unsigned long addr, unsigned long len,
6805 unsigned long pgoff, unsigned long flags)
6806{
6807 void *ptr;
6808
6809 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6810 if (IS_ERR(ptr))
6811 return PTR_ERR(ptr);
6812
6813 return (unsigned long) ptr;
6814}
6815
6816#endif /* !CONFIG_MMU */
6817
Jens Axboe2b188cc2019-01-07 10:46:33 -07006818SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6819 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6820 size_t, sigsz)
6821{
6822 struct io_ring_ctx *ctx;
6823 long ret = -EBADF;
6824 int submitted = 0;
6825 struct fd f;
6826
Jens Axboeb41e9852020-02-17 09:52:41 -07006827 if (current->task_works)
6828 task_work_run();
6829
Jens Axboe6c271ce2019-01-10 11:22:30 -07006830 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006831 return -EINVAL;
6832
6833 f = fdget(fd);
6834 if (!f.file)
6835 return -EBADF;
6836
6837 ret = -EOPNOTSUPP;
6838 if (f.file->f_op != &io_uring_fops)
6839 goto out_fput;
6840
6841 ret = -ENXIO;
6842 ctx = f.file->private_data;
6843 if (!percpu_ref_tryget(&ctx->refs))
6844 goto out_fput;
6845
Jens Axboe6c271ce2019-01-10 11:22:30 -07006846 /*
6847 * For SQ polling, the thread will do all submissions and completions.
6848 * Just return the requested submit count, and wake the thread if
6849 * we were asked to.
6850 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006851 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006852 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006853 if (!list_empty_careful(&ctx->cq_overflow_list))
6854 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006855 if (flags & IORING_ENTER_SQ_WAKEUP)
6856 wake_up(&ctx->sqo_wait);
6857 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006858 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006859 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006860
6861 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006862 /* already have mm, so io_submit_sqes() won't try to grab it */
6863 cur_mm = ctx->sqo_mm;
6864 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6865 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006866 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006867
6868 if (submitted != to_submit)
6869 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006870 }
6871 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006872 unsigned nr_events = 0;
6873
Jens Axboe2b188cc2019-01-07 10:46:33 -07006874 min_complete = min(min_complete, ctx->cq_entries);
6875
Jens Axboedef596e2019-01-09 08:59:42 -07006876 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006877 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006878 } else {
6879 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6880 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006881 }
6882
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006883out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006884 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006885out_fput:
6886 fdput(f);
6887 return submitted ? submitted : ret;
6888}
6889
Tobias Klauserbebdb652020-02-26 18:38:32 +01006890#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006891static int io_uring_show_cred(int id, void *p, void *data)
6892{
6893 const struct cred *cred = p;
6894 struct seq_file *m = data;
6895 struct user_namespace *uns = seq_user_ns(m);
6896 struct group_info *gi;
6897 kernel_cap_t cap;
6898 unsigned __capi;
6899 int g;
6900
6901 seq_printf(m, "%5d\n", id);
6902 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6903 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6904 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6905 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6906 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6907 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6908 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6909 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6910 seq_puts(m, "\n\tGroups:\t");
6911 gi = cred->group_info;
6912 for (g = 0; g < gi->ngroups; g++) {
6913 seq_put_decimal_ull(m, g ? " " : "",
6914 from_kgid_munged(uns, gi->gid[g]));
6915 }
6916 seq_puts(m, "\n\tCapEff:\t");
6917 cap = cred->cap_effective;
6918 CAP_FOR_EACH_U32(__capi)
6919 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6920 seq_putc(m, '\n');
6921 return 0;
6922}
6923
6924static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6925{
6926 int i;
6927
6928 mutex_lock(&ctx->uring_lock);
6929 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6930 for (i = 0; i < ctx->nr_user_files; i++) {
6931 struct fixed_file_table *table;
6932 struct file *f;
6933
6934 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6935 f = table->files[i & IORING_FILE_TABLE_MASK];
6936 if (f)
6937 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6938 else
6939 seq_printf(m, "%5u: <none>\n", i);
6940 }
6941 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6942 for (i = 0; i < ctx->nr_user_bufs; i++) {
6943 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6944
6945 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6946 (unsigned int) buf->len);
6947 }
6948 if (!idr_is_empty(&ctx->personality_idr)) {
6949 seq_printf(m, "Personalities:\n");
6950 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6951 }
Jens Axboed7718a92020-02-14 22:23:12 -07006952 seq_printf(m, "PollList:\n");
6953 spin_lock_irq(&ctx->completion_lock);
6954 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
6955 struct hlist_head *list = &ctx->cancel_hash[i];
6956 struct io_kiocb *req;
6957
6958 hlist_for_each_entry(req, list, hash_node)
6959 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
6960 req->task->task_works != NULL);
6961 }
6962 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07006963 mutex_unlock(&ctx->uring_lock);
6964}
6965
6966static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6967{
6968 struct io_ring_ctx *ctx = f->private_data;
6969
6970 if (percpu_ref_tryget(&ctx->refs)) {
6971 __io_uring_show_fdinfo(ctx, m);
6972 percpu_ref_put(&ctx->refs);
6973 }
6974}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006975#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006976
Jens Axboe2b188cc2019-01-07 10:46:33 -07006977static const struct file_operations io_uring_fops = {
6978 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006979 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006980 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006981#ifndef CONFIG_MMU
6982 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6983 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6984#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006985 .poll = io_uring_poll,
6986 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006987#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006988 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006989#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006990};
6991
6992static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6993 struct io_uring_params *p)
6994{
Hristo Venev75b28af2019-08-26 17:23:46 +00006995 struct io_rings *rings;
6996 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006997
Hristo Venev75b28af2019-08-26 17:23:46 +00006998 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6999 if (size == SIZE_MAX)
7000 return -EOVERFLOW;
7001
7002 rings = io_mem_alloc(size);
7003 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007004 return -ENOMEM;
7005
Hristo Venev75b28af2019-08-26 17:23:46 +00007006 ctx->rings = rings;
7007 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7008 rings->sq_ring_mask = p->sq_entries - 1;
7009 rings->cq_ring_mask = p->cq_entries - 1;
7010 rings->sq_ring_entries = p->sq_entries;
7011 rings->cq_ring_entries = p->cq_entries;
7012 ctx->sq_mask = rings->sq_ring_mask;
7013 ctx->cq_mask = rings->cq_ring_mask;
7014 ctx->sq_entries = rings->sq_ring_entries;
7015 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007016
7017 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007018 if (size == SIZE_MAX) {
7019 io_mem_free(ctx->rings);
7020 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007021 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007022 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007023
7024 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007025 if (!ctx->sq_sqes) {
7026 io_mem_free(ctx->rings);
7027 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007028 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007029 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007030
Jens Axboe2b188cc2019-01-07 10:46:33 -07007031 return 0;
7032}
7033
7034/*
7035 * Allocate an anonymous fd, this is what constitutes the application
7036 * visible backing of an io_uring instance. The application mmaps this
7037 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7038 * we have to tie this fd to a socket for file garbage collection purposes.
7039 */
7040static int io_uring_get_fd(struct io_ring_ctx *ctx)
7041{
7042 struct file *file;
7043 int ret;
7044
7045#if defined(CONFIG_UNIX)
7046 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7047 &ctx->ring_sock);
7048 if (ret)
7049 return ret;
7050#endif
7051
7052 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7053 if (ret < 0)
7054 goto err;
7055
7056 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7057 O_RDWR | O_CLOEXEC);
7058 if (IS_ERR(file)) {
7059 put_unused_fd(ret);
7060 ret = PTR_ERR(file);
7061 goto err;
7062 }
7063
7064#if defined(CONFIG_UNIX)
7065 ctx->ring_sock->file = file;
7066#endif
7067 fd_install(ret, file);
7068 return ret;
7069err:
7070#if defined(CONFIG_UNIX)
7071 sock_release(ctx->ring_sock);
7072 ctx->ring_sock = NULL;
7073#endif
7074 return ret;
7075}
7076
7077static int io_uring_create(unsigned entries, struct io_uring_params *p)
7078{
7079 struct user_struct *user = NULL;
7080 struct io_ring_ctx *ctx;
7081 bool account_mem;
7082 int ret;
7083
Jens Axboe8110c1a2019-12-28 15:39:54 -07007084 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007085 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007086 if (entries > IORING_MAX_ENTRIES) {
7087 if (!(p->flags & IORING_SETUP_CLAMP))
7088 return -EINVAL;
7089 entries = IORING_MAX_ENTRIES;
7090 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007091
7092 /*
7093 * Use twice as many entries for the CQ ring. It's possible for the
7094 * application to drive a higher depth than the size of the SQ ring,
7095 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007096 * some flexibility in overcommitting a bit. If the application has
7097 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7098 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007099 */
7100 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007101 if (p->flags & IORING_SETUP_CQSIZE) {
7102 /*
7103 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7104 * to a power-of-two, if it isn't already. We do NOT impose
7105 * any cq vs sq ring sizing.
7106 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007107 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007108 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007109 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7110 if (!(p->flags & IORING_SETUP_CLAMP))
7111 return -EINVAL;
7112 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7113 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007114 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7115 } else {
7116 p->cq_entries = 2 * p->sq_entries;
7117 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007118
7119 user = get_uid(current_user());
7120 account_mem = !capable(CAP_IPC_LOCK);
7121
7122 if (account_mem) {
7123 ret = io_account_mem(user,
7124 ring_pages(p->sq_entries, p->cq_entries));
7125 if (ret) {
7126 free_uid(user);
7127 return ret;
7128 }
7129 }
7130
7131 ctx = io_ring_ctx_alloc(p);
7132 if (!ctx) {
7133 if (account_mem)
7134 io_unaccount_mem(user, ring_pages(p->sq_entries,
7135 p->cq_entries));
7136 free_uid(user);
7137 return -ENOMEM;
7138 }
7139 ctx->compat = in_compat_syscall();
7140 ctx->account_mem = account_mem;
7141 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007142 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007143
7144 ret = io_allocate_scq_urings(ctx, p);
7145 if (ret)
7146 goto err;
7147
Jens Axboe6c271ce2019-01-10 11:22:30 -07007148 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007149 if (ret)
7150 goto err;
7151
Jens Axboe2b188cc2019-01-07 10:46:33 -07007152 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007153 p->sq_off.head = offsetof(struct io_rings, sq.head);
7154 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7155 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7156 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7157 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7158 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7159 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007160
7161 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007162 p->cq_off.head = offsetof(struct io_rings, cq.head);
7163 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7164 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7165 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7166 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7167 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007168
Jens Axboe044c1ab2019-10-28 09:15:33 -06007169 /*
7170 * Install ring fd as the very last thing, so we don't risk someone
7171 * having closed it before we finish setup
7172 */
7173 ret = io_uring_get_fd(ctx);
7174 if (ret < 0)
7175 goto err;
7176
Jens Axboeda8c9692019-12-02 18:51:26 -07007177 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007178 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007179 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007180 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007181 return ret;
7182err:
7183 io_ring_ctx_wait_and_kill(ctx);
7184 return ret;
7185}
7186
7187/*
7188 * Sets up an aio uring context, and returns the fd. Applications asks for a
7189 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7190 * params structure passed in.
7191 */
7192static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7193{
7194 struct io_uring_params p;
7195 long ret;
7196 int i;
7197
7198 if (copy_from_user(&p, params, sizeof(p)))
7199 return -EFAULT;
7200 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7201 if (p.resv[i])
7202 return -EINVAL;
7203 }
7204
Jens Axboe6c271ce2019-01-10 11:22:30 -07007205 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007206 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007207 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007208 return -EINVAL;
7209
7210 ret = io_uring_create(entries, &p);
7211 if (ret < 0)
7212 return ret;
7213
7214 if (copy_to_user(params, &p, sizeof(p)))
7215 return -EFAULT;
7216
7217 return ret;
7218}
7219
7220SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7221 struct io_uring_params __user *, params)
7222{
7223 return io_uring_setup(entries, params);
7224}
7225
Jens Axboe66f4af92020-01-16 15:36:52 -07007226static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7227{
7228 struct io_uring_probe *p;
7229 size_t size;
7230 int i, ret;
7231
7232 size = struct_size(p, ops, nr_args);
7233 if (size == SIZE_MAX)
7234 return -EOVERFLOW;
7235 p = kzalloc(size, GFP_KERNEL);
7236 if (!p)
7237 return -ENOMEM;
7238
7239 ret = -EFAULT;
7240 if (copy_from_user(p, arg, size))
7241 goto out;
7242 ret = -EINVAL;
7243 if (memchr_inv(p, 0, size))
7244 goto out;
7245
7246 p->last_op = IORING_OP_LAST - 1;
7247 if (nr_args > IORING_OP_LAST)
7248 nr_args = IORING_OP_LAST;
7249
7250 for (i = 0; i < nr_args; i++) {
7251 p->ops[i].op = i;
7252 if (!io_op_defs[i].not_supported)
7253 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7254 }
7255 p->ops_len = i;
7256
7257 ret = 0;
7258 if (copy_to_user(arg, p, size))
7259 ret = -EFAULT;
7260out:
7261 kfree(p);
7262 return ret;
7263}
7264
Jens Axboe071698e2020-01-28 10:04:42 -07007265static int io_register_personality(struct io_ring_ctx *ctx)
7266{
7267 const struct cred *creds = get_current_cred();
7268 int id;
7269
7270 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7271 USHRT_MAX, GFP_KERNEL);
7272 if (id < 0)
7273 put_cred(creds);
7274 return id;
7275}
7276
7277static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7278{
7279 const struct cred *old_creds;
7280
7281 old_creds = idr_remove(&ctx->personality_idr, id);
7282 if (old_creds) {
7283 put_cred(old_creds);
7284 return 0;
7285 }
7286
7287 return -EINVAL;
7288}
7289
7290static bool io_register_op_must_quiesce(int op)
7291{
7292 switch (op) {
7293 case IORING_UNREGISTER_FILES:
7294 case IORING_REGISTER_FILES_UPDATE:
7295 case IORING_REGISTER_PROBE:
7296 case IORING_REGISTER_PERSONALITY:
7297 case IORING_UNREGISTER_PERSONALITY:
7298 return false;
7299 default:
7300 return true;
7301 }
7302}
7303
Jens Axboeedafcce2019-01-09 09:16:05 -07007304static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7305 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007306 __releases(ctx->uring_lock)
7307 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007308{
7309 int ret;
7310
Jens Axboe35fa71a2019-04-22 10:23:23 -06007311 /*
7312 * We're inside the ring mutex, if the ref is already dying, then
7313 * someone else killed the ctx or is already going through
7314 * io_uring_register().
7315 */
7316 if (percpu_ref_is_dying(&ctx->refs))
7317 return -ENXIO;
7318
Jens Axboe071698e2020-01-28 10:04:42 -07007319 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007320 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007321
Jens Axboe05f3fb32019-12-09 11:22:50 -07007322 /*
7323 * Drop uring mutex before waiting for references to exit. If
7324 * another thread is currently inside io_uring_enter() it might
7325 * need to grab the uring_lock to make progress. If we hold it
7326 * here across the drain wait, then we can deadlock. It's safe
7327 * to drop the mutex here, since no new references will come in
7328 * after we've killed the percpu ref.
7329 */
7330 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007331 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007332 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007333 if (ret) {
7334 percpu_ref_resurrect(&ctx->refs);
7335 ret = -EINTR;
7336 goto out;
7337 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007338 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007339
7340 switch (opcode) {
7341 case IORING_REGISTER_BUFFERS:
7342 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7343 break;
7344 case IORING_UNREGISTER_BUFFERS:
7345 ret = -EINVAL;
7346 if (arg || nr_args)
7347 break;
7348 ret = io_sqe_buffer_unregister(ctx);
7349 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007350 case IORING_REGISTER_FILES:
7351 ret = io_sqe_files_register(ctx, arg, nr_args);
7352 break;
7353 case IORING_UNREGISTER_FILES:
7354 ret = -EINVAL;
7355 if (arg || nr_args)
7356 break;
7357 ret = io_sqe_files_unregister(ctx);
7358 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007359 case IORING_REGISTER_FILES_UPDATE:
7360 ret = io_sqe_files_update(ctx, arg, nr_args);
7361 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007362 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007363 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007364 ret = -EINVAL;
7365 if (nr_args != 1)
7366 break;
7367 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007368 if (ret)
7369 break;
7370 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7371 ctx->eventfd_async = 1;
7372 else
7373 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007374 break;
7375 case IORING_UNREGISTER_EVENTFD:
7376 ret = -EINVAL;
7377 if (arg || nr_args)
7378 break;
7379 ret = io_eventfd_unregister(ctx);
7380 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007381 case IORING_REGISTER_PROBE:
7382 ret = -EINVAL;
7383 if (!arg || nr_args > 256)
7384 break;
7385 ret = io_probe(ctx, arg, nr_args);
7386 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007387 case IORING_REGISTER_PERSONALITY:
7388 ret = -EINVAL;
7389 if (arg || nr_args)
7390 break;
7391 ret = io_register_personality(ctx);
7392 break;
7393 case IORING_UNREGISTER_PERSONALITY:
7394 ret = -EINVAL;
7395 if (arg)
7396 break;
7397 ret = io_unregister_personality(ctx, nr_args);
7398 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007399 default:
7400 ret = -EINVAL;
7401 break;
7402 }
7403
Jens Axboe071698e2020-01-28 10:04:42 -07007404 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007405 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007406 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007407out:
7408 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007409 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007410 return ret;
7411}
7412
7413SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7414 void __user *, arg, unsigned int, nr_args)
7415{
7416 struct io_ring_ctx *ctx;
7417 long ret = -EBADF;
7418 struct fd f;
7419
7420 f = fdget(fd);
7421 if (!f.file)
7422 return -EBADF;
7423
7424 ret = -EOPNOTSUPP;
7425 if (f.file->f_op != &io_uring_fops)
7426 goto out_fput;
7427
7428 ctx = f.file->private_data;
7429
7430 mutex_lock(&ctx->uring_lock);
7431 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7432 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007433 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7434 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007435out_fput:
7436 fdput(f);
7437 return ret;
7438}
7439
Jens Axboe2b188cc2019-01-07 10:46:33 -07007440static int __init io_uring_init(void)
7441{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007442#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7443 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7444 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7445} while (0)
7446
7447#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7448 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7449 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7450 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7451 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7452 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7453 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7454 BUILD_BUG_SQE_ELEM(8, __u64, off);
7455 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7456 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007457 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007458 BUILD_BUG_SQE_ELEM(24, __u32, len);
7459 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7460 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7461 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7462 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7463 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7464 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7465 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7466 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7467 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7468 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7469 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7470 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7471 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007472 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007473 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7474 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7475 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007476 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007477
Jens Axboed3656342019-12-18 09:50:26 -07007478 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007479 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7480 return 0;
7481};
7482__initcall(io_uring_init);