blob: d70bc7747e84daf953e9ba05cea054e73ba63056 [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
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +03001002static inline void io_prep_next_work(struct io_kiocb *req,
1003 struct io_kiocb **link)
1004{
1005 const struct io_op_def *def = &io_op_defs[req->opcode];
1006
1007 if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file)
1008 req->work.flags |= IO_WQ_WORK_UNBOUND;
1009
1010 *link = io_prep_linked_timeout(req);
1011}
1012
Jens Axboe94ae5e72019-11-14 19:39:52 -07001013static inline bool io_prep_async_work(struct io_kiocb *req,
1014 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001015{
Jens Axboed3656342019-12-18 09:50:26 -07001016 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001017 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001018
Jens Axboed3656342019-12-18 09:50:26 -07001019 if (req->flags & REQ_F_ISREG) {
1020 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001021 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001022 } else {
1023 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001024 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001025 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001026
1027 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001028
Jens Axboe94ae5e72019-11-14 19:39:52 -07001029 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001030 return do_hashed;
1031}
1032
Jackie Liua197f662019-11-08 08:09:12 -07001033static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001034{
Jackie Liua197f662019-11-08 08:09:12 -07001035 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001036 struct io_kiocb *link;
1037 bool do_hashed;
1038
1039 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001040
1041 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1042 req->flags);
1043 if (!do_hashed) {
1044 io_wq_enqueue(ctx->io_wq, &req->work);
1045 } else {
1046 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1047 file_inode(req->file));
1048 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001049
1050 if (link)
1051 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001052}
1053
Jens Axboe5262f562019-09-17 12:26:57 -06001054static void io_kill_timeout(struct io_kiocb *req)
1055{
1056 int ret;
1057
Jens Axboe2d283902019-12-04 11:08:05 -07001058 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001059 if (ret != -1) {
1060 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001061 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001062 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001063 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001064 }
1065}
1066
1067static void io_kill_timeouts(struct io_ring_ctx *ctx)
1068{
1069 struct io_kiocb *req, *tmp;
1070
1071 spin_lock_irq(&ctx->completion_lock);
1072 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1073 io_kill_timeout(req);
1074 spin_unlock_irq(&ctx->completion_lock);
1075}
1076
Jens Axboede0617e2019-04-06 21:51:27 -06001077static void io_commit_cqring(struct io_ring_ctx *ctx)
1078{
1079 struct io_kiocb *req;
1080
Jens Axboe5262f562019-09-17 12:26:57 -06001081 while ((req = io_get_timeout_req(ctx)) != NULL)
1082 io_kill_timeout(req);
1083
Jens Axboede0617e2019-04-06 21:51:27 -06001084 __io_commit_cqring(ctx);
1085
Pavel Begunkov87987892020-01-18 01:22:30 +03001086 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001087 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001088}
1089
Jens Axboe2b188cc2019-01-07 10:46:33 -07001090static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1091{
Hristo Venev75b28af2019-08-26 17:23:46 +00001092 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001093 unsigned tail;
1094
1095 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001096 /*
1097 * writes to the cq entry need to come after reading head; the
1098 * control dependency is enough as we're using WRITE_ONCE to
1099 * fill the cq entry
1100 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001101 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102 return NULL;
1103
1104 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001105 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106}
1107
Jens Axboef2842ab2020-01-08 11:04:00 -07001108static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1109{
Jens Axboef0b493e2020-02-01 21:30:11 -07001110 if (!ctx->cq_ev_fd)
1111 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001112 if (!ctx->eventfd_async)
1113 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001114 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001115}
1116
Jens Axboeb41e9852020-02-17 09:52:41 -07001117static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001118{
1119 if (waitqueue_active(&ctx->wait))
1120 wake_up(&ctx->wait);
1121 if (waitqueue_active(&ctx->sqo_wait))
1122 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001123 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001124 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001125}
1126
Jens Axboec4a2ed72019-11-21 21:01:26 -07001127/* Returns true if there are no backlogged entries after the flush */
1128static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001130 struct io_rings *rings = ctx->rings;
1131 struct io_uring_cqe *cqe;
1132 struct io_kiocb *req;
1133 unsigned long flags;
1134 LIST_HEAD(list);
1135
1136 if (!force) {
1137 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001138 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001139 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1140 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001141 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001142 }
1143
1144 spin_lock_irqsave(&ctx->completion_lock, flags);
1145
1146 /* if force is set, the ring is going away. always drop after that */
1147 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001148 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001149
Jens Axboec4a2ed72019-11-21 21:01:26 -07001150 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001151 while (!list_empty(&ctx->cq_overflow_list)) {
1152 cqe = io_get_cqring(ctx);
1153 if (!cqe && !force)
1154 break;
1155
1156 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1157 list);
1158 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001159 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001160 if (cqe) {
1161 WRITE_ONCE(cqe->user_data, req->user_data);
1162 WRITE_ONCE(cqe->res, req->result);
1163 WRITE_ONCE(cqe->flags, 0);
1164 } else {
1165 WRITE_ONCE(ctx->rings->cq_overflow,
1166 atomic_inc_return(&ctx->cached_cq_overflow));
1167 }
1168 }
1169
1170 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001171 if (cqe) {
1172 clear_bit(0, &ctx->sq_check_overflow);
1173 clear_bit(0, &ctx->cq_check_overflow);
1174 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001175 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1176 io_cqring_ev_posted(ctx);
1177
1178 while (!list_empty(&list)) {
1179 req = list_first_entry(&list, struct io_kiocb, list);
1180 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001181 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001182 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001183
1184 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001185}
1186
Jens Axboe78e19bb2019-11-06 15:21:34 -07001187static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001188{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001189 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001190 struct io_uring_cqe *cqe;
1191
Jens Axboe78e19bb2019-11-06 15:21:34 -07001192 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001193
Jens Axboe2b188cc2019-01-07 10:46:33 -07001194 /*
1195 * If we can't get a cq entry, userspace overflowed the
1196 * submission (by quite a lot). Increment the overflow count in
1197 * the ring.
1198 */
1199 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001200 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001201 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001202 WRITE_ONCE(cqe->res, res);
1203 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001204 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001205 WRITE_ONCE(ctx->rings->cq_overflow,
1206 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001207 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001208 if (list_empty(&ctx->cq_overflow_list)) {
1209 set_bit(0, &ctx->sq_check_overflow);
1210 set_bit(0, &ctx->cq_check_overflow);
1211 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001212 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001213 refcount_inc(&req->refs);
1214 req->result = res;
1215 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216 }
1217}
1218
Jens Axboe78e19bb2019-11-06 15:21:34 -07001219static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001221 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001222 unsigned long flags;
1223
1224 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001225 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001226 io_commit_cqring(ctx);
1227 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1228
Jens Axboe8c838782019-03-12 15:48:16 -06001229 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230}
1231
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001232static inline bool io_is_fallback_req(struct io_kiocb *req)
1233{
1234 return req == (struct io_kiocb *)
1235 ((unsigned long) req->ctx->fallback_req & ~1UL);
1236}
1237
1238static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1239{
1240 struct io_kiocb *req;
1241
1242 req = ctx->fallback_req;
1243 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1244 return req;
1245
1246 return NULL;
1247}
1248
Jens Axboe2579f912019-01-09 09:10:43 -07001249static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1250 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251{
Jens Axboefd6fab22019-03-14 16:30:06 -06001252 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253 struct io_kiocb *req;
1254
Jens Axboe2579f912019-01-09 09:10:43 -07001255 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001256 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001257 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001258 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001259 } else if (!state->free_reqs) {
1260 size_t sz;
1261 int ret;
1262
1263 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001264 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1265
1266 /*
1267 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1268 * retry single alloc to be on the safe side.
1269 */
1270 if (unlikely(ret <= 0)) {
1271 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1272 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001273 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001274 ret = 1;
1275 }
Jens Axboe2579f912019-01-09 09:10:43 -07001276 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001277 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001278 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001279 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001280 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001281 }
1282
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001283got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001284 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001285 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001286 req->ctx = ctx;
1287 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001288 /* one is dropped after submission, the other at completion */
1289 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001290 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001291 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001292 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001293fallback:
1294 req = io_get_fallback_req(ctx);
1295 if (req)
1296 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001297 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001298 return NULL;
1299}
1300
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001301static inline void io_put_file(struct io_kiocb *req, struct file *file,
1302 bool fixed)
1303{
1304 if (fixed)
1305 percpu_ref_put(&req->ctx->file_data->refs);
1306 else
1307 fput(file);
1308}
1309
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001310static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001311{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001312 if (likely(!io_is_fallback_req(req)))
1313 kmem_cache_free(req_cachep, req);
1314 else
1315 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1316}
1317
Jens Axboec6ca97b302019-12-28 12:11:08 -07001318static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001319{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001320 if (req->flags & REQ_F_NEED_CLEANUP)
1321 io_cleanup_req(req);
1322
YueHaibing96fd84d2020-01-07 22:22:44 +08001323 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001324 if (req->file)
1325 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001326
1327 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001328}
1329
1330static void __io_free_req(struct io_kiocb *req)
1331{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001332 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001333
Jens Axboefcb323c2019-10-24 12:39:47 -06001334 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001335 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001336 unsigned long flags;
1337
1338 spin_lock_irqsave(&ctx->inflight_lock, flags);
1339 list_del(&req->inflight_entry);
1340 if (waitqueue_active(&ctx->inflight_wait))
1341 wake_up(&ctx->inflight_wait);
1342 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1343 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001344
1345 percpu_ref_put(&req->ctx->refs);
1346 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001347}
1348
Jens Axboec6ca97b302019-12-28 12:11:08 -07001349struct req_batch {
1350 void *reqs[IO_IOPOLL_BATCH];
1351 int to_free;
1352 int need_iter;
1353};
1354
1355static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1356{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001357 int fixed_refs = rb->to_free;
1358
Jens Axboec6ca97b302019-12-28 12:11:08 -07001359 if (!rb->to_free)
1360 return;
1361 if (rb->need_iter) {
1362 int i, inflight = 0;
1363 unsigned long flags;
1364
Jens Axboe10fef4b2020-01-09 07:52:28 -07001365 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001366 for (i = 0; i < rb->to_free; i++) {
1367 struct io_kiocb *req = rb->reqs[i];
1368
Jens Axboe10fef4b2020-01-09 07:52:28 -07001369 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001370 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001371 fixed_refs++;
1372 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001373 if (req->flags & REQ_F_INFLIGHT)
1374 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001375 __io_req_aux_free(req);
1376 }
1377 if (!inflight)
1378 goto do_free;
1379
1380 spin_lock_irqsave(&ctx->inflight_lock, flags);
1381 for (i = 0; i < rb->to_free; i++) {
1382 struct io_kiocb *req = rb->reqs[i];
1383
Jens Axboe10fef4b2020-01-09 07:52:28 -07001384 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001385 list_del(&req->inflight_entry);
1386 if (!--inflight)
1387 break;
1388 }
1389 }
1390 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1391
1392 if (waitqueue_active(&ctx->inflight_wait))
1393 wake_up(&ctx->inflight_wait);
1394 }
1395do_free:
1396 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001397 if (fixed_refs)
1398 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001399 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001400 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001401}
1402
Jackie Liua197f662019-11-08 08:09:12 -07001403static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001404{
Jackie Liua197f662019-11-08 08:09:12 -07001405 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001406 int ret;
1407
Jens Axboe2d283902019-12-04 11:08:05 -07001408 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001409 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001410 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001411 io_commit_cqring(ctx);
1412 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001413 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001414 return true;
1415 }
1416
1417 return false;
1418}
1419
Jens Axboeba816ad2019-09-28 11:36:45 -06001420static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001421{
Jens Axboe2665abf2019-11-05 12:40:47 -07001422 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001423 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001424
Jens Axboe4d7dd462019-11-20 13:03:52 -07001425 /* Already got next link */
1426 if (req->flags & REQ_F_LINK_NEXT)
1427 return;
1428
Jens Axboe9e645e112019-05-10 16:07:28 -06001429 /*
1430 * The list should never be empty when we are called here. But could
1431 * potentially happen if the chain is messed up, check to be on the
1432 * safe side.
1433 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001434 while (!list_empty(&req->link_list)) {
1435 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1436 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001437
Pavel Begunkov44932332019-12-05 16:16:35 +03001438 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1439 (nxt->flags & REQ_F_TIMEOUT))) {
1440 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001441 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001442 req->flags &= ~REQ_F_LINK_TIMEOUT;
1443 continue;
1444 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001445
Pavel Begunkov44932332019-12-05 16:16:35 +03001446 list_del_init(&req->link_list);
1447 if (!list_empty(&nxt->link_list))
1448 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001449 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001450 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001451 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001452
Jens Axboe4d7dd462019-11-20 13:03:52 -07001453 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001454 if (wake_ev)
1455 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001456}
1457
1458/*
1459 * Called if REQ_F_LINK is set, and we fail the head request
1460 */
1461static void io_fail_links(struct io_kiocb *req)
1462{
Jens Axboe2665abf2019-11-05 12:40:47 -07001463 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001464 unsigned long flags;
1465
1466 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001467
1468 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001469 struct io_kiocb *link = list_first_entry(&req->link_list,
1470 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001471
Pavel Begunkov44932332019-12-05 16:16:35 +03001472 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001473 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001474
1475 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001476 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001477 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001478 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001479 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001480 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001481 }
Jens Axboe5d960722019-11-19 15:31:28 -07001482 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001483 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001484
1485 io_commit_cqring(ctx);
1486 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1487 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001488}
1489
Jens Axboe4d7dd462019-11-20 13:03:52 -07001490static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001491{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001492 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001493 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001494
Jens Axboe9e645e112019-05-10 16:07:28 -06001495 /*
1496 * If LINK is set, we have dependent requests in this chain. If we
1497 * didn't fail this request, queue the first one up, moving any other
1498 * dependencies to the next request. In case of failure, fail the rest
1499 * of the chain.
1500 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001501 if (req->flags & REQ_F_FAIL_LINK) {
1502 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001503 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1504 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001505 struct io_ring_ctx *ctx = req->ctx;
1506 unsigned long flags;
1507
1508 /*
1509 * If this is a timeout link, we could be racing with the
1510 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001511 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001512 */
1513 spin_lock_irqsave(&ctx->completion_lock, flags);
1514 io_req_link_next(req, nxt);
1515 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1516 } else {
1517 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001518 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001519}
Jens Axboe9e645e112019-05-10 16:07:28 -06001520
Jackie Liuc69f8db2019-11-09 11:00:08 +08001521static void io_free_req(struct io_kiocb *req)
1522{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001523 struct io_kiocb *nxt = NULL;
1524
1525 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001526 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001527
1528 if (nxt)
1529 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001530}
1531
Jens Axboeba816ad2019-09-28 11:36:45 -06001532/*
1533 * Drop reference to request, return next in chain (if there is one) if this
1534 * was the last reference to this request.
1535 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001536__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001537static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001538{
Jens Axboe2a44f462020-02-25 13:25:41 -07001539 if (refcount_dec_and_test(&req->refs)) {
1540 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001541 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001542 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001543}
1544
Jens Axboe2b188cc2019-01-07 10:46:33 -07001545static void io_put_req(struct io_kiocb *req)
1546{
Jens Axboedef596e2019-01-09 08:59:42 -07001547 if (refcount_dec_and_test(&req->refs))
1548 io_free_req(req);
1549}
1550
Jens Axboe978db572019-11-14 22:39:04 -07001551/*
1552 * Must only be used if we don't need to care about links, usually from
1553 * within the completion handling itself.
1554 */
1555static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001556{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001557 /* drop both submit and complete references */
1558 if (refcount_sub_and_test(2, &req->refs))
1559 __io_free_req(req);
1560}
1561
Jens Axboe978db572019-11-14 22:39:04 -07001562static void io_double_put_req(struct io_kiocb *req)
1563{
1564 /* drop both submit and complete references */
1565 if (refcount_sub_and_test(2, &req->refs))
1566 io_free_req(req);
1567}
1568
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001569static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001570{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001571 struct io_rings *rings = ctx->rings;
1572
Jens Axboead3eb2c2019-12-18 17:12:20 -07001573 if (test_bit(0, &ctx->cq_check_overflow)) {
1574 /*
1575 * noflush == true is from the waitqueue handler, just ensure
1576 * we wake up the task, and the next invocation will flush the
1577 * entries. We cannot safely to it from here.
1578 */
1579 if (noflush && !list_empty(&ctx->cq_overflow_list))
1580 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001581
Jens Axboead3eb2c2019-12-18 17:12:20 -07001582 io_cqring_overflow_flush(ctx, false);
1583 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001584
Jens Axboea3a0e432019-08-20 11:03:11 -06001585 /* See comment at the top of this file */
1586 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001587 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001588}
1589
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001590static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1591{
1592 struct io_rings *rings = ctx->rings;
1593
1594 /* make sure SQ entry isn't read before tail */
1595 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1596}
1597
Jens Axboe8237e042019-12-28 10:48:22 -07001598static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001599{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001600 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1601 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001602
Jens Axboec6ca97b302019-12-28 12:11:08 -07001603 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1604 rb->need_iter++;
1605
1606 rb->reqs[rb->to_free++] = req;
1607 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1608 io_free_req_many(req->ctx, rb);
1609 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001610}
1611
Jens Axboedef596e2019-01-09 08:59:42 -07001612/*
1613 * Find and free completed poll iocbs
1614 */
1615static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1616 struct list_head *done)
1617{
Jens Axboe8237e042019-12-28 10:48:22 -07001618 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001619 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001620
Jens Axboec6ca97b302019-12-28 12:11:08 -07001621 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001622 while (!list_empty(done)) {
1623 req = list_first_entry(done, struct io_kiocb, list);
1624 list_del(&req->list);
1625
Jens Axboe78e19bb2019-11-06 15:21:34 -07001626 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001627 (*nr_events)++;
1628
Jens Axboe8237e042019-12-28 10:48:22 -07001629 if (refcount_dec_and_test(&req->refs) &&
1630 !io_req_multi_free(&rb, req))
1631 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001632 }
Jens Axboedef596e2019-01-09 08:59:42 -07001633
Jens Axboe09bb8392019-03-13 12:39:28 -06001634 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001635 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001636}
1637
1638static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1639 long min)
1640{
1641 struct io_kiocb *req, *tmp;
1642 LIST_HEAD(done);
1643 bool spin;
1644 int ret;
1645
1646 /*
1647 * Only spin for completions if we don't have multiple devices hanging
1648 * off our complete list, and we're under the requested amount.
1649 */
1650 spin = !ctx->poll_multi_file && *nr_events < min;
1651
1652 ret = 0;
1653 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001654 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001655
1656 /*
1657 * Move completed entries to our local list. If we find a
1658 * request that requires polling, break out and complete
1659 * the done list first, if we have entries there.
1660 */
1661 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1662 list_move_tail(&req->list, &done);
1663 continue;
1664 }
1665 if (!list_empty(&done))
1666 break;
1667
1668 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1669 if (ret < 0)
1670 break;
1671
1672 if (ret && spin)
1673 spin = false;
1674 ret = 0;
1675 }
1676
1677 if (!list_empty(&done))
1678 io_iopoll_complete(ctx, nr_events, &done);
1679
1680 return ret;
1681}
1682
1683/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001684 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001685 * non-spinning poll check - we'll still enter the driver poll loop, but only
1686 * as a non-spinning completion check.
1687 */
1688static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1689 long min)
1690{
Jens Axboe08f54392019-08-21 22:19:11 -06001691 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001692 int ret;
1693
1694 ret = io_do_iopoll(ctx, nr_events, min);
1695 if (ret < 0)
1696 return ret;
1697 if (!min || *nr_events >= min)
1698 return 0;
1699 }
1700
1701 return 1;
1702}
1703
1704/*
1705 * We can't just wait for polled events to come to us, we have to actively
1706 * find and complete them.
1707 */
1708static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1709{
1710 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1711 return;
1712
1713 mutex_lock(&ctx->uring_lock);
1714 while (!list_empty(&ctx->poll_list)) {
1715 unsigned int nr_events = 0;
1716
1717 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001718
1719 /*
1720 * Ensure we allow local-to-the-cpu processing to take place,
1721 * in this case we need to ensure that we reap all events.
1722 */
1723 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001724 }
1725 mutex_unlock(&ctx->uring_lock);
1726}
1727
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001728static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1729 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001730{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001731 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001732
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001733 /*
1734 * We disallow the app entering submit/complete with polling, but we
1735 * still need to lock the ring to prevent racing with polled issue
1736 * that got punted to a workqueue.
1737 */
1738 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001739 do {
1740 int tmin = 0;
1741
Jens Axboe500f9fb2019-08-19 12:15:59 -06001742 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001743 * Don't enter poll loop if we already have events pending.
1744 * If we do, we can potentially be spinning for commands that
1745 * already triggered a CQE (eg in error).
1746 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001747 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001748 break;
1749
1750 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001751 * If a submit got punted to a workqueue, we can have the
1752 * application entering polling for a command before it gets
1753 * issued. That app will hold the uring_lock for the duration
1754 * of the poll right here, so we need to take a breather every
1755 * now and then to ensure that the issue has a chance to add
1756 * the poll to the issued list. Otherwise we can spin here
1757 * forever, while the workqueue is stuck trying to acquire the
1758 * very same mutex.
1759 */
1760 if (!(++iters & 7)) {
1761 mutex_unlock(&ctx->uring_lock);
1762 mutex_lock(&ctx->uring_lock);
1763 }
1764
Jens Axboedef596e2019-01-09 08:59:42 -07001765 if (*nr_events < min)
1766 tmin = min - *nr_events;
1767
1768 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1769 if (ret <= 0)
1770 break;
1771 ret = 0;
1772 } while (min && !*nr_events && !need_resched());
1773
Jens Axboe500f9fb2019-08-19 12:15:59 -06001774 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001775 return ret;
1776}
1777
Jens Axboe491381ce2019-10-17 09:20:46 -06001778static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779{
Jens Axboe491381ce2019-10-17 09:20:46 -06001780 /*
1781 * Tell lockdep we inherited freeze protection from submission
1782 * thread.
1783 */
1784 if (req->flags & REQ_F_ISREG) {
1785 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786
Jens Axboe491381ce2019-10-17 09:20:46 -06001787 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001789 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790}
1791
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001792static inline void req_set_fail_links(struct io_kiocb *req)
1793{
1794 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1795 req->flags |= REQ_F_FAIL_LINK;
1796}
1797
Jens Axboeba816ad2019-09-28 11:36:45 -06001798static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001799{
Jens Axboe9adbd452019-12-20 08:45:55 -07001800 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001801
Jens Axboe491381ce2019-10-17 09:20:46 -06001802 if (kiocb->ki_flags & IOCB_WRITE)
1803 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001804
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001805 if (res != req->result)
1806 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001807 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001808}
1809
1810static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1811{
Jens Axboe9adbd452019-12-20 08:45:55 -07001812 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001813
1814 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001815 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001816}
1817
Jens Axboeba816ad2019-09-28 11:36:45 -06001818static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1819{
Jens Axboe9adbd452019-12-20 08:45:55 -07001820 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001821 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001822
1823 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001824 io_put_req_find_next(req, &nxt);
1825
1826 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001827}
1828
Jens Axboedef596e2019-01-09 08:59:42 -07001829static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1830{
Jens Axboe9adbd452019-12-20 08:45:55 -07001831 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001832
Jens Axboe491381ce2019-10-17 09:20:46 -06001833 if (kiocb->ki_flags & IOCB_WRITE)
1834 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001835
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001836 if (res != req->result)
1837 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001838 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001839 if (res != -EAGAIN)
1840 req->flags |= REQ_F_IOPOLL_COMPLETED;
1841}
1842
1843/*
1844 * After the iocb has been issued, it's safe to be found on the poll list.
1845 * Adding the kiocb to the list AFTER submission ensures that we don't
1846 * find it from a io_iopoll_getevents() thread before the issuer is done
1847 * accessing the kiocb cookie.
1848 */
1849static void io_iopoll_req_issued(struct io_kiocb *req)
1850{
1851 struct io_ring_ctx *ctx = req->ctx;
1852
1853 /*
1854 * Track whether we have multiple files in our lists. This will impact
1855 * how we do polling eventually, not spinning if we're on potentially
1856 * different devices.
1857 */
1858 if (list_empty(&ctx->poll_list)) {
1859 ctx->poll_multi_file = false;
1860 } else if (!ctx->poll_multi_file) {
1861 struct io_kiocb *list_req;
1862
1863 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1864 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001865 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001866 ctx->poll_multi_file = true;
1867 }
1868
1869 /*
1870 * For fast devices, IO may have already completed. If it has, add
1871 * it to the front so we find it first.
1872 */
1873 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1874 list_add(&req->list, &ctx->poll_list);
1875 else
1876 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001877
1878 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1879 wq_has_sleeper(&ctx->sqo_wait))
1880 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001881}
1882
Jens Axboe3d6770f2019-04-13 11:50:54 -06001883static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001884{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001885 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001886 int diff = state->has_refs - state->used_refs;
1887
1888 if (diff)
1889 fput_many(state->file, diff);
1890 state->file = NULL;
1891 }
1892}
1893
1894/*
1895 * Get as many references to a file as we have IOs left in this submission,
1896 * assuming most submissions are for one file, or at least that each file
1897 * has more than one submission.
1898 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001899static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001900{
1901 if (!state)
1902 return fget(fd);
1903
1904 if (state->file) {
1905 if (state->fd == fd) {
1906 state->used_refs++;
1907 state->ios_left--;
1908 return state->file;
1909 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001910 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001911 }
1912 state->file = fget_many(fd, state->ios_left);
1913 if (!state->file)
1914 return NULL;
1915
1916 state->fd = fd;
1917 state->has_refs = state->ios_left;
1918 state->used_refs = 1;
1919 state->ios_left--;
1920 return state->file;
1921}
1922
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923/*
1924 * If we tracked the file through the SCM inflight mechanism, we could support
1925 * any file. For now, just ensure that anything potentially problematic is done
1926 * inline.
1927 */
1928static bool io_file_supports_async(struct file *file)
1929{
1930 umode_t mode = file_inode(file)->i_mode;
1931
Jens Axboe10d59342019-12-09 20:16:22 -07001932 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001933 return true;
1934 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1935 return true;
1936
1937 return false;
1938}
1939
Jens Axboe3529d8c2019-12-19 18:24:38 -07001940static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1941 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942{
Jens Axboedef596e2019-01-09 08:59:42 -07001943 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001944 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001945 unsigned ioprio;
1946 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001947
Jens Axboe491381ce2019-10-17 09:20:46 -06001948 if (S_ISREG(file_inode(req->file)->i_mode))
1949 req->flags |= REQ_F_ISREG;
1950
Jens Axboe2b188cc2019-01-07 10:46:33 -07001951 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001952 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1953 req->flags |= REQ_F_CUR_POS;
1954 kiocb->ki_pos = req->file->f_pos;
1955 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001957 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1958 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1959 if (unlikely(ret))
1960 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001961
1962 ioprio = READ_ONCE(sqe->ioprio);
1963 if (ioprio) {
1964 ret = ioprio_check_cap(ioprio);
1965 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001966 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967
1968 kiocb->ki_ioprio = ioprio;
1969 } else
1970 kiocb->ki_ioprio = get_current_ioprio();
1971
Stefan Bühler8449eed2019-04-27 20:34:19 +02001972 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001973 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1974 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001975 req->flags |= REQ_F_NOWAIT;
1976
1977 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001978 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001979
Jens Axboedef596e2019-01-09 08:59:42 -07001980 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001981 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1982 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001983 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001984
Jens Axboedef596e2019-01-09 08:59:42 -07001985 kiocb->ki_flags |= IOCB_HIPRI;
1986 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001987 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001988 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001989 if (kiocb->ki_flags & IOCB_HIPRI)
1990 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001991 kiocb->ki_complete = io_complete_rw;
1992 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001993
Jens Axboe3529d8c2019-12-19 18:24:38 -07001994 req->rw.addr = READ_ONCE(sqe->addr);
1995 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001996 /* we own ->private, reuse it for the buffer index */
1997 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001998 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001999 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002000}
2001
2002static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2003{
2004 switch (ret) {
2005 case -EIOCBQUEUED:
2006 break;
2007 case -ERESTARTSYS:
2008 case -ERESTARTNOINTR:
2009 case -ERESTARTNOHAND:
2010 case -ERESTART_RESTARTBLOCK:
2011 /*
2012 * We can't just restart the syscall, since previously
2013 * submitted sqes may already be in progress. Just fail this
2014 * IO with EINTR.
2015 */
2016 ret = -EINTR;
2017 /* fall through */
2018 default:
2019 kiocb->ki_complete(kiocb, ret, 0);
2020 }
2021}
2022
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002023static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt)
Jens Axboeba816ad2019-09-28 11:36:45 -06002024{
Jens Axboeba042912019-12-25 16:33:42 -07002025 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2026
2027 if (req->flags & REQ_F_CUR_POS)
2028 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002029 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06002030 *nxt = __io_complete_rw(kiocb, ret);
2031 else
2032 io_rw_done(kiocb, ret);
2033}
2034
Jens Axboe9adbd452019-12-20 08:45:55 -07002035static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002036 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002037{
Jens Axboe9adbd452019-12-20 08:45:55 -07002038 struct io_ring_ctx *ctx = req->ctx;
2039 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002040 struct io_mapped_ubuf *imu;
2041 unsigned index, buf_index;
2042 size_t offset;
2043 u64 buf_addr;
2044
2045 /* attempt to use fixed buffers without having provided iovecs */
2046 if (unlikely(!ctx->user_bufs))
2047 return -EFAULT;
2048
Jens Axboe9adbd452019-12-20 08:45:55 -07002049 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002050 if (unlikely(buf_index >= ctx->nr_user_bufs))
2051 return -EFAULT;
2052
2053 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2054 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002055 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002056
2057 /* overflow */
2058 if (buf_addr + len < buf_addr)
2059 return -EFAULT;
2060 /* not inside the mapped region */
2061 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2062 return -EFAULT;
2063
2064 /*
2065 * May not be a start of buffer, set size appropriately
2066 * and advance us to the beginning.
2067 */
2068 offset = buf_addr - imu->ubuf;
2069 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002070
2071 if (offset) {
2072 /*
2073 * Don't use iov_iter_advance() here, as it's really slow for
2074 * using the latter parts of a big fixed buffer - it iterates
2075 * over each segment manually. We can cheat a bit here, because
2076 * we know that:
2077 *
2078 * 1) it's a BVEC iter, we set it up
2079 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2080 * first and last bvec
2081 *
2082 * So just find our index, and adjust the iterator afterwards.
2083 * If the offset is within the first bvec (or the whole first
2084 * bvec, just use iov_iter_advance(). This makes it easier
2085 * since we can just skip the first segment, which may not
2086 * be PAGE_SIZE aligned.
2087 */
2088 const struct bio_vec *bvec = imu->bvec;
2089
2090 if (offset <= bvec->bv_len) {
2091 iov_iter_advance(iter, offset);
2092 } else {
2093 unsigned long seg_skip;
2094
2095 /* skip first vec */
2096 offset -= bvec->bv_len;
2097 seg_skip = 1 + (offset >> PAGE_SHIFT);
2098
2099 iter->bvec = bvec + seg_skip;
2100 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002101 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002102 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002103 }
2104 }
2105
Jens Axboe5e559562019-11-13 16:12:46 -07002106 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002107}
2108
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002109static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2110 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002111{
Jens Axboe9adbd452019-12-20 08:45:55 -07002112 void __user *buf = u64_to_user_ptr(req->rw.addr);
2113 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002114 u8 opcode;
2115
Jens Axboed625c6e2019-12-17 19:53:05 -07002116 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002117 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002118 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002119 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002120 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002121
Jens Axboe9adbd452019-12-20 08:45:55 -07002122 /* buffer index only valid with fixed read/write */
2123 if (req->rw.kiocb.private)
2124 return -EINVAL;
2125
Jens Axboe3a6820f2019-12-22 15:19:35 -07002126 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2127 ssize_t ret;
2128 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2129 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002130 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002131 }
2132
Jens Axboef67676d2019-12-02 11:03:47 -07002133 if (req->io) {
2134 struct io_async_rw *iorw = &req->io->rw;
2135
2136 *iovec = iorw->iov;
2137 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2138 if (iorw->iov == iorw->fast_iov)
2139 *iovec = NULL;
2140 return iorw->size;
2141 }
2142
Jens Axboe2b188cc2019-01-07 10:46:33 -07002143#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002144 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002145 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2146 iovec, iter);
2147#endif
2148
2149 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2150}
2151
Jens Axboe32960612019-09-23 11:05:34 -06002152/*
2153 * For files that don't have ->read_iter() and ->write_iter(), handle them
2154 * by looping over ->read() or ->write() manually.
2155 */
2156static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2157 struct iov_iter *iter)
2158{
2159 ssize_t ret = 0;
2160
2161 /*
2162 * Don't support polled IO through this interface, and we can't
2163 * support non-blocking either. For the latter, this just causes
2164 * the kiocb to be handled from an async context.
2165 */
2166 if (kiocb->ki_flags & IOCB_HIPRI)
2167 return -EOPNOTSUPP;
2168 if (kiocb->ki_flags & IOCB_NOWAIT)
2169 return -EAGAIN;
2170
2171 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002172 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002173 ssize_t nr;
2174
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002175 if (!iov_iter_is_bvec(iter)) {
2176 iovec = iov_iter_iovec(iter);
2177 } else {
2178 /* fixed buffers import bvec */
2179 iovec.iov_base = kmap(iter->bvec->bv_page)
2180 + iter->iov_offset;
2181 iovec.iov_len = min(iter->count,
2182 iter->bvec->bv_len - iter->iov_offset);
2183 }
2184
Jens Axboe32960612019-09-23 11:05:34 -06002185 if (rw == READ) {
2186 nr = file->f_op->read(file, iovec.iov_base,
2187 iovec.iov_len, &kiocb->ki_pos);
2188 } else {
2189 nr = file->f_op->write(file, iovec.iov_base,
2190 iovec.iov_len, &kiocb->ki_pos);
2191 }
2192
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002193 if (iov_iter_is_bvec(iter))
2194 kunmap(iter->bvec->bv_page);
2195
Jens Axboe32960612019-09-23 11:05:34 -06002196 if (nr < 0) {
2197 if (!ret)
2198 ret = nr;
2199 break;
2200 }
2201 ret += nr;
2202 if (nr != iovec.iov_len)
2203 break;
2204 iov_iter_advance(iter, nr);
2205 }
2206
2207 return ret;
2208}
2209
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002210static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002211 struct iovec *iovec, struct iovec *fast_iov,
2212 struct iov_iter *iter)
2213{
2214 req->io->rw.nr_segs = iter->nr_segs;
2215 req->io->rw.size = io_size;
2216 req->io->rw.iov = iovec;
2217 if (!req->io->rw.iov) {
2218 req->io->rw.iov = req->io->rw.fast_iov;
2219 memcpy(req->io->rw.iov, fast_iov,
2220 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002221 } else {
2222 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002223 }
2224}
2225
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002226static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002227{
Jens Axboed3656342019-12-18 09:50:26 -07002228 if (!io_op_defs[req->opcode].async_ctx)
2229 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002230 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002231 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002232}
2233
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002234static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2235 struct iovec *iovec, struct iovec *fast_iov,
2236 struct iov_iter *iter)
2237{
Jens Axboe980ad262020-01-24 23:08:54 -07002238 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002239 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002240 if (!req->io) {
2241 if (io_alloc_async_ctx(req))
2242 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002243
Jens Axboe5d204bc2020-01-31 12:06:52 -07002244 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2245 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002246 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002247}
2248
Jens Axboe3529d8c2019-12-19 18:24:38 -07002249static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2250 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002251{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002252 struct io_async_ctx *io;
2253 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002254 ssize_t ret;
2255
Jens Axboe3529d8c2019-12-19 18:24:38 -07002256 ret = io_prep_rw(req, sqe, force_nonblock);
2257 if (ret)
2258 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002259
Jens Axboe3529d8c2019-12-19 18:24:38 -07002260 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2261 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002262
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002263 /* either don't need iovec imported or already have it */
2264 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002265 return 0;
2266
2267 io = req->io;
2268 io->rw.iov = io->rw.fast_iov;
2269 req->io = NULL;
2270 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2271 req->io = io;
2272 if (ret < 0)
2273 return ret;
2274
2275 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2276 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002277}
2278
Pavel Begunkov267bc902019-11-07 01:41:08 +03002279static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002280 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281{
2282 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002283 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002284 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002285 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002286 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002287
Jens Axboe3529d8c2019-12-19 18:24:38 -07002288 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002289 if (ret < 0)
2290 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002291
Jens Axboefd6c2e42019-12-18 12:19:41 -07002292 /* Ensure we clear previously set non-block flag */
2293 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002294 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002295
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002296 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002297 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002298 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002299 req->result = io_size;
2300
2301 /*
2302 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2303 * we know to async punt it even if it was opened O_NONBLOCK
2304 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002305 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002306 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002307
Jens Axboe31b51512019-01-18 22:56:34 -07002308 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002309 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002310 if (!ret) {
2311 ssize_t ret2;
2312
Jens Axboe9adbd452019-12-20 08:45:55 -07002313 if (req->file->f_op->read_iter)
2314 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002315 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002316 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002317
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002318 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002319 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002320 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002321 } else {
2322copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002323 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002324 inline_vecs, &iter);
2325 if (ret)
2326 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002327 /* any defer here is final, must blocking retry */
2328 if (!(req->flags & REQ_F_NOWAIT))
2329 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002330 return -EAGAIN;
2331 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002332 }
Jens Axboef67676d2019-12-02 11:03:47 -07002333out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002334 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002335 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002336 return ret;
2337}
2338
Jens Axboe3529d8c2019-12-19 18:24:38 -07002339static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2340 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002341{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002342 struct io_async_ctx *io;
2343 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002344 ssize_t ret;
2345
Jens Axboe3529d8c2019-12-19 18:24:38 -07002346 ret = io_prep_rw(req, sqe, force_nonblock);
2347 if (ret)
2348 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002349
Jens Axboe3529d8c2019-12-19 18:24:38 -07002350 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2351 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002352
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002353 /* either don't need iovec imported or already have it */
2354 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002355 return 0;
2356
2357 io = req->io;
2358 io->rw.iov = io->rw.fast_iov;
2359 req->io = NULL;
2360 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2361 req->io = io;
2362 if (ret < 0)
2363 return ret;
2364
2365 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2366 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002367}
2368
Pavel Begunkov267bc902019-11-07 01:41:08 +03002369static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002370 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002371{
2372 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002373 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002374 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002375 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002376 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002377
Jens Axboe3529d8c2019-12-19 18:24:38 -07002378 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002379 if (ret < 0)
2380 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002381
Jens Axboefd6c2e42019-12-18 12:19:41 -07002382 /* Ensure we clear previously set non-block flag */
2383 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002384 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002385
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002386 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002387 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002388 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002389 req->result = io_size;
2390
2391 /*
2392 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2393 * we know to async punt it even if it was opened O_NONBLOCK
2394 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002395 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002396 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002397
Jens Axboe10d59342019-12-09 20:16:22 -07002398 /* file path doesn't support NOWAIT for non-direct_IO */
2399 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2400 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002401 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002402
Jens Axboe31b51512019-01-18 22:56:34 -07002403 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002404 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002405 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002406 ssize_t ret2;
2407
Jens Axboe2b188cc2019-01-07 10:46:33 -07002408 /*
2409 * Open-code file_start_write here to grab freeze protection,
2410 * which will be released by another thread in
2411 * io_complete_rw(). Fool lockdep by telling it the lock got
2412 * released so that it doesn't complain about the held lock when
2413 * we return to userspace.
2414 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002415 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002416 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002417 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002418 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002419 SB_FREEZE_WRITE);
2420 }
2421 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002422
Jens Axboe9adbd452019-12-20 08:45:55 -07002423 if (req->file->f_op->write_iter)
2424 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002425 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002426 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002427 /*
2428 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2429 * retry them without IOCB_NOWAIT.
2430 */
2431 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2432 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002433 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002434 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002435 } else {
2436copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002437 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002438 inline_vecs, &iter);
2439 if (ret)
2440 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002441 /* any defer here is final, must blocking retry */
2442 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002443 return -EAGAIN;
2444 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002445 }
Jens Axboe31b51512019-01-18 22:56:34 -07002446out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002447 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002448 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002449 return ret;
2450}
2451
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002452static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2453{
2454 struct io_splice* sp = &req->splice;
2455 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2456 int ret;
2457
2458 if (req->flags & REQ_F_NEED_CLEANUP)
2459 return 0;
2460
2461 sp->file_in = NULL;
2462 sp->off_in = READ_ONCE(sqe->splice_off_in);
2463 sp->off_out = READ_ONCE(sqe->off);
2464 sp->len = READ_ONCE(sqe->len);
2465 sp->flags = READ_ONCE(sqe->splice_flags);
2466
2467 if (unlikely(sp->flags & ~valid_flags))
2468 return -EINVAL;
2469
2470 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2471 (sp->flags & SPLICE_F_FD_IN_FIXED));
2472 if (ret)
2473 return ret;
2474 req->flags |= REQ_F_NEED_CLEANUP;
2475
2476 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2477 req->work.flags |= IO_WQ_WORK_UNBOUND;
2478
2479 return 0;
2480}
2481
2482static bool io_splice_punt(struct file *file)
2483{
2484 if (get_pipe_info(file))
2485 return false;
2486 if (!io_file_supports_async(file))
2487 return true;
2488 return !(file->f_mode & O_NONBLOCK);
2489}
2490
2491static int io_splice(struct io_kiocb *req, struct io_kiocb **nxt,
2492 bool force_nonblock)
2493{
2494 struct io_splice *sp = &req->splice;
2495 struct file *in = sp->file_in;
2496 struct file *out = sp->file_out;
2497 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2498 loff_t *poff_in, *poff_out;
2499 long ret;
2500
2501 if (force_nonblock) {
2502 if (io_splice_punt(in) || io_splice_punt(out))
2503 return -EAGAIN;
2504 flags |= SPLICE_F_NONBLOCK;
2505 }
2506
2507 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2508 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2509 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2510 if (force_nonblock && ret == -EAGAIN)
2511 return -EAGAIN;
2512
2513 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2514 req->flags &= ~REQ_F_NEED_CLEANUP;
2515
2516 io_cqring_add_event(req, ret);
2517 if (ret != sp->len)
2518 req_set_fail_links(req);
2519 io_put_req_find_next(req, nxt);
2520 return 0;
2521}
2522
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523/*
2524 * IORING_OP_NOP just posts a completion event, nothing else.
2525 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002526static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002527{
2528 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002529
Jens Axboedef596e2019-01-09 08:59:42 -07002530 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2531 return -EINVAL;
2532
Jens Axboe78e19bb2019-11-06 15:21:34 -07002533 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002534 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002535 return 0;
2536}
2537
Jens Axboe3529d8c2019-12-19 18:24:38 -07002538static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002539{
Jens Axboe6b063142019-01-10 22:13:58 -07002540 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002541
Jens Axboe09bb8392019-03-13 12:39:28 -06002542 if (!req->file)
2543 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002544
Jens Axboe6b063142019-01-10 22:13:58 -07002545 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002546 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002547 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002548 return -EINVAL;
2549
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002550 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2551 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2552 return -EINVAL;
2553
2554 req->sync.off = READ_ONCE(sqe->off);
2555 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002556 return 0;
2557}
2558
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002559static bool io_req_cancelled(struct io_kiocb *req)
2560{
2561 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2562 req_set_fail_links(req);
2563 io_cqring_add_event(req, -ECANCELED);
2564 io_put_req(req);
2565 return true;
2566 }
2567
2568 return false;
2569}
2570
Jens Axboe78912932020-01-14 22:09:06 -07002571static void io_link_work_cb(struct io_wq_work **workptr)
2572{
2573 struct io_wq_work *work = *workptr;
2574 struct io_kiocb *link = work->data;
2575
2576 io_queue_linked_timeout(link);
Pavel Begunkov5eae8612020-02-28 10:36:38 +03002577 io_wq_submit_work(workptr);
Jens Axboe78912932020-01-14 22:09:06 -07002578}
2579
2580static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2581{
2582 struct io_kiocb *link;
2583
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +03002584 io_prep_next_work(nxt, &link);
Jens Axboe78912932020-01-14 22:09:06 -07002585 *workptr = &nxt->work;
2586 if (link) {
Jens Axboe78912932020-01-14 22:09:06 -07002587 nxt->work.func = io_link_work_cb;
2588 nxt->work.data = link;
2589 }
2590}
2591
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002592static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002593{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002594 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002595 int ret;
2596
Jens Axboe9adbd452019-12-20 08:45:55 -07002597 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002598 end > 0 ? end : LLONG_MAX,
2599 req->sync.flags & IORING_FSYNC_DATASYNC);
2600 if (ret < 0)
2601 req_set_fail_links(req);
2602 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002603 io_put_req_find_next(req, nxt);
2604}
2605
2606static void io_fsync_finish(struct io_wq_work **workptr)
2607{
2608 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2609 struct io_kiocb *nxt = NULL;
2610
2611 if (io_req_cancelled(req))
2612 return;
2613 __io_fsync(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002614 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002615 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002616}
2617
Jens Axboefc4df992019-12-10 14:38:45 -07002618static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2619 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002620{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002621 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002622 if (force_nonblock) {
2623 io_put_req(req);
2624 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002625 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002626 }
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002627 __io_fsync(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002628 return 0;
2629}
2630
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002631static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboed63d1b52019-12-10 10:38:56 -07002632{
Jens Axboed63d1b52019-12-10 10:38:56 -07002633 int ret;
2634
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002635 if (io_req_cancelled(req))
2636 return;
2637
Jens Axboed63d1b52019-12-10 10:38:56 -07002638 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2639 req->sync.len);
2640 if (ret < 0)
2641 req_set_fail_links(req);
2642 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002643 io_put_req_find_next(req, nxt);
2644}
2645
2646static void io_fallocate_finish(struct io_wq_work **workptr)
2647{
2648 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2649 struct io_kiocb *nxt = NULL;
2650
2651 __io_fallocate(req, &nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002652 if (nxt)
2653 io_wq_assign_next(workptr, nxt);
2654}
2655
2656static int io_fallocate_prep(struct io_kiocb *req,
2657 const struct io_uring_sqe *sqe)
2658{
2659 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2660 return -EINVAL;
2661
2662 req->sync.off = READ_ONCE(sqe->off);
2663 req->sync.len = READ_ONCE(sqe->addr);
2664 req->sync.mode = READ_ONCE(sqe->len);
2665 return 0;
2666}
2667
2668static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2669 bool force_nonblock)
2670{
Jens Axboed63d1b52019-12-10 10:38:56 -07002671 /* fallocate always requiring blocking context */
2672 if (force_nonblock) {
2673 io_put_req(req);
2674 req->work.func = io_fallocate_finish;
2675 return -EAGAIN;
2676 }
2677
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002678 __io_fallocate(req, nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002679 return 0;
2680}
2681
Jens Axboe15b71ab2019-12-11 11:20:36 -07002682static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2683{
Jens Axboef8748882020-01-08 17:47:02 -07002684 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002685 int ret;
2686
2687 if (sqe->ioprio || sqe->buf_index)
2688 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002689 if (sqe->flags & IOSQE_FIXED_FILE)
2690 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002691 if (req->flags & REQ_F_NEED_CLEANUP)
2692 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002693
2694 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002695 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002696 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002697 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002698
Jens Axboef8748882020-01-08 17:47:02 -07002699 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002700 if (IS_ERR(req->open.filename)) {
2701 ret = PTR_ERR(req->open.filename);
2702 req->open.filename = NULL;
2703 return ret;
2704 }
2705
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002706 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002707 return 0;
2708}
2709
Jens Axboecebdb982020-01-08 17:59:24 -07002710static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2711{
2712 struct open_how __user *how;
2713 const char __user *fname;
2714 size_t len;
2715 int ret;
2716
2717 if (sqe->ioprio || sqe->buf_index)
2718 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002719 if (sqe->flags & IOSQE_FIXED_FILE)
2720 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002721 if (req->flags & REQ_F_NEED_CLEANUP)
2722 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002723
2724 req->open.dfd = READ_ONCE(sqe->fd);
2725 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2726 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2727 len = READ_ONCE(sqe->len);
2728
2729 if (len < OPEN_HOW_SIZE_VER0)
2730 return -EINVAL;
2731
2732 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2733 len);
2734 if (ret)
2735 return ret;
2736
2737 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2738 req->open.how.flags |= O_LARGEFILE;
2739
2740 req->open.filename = getname(fname);
2741 if (IS_ERR(req->open.filename)) {
2742 ret = PTR_ERR(req->open.filename);
2743 req->open.filename = NULL;
2744 return ret;
2745 }
2746
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002747 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002748 return 0;
2749}
2750
2751static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2752 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002753{
2754 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002755 struct file *file;
2756 int ret;
2757
Jens Axboef86cd202020-01-29 13:46:44 -07002758 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002759 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002760
Jens Axboecebdb982020-01-08 17:59:24 -07002761 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002762 if (ret)
2763 goto err;
2764
Jens Axboecebdb982020-01-08 17:59:24 -07002765 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002766 if (ret < 0)
2767 goto err;
2768
2769 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2770 if (IS_ERR(file)) {
2771 put_unused_fd(ret);
2772 ret = PTR_ERR(file);
2773 } else {
2774 fsnotify_open(file);
2775 fd_install(ret, file);
2776 }
2777err:
2778 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002779 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002780 if (ret < 0)
2781 req_set_fail_links(req);
2782 io_cqring_add_event(req, ret);
2783 io_put_req_find_next(req, nxt);
2784 return 0;
2785}
2786
Jens Axboecebdb982020-01-08 17:59:24 -07002787static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2788 bool force_nonblock)
2789{
2790 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2791 return io_openat2(req, nxt, force_nonblock);
2792}
2793
Jens Axboe3e4827b2020-01-08 15:18:09 -07002794static int io_epoll_ctl_prep(struct io_kiocb *req,
2795 const struct io_uring_sqe *sqe)
2796{
2797#if defined(CONFIG_EPOLL)
2798 if (sqe->ioprio || sqe->buf_index)
2799 return -EINVAL;
2800
2801 req->epoll.epfd = READ_ONCE(sqe->fd);
2802 req->epoll.op = READ_ONCE(sqe->len);
2803 req->epoll.fd = READ_ONCE(sqe->off);
2804
2805 if (ep_op_has_event(req->epoll.op)) {
2806 struct epoll_event __user *ev;
2807
2808 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2809 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2810 return -EFAULT;
2811 }
2812
2813 return 0;
2814#else
2815 return -EOPNOTSUPP;
2816#endif
2817}
2818
2819static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2820 bool force_nonblock)
2821{
2822#if defined(CONFIG_EPOLL)
2823 struct io_epoll *ie = &req->epoll;
2824 int ret;
2825
2826 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2827 if (force_nonblock && ret == -EAGAIN)
2828 return -EAGAIN;
2829
2830 if (ret < 0)
2831 req_set_fail_links(req);
2832 io_cqring_add_event(req, ret);
2833 io_put_req_find_next(req, nxt);
2834 return 0;
2835#else
2836 return -EOPNOTSUPP;
2837#endif
2838}
2839
Jens Axboec1ca7572019-12-25 22:18:28 -07002840static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2841{
2842#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2843 if (sqe->ioprio || sqe->buf_index || sqe->off)
2844 return -EINVAL;
2845
2846 req->madvise.addr = READ_ONCE(sqe->addr);
2847 req->madvise.len = READ_ONCE(sqe->len);
2848 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2849 return 0;
2850#else
2851 return -EOPNOTSUPP;
2852#endif
2853}
2854
2855static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2856 bool force_nonblock)
2857{
2858#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2859 struct io_madvise *ma = &req->madvise;
2860 int ret;
2861
2862 if (force_nonblock)
2863 return -EAGAIN;
2864
2865 ret = do_madvise(ma->addr, ma->len, ma->advice);
2866 if (ret < 0)
2867 req_set_fail_links(req);
2868 io_cqring_add_event(req, ret);
2869 io_put_req_find_next(req, nxt);
2870 return 0;
2871#else
2872 return -EOPNOTSUPP;
2873#endif
2874}
2875
Jens Axboe4840e412019-12-25 22:03:45 -07002876static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2877{
2878 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2879 return -EINVAL;
2880
2881 req->fadvise.offset = READ_ONCE(sqe->off);
2882 req->fadvise.len = READ_ONCE(sqe->len);
2883 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2884 return 0;
2885}
2886
2887static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2888 bool force_nonblock)
2889{
2890 struct io_fadvise *fa = &req->fadvise;
2891 int ret;
2892
Jens Axboe3e694262020-02-01 09:22:49 -07002893 if (force_nonblock) {
2894 switch (fa->advice) {
2895 case POSIX_FADV_NORMAL:
2896 case POSIX_FADV_RANDOM:
2897 case POSIX_FADV_SEQUENTIAL:
2898 break;
2899 default:
2900 return -EAGAIN;
2901 }
2902 }
Jens Axboe4840e412019-12-25 22:03:45 -07002903
2904 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2905 if (ret < 0)
2906 req_set_fail_links(req);
2907 io_cqring_add_event(req, ret);
2908 io_put_req_find_next(req, nxt);
2909 return 0;
2910}
2911
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002912static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2913{
Jens Axboef8748882020-01-08 17:47:02 -07002914 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002915 unsigned lookup_flags;
2916 int ret;
2917
2918 if (sqe->ioprio || sqe->buf_index)
2919 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002920 if (sqe->flags & IOSQE_FIXED_FILE)
2921 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002922 if (req->flags & REQ_F_NEED_CLEANUP)
2923 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002924
2925 req->open.dfd = READ_ONCE(sqe->fd);
2926 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002927 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002928 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002929 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002930
Jens Axboec12cedf2020-01-08 17:41:21 -07002931 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002932 return -EINVAL;
2933
Jens Axboef8748882020-01-08 17:47:02 -07002934 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002935 if (IS_ERR(req->open.filename)) {
2936 ret = PTR_ERR(req->open.filename);
2937 req->open.filename = NULL;
2938 return ret;
2939 }
2940
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002941 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002942 return 0;
2943}
2944
2945static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2946 bool force_nonblock)
2947{
2948 struct io_open *ctx = &req->open;
2949 unsigned lookup_flags;
2950 struct path path;
2951 struct kstat stat;
2952 int ret;
2953
2954 if (force_nonblock)
2955 return -EAGAIN;
2956
Jens Axboec12cedf2020-01-08 17:41:21 -07002957 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002958 return -EINVAL;
2959
2960retry:
2961 /* filename_lookup() drops it, keep a reference */
2962 ctx->filename->refcnt++;
2963
2964 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2965 NULL);
2966 if (ret)
2967 goto err;
2968
Jens Axboec12cedf2020-01-08 17:41:21 -07002969 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002970 path_put(&path);
2971 if (retry_estale(ret, lookup_flags)) {
2972 lookup_flags |= LOOKUP_REVAL;
2973 goto retry;
2974 }
2975 if (!ret)
2976 ret = cp_statx(&stat, ctx->buffer);
2977err:
2978 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002979 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002980 if (ret < 0)
2981 req_set_fail_links(req);
2982 io_cqring_add_event(req, ret);
2983 io_put_req_find_next(req, nxt);
2984 return 0;
2985}
2986
Jens Axboeb5dba592019-12-11 14:02:38 -07002987static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2988{
2989 /*
2990 * If we queue this for async, it must not be cancellable. That would
2991 * leave the 'file' in an undeterminate state.
2992 */
2993 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2994
2995 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2996 sqe->rw_flags || sqe->buf_index)
2997 return -EINVAL;
2998 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002999 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003000
3001 req->close.fd = READ_ONCE(sqe->fd);
3002 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003003 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003004 return -EBADF;
3005
3006 return 0;
3007}
3008
Pavel Begunkova93b3332020-02-08 14:04:34 +03003009/* only called when __close_fd_get_file() is done */
3010static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
3011{
3012 int ret;
3013
3014 ret = filp_close(req->close.put_file, req->work.files);
3015 if (ret < 0)
3016 req_set_fail_links(req);
3017 io_cqring_add_event(req, ret);
3018 fput(req->close.put_file);
3019 io_put_req_find_next(req, nxt);
3020}
3021
Jens Axboeb5dba592019-12-11 14:02:38 -07003022static void io_close_finish(struct io_wq_work **workptr)
3023{
3024 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3025 struct io_kiocb *nxt = NULL;
3026
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003027 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03003028 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07003029 if (nxt)
3030 io_wq_assign_next(workptr, nxt);
3031}
3032
3033static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
3034 bool force_nonblock)
3035{
3036 int ret;
3037
3038 req->close.put_file = NULL;
3039 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3040 if (ret < 0)
3041 return ret;
3042
3043 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07003044 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07003045 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07003046
3047 /*
3048 * No ->flush(), safely close from here and just punt the
3049 * fput() to async context.
3050 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03003051 __io_close_finish(req, nxt);
3052 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003053eagain:
3054 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07003055 /*
3056 * Do manual async queue here to avoid grabbing files - we don't
3057 * need the files, and it'll cause io_close_finish() to close
3058 * the file again and cause a double CQE entry for this request
3059 */
3060 io_queue_async_work(req);
3061 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003062}
3063
Jens Axboe3529d8c2019-12-19 18:24:38 -07003064static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003065{
3066 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003067
3068 if (!req->file)
3069 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003070
3071 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3072 return -EINVAL;
3073 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3074 return -EINVAL;
3075
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003076 req->sync.off = READ_ONCE(sqe->off);
3077 req->sync.len = READ_ONCE(sqe->len);
3078 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003079 return 0;
3080}
3081
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003082static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003083{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003084 int ret;
3085
Jens Axboe9adbd452019-12-20 08:45:55 -07003086 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003087 req->sync.flags);
3088 if (ret < 0)
3089 req_set_fail_links(req);
3090 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003091 io_put_req_find_next(req, nxt);
3092}
3093
3094
3095static void io_sync_file_range_finish(struct io_wq_work **workptr)
3096{
3097 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3098 struct io_kiocb *nxt = NULL;
3099
3100 if (io_req_cancelled(req))
3101 return;
3102 __io_sync_file_range(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003103 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003104 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003105}
3106
Jens Axboefc4df992019-12-10 14:38:45 -07003107static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003108 bool force_nonblock)
3109{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003110 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003111 if (force_nonblock) {
3112 io_put_req(req);
3113 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003114 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003115 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003116
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003117 __io_sync_file_range(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003118 return 0;
3119}
3120
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003121static int io_setup_async_msg(struct io_kiocb *req,
3122 struct io_async_msghdr *kmsg)
3123{
3124 if (req->io)
3125 return -EAGAIN;
3126 if (io_alloc_async_ctx(req)) {
3127 if (kmsg->iov != kmsg->fast_iov)
3128 kfree(kmsg->iov);
3129 return -ENOMEM;
3130 }
3131 req->flags |= REQ_F_NEED_CLEANUP;
3132 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3133 return -EAGAIN;
3134}
3135
Jens Axboe3529d8c2019-12-19 18:24:38 -07003136static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003137{
Jens Axboe03b12302019-12-02 18:50:25 -07003138#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003139 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003140 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003141 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003142
Jens Axboee47293f2019-12-20 08:58:21 -07003143 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3144 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003145 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003146
Jens Axboed8768362020-02-27 14:17:49 -07003147#ifdef CONFIG_COMPAT
3148 if (req->ctx->compat)
3149 sr->msg_flags |= MSG_CMSG_COMPAT;
3150#endif
3151
Jens Axboefddafac2020-01-04 20:19:44 -07003152 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003153 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003154 /* iovec is already imported */
3155 if (req->flags & REQ_F_NEED_CLEANUP)
3156 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003157
Jens Axboed9688562019-12-09 19:35:20 -07003158 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003159 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003160 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003161 if (!ret)
3162 req->flags |= REQ_F_NEED_CLEANUP;
3163 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003164#else
Jens Axboee47293f2019-12-20 08:58:21 -07003165 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003166#endif
3167}
3168
Jens Axboefc4df992019-12-10 14:38:45 -07003169static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3170 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003171{
3172#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003173 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003174 struct socket *sock;
3175 int ret;
3176
3177 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3178 return -EINVAL;
3179
3180 sock = sock_from_file(req->file, &ret);
3181 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003182 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003183 unsigned flags;
3184
Jens Axboe03b12302019-12-02 18:50:25 -07003185 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003186 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003187 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003188 /* if iov is set, it's allocated already */
3189 if (!kmsg->iov)
3190 kmsg->iov = kmsg->fast_iov;
3191 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003192 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003193 struct io_sr_msg *sr = &req->sr_msg;
3194
Jens Axboe0b416c32019-12-15 10:57:46 -07003195 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003196 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003197
3198 io.msg.iov = io.msg.fast_iov;
3199 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3200 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003201 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003202 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003203 }
3204
Jens Axboee47293f2019-12-20 08:58:21 -07003205 flags = req->sr_msg.msg_flags;
3206 if (flags & MSG_DONTWAIT)
3207 req->flags |= REQ_F_NOWAIT;
3208 else if (force_nonblock)
3209 flags |= MSG_DONTWAIT;
3210
Jens Axboe0b416c32019-12-15 10:57:46 -07003211 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003212 if (force_nonblock && ret == -EAGAIN)
3213 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003214 if (ret == -ERESTARTSYS)
3215 ret = -EINTR;
3216 }
3217
Pavel Begunkov1e950812020-02-06 19:51:16 +03003218 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003219 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003220 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003221 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003222 if (ret < 0)
3223 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003224 io_put_req_find_next(req, nxt);
3225 return 0;
3226#else
3227 return -EOPNOTSUPP;
3228#endif
3229}
3230
Jens Axboefddafac2020-01-04 20:19:44 -07003231static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3232 bool force_nonblock)
3233{
3234#if defined(CONFIG_NET)
3235 struct socket *sock;
3236 int ret;
3237
3238 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3239 return -EINVAL;
3240
3241 sock = sock_from_file(req->file, &ret);
3242 if (sock) {
3243 struct io_sr_msg *sr = &req->sr_msg;
3244 struct msghdr msg;
3245 struct iovec iov;
3246 unsigned flags;
3247
3248 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3249 &msg.msg_iter);
3250 if (ret)
3251 return ret;
3252
3253 msg.msg_name = NULL;
3254 msg.msg_control = NULL;
3255 msg.msg_controllen = 0;
3256 msg.msg_namelen = 0;
3257
3258 flags = req->sr_msg.msg_flags;
3259 if (flags & MSG_DONTWAIT)
3260 req->flags |= REQ_F_NOWAIT;
3261 else if (force_nonblock)
3262 flags |= MSG_DONTWAIT;
3263
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003264 msg.msg_flags = flags;
3265 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003266 if (force_nonblock && ret == -EAGAIN)
3267 return -EAGAIN;
3268 if (ret == -ERESTARTSYS)
3269 ret = -EINTR;
3270 }
3271
3272 io_cqring_add_event(req, ret);
3273 if (ret < 0)
3274 req_set_fail_links(req);
3275 io_put_req_find_next(req, nxt);
3276 return 0;
3277#else
3278 return -EOPNOTSUPP;
3279#endif
3280}
3281
Jens Axboe3529d8c2019-12-19 18:24:38 -07003282static int io_recvmsg_prep(struct io_kiocb *req,
3283 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003284{
3285#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003286 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003287 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003288 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003289
Jens Axboe3529d8c2019-12-19 18:24:38 -07003290 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3291 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003292 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003293
Jens Axboed8768362020-02-27 14:17:49 -07003294#ifdef CONFIG_COMPAT
3295 if (req->ctx->compat)
3296 sr->msg_flags |= MSG_CMSG_COMPAT;
3297#endif
3298
Jens Axboefddafac2020-01-04 20:19:44 -07003299 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003300 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003301 /* iovec is already imported */
3302 if (req->flags & REQ_F_NEED_CLEANUP)
3303 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003304
Jens Axboed9688562019-12-09 19:35:20 -07003305 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003306 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003307 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003308 if (!ret)
3309 req->flags |= REQ_F_NEED_CLEANUP;
3310 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003311#else
Jens Axboee47293f2019-12-20 08:58:21 -07003312 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003313#endif
3314}
3315
Jens Axboefc4df992019-12-10 14:38:45 -07003316static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3317 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003318{
3319#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003320 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003321 struct socket *sock;
3322 int ret;
3323
3324 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3325 return -EINVAL;
3326
3327 sock = sock_from_file(req->file, &ret);
3328 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003329 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003330 unsigned flags;
3331
Jens Axboe03b12302019-12-02 18:50:25 -07003332 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003333 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003334 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003335 /* if iov is set, it's allocated already */
3336 if (!kmsg->iov)
3337 kmsg->iov = kmsg->fast_iov;
3338 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003339 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003340 struct io_sr_msg *sr = &req->sr_msg;
3341
Jens Axboe0b416c32019-12-15 10:57:46 -07003342 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003343 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003344
3345 io.msg.iov = io.msg.fast_iov;
3346 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3347 sr->msg_flags, &io.msg.uaddr,
3348 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003349 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003350 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003351 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003352
Jens Axboee47293f2019-12-20 08:58:21 -07003353 flags = req->sr_msg.msg_flags;
3354 if (flags & MSG_DONTWAIT)
3355 req->flags |= REQ_F_NOWAIT;
3356 else if (force_nonblock)
3357 flags |= MSG_DONTWAIT;
3358
3359 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3360 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003361 if (force_nonblock && ret == -EAGAIN)
3362 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003363 if (ret == -ERESTARTSYS)
3364 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003365 }
3366
Pavel Begunkov1e950812020-02-06 19:51:16 +03003367 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003368 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003369 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003370 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003371 if (ret < 0)
3372 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003373 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003374 return 0;
3375#else
3376 return -EOPNOTSUPP;
3377#endif
3378}
3379
Jens Axboefddafac2020-01-04 20:19:44 -07003380static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3381 bool force_nonblock)
3382{
3383#if defined(CONFIG_NET)
3384 struct socket *sock;
3385 int ret;
3386
3387 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3388 return -EINVAL;
3389
3390 sock = sock_from_file(req->file, &ret);
3391 if (sock) {
3392 struct io_sr_msg *sr = &req->sr_msg;
3393 struct msghdr msg;
3394 struct iovec iov;
3395 unsigned flags;
3396
3397 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3398 &msg.msg_iter);
3399 if (ret)
3400 return ret;
3401
3402 msg.msg_name = NULL;
3403 msg.msg_control = NULL;
3404 msg.msg_controllen = 0;
3405 msg.msg_namelen = 0;
3406 msg.msg_iocb = NULL;
3407 msg.msg_flags = 0;
3408
3409 flags = req->sr_msg.msg_flags;
3410 if (flags & MSG_DONTWAIT)
3411 req->flags |= REQ_F_NOWAIT;
3412 else if (force_nonblock)
3413 flags |= MSG_DONTWAIT;
3414
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003415 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003416 if (force_nonblock && ret == -EAGAIN)
3417 return -EAGAIN;
3418 if (ret == -ERESTARTSYS)
3419 ret = -EINTR;
3420 }
3421
3422 io_cqring_add_event(req, ret);
3423 if (ret < 0)
3424 req_set_fail_links(req);
3425 io_put_req_find_next(req, nxt);
3426 return 0;
3427#else
3428 return -EOPNOTSUPP;
3429#endif
3430}
3431
3432
Jens Axboe3529d8c2019-12-19 18:24:38 -07003433static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003434{
3435#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003436 struct io_accept *accept = &req->accept;
3437
Jens Axboe17f2fe32019-10-17 14:42:58 -06003438 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3439 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003440 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003441 return -EINVAL;
3442
Jens Axboed55e5f52019-12-11 16:12:15 -07003443 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3444 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003445 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003446 return 0;
3447#else
3448 return -EOPNOTSUPP;
3449#endif
3450}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003451
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003452#if defined(CONFIG_NET)
3453static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3454 bool force_nonblock)
3455{
3456 struct io_accept *accept = &req->accept;
3457 unsigned file_flags;
3458 int ret;
3459
3460 file_flags = force_nonblock ? O_NONBLOCK : 0;
3461 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3462 accept->addr_len, accept->flags);
3463 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003464 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003465 if (ret == -ERESTARTSYS)
3466 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003467 if (ret < 0)
3468 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003469 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003470 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003471 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003472}
3473
3474static void io_accept_finish(struct io_wq_work **workptr)
3475{
3476 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3477 struct io_kiocb *nxt = NULL;
3478
Jens Axboee441d1c2020-02-20 09:59:02 -07003479 io_put_req(req);
3480
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003481 if (io_req_cancelled(req))
3482 return;
3483 __io_accept(req, &nxt, false);
3484 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003485 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003486}
3487#endif
3488
3489static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3490 bool force_nonblock)
3491{
3492#if defined(CONFIG_NET)
3493 int ret;
3494
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003495 ret = __io_accept(req, nxt, force_nonblock);
3496 if (ret == -EAGAIN && force_nonblock) {
3497 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003498 return -EAGAIN;
3499 }
3500 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003501#else
3502 return -EOPNOTSUPP;
3503#endif
3504}
3505
Jens Axboe3529d8c2019-12-19 18:24:38 -07003506static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003507{
3508#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003509 struct io_connect *conn = &req->connect;
3510 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003511
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003512 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3513 return -EINVAL;
3514 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3515 return -EINVAL;
3516
Jens Axboe3529d8c2019-12-19 18:24:38 -07003517 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3518 conn->addr_len = READ_ONCE(sqe->addr2);
3519
3520 if (!io)
3521 return 0;
3522
3523 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003524 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003525#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003526 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003527#endif
3528}
3529
Jens Axboefc4df992019-12-10 14:38:45 -07003530static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3531 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003532{
3533#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003534 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003535 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003536 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003537
Jens Axboef499a022019-12-02 16:28:46 -07003538 if (req->io) {
3539 io = req->io;
3540 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003541 ret = move_addr_to_kernel(req->connect.addr,
3542 req->connect.addr_len,
3543 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003544 if (ret)
3545 goto out;
3546 io = &__io;
3547 }
3548
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003549 file_flags = force_nonblock ? O_NONBLOCK : 0;
3550
3551 ret = __sys_connect_file(req->file, &io->connect.address,
3552 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003553 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003554 if (req->io)
3555 return -EAGAIN;
3556 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003557 ret = -ENOMEM;
3558 goto out;
3559 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003560 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003561 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003562 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003563 if (ret == -ERESTARTSYS)
3564 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003565out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003566 if (ret < 0)
3567 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003568 io_cqring_add_event(req, ret);
3569 io_put_req_find_next(req, nxt);
3570 return 0;
3571#else
3572 return -EOPNOTSUPP;
3573#endif
3574}
3575
Jens Axboed7718a92020-02-14 22:23:12 -07003576struct io_poll_table {
3577 struct poll_table_struct pt;
3578 struct io_kiocb *req;
3579 int error;
3580};
3581
3582static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
3583 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003584{
Jens Axboed7718a92020-02-14 22:23:12 -07003585 if (unlikely(poll->head)) {
3586 pt->error = -EINVAL;
3587 return;
3588 }
3589
3590 pt->error = 0;
3591 poll->head = head;
3592 add_wait_queue(head, &poll->wait);
3593}
3594
3595static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
3596 struct poll_table_struct *p)
3597{
3598 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3599
3600 __io_queue_proc(&pt->req->apoll->poll, pt, head);
3601}
3602
3603static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
3604 __poll_t mask, task_work_func_t func)
3605{
3606 struct task_struct *tsk;
3607
3608 /* for instances that support it check for an event match first: */
3609 if (mask && !(mask & poll->events))
3610 return 0;
3611
3612 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
3613
3614 list_del_init(&poll->wait.entry);
3615
3616 tsk = req->task;
3617 req->result = mask;
3618 init_task_work(&req->task_work, func);
3619 /*
3620 * If this fails, then the task is exiting. If that is the case, then
3621 * the exit check will ultimately cancel these work items. Hence we
3622 * don't need to check here and handle it specifically.
3623 */
3624 task_work_add(tsk, &req->task_work, true);
3625 wake_up_process(tsk);
3626 return 1;
3627}
3628
3629static void io_async_task_func(struct callback_head *cb)
3630{
3631 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3632 struct async_poll *apoll = req->apoll;
3633 struct io_ring_ctx *ctx = req->ctx;
3634
3635 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
3636
3637 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
3638
3639 if (hash_hashed(&req->hash_node)) {
3640 spin_lock_irq(&ctx->completion_lock);
3641 hash_del(&req->hash_node);
3642 spin_unlock_irq(&ctx->completion_lock);
3643 }
3644
3645 /* restore ->work in case we need to retry again */
3646 memcpy(&req->work, &apoll->work, sizeof(req->work));
3647
3648 __set_current_state(TASK_RUNNING);
3649 mutex_lock(&ctx->uring_lock);
3650 __io_queue_sqe(req, NULL);
3651 mutex_unlock(&ctx->uring_lock);
3652
3653 kfree(apoll);
3654}
3655
3656static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3657 void *key)
3658{
3659 struct io_kiocb *req = wait->private;
3660 struct io_poll_iocb *poll = &req->apoll->poll;
3661
3662 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
3663 key_to_poll(key));
3664
3665 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
3666}
3667
3668static void io_poll_req_insert(struct io_kiocb *req)
3669{
3670 struct io_ring_ctx *ctx = req->ctx;
3671 struct hlist_head *list;
3672
3673 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3674 hlist_add_head(&req->hash_node, list);
3675}
3676
3677static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
3678 struct io_poll_iocb *poll,
3679 struct io_poll_table *ipt, __poll_t mask,
3680 wait_queue_func_t wake_func)
3681 __acquires(&ctx->completion_lock)
3682{
3683 struct io_ring_ctx *ctx = req->ctx;
3684 bool cancel = false;
3685
3686 poll->file = req->file;
3687 poll->head = NULL;
3688 poll->done = poll->canceled = false;
3689 poll->events = mask;
3690
3691 ipt->pt._key = mask;
3692 ipt->req = req;
3693 ipt->error = -EINVAL;
3694
3695 INIT_LIST_HEAD(&poll->wait.entry);
3696 init_waitqueue_func_entry(&poll->wait, wake_func);
3697 poll->wait.private = req;
3698
3699 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
3700
3701 spin_lock_irq(&ctx->completion_lock);
3702 if (likely(poll->head)) {
3703 spin_lock(&poll->head->lock);
3704 if (unlikely(list_empty(&poll->wait.entry))) {
3705 if (ipt->error)
3706 cancel = true;
3707 ipt->error = 0;
3708 mask = 0;
3709 }
3710 if (mask || ipt->error)
3711 list_del_init(&poll->wait.entry);
3712 else if (cancel)
3713 WRITE_ONCE(poll->canceled, true);
3714 else if (!poll->done) /* actually waiting for an event */
3715 io_poll_req_insert(req);
3716 spin_unlock(&poll->head->lock);
3717 }
3718
3719 return mask;
3720}
3721
3722static bool io_arm_poll_handler(struct io_kiocb *req)
3723{
3724 const struct io_op_def *def = &io_op_defs[req->opcode];
3725 struct io_ring_ctx *ctx = req->ctx;
3726 struct async_poll *apoll;
3727 struct io_poll_table ipt;
3728 __poll_t mask, ret;
3729
3730 if (!req->file || !file_can_poll(req->file))
3731 return false;
3732 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
3733 return false;
3734 if (!def->pollin && !def->pollout)
3735 return false;
3736
3737 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
3738 if (unlikely(!apoll))
3739 return false;
3740
3741 req->flags |= REQ_F_POLLED;
3742 memcpy(&apoll->work, &req->work, sizeof(req->work));
3743
3744 /*
3745 * Don't need a reference here, as we're adding it to the task
3746 * task_works list. If the task exits, the list is pruned.
3747 */
3748 req->task = current;
3749 req->apoll = apoll;
3750 INIT_HLIST_NODE(&req->hash_node);
3751
3752 if (def->pollin)
3753 mask = POLLIN | POLLRDNORM;
3754 if (def->pollout)
3755 mask |= POLLOUT | POLLWRNORM;
3756 mask |= POLLERR | POLLPRI;
3757
3758 ipt.pt._qproc = io_async_queue_proc;
3759
3760 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
3761 io_async_wake);
3762 if (ret) {
3763 ipt.error = 0;
3764 apoll->poll.done = true;
3765 spin_unlock_irq(&ctx->completion_lock);
3766 memcpy(&req->work, &apoll->work, sizeof(req->work));
3767 kfree(apoll);
3768 return false;
3769 }
3770 spin_unlock_irq(&ctx->completion_lock);
3771 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
3772 apoll->poll.events);
3773 return true;
3774}
3775
3776static bool __io_poll_remove_one(struct io_kiocb *req,
3777 struct io_poll_iocb *poll)
3778{
Jens Axboeb41e9852020-02-17 09:52:41 -07003779 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003780
3781 spin_lock(&poll->head->lock);
3782 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003783 if (!list_empty(&poll->wait.entry)) {
3784 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07003785 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003786 }
3787 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07003788 return do_complete;
3789}
3790
3791static bool io_poll_remove_one(struct io_kiocb *req)
3792{
3793 bool do_complete;
3794
3795 if (req->opcode == IORING_OP_POLL_ADD) {
3796 do_complete = __io_poll_remove_one(req, &req->poll);
3797 } else {
3798 /* non-poll requests have submit ref still */
3799 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
3800 if (do_complete)
3801 io_put_req(req);
3802 }
3803
Jens Axboe78076bb2019-12-04 19:56:40 -07003804 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07003805
Jens Axboeb41e9852020-02-17 09:52:41 -07003806 if (do_complete) {
3807 io_cqring_fill_event(req, -ECANCELED);
3808 io_commit_cqring(req->ctx);
3809 req->flags |= REQ_F_COMP_LOCKED;
3810 io_put_req(req);
3811 }
3812
3813 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003814}
3815
3816static void io_poll_remove_all(struct io_ring_ctx *ctx)
3817{
Jens Axboe78076bb2019-12-04 19:56:40 -07003818 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003819 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003820 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003821
3822 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003823 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3824 struct hlist_head *list;
3825
3826 list = &ctx->cancel_hash[i];
3827 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3828 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003829 }
3830 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003831
3832 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003833}
3834
Jens Axboe47f46762019-11-09 17:43:02 -07003835static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3836{
Jens Axboe78076bb2019-12-04 19:56:40 -07003837 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003838 struct io_kiocb *req;
3839
Jens Axboe78076bb2019-12-04 19:56:40 -07003840 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3841 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07003842 if (sqe_addr != req->user_data)
3843 continue;
3844 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07003845 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07003846 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07003847 }
3848
3849 return -ENOENT;
3850}
3851
Jens Axboe3529d8c2019-12-19 18:24:38 -07003852static int io_poll_remove_prep(struct io_kiocb *req,
3853 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003854{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003855 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3856 return -EINVAL;
3857 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3858 sqe->poll_events)
3859 return -EINVAL;
3860
Jens Axboe0969e782019-12-17 18:40:57 -07003861 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003862 return 0;
3863}
3864
3865/*
3866 * Find a running poll command that matches one specified in sqe->addr,
3867 * and remove it if found.
3868 */
3869static int io_poll_remove(struct io_kiocb *req)
3870{
3871 struct io_ring_ctx *ctx = req->ctx;
3872 u64 addr;
3873 int ret;
3874
Jens Axboe0969e782019-12-17 18:40:57 -07003875 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003876 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003877 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003878 spin_unlock_irq(&ctx->completion_lock);
3879
Jens Axboe78e19bb2019-11-06 15:21:34 -07003880 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003881 if (ret < 0)
3882 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003883 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003884 return 0;
3885}
3886
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003887static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003888{
Jackie Liua197f662019-11-08 08:09:12 -07003889 struct io_ring_ctx *ctx = req->ctx;
3890
Jens Axboe8c838782019-03-12 15:48:16 -06003891 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03003892 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003893 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003894}
3895
Jens Axboeb41e9852020-02-17 09:52:41 -07003896static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003897{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003898 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003899
Jens Axboe221c5eb2019-01-17 09:41:58 -07003900 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003901 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07003902 io_poll_complete(req, req->result, 0);
3903 req->flags |= REQ_F_COMP_LOCKED;
3904 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003905 spin_unlock_irq(&ctx->completion_lock);
3906
Jens Axboe8c838782019-03-12 15:48:16 -06003907 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07003908}
Jens Axboe89723d02019-11-05 15:32:58 -07003909
Jens Axboeb41e9852020-02-17 09:52:41 -07003910static void io_poll_task_func(struct callback_head *cb)
3911{
3912 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3913 struct io_kiocb *nxt = NULL;
3914
3915 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07003916 if (nxt) {
3917 struct io_ring_ctx *ctx = nxt->ctx;
3918
3919 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003920 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07003921 mutex_unlock(&ctx->uring_lock);
3922 }
Jens Axboef0b493e2020-02-01 21:30:11 -07003923}
3924
Jens Axboe221c5eb2019-01-17 09:41:58 -07003925static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3926 void *key)
3927{
Jens Axboec2f2eb72020-02-10 09:07:05 -07003928 struct io_kiocb *req = wait->private;
3929 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003930
Jens Axboed7718a92020-02-14 22:23:12 -07003931 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003932}
3933
Jens Axboe221c5eb2019-01-17 09:41:58 -07003934static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3935 struct poll_table_struct *p)
3936{
3937 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3938
Jens Axboed7718a92020-02-14 22:23:12 -07003939 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07003940}
3941
Jens Axboe3529d8c2019-12-19 18:24:38 -07003942static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003943{
3944 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003945 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003946
3947 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3948 return -EINVAL;
3949 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3950 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003951 if (!poll->file)
3952 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003953
Jens Axboe221c5eb2019-01-17 09:41:58 -07003954 events = READ_ONCE(sqe->poll_events);
3955 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07003956
Jens Axboed7718a92020-02-14 22:23:12 -07003957 /*
3958 * Don't need a reference here, as we're adding it to the task
3959 * task_works list. If the task exits, the list is pruned.
3960 */
Jens Axboeb41e9852020-02-17 09:52:41 -07003961 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07003962 return 0;
3963}
3964
3965static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3966{
3967 struct io_poll_iocb *poll = &req->poll;
3968 struct io_ring_ctx *ctx = req->ctx;
3969 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07003970 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003971
Jens Axboe78076bb2019-12-04 19:56:40 -07003972 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06003973 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07003974 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06003975
Jens Axboed7718a92020-02-14 22:23:12 -07003976 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
3977 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003978
Jens Axboe8c838782019-03-12 15:48:16 -06003979 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003980 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003981 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003982 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003983 spin_unlock_irq(&ctx->completion_lock);
3984
Jens Axboe8c838782019-03-12 15:48:16 -06003985 if (mask) {
3986 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003987 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003988 }
Jens Axboe8c838782019-03-12 15:48:16 -06003989 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003990}
3991
Jens Axboe5262f562019-09-17 12:26:57 -06003992static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3993{
Jens Axboead8a48a2019-11-15 08:49:11 -07003994 struct io_timeout_data *data = container_of(timer,
3995 struct io_timeout_data, timer);
3996 struct io_kiocb *req = data->req;
3997 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003998 unsigned long flags;
3999
Jens Axboe5262f562019-09-17 12:26:57 -06004000 atomic_inc(&ctx->cq_timeouts);
4001
4002 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004003 /*
Jens Axboe11365042019-10-16 09:08:32 -06004004 * We could be racing with timeout deletion. If the list is empty,
4005 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004006 */
Jens Axboe842f9612019-10-29 12:34:10 -06004007 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004008 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004009
Jens Axboe11365042019-10-16 09:08:32 -06004010 /*
4011 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004012 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004013 * pointer will be increased, otherwise other timeout reqs may
4014 * return in advance without waiting for enough wait_nr.
4015 */
4016 prev = req;
4017 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4018 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004019 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004020 }
Jens Axboe842f9612019-10-29 12:34:10 -06004021
Jens Axboe78e19bb2019-11-06 15:21:34 -07004022 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004023 io_commit_cqring(ctx);
4024 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4025
4026 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004027 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004028 io_put_req(req);
4029 return HRTIMER_NORESTART;
4030}
4031
Jens Axboe47f46762019-11-09 17:43:02 -07004032static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4033{
4034 struct io_kiocb *req;
4035 int ret = -ENOENT;
4036
4037 list_for_each_entry(req, &ctx->timeout_list, list) {
4038 if (user_data == req->user_data) {
4039 list_del_init(&req->list);
4040 ret = 0;
4041 break;
4042 }
4043 }
4044
4045 if (ret == -ENOENT)
4046 return ret;
4047
Jens Axboe2d283902019-12-04 11:08:05 -07004048 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004049 if (ret == -1)
4050 return -EALREADY;
4051
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004052 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004053 io_cqring_fill_event(req, -ECANCELED);
4054 io_put_req(req);
4055 return 0;
4056}
4057
Jens Axboe3529d8c2019-12-19 18:24:38 -07004058static int io_timeout_remove_prep(struct io_kiocb *req,
4059 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004060{
Jens Axboeb29472e2019-12-17 18:50:29 -07004061 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4062 return -EINVAL;
4063 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4064 return -EINVAL;
4065
4066 req->timeout.addr = READ_ONCE(sqe->addr);
4067 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4068 if (req->timeout.flags)
4069 return -EINVAL;
4070
Jens Axboeb29472e2019-12-17 18:50:29 -07004071 return 0;
4072}
4073
Jens Axboe11365042019-10-16 09:08:32 -06004074/*
4075 * Remove or update an existing timeout command
4076 */
Jens Axboefc4df992019-12-10 14:38:45 -07004077static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004078{
4079 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004080 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004081
Jens Axboe11365042019-10-16 09:08:32 -06004082 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004083 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004084
Jens Axboe47f46762019-11-09 17:43:02 -07004085 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004086 io_commit_cqring(ctx);
4087 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004088 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004089 if (ret < 0)
4090 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004091 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004092 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004093}
4094
Jens Axboe3529d8c2019-12-19 18:24:38 -07004095static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004096 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004097{
Jens Axboead8a48a2019-11-15 08:49:11 -07004098 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004099 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004100
Jens Axboead8a48a2019-11-15 08:49:11 -07004101 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004102 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004103 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004104 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004105 if (sqe->off && is_timeout_link)
4106 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004107 flags = READ_ONCE(sqe->timeout_flags);
4108 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004109 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004110
Jens Axboe26a61672019-12-20 09:02:01 -07004111 req->timeout.count = READ_ONCE(sqe->off);
4112
Jens Axboe3529d8c2019-12-19 18:24:38 -07004113 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004114 return -ENOMEM;
4115
4116 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004117 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004118 req->flags |= REQ_F_TIMEOUT;
4119
4120 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004121 return -EFAULT;
4122
Jens Axboe11365042019-10-16 09:08:32 -06004123 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004124 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004125 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004126 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004127
Jens Axboead8a48a2019-11-15 08:49:11 -07004128 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4129 return 0;
4130}
4131
Jens Axboefc4df992019-12-10 14:38:45 -07004132static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004133{
4134 unsigned count;
4135 struct io_ring_ctx *ctx = req->ctx;
4136 struct io_timeout_data *data;
4137 struct list_head *entry;
4138 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004139
Jens Axboe2d283902019-12-04 11:08:05 -07004140 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004141
Jens Axboe5262f562019-09-17 12:26:57 -06004142 /*
4143 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004144 * timeout event to be satisfied. If it isn't set, then this is
4145 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004146 */
Jens Axboe26a61672019-12-20 09:02:01 -07004147 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004148 if (!count) {
4149 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4150 spin_lock_irq(&ctx->completion_lock);
4151 entry = ctx->timeout_list.prev;
4152 goto add;
4153 }
Jens Axboe5262f562019-09-17 12:26:57 -06004154
4155 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004156 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004157
4158 /*
4159 * Insertion sort, ensuring the first entry in the list is always
4160 * the one we need first.
4161 */
Jens Axboe5262f562019-09-17 12:26:57 -06004162 spin_lock_irq(&ctx->completion_lock);
4163 list_for_each_prev(entry, &ctx->timeout_list) {
4164 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004165 unsigned nxt_sq_head;
4166 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004167 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004168
Jens Axboe93bd25b2019-11-11 23:34:31 -07004169 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4170 continue;
4171
yangerkun5da0fb12019-10-15 21:59:29 +08004172 /*
4173 * Since cached_sq_head + count - 1 can overflow, use type long
4174 * long to store it.
4175 */
4176 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004177 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4178 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004179
4180 /*
4181 * cached_sq_head may overflow, and it will never overflow twice
4182 * once there is some timeout req still be valid.
4183 */
4184 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004185 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004186
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004187 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004188 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004189
4190 /*
4191 * Sequence of reqs after the insert one and itself should
4192 * be adjusted because each timeout req consumes a slot.
4193 */
4194 span++;
4195 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004196 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004197 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004198add:
Jens Axboe5262f562019-09-17 12:26:57 -06004199 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004200 data->timer.function = io_timeout_fn;
4201 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004202 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004203 return 0;
4204}
4205
Jens Axboe62755e32019-10-28 21:49:21 -06004206static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004207{
Jens Axboe62755e32019-10-28 21:49:21 -06004208 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004209
Jens Axboe62755e32019-10-28 21:49:21 -06004210 return req->user_data == (unsigned long) data;
4211}
4212
Jens Axboee977d6d2019-11-05 12:39:45 -07004213static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004214{
Jens Axboe62755e32019-10-28 21:49:21 -06004215 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004216 int ret = 0;
4217
Jens Axboe62755e32019-10-28 21:49:21 -06004218 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4219 switch (cancel_ret) {
4220 case IO_WQ_CANCEL_OK:
4221 ret = 0;
4222 break;
4223 case IO_WQ_CANCEL_RUNNING:
4224 ret = -EALREADY;
4225 break;
4226 case IO_WQ_CANCEL_NOTFOUND:
4227 ret = -ENOENT;
4228 break;
4229 }
4230
Jens Axboee977d6d2019-11-05 12:39:45 -07004231 return ret;
4232}
4233
Jens Axboe47f46762019-11-09 17:43:02 -07004234static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4235 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004236 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004237{
4238 unsigned long flags;
4239 int ret;
4240
4241 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4242 if (ret != -ENOENT) {
4243 spin_lock_irqsave(&ctx->completion_lock, flags);
4244 goto done;
4245 }
4246
4247 spin_lock_irqsave(&ctx->completion_lock, flags);
4248 ret = io_timeout_cancel(ctx, sqe_addr);
4249 if (ret != -ENOENT)
4250 goto done;
4251 ret = io_poll_cancel(ctx, sqe_addr);
4252done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004253 if (!ret)
4254 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004255 io_cqring_fill_event(req, ret);
4256 io_commit_cqring(ctx);
4257 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4258 io_cqring_ev_posted(ctx);
4259
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004260 if (ret < 0)
4261 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004262 io_put_req_find_next(req, nxt);
4263}
4264
Jens Axboe3529d8c2019-12-19 18:24:38 -07004265static int io_async_cancel_prep(struct io_kiocb *req,
4266 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004267{
Jens Axboefbf23842019-12-17 18:45:56 -07004268 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004269 return -EINVAL;
4270 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4271 sqe->cancel_flags)
4272 return -EINVAL;
4273
Jens Axboefbf23842019-12-17 18:45:56 -07004274 req->cancel.addr = READ_ONCE(sqe->addr);
4275 return 0;
4276}
4277
4278static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4279{
4280 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004281
4282 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004283 return 0;
4284}
4285
Jens Axboe05f3fb32019-12-09 11:22:50 -07004286static int io_files_update_prep(struct io_kiocb *req,
4287 const struct io_uring_sqe *sqe)
4288{
4289 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4290 return -EINVAL;
4291
4292 req->files_update.offset = READ_ONCE(sqe->off);
4293 req->files_update.nr_args = READ_ONCE(sqe->len);
4294 if (!req->files_update.nr_args)
4295 return -EINVAL;
4296 req->files_update.arg = READ_ONCE(sqe->addr);
4297 return 0;
4298}
4299
4300static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4301{
4302 struct io_ring_ctx *ctx = req->ctx;
4303 struct io_uring_files_update up;
4304 int ret;
4305
Jens Axboef86cd202020-01-29 13:46:44 -07004306 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004307 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004308
4309 up.offset = req->files_update.offset;
4310 up.fds = req->files_update.arg;
4311
4312 mutex_lock(&ctx->uring_lock);
4313 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4314 mutex_unlock(&ctx->uring_lock);
4315
4316 if (ret < 0)
4317 req_set_fail_links(req);
4318 io_cqring_add_event(req, ret);
4319 io_put_req(req);
4320 return 0;
4321}
4322
Jens Axboe3529d8c2019-12-19 18:24:38 -07004323static int io_req_defer_prep(struct io_kiocb *req,
4324 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004325{
Jens Axboee7815732019-12-17 19:45:06 -07004326 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004327
Jens Axboef86cd202020-01-29 13:46:44 -07004328 if (io_op_defs[req->opcode].file_table) {
4329 ret = io_grab_files(req);
4330 if (unlikely(ret))
4331 return ret;
4332 }
4333
Jens Axboecccf0ee2020-01-27 16:34:48 -07004334 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4335
Jens Axboed625c6e2019-12-17 19:53:05 -07004336 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004337 case IORING_OP_NOP:
4338 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004339 case IORING_OP_READV:
4340 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004341 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004342 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004343 break;
4344 case IORING_OP_WRITEV:
4345 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004346 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004347 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004348 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004349 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004350 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004351 break;
4352 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004353 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004354 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004355 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004356 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004357 break;
4358 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004359 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004360 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004361 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004362 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004364 break;
4365 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004366 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004367 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004368 break;
Jens Axboef499a022019-12-02 16:28:46 -07004369 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004370 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004371 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004372 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004373 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004374 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004375 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004376 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004377 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004378 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004379 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004380 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004381 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004382 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004383 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004384 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004385 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004386 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004387 case IORING_OP_FALLOCATE:
4388 ret = io_fallocate_prep(req, sqe);
4389 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004390 case IORING_OP_OPENAT:
4391 ret = io_openat_prep(req, sqe);
4392 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004393 case IORING_OP_CLOSE:
4394 ret = io_close_prep(req, sqe);
4395 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004396 case IORING_OP_FILES_UPDATE:
4397 ret = io_files_update_prep(req, sqe);
4398 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004399 case IORING_OP_STATX:
4400 ret = io_statx_prep(req, sqe);
4401 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004402 case IORING_OP_FADVISE:
4403 ret = io_fadvise_prep(req, sqe);
4404 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004405 case IORING_OP_MADVISE:
4406 ret = io_madvise_prep(req, sqe);
4407 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004408 case IORING_OP_OPENAT2:
4409 ret = io_openat2_prep(req, sqe);
4410 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004411 case IORING_OP_EPOLL_CTL:
4412 ret = io_epoll_ctl_prep(req, sqe);
4413 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004414 case IORING_OP_SPLICE:
4415 ret = io_splice_prep(req, sqe);
4416 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004417 default:
Jens Axboee7815732019-12-17 19:45:06 -07004418 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4419 req->opcode);
4420 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004421 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004422 }
4423
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004424 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004425}
4426
Jens Axboe3529d8c2019-12-19 18:24:38 -07004427static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004428{
Jackie Liua197f662019-11-08 08:09:12 -07004429 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004430 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004431
Bob Liu9d858b22019-11-13 18:06:25 +08004432 /* Still need defer if there is pending req in defer list. */
4433 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004434 return 0;
4435
Jens Axboe3529d8c2019-12-19 18:24:38 -07004436 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004437 return -EAGAIN;
4438
Jens Axboe3529d8c2019-12-19 18:24:38 -07004439 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004440 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004441 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004442
Jens Axboede0617e2019-04-06 21:51:27 -06004443 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004444 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004445 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004446 return 0;
4447 }
4448
Jens Axboe915967f2019-11-21 09:01:20 -07004449 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004450 list_add_tail(&req->list, &ctx->defer_list);
4451 spin_unlock_irq(&ctx->completion_lock);
4452 return -EIOCBQUEUED;
4453}
4454
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004455static void io_cleanup_req(struct io_kiocb *req)
4456{
4457 struct io_async_ctx *io = req->io;
4458
4459 switch (req->opcode) {
4460 case IORING_OP_READV:
4461 case IORING_OP_READ_FIXED:
4462 case IORING_OP_READ:
4463 case IORING_OP_WRITEV:
4464 case IORING_OP_WRITE_FIXED:
4465 case IORING_OP_WRITE:
4466 if (io->rw.iov != io->rw.fast_iov)
4467 kfree(io->rw.iov);
4468 break;
4469 case IORING_OP_SENDMSG:
4470 case IORING_OP_RECVMSG:
4471 if (io->msg.iov != io->msg.fast_iov)
4472 kfree(io->msg.iov);
4473 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004474 case IORING_OP_OPENAT:
4475 case IORING_OP_OPENAT2:
4476 case IORING_OP_STATX:
4477 putname(req->open.filename);
4478 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004479 case IORING_OP_SPLICE:
4480 io_put_file(req, req->splice.file_in,
4481 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4482 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004483 }
4484
4485 req->flags &= ~REQ_F_NEED_CLEANUP;
4486}
4487
Jens Axboe3529d8c2019-12-19 18:24:38 -07004488static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4489 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004490{
Jackie Liua197f662019-11-08 08:09:12 -07004491 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004492 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004493
Jens Axboed625c6e2019-12-17 19:53:05 -07004494 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004495 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004496 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004497 break;
4498 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004499 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004500 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004501 if (sqe) {
4502 ret = io_read_prep(req, sqe, force_nonblock);
4503 if (ret < 0)
4504 break;
4505 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004506 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004507 break;
4508 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004509 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004510 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004511 if (sqe) {
4512 ret = io_write_prep(req, sqe, force_nonblock);
4513 if (ret < 0)
4514 break;
4515 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004516 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004517 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004518 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004519 if (sqe) {
4520 ret = io_prep_fsync(req, sqe);
4521 if (ret < 0)
4522 break;
4523 }
Jens Axboefc4df992019-12-10 14:38:45 -07004524 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004525 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004526 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004527 if (sqe) {
4528 ret = io_poll_add_prep(req, sqe);
4529 if (ret)
4530 break;
4531 }
Jens Axboefc4df992019-12-10 14:38:45 -07004532 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004533 break;
4534 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004535 if (sqe) {
4536 ret = io_poll_remove_prep(req, sqe);
4537 if (ret < 0)
4538 break;
4539 }
Jens Axboefc4df992019-12-10 14:38:45 -07004540 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004541 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004542 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004543 if (sqe) {
4544 ret = io_prep_sfr(req, sqe);
4545 if (ret < 0)
4546 break;
4547 }
Jens Axboefc4df992019-12-10 14:38:45 -07004548 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004549 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004550 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004551 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004552 if (sqe) {
4553 ret = io_sendmsg_prep(req, sqe);
4554 if (ret < 0)
4555 break;
4556 }
Jens Axboefddafac2020-01-04 20:19:44 -07004557 if (req->opcode == IORING_OP_SENDMSG)
4558 ret = io_sendmsg(req, nxt, force_nonblock);
4559 else
4560 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004561 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004562 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004563 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004564 if (sqe) {
4565 ret = io_recvmsg_prep(req, sqe);
4566 if (ret)
4567 break;
4568 }
Jens Axboefddafac2020-01-04 20:19:44 -07004569 if (req->opcode == IORING_OP_RECVMSG)
4570 ret = io_recvmsg(req, nxt, force_nonblock);
4571 else
4572 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004573 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004574 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004575 if (sqe) {
4576 ret = io_timeout_prep(req, sqe, false);
4577 if (ret)
4578 break;
4579 }
Jens Axboefc4df992019-12-10 14:38:45 -07004580 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004581 break;
Jens Axboe11365042019-10-16 09:08:32 -06004582 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004583 if (sqe) {
4584 ret = io_timeout_remove_prep(req, sqe);
4585 if (ret)
4586 break;
4587 }
Jens Axboefc4df992019-12-10 14:38:45 -07004588 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004589 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004590 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004591 if (sqe) {
4592 ret = io_accept_prep(req, sqe);
4593 if (ret)
4594 break;
4595 }
Jens Axboefc4df992019-12-10 14:38:45 -07004596 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004597 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004598 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004599 if (sqe) {
4600 ret = io_connect_prep(req, sqe);
4601 if (ret)
4602 break;
4603 }
Jens Axboefc4df992019-12-10 14:38:45 -07004604 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004605 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004606 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004607 if (sqe) {
4608 ret = io_async_cancel_prep(req, sqe);
4609 if (ret)
4610 break;
4611 }
Jens Axboefc4df992019-12-10 14:38:45 -07004612 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004613 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004614 case IORING_OP_FALLOCATE:
4615 if (sqe) {
4616 ret = io_fallocate_prep(req, sqe);
4617 if (ret)
4618 break;
4619 }
4620 ret = io_fallocate(req, nxt, force_nonblock);
4621 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004622 case IORING_OP_OPENAT:
4623 if (sqe) {
4624 ret = io_openat_prep(req, sqe);
4625 if (ret)
4626 break;
4627 }
4628 ret = io_openat(req, nxt, force_nonblock);
4629 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004630 case IORING_OP_CLOSE:
4631 if (sqe) {
4632 ret = io_close_prep(req, sqe);
4633 if (ret)
4634 break;
4635 }
4636 ret = io_close(req, nxt, force_nonblock);
4637 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004638 case IORING_OP_FILES_UPDATE:
4639 if (sqe) {
4640 ret = io_files_update_prep(req, sqe);
4641 if (ret)
4642 break;
4643 }
4644 ret = io_files_update(req, force_nonblock);
4645 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004646 case IORING_OP_STATX:
4647 if (sqe) {
4648 ret = io_statx_prep(req, sqe);
4649 if (ret)
4650 break;
4651 }
4652 ret = io_statx(req, nxt, force_nonblock);
4653 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004654 case IORING_OP_FADVISE:
4655 if (sqe) {
4656 ret = io_fadvise_prep(req, sqe);
4657 if (ret)
4658 break;
4659 }
4660 ret = io_fadvise(req, nxt, force_nonblock);
4661 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004662 case IORING_OP_MADVISE:
4663 if (sqe) {
4664 ret = io_madvise_prep(req, sqe);
4665 if (ret)
4666 break;
4667 }
4668 ret = io_madvise(req, nxt, force_nonblock);
4669 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004670 case IORING_OP_OPENAT2:
4671 if (sqe) {
4672 ret = io_openat2_prep(req, sqe);
4673 if (ret)
4674 break;
4675 }
4676 ret = io_openat2(req, nxt, force_nonblock);
4677 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004678 case IORING_OP_EPOLL_CTL:
4679 if (sqe) {
4680 ret = io_epoll_ctl_prep(req, sqe);
4681 if (ret)
4682 break;
4683 }
4684 ret = io_epoll_ctl(req, nxt, force_nonblock);
4685 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004686 case IORING_OP_SPLICE:
4687 if (sqe) {
4688 ret = io_splice_prep(req, sqe);
4689 if (ret < 0)
4690 break;
4691 }
4692 ret = io_splice(req, nxt, force_nonblock);
4693 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004694 default:
4695 ret = -EINVAL;
4696 break;
4697 }
4698
Jens Axboedef596e2019-01-09 08:59:42 -07004699 if (ret)
4700 return ret;
4701
4702 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004703 const bool in_async = io_wq_current_is_worker();
4704
Jens Axboe9e645e112019-05-10 16:07:28 -06004705 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004706 return -EAGAIN;
4707
Jens Axboe11ba8202020-01-15 21:51:17 -07004708 /* workqueue context doesn't hold uring_lock, grab it now */
4709 if (in_async)
4710 mutex_lock(&ctx->uring_lock);
4711
Jens Axboedef596e2019-01-09 08:59:42 -07004712 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004713
4714 if (in_async)
4715 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004716 }
4717
4718 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004719}
4720
Jens Axboe561fb042019-10-24 07:25:42 -06004721static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004722{
Jens Axboe561fb042019-10-24 07:25:42 -06004723 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004725 struct io_kiocb *nxt = NULL;
4726 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004727
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004728 /* if NO_CANCEL is set, we must still run the work */
4729 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4730 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004731 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004732 }
Jens Axboe31b51512019-01-18 22:56:34 -07004733
Jens Axboe561fb042019-10-24 07:25:42 -06004734 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004735 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004736 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004737 /*
4738 * We can get EAGAIN for polled IO even though we're
4739 * forcing a sync submission from here, since we can't
4740 * wait for request slots on the block side.
4741 */
4742 if (ret != -EAGAIN)
4743 break;
4744 cond_resched();
4745 } while (1);
4746 }
Jens Axboe31b51512019-01-18 22:56:34 -07004747
Jens Axboe561fb042019-10-24 07:25:42 -06004748 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004749 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004750
Jens Axboe561fb042019-10-24 07:25:42 -06004751 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004752 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004753 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004754 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004755 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004756
Jens Axboe561fb042019-10-24 07:25:42 -06004757 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004758 if (!ret && nxt)
4759 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004760}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004761
Jens Axboe15b71ab2019-12-11 11:20:36 -07004762static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004763{
Jens Axboed3656342019-12-18 09:50:26 -07004764 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004765 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004766 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004767 return 0;
4768 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004769}
4770
Jens Axboe65e19f52019-10-26 07:20:21 -06004771static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4772 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004773{
Jens Axboe65e19f52019-10-26 07:20:21 -06004774 struct fixed_file_table *table;
4775
Jens Axboe05f3fb32019-12-09 11:22:50 -07004776 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4777 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004778}
4779
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004780static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
4781 int fd, struct file **out_file, bool fixed)
4782{
4783 struct io_ring_ctx *ctx = req->ctx;
4784 struct file *file;
4785
4786 if (fixed) {
4787 if (unlikely(!ctx->file_data ||
4788 (unsigned) fd >= ctx->nr_user_files))
4789 return -EBADF;
4790 fd = array_index_nospec(fd, ctx->nr_user_files);
4791 file = io_file_from_index(ctx, fd);
4792 if (!file)
4793 return -EBADF;
4794 percpu_ref_get(&ctx->file_data->refs);
4795 } else {
4796 trace_io_uring_file_get(ctx, fd);
4797 file = __io_file_get(state, fd);
4798 if (unlikely(!file))
4799 return -EBADF;
4800 }
4801
4802 *out_file = file;
4803 return 0;
4804}
4805
Jens Axboe3529d8c2019-12-19 18:24:38 -07004806static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4807 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004808{
4809 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004810 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004811 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06004812
Jens Axboe3529d8c2019-12-19 18:24:38 -07004813 flags = READ_ONCE(sqe->flags);
4814 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004815
Jens Axboed3656342019-12-18 09:50:26 -07004816 if (!io_req_needs_file(req, fd))
4817 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004818
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004819 fixed = (flags & IOSQE_FIXED_FILE);
4820 if (unlikely(!fixed && req->needs_fixed_file))
4821 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004822
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004823 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06004824}
4825
Jackie Liua197f662019-11-08 08:09:12 -07004826static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004827{
Jens Axboefcb323c2019-10-24 12:39:47 -06004828 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004829 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004830
Jens Axboef86cd202020-01-29 13:46:44 -07004831 if (req->work.files)
4832 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004833 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004834 return -EBADF;
4835
Jens Axboefcb323c2019-10-24 12:39:47 -06004836 rcu_read_lock();
4837 spin_lock_irq(&ctx->inflight_lock);
4838 /*
4839 * We use the f_ops->flush() handler to ensure that we can flush
4840 * out work accessing these files if the fd is closed. Check if
4841 * the fd has changed since we started down this path, and disallow
4842 * this operation if it has.
4843 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004844 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004845 list_add(&req->inflight_entry, &ctx->inflight_list);
4846 req->flags |= REQ_F_INFLIGHT;
4847 req->work.files = current->files;
4848 ret = 0;
4849 }
4850 spin_unlock_irq(&ctx->inflight_lock);
4851 rcu_read_unlock();
4852
4853 return ret;
4854}
4855
Jens Axboe2665abf2019-11-05 12:40:47 -07004856static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4857{
Jens Axboead8a48a2019-11-15 08:49:11 -07004858 struct io_timeout_data *data = container_of(timer,
4859 struct io_timeout_data, timer);
4860 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004861 struct io_ring_ctx *ctx = req->ctx;
4862 struct io_kiocb *prev = NULL;
4863 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004864
4865 spin_lock_irqsave(&ctx->completion_lock, flags);
4866
4867 /*
4868 * We don't expect the list to be empty, that will only happen if we
4869 * race with the completion of the linked work.
4870 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004871 if (!list_empty(&req->link_list)) {
4872 prev = list_entry(req->link_list.prev, struct io_kiocb,
4873 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004874 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004875 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004876 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4877 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004878 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004879 }
4880
4881 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4882
4883 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004884 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004885 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4886 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004887 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004888 } else {
4889 io_cqring_add_event(req, -ETIME);
4890 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004891 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004892 return HRTIMER_NORESTART;
4893}
4894
Jens Axboead8a48a2019-11-15 08:49:11 -07004895static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004896{
Jens Axboe76a46e02019-11-10 23:34:16 -07004897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004898
Jens Axboe76a46e02019-11-10 23:34:16 -07004899 /*
4900 * If the list is now empty, then our linked request finished before
4901 * we got a chance to setup the timer
4902 */
4903 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004904 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004905 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004906
Jens Axboead8a48a2019-11-15 08:49:11 -07004907 data->timer.function = io_link_timeout_fn;
4908 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4909 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004910 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004911 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004912
Jens Axboe2665abf2019-11-05 12:40:47 -07004913 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004914 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004915}
4916
Jens Axboead8a48a2019-11-15 08:49:11 -07004917static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004918{
4919 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004920
Jens Axboe2665abf2019-11-05 12:40:47 -07004921 if (!(req->flags & REQ_F_LINK))
4922 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004923 /* for polled retry, if flag is set, we already went through here */
4924 if (req->flags & REQ_F_POLLED)
4925 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004926
Pavel Begunkov44932332019-12-05 16:16:35 +03004927 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4928 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004929 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004930 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004931
Jens Axboe76a46e02019-11-10 23:34:16 -07004932 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004933 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004934}
4935
Jens Axboe3529d8c2019-12-19 18:24:38 -07004936static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004937{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004938 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004939 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07004940 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004941 int ret;
4942
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004943again:
4944 linked_timeout = io_prep_linked_timeout(req);
4945
Jens Axboe193155c2020-02-22 23:22:19 -07004946 if (req->work.creds && req->work.creds != current_cred()) {
4947 if (old_creds)
4948 revert_creds(old_creds);
4949 if (old_creds == req->work.creds)
4950 old_creds = NULL; /* restored original creds */
4951 else
4952 old_creds = override_creds(req->work.creds);
4953 }
4954
Jens Axboe3529d8c2019-12-19 18:24:38 -07004955 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004956
4957 /*
4958 * We async punt it if the file wasn't marked NOWAIT, or if the file
4959 * doesn't support non-blocking read/write attempts
4960 */
4961 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4962 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07004963 if (io_arm_poll_handler(req)) {
4964 if (linked_timeout)
4965 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004966 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07004967 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004968punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004969 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004970 ret = io_grab_files(req);
4971 if (ret)
4972 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004973 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004974
4975 /*
4976 * Queued up for async execution, worker will release
4977 * submit reference when the iocb is actually submitted.
4978 */
4979 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004980 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004981 }
Jens Axboee65ef562019-03-12 10:16:44 -06004982
Jens Axboefcb323c2019-10-24 12:39:47 -06004983err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004984 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06004985 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004986 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004987
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004988 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004989 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004990 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004991 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004992 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004993 }
4994
Jens Axboee65ef562019-03-12 10:16:44 -06004995 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004996 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004997 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004998 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004999 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005000 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005001 if (nxt) {
5002 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005003
5004 if (req->flags & REQ_F_FORCE_ASYNC)
5005 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005006 goto again;
5007 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005008exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005009 if (old_creds)
5010 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005011}
5012
Jens Axboe3529d8c2019-12-19 18:24:38 -07005013static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005014{
5015 int ret;
5016
Jens Axboe3529d8c2019-12-19 18:24:38 -07005017 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005018 if (ret) {
5019 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005020fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005021 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005022 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005023 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005024 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005025 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005026 ret = io_req_defer_prep(req, sqe);
5027 if (unlikely(ret < 0))
5028 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005029 /*
5030 * Never try inline submit of IOSQE_ASYNC is set, go straight
5031 * to async execution.
5032 */
5033 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5034 io_queue_async_work(req);
5035 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005036 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005037 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005038}
5039
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005040static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005041{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005042 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005043 io_cqring_add_event(req, -ECANCELED);
5044 io_double_put_req(req);
5045 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005046 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005047}
5048
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005049#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07005050 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06005051
Jens Axboe3529d8c2019-12-19 18:24:38 -07005052static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5053 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005054{
Jackie Liua197f662019-11-08 08:09:12 -07005055 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005056 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005057 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005058
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005059 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005060
5061 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005062 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005063 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005064 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005065 }
5066
Jens Axboe75c6a032020-01-28 10:15:23 -07005067 id = READ_ONCE(sqe->personality);
5068 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005069 req->work.creds = idr_find(&ctx->personality_idr, id);
5070 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005071 ret = -EINVAL;
5072 goto err_req;
5073 }
Jens Axboe193155c2020-02-22 23:22:19 -07005074 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005075 }
5076
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005077 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005078 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5079 IOSQE_ASYNC | IOSQE_FIXED_FILE);
Jens Axboe9e645e112019-05-10 16:07:28 -06005080
Jens Axboe3529d8c2019-12-19 18:24:38 -07005081 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005082 if (unlikely(ret)) {
5083err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005084 io_cqring_add_event(req, ret);
5085 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005086 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005087 }
5088
Jens Axboe9e645e112019-05-10 16:07:28 -06005089 /*
5090 * If we already have a head request, queue this one for async
5091 * submittal once the head completes. If we don't have a head but
5092 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5093 * submitted sync once the chain is complete. If none of those
5094 * conditions are true (normal request), then just queue it.
5095 */
5096 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005097 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005098
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005099 /*
5100 * Taking sequential execution of a link, draining both sides
5101 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5102 * requests in the link. So, it drains the head and the
5103 * next after the link request. The last one is done via
5104 * drain_next flag to persist the effect across calls.
5105 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005106 if (sqe_flags & IOSQE_IO_DRAIN) {
5107 head->flags |= REQ_F_IO_DRAIN;
5108 ctx->drain_next = 1;
5109 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005110 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005111 ret = -EAGAIN;
5112 goto err_req;
5113 }
5114
Jens Axboe3529d8c2019-12-19 18:24:38 -07005115 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005116 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005117 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005118 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005119 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005120 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005121 trace_io_uring_link(ctx, req, head);
5122 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005123
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005124 /* last request of a link, enqueue the link */
5125 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5126 io_queue_link_head(head);
5127 *link = NULL;
5128 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005129 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005130 if (unlikely(ctx->drain_next)) {
5131 req->flags |= REQ_F_IO_DRAIN;
5132 req->ctx->drain_next = 0;
5133 }
5134 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5135 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005136 INIT_LIST_HEAD(&req->link_list);
5137 ret = io_req_defer_prep(req, sqe);
5138 if (ret)
5139 req->flags |= REQ_F_FAIL_LINK;
5140 *link = req;
5141 } else {
5142 io_queue_sqe(req, sqe);
5143 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005144 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005145
5146 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005147}
5148
Jens Axboe9a56a232019-01-09 09:06:50 -07005149/*
5150 * Batched submission is done, ensure local IO is flushed out.
5151 */
5152static void io_submit_state_end(struct io_submit_state *state)
5153{
5154 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005155 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005156 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005157 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005158}
5159
5160/*
5161 * Start submission side cache.
5162 */
5163static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005164 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005165{
5166 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005167 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005168 state->file = NULL;
5169 state->ios_left = max_ios;
5170}
5171
Jens Axboe2b188cc2019-01-07 10:46:33 -07005172static void io_commit_sqring(struct io_ring_ctx *ctx)
5173{
Hristo Venev75b28af2019-08-26 17:23:46 +00005174 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005175
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005176 /*
5177 * Ensure any loads from the SQEs are done at this point,
5178 * since once we write the new head, the application could
5179 * write new data to them.
5180 */
5181 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005182}
5183
5184/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005185 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005186 * that is mapped by userspace. This means that care needs to be taken to
5187 * ensure that reads are stable, as we cannot rely on userspace always
5188 * being a good citizen. If members of the sqe are validated and then later
5189 * used, it's important that those reads are done through READ_ONCE() to
5190 * prevent a re-load down the line.
5191 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005192static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5193 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005194{
Hristo Venev75b28af2019-08-26 17:23:46 +00005195 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005196 unsigned head;
5197
5198 /*
5199 * The cached sq head (or cq tail) serves two purposes:
5200 *
5201 * 1) allows us to batch the cost of updating the user visible
5202 * head updates.
5203 * 2) allows the kernel side to track the head on its own, even
5204 * though the application is the one updating it.
5205 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005206 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005207 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005208 /*
5209 * All io need record the previous position, if LINK vs DARIN,
5210 * it can be used to mark the position of the first IO in the
5211 * link list.
5212 */
5213 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005214 *sqe_ptr = &ctx->sq_sqes[head];
5215 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5216 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005217 ctx->cached_sq_head++;
5218 return true;
5219 }
5220
5221 /* drop invalid entries */
5222 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005223 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005224 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005225 return false;
5226}
5227
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005228static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005229 struct file *ring_file, int ring_fd,
5230 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005231{
5232 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005233 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005234 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005235 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005236
Jens Axboec4a2ed72019-11-21 21:01:26 -07005237 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005238 if (test_bit(0, &ctx->sq_check_overflow)) {
5239 if (!list_empty(&ctx->cq_overflow_list) &&
5240 !io_cqring_overflow_flush(ctx, false))
5241 return -EBUSY;
5242 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005243
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005244 /* make sure SQ entry isn't read before tail */
5245 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005246
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005247 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5248 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005249
5250 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005251 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005252 statep = &state;
5253 }
5254
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005255 ctx->ring_fd = ring_fd;
5256 ctx->ring_file = ring_file;
5257
Jens Axboe6c271ce2019-01-10 11:22:30 -07005258 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005259 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005260 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005261 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005262
Pavel Begunkov196be952019-11-07 01:41:06 +03005263 req = io_get_req(ctx, statep);
5264 if (unlikely(!req)) {
5265 if (!submitted)
5266 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005267 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005268 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005269 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005270 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005271 break;
5272 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005273
Jens Axboed3656342019-12-18 09:50:26 -07005274 /* will complete beyond this point, count as submitted */
5275 submitted++;
5276
5277 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005278 err = -EINVAL;
5279fail_req:
5280 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005281 io_double_put_req(req);
5282 break;
5283 }
5284
5285 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005286 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005287 if (unlikely(mm_fault)) {
5288 err = -EFAULT;
5289 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005290 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005291 use_mm(ctx->sqo_mm);
5292 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005293 }
5294
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005295 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005296 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5297 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005298 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005299 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005300 }
5301
Pavel Begunkov9466f432020-01-25 22:34:01 +03005302 if (unlikely(submitted != nr)) {
5303 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5304
5305 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5306 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005307 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005308 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005309 if (statep)
5310 io_submit_state_end(&state);
5311
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005312 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5313 io_commit_sqring(ctx);
5314
Jens Axboe6c271ce2019-01-10 11:22:30 -07005315 return submitted;
5316}
5317
5318static int io_sq_thread(void *data)
5319{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005320 struct io_ring_ctx *ctx = data;
5321 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005322 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005323 mm_segment_t old_fs;
5324 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005325 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005326 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005327
Jens Axboe206aefd2019-11-07 18:27:42 -07005328 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005329
Jens Axboe6c271ce2019-01-10 11:22:30 -07005330 old_fs = get_fs();
5331 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005332 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005333
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005334 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005335 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005336 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005337
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005338 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005339 unsigned nr_events = 0;
5340
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005341 mutex_lock(&ctx->uring_lock);
5342 if (!list_empty(&ctx->poll_list))
5343 io_iopoll_getevents(ctx, &nr_events, 0);
5344 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005345 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005346 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005347 }
5348
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005349 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005350
5351 /*
5352 * If submit got -EBUSY, flag us as needing the application
5353 * to enter the kernel to reap and flush events.
5354 */
5355 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005356 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005357 * Drop cur_mm before scheduling, we can't hold it for
5358 * long periods (or over schedule()). Do this before
5359 * adding ourselves to the waitqueue, as the unuse/drop
5360 * may sleep.
5361 */
5362 if (cur_mm) {
5363 unuse_mm(cur_mm);
5364 mmput(cur_mm);
5365 cur_mm = NULL;
5366 }
5367
5368 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005369 * We're polling. If we're within the defined idle
5370 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005371 * to sleep. The exception is if we got EBUSY doing
5372 * more IO, we should wait for the application to
5373 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005374 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005375 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005376 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5377 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005378 if (current->task_works)
5379 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005380 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005381 continue;
5382 }
5383
Jens Axboe6c271ce2019-01-10 11:22:30 -07005384 prepare_to_wait(&ctx->sqo_wait, &wait,
5385 TASK_INTERRUPTIBLE);
5386
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005387 /*
5388 * While doing polled IO, before going to sleep, we need
5389 * to check if there are new reqs added to poll_list, it
5390 * is because reqs may have been punted to io worker and
5391 * will be added to poll_list later, hence check the
5392 * poll_list again.
5393 */
5394 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5395 !list_empty_careful(&ctx->poll_list)) {
5396 finish_wait(&ctx->sqo_wait, &wait);
5397 continue;
5398 }
5399
Jens Axboe6c271ce2019-01-10 11:22:30 -07005400 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005401 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005402 /* make sure to read SQ tail after writing flags */
5403 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005404
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005405 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005406 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005407 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005408 finish_wait(&ctx->sqo_wait, &wait);
5409 break;
5410 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005411 if (current->task_works) {
5412 task_work_run();
5413 continue;
5414 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005415 if (signal_pending(current))
5416 flush_signals(current);
5417 schedule();
5418 finish_wait(&ctx->sqo_wait, &wait);
5419
Hristo Venev75b28af2019-08-26 17:23:46 +00005420 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005421 continue;
5422 }
5423 finish_wait(&ctx->sqo_wait, &wait);
5424
Hristo Venev75b28af2019-08-26 17:23:46 +00005425 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005426 }
5427
Jens Axboe8a4955f2019-12-09 14:52:35 -07005428 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005429 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005430 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005431 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005432 }
5433
Jens Axboeb41e9852020-02-17 09:52:41 -07005434 if (current->task_works)
5435 task_work_run();
5436
Jens Axboe6c271ce2019-01-10 11:22:30 -07005437 set_fs(old_fs);
5438 if (cur_mm) {
5439 unuse_mm(cur_mm);
5440 mmput(cur_mm);
5441 }
Jens Axboe181e4482019-11-25 08:52:30 -07005442 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005443
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005444 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005445
Jens Axboe6c271ce2019-01-10 11:22:30 -07005446 return 0;
5447}
5448
Jens Axboebda52162019-09-24 13:47:15 -06005449struct io_wait_queue {
5450 struct wait_queue_entry wq;
5451 struct io_ring_ctx *ctx;
5452 unsigned to_wait;
5453 unsigned nr_timeouts;
5454};
5455
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005456static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005457{
5458 struct io_ring_ctx *ctx = iowq->ctx;
5459
5460 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005461 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005462 * started waiting. For timeouts, we always want to return to userspace,
5463 * regardless of event count.
5464 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005465 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005466 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5467}
5468
5469static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5470 int wake_flags, void *key)
5471{
5472 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5473 wq);
5474
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005475 /* use noflush == true, as we can't safely rely on locking context */
5476 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005477 return -1;
5478
5479 return autoremove_wake_function(curr, mode, wake_flags, key);
5480}
5481
Jens Axboe2b188cc2019-01-07 10:46:33 -07005482/*
5483 * Wait until events become available, if we don't already have some. The
5484 * application must reap them itself, as they reside on the shared cq ring.
5485 */
5486static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5487 const sigset_t __user *sig, size_t sigsz)
5488{
Jens Axboebda52162019-09-24 13:47:15 -06005489 struct io_wait_queue iowq = {
5490 .wq = {
5491 .private = current,
5492 .func = io_wake_function,
5493 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5494 },
5495 .ctx = ctx,
5496 .to_wait = min_events,
5497 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005498 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005499 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005500
Jens Axboeb41e9852020-02-17 09:52:41 -07005501 do {
5502 if (io_cqring_events(ctx, false) >= min_events)
5503 return 0;
5504 if (!current->task_works)
5505 break;
5506 task_work_run();
5507 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005508
5509 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005510#ifdef CONFIG_COMPAT
5511 if (in_compat_syscall())
5512 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005513 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005514 else
5515#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005516 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005517
Jens Axboe2b188cc2019-01-07 10:46:33 -07005518 if (ret)
5519 return ret;
5520 }
5521
Jens Axboebda52162019-09-24 13:47:15 -06005522 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005523 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005524 do {
5525 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5526 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005527 if (current->task_works)
5528 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005529 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005530 break;
5531 schedule();
5532 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005533 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005534 break;
5535 }
5536 } while (1);
5537 finish_wait(&ctx->wait, &iowq.wq);
5538
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005539 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005540
Hristo Venev75b28af2019-08-26 17:23:46 +00005541 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005542}
5543
Jens Axboe6b063142019-01-10 22:13:58 -07005544static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5545{
5546#if defined(CONFIG_UNIX)
5547 if (ctx->ring_sock) {
5548 struct sock *sock = ctx->ring_sock->sk;
5549 struct sk_buff *skb;
5550
5551 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5552 kfree_skb(skb);
5553 }
5554#else
5555 int i;
5556
Jens Axboe65e19f52019-10-26 07:20:21 -06005557 for (i = 0; i < ctx->nr_user_files; i++) {
5558 struct file *file;
5559
5560 file = io_file_from_index(ctx, i);
5561 if (file)
5562 fput(file);
5563 }
Jens Axboe6b063142019-01-10 22:13:58 -07005564#endif
5565}
5566
Jens Axboe05f3fb32019-12-09 11:22:50 -07005567static void io_file_ref_kill(struct percpu_ref *ref)
5568{
5569 struct fixed_file_data *data;
5570
5571 data = container_of(ref, struct fixed_file_data, refs);
5572 complete(&data->done);
5573}
5574
Jens Axboe6b063142019-01-10 22:13:58 -07005575static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5576{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005577 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005578 unsigned nr_tables, i;
5579
Jens Axboe05f3fb32019-12-09 11:22:50 -07005580 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005581 return -ENXIO;
5582
Jens Axboe05f3fb32019-12-09 11:22:50 -07005583 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005584 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005585 wait_for_completion(&data->done);
5586 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005587 percpu_ref_exit(&data->refs);
5588
Jens Axboe6b063142019-01-10 22:13:58 -07005589 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005590 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5591 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005592 kfree(data->table[i].files);
5593 kfree(data->table);
5594 kfree(data);
5595 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005596 ctx->nr_user_files = 0;
5597 return 0;
5598}
5599
Jens Axboe6c271ce2019-01-10 11:22:30 -07005600static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5601{
5602 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005603 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005604 /*
5605 * The park is a bit of a work-around, without it we get
5606 * warning spews on shutdown with SQPOLL set and affinity
5607 * set to a single CPU.
5608 */
Jens Axboe06058632019-04-13 09:26:03 -06005609 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005610 kthread_stop(ctx->sqo_thread);
5611 ctx->sqo_thread = NULL;
5612 }
5613}
5614
Jens Axboe6b063142019-01-10 22:13:58 -07005615static void io_finish_async(struct io_ring_ctx *ctx)
5616{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005617 io_sq_thread_stop(ctx);
5618
Jens Axboe561fb042019-10-24 07:25:42 -06005619 if (ctx->io_wq) {
5620 io_wq_destroy(ctx->io_wq);
5621 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005622 }
5623}
5624
5625#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005626/*
5627 * Ensure the UNIX gc is aware of our file set, so we are certain that
5628 * the io_uring can be safely unregistered on process exit, even if we have
5629 * loops in the file referencing.
5630 */
5631static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5632{
5633 struct sock *sk = ctx->ring_sock->sk;
5634 struct scm_fp_list *fpl;
5635 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005636 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005637
5638 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5639 unsigned long inflight = ctx->user->unix_inflight + nr;
5640
5641 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5642 return -EMFILE;
5643 }
5644
5645 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5646 if (!fpl)
5647 return -ENOMEM;
5648
5649 skb = alloc_skb(0, GFP_KERNEL);
5650 if (!skb) {
5651 kfree(fpl);
5652 return -ENOMEM;
5653 }
5654
5655 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005656
Jens Axboe08a45172019-10-03 08:11:03 -06005657 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005658 fpl->user = get_uid(ctx->user);
5659 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005660 struct file *file = io_file_from_index(ctx, i + offset);
5661
5662 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005663 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005664 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005665 unix_inflight(fpl->user, fpl->fp[nr_files]);
5666 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005667 }
5668
Jens Axboe08a45172019-10-03 08:11:03 -06005669 if (nr_files) {
5670 fpl->max = SCM_MAX_FD;
5671 fpl->count = nr_files;
5672 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005673 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005674 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5675 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005676
Jens Axboe08a45172019-10-03 08:11:03 -06005677 for (i = 0; i < nr_files; i++)
5678 fput(fpl->fp[i]);
5679 } else {
5680 kfree_skb(skb);
5681 kfree(fpl);
5682 }
Jens Axboe6b063142019-01-10 22:13:58 -07005683
5684 return 0;
5685}
5686
5687/*
5688 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5689 * causes regular reference counting to break down. We rely on the UNIX
5690 * garbage collection to take care of this problem for us.
5691 */
5692static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5693{
5694 unsigned left, total;
5695 int ret = 0;
5696
5697 total = 0;
5698 left = ctx->nr_user_files;
5699 while (left) {
5700 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005701
5702 ret = __io_sqe_files_scm(ctx, this_files, total);
5703 if (ret)
5704 break;
5705 left -= this_files;
5706 total += this_files;
5707 }
5708
5709 if (!ret)
5710 return 0;
5711
5712 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005713 struct file *file = io_file_from_index(ctx, total);
5714
5715 if (file)
5716 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005717 total++;
5718 }
5719
5720 return ret;
5721}
5722#else
5723static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5724{
5725 return 0;
5726}
5727#endif
5728
Jens Axboe65e19f52019-10-26 07:20:21 -06005729static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5730 unsigned nr_files)
5731{
5732 int i;
5733
5734 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005735 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005736 unsigned this_files;
5737
5738 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5739 table->files = kcalloc(this_files, sizeof(struct file *),
5740 GFP_KERNEL);
5741 if (!table->files)
5742 break;
5743 nr_files -= this_files;
5744 }
5745
5746 if (i == nr_tables)
5747 return 0;
5748
5749 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005750 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005751 kfree(table->files);
5752 }
5753 return 1;
5754}
5755
Jens Axboe05f3fb32019-12-09 11:22:50 -07005756static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005757{
5758#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005759 struct sock *sock = ctx->ring_sock->sk;
5760 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5761 struct sk_buff *skb;
5762 int i;
5763
5764 __skb_queue_head_init(&list);
5765
5766 /*
5767 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5768 * remove this entry and rearrange the file array.
5769 */
5770 skb = skb_dequeue(head);
5771 while (skb) {
5772 struct scm_fp_list *fp;
5773
5774 fp = UNIXCB(skb).fp;
5775 for (i = 0; i < fp->count; i++) {
5776 int left;
5777
5778 if (fp->fp[i] != file)
5779 continue;
5780
5781 unix_notinflight(fp->user, fp->fp[i]);
5782 left = fp->count - 1 - i;
5783 if (left) {
5784 memmove(&fp->fp[i], &fp->fp[i + 1],
5785 left * sizeof(struct file *));
5786 }
5787 fp->count--;
5788 if (!fp->count) {
5789 kfree_skb(skb);
5790 skb = NULL;
5791 } else {
5792 __skb_queue_tail(&list, skb);
5793 }
5794 fput(file);
5795 file = NULL;
5796 break;
5797 }
5798
5799 if (!file)
5800 break;
5801
5802 __skb_queue_tail(&list, skb);
5803
5804 skb = skb_dequeue(head);
5805 }
5806
5807 if (skb_peek(&list)) {
5808 spin_lock_irq(&head->lock);
5809 while ((skb = __skb_dequeue(&list)) != NULL)
5810 __skb_queue_tail(head, skb);
5811 spin_unlock_irq(&head->lock);
5812 }
5813#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005814 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005815#endif
5816}
5817
Jens Axboe05f3fb32019-12-09 11:22:50 -07005818struct io_file_put {
5819 struct llist_node llist;
5820 struct file *file;
5821 struct completion *done;
5822};
5823
Jens Axboe2faf8522020-02-04 19:54:55 -07005824static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005825{
5826 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005827 struct llist_node *node;
5828
Jens Axboe05f3fb32019-12-09 11:22:50 -07005829 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5830 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5831 io_ring_file_put(data->ctx, pfile->file);
5832 if (pfile->done)
5833 complete(pfile->done);
5834 else
5835 kfree(pfile);
5836 }
5837 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005838}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005839
Jens Axboe2faf8522020-02-04 19:54:55 -07005840static void io_ring_file_ref_switch(struct work_struct *work)
5841{
5842 struct fixed_file_data *data;
5843
5844 data = container_of(work, struct fixed_file_data, ref_work);
5845 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005846 percpu_ref_switch_to_percpu(&data->refs);
5847}
5848
5849static void io_file_data_ref_zero(struct percpu_ref *ref)
5850{
5851 struct fixed_file_data *data;
5852
5853 data = container_of(ref, struct fixed_file_data, refs);
5854
Jens Axboe2faf8522020-02-04 19:54:55 -07005855 /*
5856 * We can't safely switch from inside this context, punt to wq. If
5857 * the table ref is going away, the table is being unregistered.
5858 * Don't queue up the async work for that case, the caller will
5859 * handle it.
5860 */
5861 if (!percpu_ref_is_dying(&data->refs))
5862 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005863}
5864
5865static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5866 unsigned nr_args)
5867{
5868 __s32 __user *fds = (__s32 __user *) arg;
5869 unsigned nr_tables;
5870 struct file *file;
5871 int fd, ret = 0;
5872 unsigned i;
5873
5874 if (ctx->file_data)
5875 return -EBUSY;
5876 if (!nr_args)
5877 return -EINVAL;
5878 if (nr_args > IORING_MAX_FIXED_FILES)
5879 return -EMFILE;
5880
5881 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5882 if (!ctx->file_data)
5883 return -ENOMEM;
5884 ctx->file_data->ctx = ctx;
5885 init_completion(&ctx->file_data->done);
5886
5887 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5888 ctx->file_data->table = kcalloc(nr_tables,
5889 sizeof(struct fixed_file_table),
5890 GFP_KERNEL);
5891 if (!ctx->file_data->table) {
5892 kfree(ctx->file_data);
5893 ctx->file_data = NULL;
5894 return -ENOMEM;
5895 }
5896
5897 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5898 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5899 kfree(ctx->file_data->table);
5900 kfree(ctx->file_data);
5901 ctx->file_data = NULL;
5902 return -ENOMEM;
5903 }
5904 ctx->file_data->put_llist.first = NULL;
5905 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5906
5907 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5908 percpu_ref_exit(&ctx->file_data->refs);
5909 kfree(ctx->file_data->table);
5910 kfree(ctx->file_data);
5911 ctx->file_data = NULL;
5912 return -ENOMEM;
5913 }
5914
5915 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5916 struct fixed_file_table *table;
5917 unsigned index;
5918
5919 ret = -EFAULT;
5920 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5921 break;
5922 /* allow sparse sets */
5923 if (fd == -1) {
5924 ret = 0;
5925 continue;
5926 }
5927
5928 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5929 index = i & IORING_FILE_TABLE_MASK;
5930 file = fget(fd);
5931
5932 ret = -EBADF;
5933 if (!file)
5934 break;
5935
5936 /*
5937 * Don't allow io_uring instances to be registered. If UNIX
5938 * isn't enabled, then this causes a reference cycle and this
5939 * instance can never get freed. If UNIX is enabled we'll
5940 * handle it just fine, but there's still no point in allowing
5941 * a ring fd as it doesn't support regular read/write anyway.
5942 */
5943 if (file->f_op == &io_uring_fops) {
5944 fput(file);
5945 break;
5946 }
5947 ret = 0;
5948 table->files[index] = file;
5949 }
5950
5951 if (ret) {
5952 for (i = 0; i < ctx->nr_user_files; i++) {
5953 file = io_file_from_index(ctx, i);
5954 if (file)
5955 fput(file);
5956 }
5957 for (i = 0; i < nr_tables; i++)
5958 kfree(ctx->file_data->table[i].files);
5959
5960 kfree(ctx->file_data->table);
5961 kfree(ctx->file_data);
5962 ctx->file_data = NULL;
5963 ctx->nr_user_files = 0;
5964 return ret;
5965 }
5966
5967 ret = io_sqe_files_scm(ctx);
5968 if (ret)
5969 io_sqe_files_unregister(ctx);
5970
5971 return ret;
5972}
5973
Jens Axboec3a31e62019-10-03 13:59:56 -06005974static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5975 int index)
5976{
5977#if defined(CONFIG_UNIX)
5978 struct sock *sock = ctx->ring_sock->sk;
5979 struct sk_buff_head *head = &sock->sk_receive_queue;
5980 struct sk_buff *skb;
5981
5982 /*
5983 * See if we can merge this file into an existing skb SCM_RIGHTS
5984 * file set. If there's no room, fall back to allocating a new skb
5985 * and filling it in.
5986 */
5987 spin_lock_irq(&head->lock);
5988 skb = skb_peek(head);
5989 if (skb) {
5990 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5991
5992 if (fpl->count < SCM_MAX_FD) {
5993 __skb_unlink(skb, head);
5994 spin_unlock_irq(&head->lock);
5995 fpl->fp[fpl->count] = get_file(file);
5996 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5997 fpl->count++;
5998 spin_lock_irq(&head->lock);
5999 __skb_queue_head(head, skb);
6000 } else {
6001 skb = NULL;
6002 }
6003 }
6004 spin_unlock_irq(&head->lock);
6005
6006 if (skb) {
6007 fput(file);
6008 return 0;
6009 }
6010
6011 return __io_sqe_files_scm(ctx, 1, index);
6012#else
6013 return 0;
6014#endif
6015}
6016
Jens Axboe05f3fb32019-12-09 11:22:50 -07006017static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006018{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006019 struct fixed_file_data *data;
6020
Jens Axboedd3db2a2020-02-26 10:23:43 -07006021 /*
6022 * Juggle reference to ensure we hit zero, if needed, so we can
6023 * switch back to percpu mode
6024 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006025 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006026 percpu_ref_put(&data->refs);
6027 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006028}
6029
6030static bool io_queue_file_removal(struct fixed_file_data *data,
6031 struct file *file)
6032{
6033 struct io_file_put *pfile, pfile_stack;
6034 DECLARE_COMPLETION_ONSTACK(done);
6035
6036 /*
6037 * If we fail allocating the struct we need for doing async reomval
6038 * of this file, just punt to sync and wait for it.
6039 */
6040 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6041 if (!pfile) {
6042 pfile = &pfile_stack;
6043 pfile->done = &done;
6044 }
6045
6046 pfile->file = file;
6047 llist_add(&pfile->llist, &data->put_llist);
6048
6049 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006050 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006051 wait_for_completion(&done);
6052 flush_work(&data->ref_work);
6053 return false;
6054 }
6055
6056 return true;
6057}
6058
6059static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6060 struct io_uring_files_update *up,
6061 unsigned nr_args)
6062{
6063 struct fixed_file_data *data = ctx->file_data;
6064 bool ref_switch = false;
6065 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006066 __s32 __user *fds;
6067 int fd, i, err;
6068 __u32 done;
6069
Jens Axboe05f3fb32019-12-09 11:22:50 -07006070 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006071 return -EOVERFLOW;
6072 if (done > ctx->nr_user_files)
6073 return -EINVAL;
6074
6075 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006076 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006077 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006078 struct fixed_file_table *table;
6079 unsigned index;
6080
Jens Axboec3a31e62019-10-03 13:59:56 -06006081 err = 0;
6082 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6083 err = -EFAULT;
6084 break;
6085 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006086 i = array_index_nospec(up->offset, ctx->nr_user_files);
6087 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006088 index = i & IORING_FILE_TABLE_MASK;
6089 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006090 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006091 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006092 if (io_queue_file_removal(data, file))
6093 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006094 }
6095 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006096 file = fget(fd);
6097 if (!file) {
6098 err = -EBADF;
6099 break;
6100 }
6101 /*
6102 * Don't allow io_uring instances to be registered. If
6103 * UNIX isn't enabled, then this causes a reference
6104 * cycle and this instance can never get freed. If UNIX
6105 * is enabled we'll handle it just fine, but there's
6106 * still no point in allowing a ring fd as it doesn't
6107 * support regular read/write anyway.
6108 */
6109 if (file->f_op == &io_uring_fops) {
6110 fput(file);
6111 err = -EBADF;
6112 break;
6113 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006114 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006115 err = io_sqe_file_register(ctx, file, i);
6116 if (err)
6117 break;
6118 }
6119 nr_args--;
6120 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006121 up->offset++;
6122 }
6123
Jens Axboedd3db2a2020-02-26 10:23:43 -07006124 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006125 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006126
6127 return done ? done : err;
6128}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006129static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6130 unsigned nr_args)
6131{
6132 struct io_uring_files_update up;
6133
6134 if (!ctx->file_data)
6135 return -ENXIO;
6136 if (!nr_args)
6137 return -EINVAL;
6138 if (copy_from_user(&up, arg, sizeof(up)))
6139 return -EFAULT;
6140 if (up.resv)
6141 return -EINVAL;
6142
6143 return __io_sqe_files_update(ctx, &up, nr_args);
6144}
Jens Axboec3a31e62019-10-03 13:59:56 -06006145
Jens Axboe7d723062019-11-12 22:31:31 -07006146static void io_put_work(struct io_wq_work *work)
6147{
6148 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6149
6150 io_put_req(req);
6151}
6152
6153static void io_get_work(struct io_wq_work *work)
6154{
6155 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6156
6157 refcount_inc(&req->refs);
6158}
6159
Pavel Begunkov24369c22020-01-28 03:15:48 +03006160static int io_init_wq_offload(struct io_ring_ctx *ctx,
6161 struct io_uring_params *p)
6162{
6163 struct io_wq_data data;
6164 struct fd f;
6165 struct io_ring_ctx *ctx_attach;
6166 unsigned int concurrency;
6167 int ret = 0;
6168
6169 data.user = ctx->user;
6170 data.get_work = io_get_work;
6171 data.put_work = io_put_work;
6172
6173 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6174 /* Do QD, or 4 * CPUS, whatever is smallest */
6175 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6176
6177 ctx->io_wq = io_wq_create(concurrency, &data);
6178 if (IS_ERR(ctx->io_wq)) {
6179 ret = PTR_ERR(ctx->io_wq);
6180 ctx->io_wq = NULL;
6181 }
6182 return ret;
6183 }
6184
6185 f = fdget(p->wq_fd);
6186 if (!f.file)
6187 return -EBADF;
6188
6189 if (f.file->f_op != &io_uring_fops) {
6190 ret = -EINVAL;
6191 goto out_fput;
6192 }
6193
6194 ctx_attach = f.file->private_data;
6195 /* @io_wq is protected by holding the fd */
6196 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6197 ret = -EINVAL;
6198 goto out_fput;
6199 }
6200
6201 ctx->io_wq = ctx_attach->io_wq;
6202out_fput:
6203 fdput(f);
6204 return ret;
6205}
6206
Jens Axboe6c271ce2019-01-10 11:22:30 -07006207static int io_sq_offload_start(struct io_ring_ctx *ctx,
6208 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209{
6210 int ret;
6211
Jens Axboe6c271ce2019-01-10 11:22:30 -07006212 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006213 mmgrab(current->mm);
6214 ctx->sqo_mm = current->mm;
6215
Jens Axboe6c271ce2019-01-10 11:22:30 -07006216 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006217 ret = -EPERM;
6218 if (!capable(CAP_SYS_ADMIN))
6219 goto err;
6220
Jens Axboe917257d2019-04-13 09:28:55 -06006221 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6222 if (!ctx->sq_thread_idle)
6223 ctx->sq_thread_idle = HZ;
6224
Jens Axboe6c271ce2019-01-10 11:22:30 -07006225 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006226 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006227
Jens Axboe917257d2019-04-13 09:28:55 -06006228 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006229 if (cpu >= nr_cpu_ids)
6230 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006231 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006232 goto err;
6233
Jens Axboe6c271ce2019-01-10 11:22:30 -07006234 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6235 ctx, cpu,
6236 "io_uring-sq");
6237 } else {
6238 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6239 "io_uring-sq");
6240 }
6241 if (IS_ERR(ctx->sqo_thread)) {
6242 ret = PTR_ERR(ctx->sqo_thread);
6243 ctx->sqo_thread = NULL;
6244 goto err;
6245 }
6246 wake_up_process(ctx->sqo_thread);
6247 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6248 /* Can't have SQ_AFF without SQPOLL */
6249 ret = -EINVAL;
6250 goto err;
6251 }
6252
Pavel Begunkov24369c22020-01-28 03:15:48 +03006253 ret = io_init_wq_offload(ctx, p);
6254 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006255 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006256
6257 return 0;
6258err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006259 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006260 mmdrop(ctx->sqo_mm);
6261 ctx->sqo_mm = NULL;
6262 return ret;
6263}
6264
6265static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6266{
6267 atomic_long_sub(nr_pages, &user->locked_vm);
6268}
6269
6270static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6271{
6272 unsigned long page_limit, cur_pages, new_pages;
6273
6274 /* Don't allow more pages than we can safely lock */
6275 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6276
6277 do {
6278 cur_pages = atomic_long_read(&user->locked_vm);
6279 new_pages = cur_pages + nr_pages;
6280 if (new_pages > page_limit)
6281 return -ENOMEM;
6282 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6283 new_pages) != cur_pages);
6284
6285 return 0;
6286}
6287
6288static void io_mem_free(void *ptr)
6289{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006290 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006291
Mark Rutland52e04ef2019-04-30 17:30:21 +01006292 if (!ptr)
6293 return;
6294
6295 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006296 if (put_page_testzero(page))
6297 free_compound_page(page);
6298}
6299
6300static void *io_mem_alloc(size_t size)
6301{
6302 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6303 __GFP_NORETRY;
6304
6305 return (void *) __get_free_pages(gfp_flags, get_order(size));
6306}
6307
Hristo Venev75b28af2019-08-26 17:23:46 +00006308static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6309 size_t *sq_offset)
6310{
6311 struct io_rings *rings;
6312 size_t off, sq_array_size;
6313
6314 off = struct_size(rings, cqes, cq_entries);
6315 if (off == SIZE_MAX)
6316 return SIZE_MAX;
6317
6318#ifdef CONFIG_SMP
6319 off = ALIGN(off, SMP_CACHE_BYTES);
6320 if (off == 0)
6321 return SIZE_MAX;
6322#endif
6323
6324 sq_array_size = array_size(sizeof(u32), sq_entries);
6325 if (sq_array_size == SIZE_MAX)
6326 return SIZE_MAX;
6327
6328 if (check_add_overflow(off, sq_array_size, &off))
6329 return SIZE_MAX;
6330
6331 if (sq_offset)
6332 *sq_offset = off;
6333
6334 return off;
6335}
6336
Jens Axboe2b188cc2019-01-07 10:46:33 -07006337static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6338{
Hristo Venev75b28af2019-08-26 17:23:46 +00006339 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006340
Hristo Venev75b28af2019-08-26 17:23:46 +00006341 pages = (size_t)1 << get_order(
6342 rings_size(sq_entries, cq_entries, NULL));
6343 pages += (size_t)1 << get_order(
6344 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006345
Hristo Venev75b28af2019-08-26 17:23:46 +00006346 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006347}
6348
Jens Axboeedafcce2019-01-09 09:16:05 -07006349static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6350{
6351 int i, j;
6352
6353 if (!ctx->user_bufs)
6354 return -ENXIO;
6355
6356 for (i = 0; i < ctx->nr_user_bufs; i++) {
6357 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6358
6359 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006360 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006361
6362 if (ctx->account_mem)
6363 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006364 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006365 imu->nr_bvecs = 0;
6366 }
6367
6368 kfree(ctx->user_bufs);
6369 ctx->user_bufs = NULL;
6370 ctx->nr_user_bufs = 0;
6371 return 0;
6372}
6373
6374static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6375 void __user *arg, unsigned index)
6376{
6377 struct iovec __user *src;
6378
6379#ifdef CONFIG_COMPAT
6380 if (ctx->compat) {
6381 struct compat_iovec __user *ciovs;
6382 struct compat_iovec ciov;
6383
6384 ciovs = (struct compat_iovec __user *) arg;
6385 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6386 return -EFAULT;
6387
Jens Axboed55e5f52019-12-11 16:12:15 -07006388 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006389 dst->iov_len = ciov.iov_len;
6390 return 0;
6391 }
6392#endif
6393 src = (struct iovec __user *) arg;
6394 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6395 return -EFAULT;
6396 return 0;
6397}
6398
6399static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6400 unsigned nr_args)
6401{
6402 struct vm_area_struct **vmas = NULL;
6403 struct page **pages = NULL;
6404 int i, j, got_pages = 0;
6405 int ret = -EINVAL;
6406
6407 if (ctx->user_bufs)
6408 return -EBUSY;
6409 if (!nr_args || nr_args > UIO_MAXIOV)
6410 return -EINVAL;
6411
6412 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6413 GFP_KERNEL);
6414 if (!ctx->user_bufs)
6415 return -ENOMEM;
6416
6417 for (i = 0; i < nr_args; i++) {
6418 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6419 unsigned long off, start, end, ubuf;
6420 int pret, nr_pages;
6421 struct iovec iov;
6422 size_t size;
6423
6424 ret = io_copy_iov(ctx, &iov, arg, i);
6425 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006426 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006427
6428 /*
6429 * Don't impose further limits on the size and buffer
6430 * constraints here, we'll -EINVAL later when IO is
6431 * submitted if they are wrong.
6432 */
6433 ret = -EFAULT;
6434 if (!iov.iov_base || !iov.iov_len)
6435 goto err;
6436
6437 /* arbitrary limit, but we need something */
6438 if (iov.iov_len > SZ_1G)
6439 goto err;
6440
6441 ubuf = (unsigned long) iov.iov_base;
6442 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6443 start = ubuf >> PAGE_SHIFT;
6444 nr_pages = end - start;
6445
6446 if (ctx->account_mem) {
6447 ret = io_account_mem(ctx->user, nr_pages);
6448 if (ret)
6449 goto err;
6450 }
6451
6452 ret = 0;
6453 if (!pages || nr_pages > got_pages) {
6454 kfree(vmas);
6455 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006456 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006457 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006458 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006459 sizeof(struct vm_area_struct *),
6460 GFP_KERNEL);
6461 if (!pages || !vmas) {
6462 ret = -ENOMEM;
6463 if (ctx->account_mem)
6464 io_unaccount_mem(ctx->user, nr_pages);
6465 goto err;
6466 }
6467 got_pages = nr_pages;
6468 }
6469
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006470 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006471 GFP_KERNEL);
6472 ret = -ENOMEM;
6473 if (!imu->bvec) {
6474 if (ctx->account_mem)
6475 io_unaccount_mem(ctx->user, nr_pages);
6476 goto err;
6477 }
6478
6479 ret = 0;
6480 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006481 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006482 FOLL_WRITE | FOLL_LONGTERM,
6483 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006484 if (pret == nr_pages) {
6485 /* don't support file backed memory */
6486 for (j = 0; j < nr_pages; j++) {
6487 struct vm_area_struct *vma = vmas[j];
6488
6489 if (vma->vm_file &&
6490 !is_file_hugepages(vma->vm_file)) {
6491 ret = -EOPNOTSUPP;
6492 break;
6493 }
6494 }
6495 } else {
6496 ret = pret < 0 ? pret : -EFAULT;
6497 }
6498 up_read(&current->mm->mmap_sem);
6499 if (ret) {
6500 /*
6501 * if we did partial map, or found file backed vmas,
6502 * release any pages we did get
6503 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006504 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006505 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006506 if (ctx->account_mem)
6507 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006508 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006509 goto err;
6510 }
6511
6512 off = ubuf & ~PAGE_MASK;
6513 size = iov.iov_len;
6514 for (j = 0; j < nr_pages; j++) {
6515 size_t vec_len;
6516
6517 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6518 imu->bvec[j].bv_page = pages[j];
6519 imu->bvec[j].bv_len = vec_len;
6520 imu->bvec[j].bv_offset = off;
6521 off = 0;
6522 size -= vec_len;
6523 }
6524 /* store original address for later verification */
6525 imu->ubuf = ubuf;
6526 imu->len = iov.iov_len;
6527 imu->nr_bvecs = nr_pages;
6528
6529 ctx->nr_user_bufs++;
6530 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006531 kvfree(pages);
6532 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006533 return 0;
6534err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006535 kvfree(pages);
6536 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006537 io_sqe_buffer_unregister(ctx);
6538 return ret;
6539}
6540
Jens Axboe9b402842019-04-11 11:45:41 -06006541static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6542{
6543 __s32 __user *fds = arg;
6544 int fd;
6545
6546 if (ctx->cq_ev_fd)
6547 return -EBUSY;
6548
6549 if (copy_from_user(&fd, fds, sizeof(*fds)))
6550 return -EFAULT;
6551
6552 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6553 if (IS_ERR(ctx->cq_ev_fd)) {
6554 int ret = PTR_ERR(ctx->cq_ev_fd);
6555 ctx->cq_ev_fd = NULL;
6556 return ret;
6557 }
6558
6559 return 0;
6560}
6561
6562static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6563{
6564 if (ctx->cq_ev_fd) {
6565 eventfd_ctx_put(ctx->cq_ev_fd);
6566 ctx->cq_ev_fd = NULL;
6567 return 0;
6568 }
6569
6570 return -ENXIO;
6571}
6572
Jens Axboe2b188cc2019-01-07 10:46:33 -07006573static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6574{
Jens Axboe6b063142019-01-10 22:13:58 -07006575 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006576 if (ctx->sqo_mm)
6577 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006578
6579 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006580 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006581 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006582 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006583 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006584
Jens Axboe2b188cc2019-01-07 10:46:33 -07006585#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006586 if (ctx->ring_sock) {
6587 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006588 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006589 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006590#endif
6591
Hristo Venev75b28af2019-08-26 17:23:46 +00006592 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006593 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006594
6595 percpu_ref_exit(&ctx->refs);
6596 if (ctx->account_mem)
6597 io_unaccount_mem(ctx->user,
6598 ring_pages(ctx->sq_entries, ctx->cq_entries));
6599 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006600 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006601 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006602 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006603 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006604 kfree(ctx);
6605}
6606
6607static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6608{
6609 struct io_ring_ctx *ctx = file->private_data;
6610 __poll_t mask = 0;
6611
6612 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006613 /*
6614 * synchronizes with barrier from wq_has_sleeper call in
6615 * io_commit_cqring
6616 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006617 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006618 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6619 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006620 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006621 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006622 mask |= EPOLLIN | EPOLLRDNORM;
6623
6624 return mask;
6625}
6626
6627static int io_uring_fasync(int fd, struct file *file, int on)
6628{
6629 struct io_ring_ctx *ctx = file->private_data;
6630
6631 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6632}
6633
Jens Axboe071698e2020-01-28 10:04:42 -07006634static int io_remove_personalities(int id, void *p, void *data)
6635{
6636 struct io_ring_ctx *ctx = data;
6637 const struct cred *cred;
6638
6639 cred = idr_remove(&ctx->personality_idr, id);
6640 if (cred)
6641 put_cred(cred);
6642 return 0;
6643}
6644
Jens Axboe2b188cc2019-01-07 10:46:33 -07006645static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6646{
6647 mutex_lock(&ctx->uring_lock);
6648 percpu_ref_kill(&ctx->refs);
6649 mutex_unlock(&ctx->uring_lock);
6650
Jens Axboedf069d82020-02-04 16:48:34 -07006651 /*
6652 * Wait for sq thread to idle, if we have one. It won't spin on new
6653 * work after we've killed the ctx ref above. This is important to do
6654 * before we cancel existing commands, as the thread could otherwise
6655 * be queueing new work post that. If that's work we need to cancel,
6656 * it could cause shutdown to hang.
6657 */
6658 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6659 cpu_relax();
6660
Jens Axboe5262f562019-09-17 12:26:57 -06006661 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006662 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006663
6664 if (ctx->io_wq)
6665 io_wq_cancel_all(ctx->io_wq);
6666
Jens Axboedef596e2019-01-09 08:59:42 -07006667 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006668 /* if we failed setting up the ctx, we might not have any rings */
6669 if (ctx->rings)
6670 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006671 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006672 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006673 io_ring_ctx_free(ctx);
6674}
6675
6676static int io_uring_release(struct inode *inode, struct file *file)
6677{
6678 struct io_ring_ctx *ctx = file->private_data;
6679
6680 file->private_data = NULL;
6681 io_ring_ctx_wait_and_kill(ctx);
6682 return 0;
6683}
6684
Jens Axboefcb323c2019-10-24 12:39:47 -06006685static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6686 struct files_struct *files)
6687{
6688 struct io_kiocb *req;
6689 DEFINE_WAIT(wait);
6690
6691 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006692 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006693
6694 spin_lock_irq(&ctx->inflight_lock);
6695 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006696 if (req->work.files != files)
6697 continue;
6698 /* req is being completed, ignore */
6699 if (!refcount_inc_not_zero(&req->refs))
6700 continue;
6701 cancel_req = req;
6702 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006703 }
Jens Axboe768134d2019-11-10 20:30:53 -07006704 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006705 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006706 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006707 spin_unlock_irq(&ctx->inflight_lock);
6708
Jens Axboe768134d2019-11-10 20:30:53 -07006709 /* We need to keep going until we don't find a matching req */
6710 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006711 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006712
Jens Axboe2ca10252020-02-13 17:17:35 -07006713 if (cancel_req->flags & REQ_F_OVERFLOW) {
6714 spin_lock_irq(&ctx->completion_lock);
6715 list_del(&cancel_req->list);
6716 cancel_req->flags &= ~REQ_F_OVERFLOW;
6717 if (list_empty(&ctx->cq_overflow_list)) {
6718 clear_bit(0, &ctx->sq_check_overflow);
6719 clear_bit(0, &ctx->cq_check_overflow);
6720 }
6721 spin_unlock_irq(&ctx->completion_lock);
6722
6723 WRITE_ONCE(ctx->rings->cq_overflow,
6724 atomic_inc_return(&ctx->cached_cq_overflow));
6725
6726 /*
6727 * Put inflight ref and overflow ref. If that's
6728 * all we had, then we're done with this request.
6729 */
6730 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6731 io_put_req(cancel_req);
6732 continue;
6733 }
6734 }
6735
Bob Liu2f6d9b92019-11-13 18:06:24 +08006736 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6737 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006738 schedule();
6739 }
Jens Axboe768134d2019-11-10 20:30:53 -07006740 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006741}
6742
6743static int io_uring_flush(struct file *file, void *data)
6744{
6745 struct io_ring_ctx *ctx = file->private_data;
6746
6747 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006748
6749 /*
6750 * If the task is going away, cancel work it may have pending
6751 */
6752 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6753 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6754
Jens Axboefcb323c2019-10-24 12:39:47 -06006755 return 0;
6756}
6757
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006758static void *io_uring_validate_mmap_request(struct file *file,
6759 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006760{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006761 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006762 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763 struct page *page;
6764 void *ptr;
6765
6766 switch (offset) {
6767 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006768 case IORING_OFF_CQ_RING:
6769 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770 break;
6771 case IORING_OFF_SQES:
6772 ptr = ctx->sq_sqes;
6773 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006774 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006775 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006776 }
6777
6778 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006779 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006780 return ERR_PTR(-EINVAL);
6781
6782 return ptr;
6783}
6784
6785#ifdef CONFIG_MMU
6786
6787static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6788{
6789 size_t sz = vma->vm_end - vma->vm_start;
6790 unsigned long pfn;
6791 void *ptr;
6792
6793 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6794 if (IS_ERR(ptr))
6795 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006796
6797 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6798 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6799}
6800
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006801#else /* !CONFIG_MMU */
6802
6803static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6804{
6805 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6806}
6807
6808static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6809{
6810 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6811}
6812
6813static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6814 unsigned long addr, unsigned long len,
6815 unsigned long pgoff, unsigned long flags)
6816{
6817 void *ptr;
6818
6819 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6820 if (IS_ERR(ptr))
6821 return PTR_ERR(ptr);
6822
6823 return (unsigned long) ptr;
6824}
6825
6826#endif /* !CONFIG_MMU */
6827
Jens Axboe2b188cc2019-01-07 10:46:33 -07006828SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6829 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6830 size_t, sigsz)
6831{
6832 struct io_ring_ctx *ctx;
6833 long ret = -EBADF;
6834 int submitted = 0;
6835 struct fd f;
6836
Jens Axboeb41e9852020-02-17 09:52:41 -07006837 if (current->task_works)
6838 task_work_run();
6839
Jens Axboe6c271ce2019-01-10 11:22:30 -07006840 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006841 return -EINVAL;
6842
6843 f = fdget(fd);
6844 if (!f.file)
6845 return -EBADF;
6846
6847 ret = -EOPNOTSUPP;
6848 if (f.file->f_op != &io_uring_fops)
6849 goto out_fput;
6850
6851 ret = -ENXIO;
6852 ctx = f.file->private_data;
6853 if (!percpu_ref_tryget(&ctx->refs))
6854 goto out_fput;
6855
Jens Axboe6c271ce2019-01-10 11:22:30 -07006856 /*
6857 * For SQ polling, the thread will do all submissions and completions.
6858 * Just return the requested submit count, and wake the thread if
6859 * we were asked to.
6860 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006861 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006862 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006863 if (!list_empty_careful(&ctx->cq_overflow_list))
6864 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006865 if (flags & IORING_ENTER_SQ_WAKEUP)
6866 wake_up(&ctx->sqo_wait);
6867 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006868 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006869 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006870
6871 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006872 /* already have mm, so io_submit_sqes() won't try to grab it */
6873 cur_mm = ctx->sqo_mm;
6874 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6875 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006876 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006877
6878 if (submitted != to_submit)
6879 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006880 }
6881 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006882 unsigned nr_events = 0;
6883
Jens Axboe2b188cc2019-01-07 10:46:33 -07006884 min_complete = min(min_complete, ctx->cq_entries);
6885
Jens Axboedef596e2019-01-09 08:59:42 -07006886 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006887 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006888 } else {
6889 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6890 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006891 }
6892
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006893out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006894 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006895out_fput:
6896 fdput(f);
6897 return submitted ? submitted : ret;
6898}
6899
Tobias Klauserbebdb652020-02-26 18:38:32 +01006900#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006901static int io_uring_show_cred(int id, void *p, void *data)
6902{
6903 const struct cred *cred = p;
6904 struct seq_file *m = data;
6905 struct user_namespace *uns = seq_user_ns(m);
6906 struct group_info *gi;
6907 kernel_cap_t cap;
6908 unsigned __capi;
6909 int g;
6910
6911 seq_printf(m, "%5d\n", id);
6912 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6913 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6914 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6915 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6916 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6917 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6918 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6919 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6920 seq_puts(m, "\n\tGroups:\t");
6921 gi = cred->group_info;
6922 for (g = 0; g < gi->ngroups; g++) {
6923 seq_put_decimal_ull(m, g ? " " : "",
6924 from_kgid_munged(uns, gi->gid[g]));
6925 }
6926 seq_puts(m, "\n\tCapEff:\t");
6927 cap = cred->cap_effective;
6928 CAP_FOR_EACH_U32(__capi)
6929 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6930 seq_putc(m, '\n');
6931 return 0;
6932}
6933
6934static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6935{
6936 int i;
6937
6938 mutex_lock(&ctx->uring_lock);
6939 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6940 for (i = 0; i < ctx->nr_user_files; i++) {
6941 struct fixed_file_table *table;
6942 struct file *f;
6943
6944 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6945 f = table->files[i & IORING_FILE_TABLE_MASK];
6946 if (f)
6947 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6948 else
6949 seq_printf(m, "%5u: <none>\n", i);
6950 }
6951 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6952 for (i = 0; i < ctx->nr_user_bufs; i++) {
6953 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6954
6955 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6956 (unsigned int) buf->len);
6957 }
6958 if (!idr_is_empty(&ctx->personality_idr)) {
6959 seq_printf(m, "Personalities:\n");
6960 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6961 }
Jens Axboed7718a92020-02-14 22:23:12 -07006962 seq_printf(m, "PollList:\n");
6963 spin_lock_irq(&ctx->completion_lock);
6964 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
6965 struct hlist_head *list = &ctx->cancel_hash[i];
6966 struct io_kiocb *req;
6967
6968 hlist_for_each_entry(req, list, hash_node)
6969 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
6970 req->task->task_works != NULL);
6971 }
6972 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07006973 mutex_unlock(&ctx->uring_lock);
6974}
6975
6976static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6977{
6978 struct io_ring_ctx *ctx = f->private_data;
6979
6980 if (percpu_ref_tryget(&ctx->refs)) {
6981 __io_uring_show_fdinfo(ctx, m);
6982 percpu_ref_put(&ctx->refs);
6983 }
6984}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006985#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006986
Jens Axboe2b188cc2019-01-07 10:46:33 -07006987static const struct file_operations io_uring_fops = {
6988 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006989 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006990 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006991#ifndef CONFIG_MMU
6992 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6993 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6994#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006995 .poll = io_uring_poll,
6996 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006997#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006998 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006999#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007000};
7001
7002static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7003 struct io_uring_params *p)
7004{
Hristo Venev75b28af2019-08-26 17:23:46 +00007005 struct io_rings *rings;
7006 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007007
Hristo Venev75b28af2019-08-26 17:23:46 +00007008 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7009 if (size == SIZE_MAX)
7010 return -EOVERFLOW;
7011
7012 rings = io_mem_alloc(size);
7013 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007014 return -ENOMEM;
7015
Hristo Venev75b28af2019-08-26 17:23:46 +00007016 ctx->rings = rings;
7017 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7018 rings->sq_ring_mask = p->sq_entries - 1;
7019 rings->cq_ring_mask = p->cq_entries - 1;
7020 rings->sq_ring_entries = p->sq_entries;
7021 rings->cq_ring_entries = p->cq_entries;
7022 ctx->sq_mask = rings->sq_ring_mask;
7023 ctx->cq_mask = rings->cq_ring_mask;
7024 ctx->sq_entries = rings->sq_ring_entries;
7025 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007026
7027 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007028 if (size == SIZE_MAX) {
7029 io_mem_free(ctx->rings);
7030 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007031 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007032 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007033
7034 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007035 if (!ctx->sq_sqes) {
7036 io_mem_free(ctx->rings);
7037 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007038 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007039 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007040
Jens Axboe2b188cc2019-01-07 10:46:33 -07007041 return 0;
7042}
7043
7044/*
7045 * Allocate an anonymous fd, this is what constitutes the application
7046 * visible backing of an io_uring instance. The application mmaps this
7047 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7048 * we have to tie this fd to a socket for file garbage collection purposes.
7049 */
7050static int io_uring_get_fd(struct io_ring_ctx *ctx)
7051{
7052 struct file *file;
7053 int ret;
7054
7055#if defined(CONFIG_UNIX)
7056 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7057 &ctx->ring_sock);
7058 if (ret)
7059 return ret;
7060#endif
7061
7062 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7063 if (ret < 0)
7064 goto err;
7065
7066 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7067 O_RDWR | O_CLOEXEC);
7068 if (IS_ERR(file)) {
7069 put_unused_fd(ret);
7070 ret = PTR_ERR(file);
7071 goto err;
7072 }
7073
7074#if defined(CONFIG_UNIX)
7075 ctx->ring_sock->file = file;
7076#endif
7077 fd_install(ret, file);
7078 return ret;
7079err:
7080#if defined(CONFIG_UNIX)
7081 sock_release(ctx->ring_sock);
7082 ctx->ring_sock = NULL;
7083#endif
7084 return ret;
7085}
7086
7087static int io_uring_create(unsigned entries, struct io_uring_params *p)
7088{
7089 struct user_struct *user = NULL;
7090 struct io_ring_ctx *ctx;
7091 bool account_mem;
7092 int ret;
7093
Jens Axboe8110c1a2019-12-28 15:39:54 -07007094 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007095 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007096 if (entries > IORING_MAX_ENTRIES) {
7097 if (!(p->flags & IORING_SETUP_CLAMP))
7098 return -EINVAL;
7099 entries = IORING_MAX_ENTRIES;
7100 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007101
7102 /*
7103 * Use twice as many entries for the CQ ring. It's possible for the
7104 * application to drive a higher depth than the size of the SQ ring,
7105 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007106 * some flexibility in overcommitting a bit. If the application has
7107 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7108 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007109 */
7110 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007111 if (p->flags & IORING_SETUP_CQSIZE) {
7112 /*
7113 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7114 * to a power-of-two, if it isn't already. We do NOT impose
7115 * any cq vs sq ring sizing.
7116 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007117 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007118 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007119 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7120 if (!(p->flags & IORING_SETUP_CLAMP))
7121 return -EINVAL;
7122 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7123 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007124 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7125 } else {
7126 p->cq_entries = 2 * p->sq_entries;
7127 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007128
7129 user = get_uid(current_user());
7130 account_mem = !capable(CAP_IPC_LOCK);
7131
7132 if (account_mem) {
7133 ret = io_account_mem(user,
7134 ring_pages(p->sq_entries, p->cq_entries));
7135 if (ret) {
7136 free_uid(user);
7137 return ret;
7138 }
7139 }
7140
7141 ctx = io_ring_ctx_alloc(p);
7142 if (!ctx) {
7143 if (account_mem)
7144 io_unaccount_mem(user, ring_pages(p->sq_entries,
7145 p->cq_entries));
7146 free_uid(user);
7147 return -ENOMEM;
7148 }
7149 ctx->compat = in_compat_syscall();
7150 ctx->account_mem = account_mem;
7151 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007152 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007153
7154 ret = io_allocate_scq_urings(ctx, p);
7155 if (ret)
7156 goto err;
7157
Jens Axboe6c271ce2019-01-10 11:22:30 -07007158 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007159 if (ret)
7160 goto err;
7161
Jens Axboe2b188cc2019-01-07 10:46:33 -07007162 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007163 p->sq_off.head = offsetof(struct io_rings, sq.head);
7164 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7165 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7166 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7167 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7168 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7169 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007170
7171 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007172 p->cq_off.head = offsetof(struct io_rings, cq.head);
7173 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7174 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7175 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7176 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7177 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007178
Jens Axboe044c1ab2019-10-28 09:15:33 -06007179 /*
7180 * Install ring fd as the very last thing, so we don't risk someone
7181 * having closed it before we finish setup
7182 */
7183 ret = io_uring_get_fd(ctx);
7184 if (ret < 0)
7185 goto err;
7186
Jens Axboeda8c9692019-12-02 18:51:26 -07007187 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007188 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007189 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007190 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007191 return ret;
7192err:
7193 io_ring_ctx_wait_and_kill(ctx);
7194 return ret;
7195}
7196
7197/*
7198 * Sets up an aio uring context, and returns the fd. Applications asks for a
7199 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7200 * params structure passed in.
7201 */
7202static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7203{
7204 struct io_uring_params p;
7205 long ret;
7206 int i;
7207
7208 if (copy_from_user(&p, params, sizeof(p)))
7209 return -EFAULT;
7210 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7211 if (p.resv[i])
7212 return -EINVAL;
7213 }
7214
Jens Axboe6c271ce2019-01-10 11:22:30 -07007215 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007216 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007217 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007218 return -EINVAL;
7219
7220 ret = io_uring_create(entries, &p);
7221 if (ret < 0)
7222 return ret;
7223
7224 if (copy_to_user(params, &p, sizeof(p)))
7225 return -EFAULT;
7226
7227 return ret;
7228}
7229
7230SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7231 struct io_uring_params __user *, params)
7232{
7233 return io_uring_setup(entries, params);
7234}
7235
Jens Axboe66f4af92020-01-16 15:36:52 -07007236static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7237{
7238 struct io_uring_probe *p;
7239 size_t size;
7240 int i, ret;
7241
7242 size = struct_size(p, ops, nr_args);
7243 if (size == SIZE_MAX)
7244 return -EOVERFLOW;
7245 p = kzalloc(size, GFP_KERNEL);
7246 if (!p)
7247 return -ENOMEM;
7248
7249 ret = -EFAULT;
7250 if (copy_from_user(p, arg, size))
7251 goto out;
7252 ret = -EINVAL;
7253 if (memchr_inv(p, 0, size))
7254 goto out;
7255
7256 p->last_op = IORING_OP_LAST - 1;
7257 if (nr_args > IORING_OP_LAST)
7258 nr_args = IORING_OP_LAST;
7259
7260 for (i = 0; i < nr_args; i++) {
7261 p->ops[i].op = i;
7262 if (!io_op_defs[i].not_supported)
7263 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7264 }
7265 p->ops_len = i;
7266
7267 ret = 0;
7268 if (copy_to_user(arg, p, size))
7269 ret = -EFAULT;
7270out:
7271 kfree(p);
7272 return ret;
7273}
7274
Jens Axboe071698e2020-01-28 10:04:42 -07007275static int io_register_personality(struct io_ring_ctx *ctx)
7276{
7277 const struct cred *creds = get_current_cred();
7278 int id;
7279
7280 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7281 USHRT_MAX, GFP_KERNEL);
7282 if (id < 0)
7283 put_cred(creds);
7284 return id;
7285}
7286
7287static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7288{
7289 const struct cred *old_creds;
7290
7291 old_creds = idr_remove(&ctx->personality_idr, id);
7292 if (old_creds) {
7293 put_cred(old_creds);
7294 return 0;
7295 }
7296
7297 return -EINVAL;
7298}
7299
7300static bool io_register_op_must_quiesce(int op)
7301{
7302 switch (op) {
7303 case IORING_UNREGISTER_FILES:
7304 case IORING_REGISTER_FILES_UPDATE:
7305 case IORING_REGISTER_PROBE:
7306 case IORING_REGISTER_PERSONALITY:
7307 case IORING_UNREGISTER_PERSONALITY:
7308 return false;
7309 default:
7310 return true;
7311 }
7312}
7313
Jens Axboeedafcce2019-01-09 09:16:05 -07007314static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7315 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007316 __releases(ctx->uring_lock)
7317 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007318{
7319 int ret;
7320
Jens Axboe35fa71a2019-04-22 10:23:23 -06007321 /*
7322 * We're inside the ring mutex, if the ref is already dying, then
7323 * someone else killed the ctx or is already going through
7324 * io_uring_register().
7325 */
7326 if (percpu_ref_is_dying(&ctx->refs))
7327 return -ENXIO;
7328
Jens Axboe071698e2020-01-28 10:04:42 -07007329 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007330 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007331
Jens Axboe05f3fb32019-12-09 11:22:50 -07007332 /*
7333 * Drop uring mutex before waiting for references to exit. If
7334 * another thread is currently inside io_uring_enter() it might
7335 * need to grab the uring_lock to make progress. If we hold it
7336 * here across the drain wait, then we can deadlock. It's safe
7337 * to drop the mutex here, since no new references will come in
7338 * after we've killed the percpu ref.
7339 */
7340 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007341 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007342 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007343 if (ret) {
7344 percpu_ref_resurrect(&ctx->refs);
7345 ret = -EINTR;
7346 goto out;
7347 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007348 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007349
7350 switch (opcode) {
7351 case IORING_REGISTER_BUFFERS:
7352 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7353 break;
7354 case IORING_UNREGISTER_BUFFERS:
7355 ret = -EINVAL;
7356 if (arg || nr_args)
7357 break;
7358 ret = io_sqe_buffer_unregister(ctx);
7359 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007360 case IORING_REGISTER_FILES:
7361 ret = io_sqe_files_register(ctx, arg, nr_args);
7362 break;
7363 case IORING_UNREGISTER_FILES:
7364 ret = -EINVAL;
7365 if (arg || nr_args)
7366 break;
7367 ret = io_sqe_files_unregister(ctx);
7368 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007369 case IORING_REGISTER_FILES_UPDATE:
7370 ret = io_sqe_files_update(ctx, arg, nr_args);
7371 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007372 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007373 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007374 ret = -EINVAL;
7375 if (nr_args != 1)
7376 break;
7377 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007378 if (ret)
7379 break;
7380 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7381 ctx->eventfd_async = 1;
7382 else
7383 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007384 break;
7385 case IORING_UNREGISTER_EVENTFD:
7386 ret = -EINVAL;
7387 if (arg || nr_args)
7388 break;
7389 ret = io_eventfd_unregister(ctx);
7390 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007391 case IORING_REGISTER_PROBE:
7392 ret = -EINVAL;
7393 if (!arg || nr_args > 256)
7394 break;
7395 ret = io_probe(ctx, arg, nr_args);
7396 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007397 case IORING_REGISTER_PERSONALITY:
7398 ret = -EINVAL;
7399 if (arg || nr_args)
7400 break;
7401 ret = io_register_personality(ctx);
7402 break;
7403 case IORING_UNREGISTER_PERSONALITY:
7404 ret = -EINVAL;
7405 if (arg)
7406 break;
7407 ret = io_unregister_personality(ctx, nr_args);
7408 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007409 default:
7410 ret = -EINVAL;
7411 break;
7412 }
7413
Jens Axboe071698e2020-01-28 10:04:42 -07007414 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007415 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007416 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007417out:
7418 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007419 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007420 return ret;
7421}
7422
7423SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7424 void __user *, arg, unsigned int, nr_args)
7425{
7426 struct io_ring_ctx *ctx;
7427 long ret = -EBADF;
7428 struct fd f;
7429
7430 f = fdget(fd);
7431 if (!f.file)
7432 return -EBADF;
7433
7434 ret = -EOPNOTSUPP;
7435 if (f.file->f_op != &io_uring_fops)
7436 goto out_fput;
7437
7438 ctx = f.file->private_data;
7439
7440 mutex_lock(&ctx->uring_lock);
7441 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7442 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007443 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7444 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007445out_fput:
7446 fdput(f);
7447 return ret;
7448}
7449
Jens Axboe2b188cc2019-01-07 10:46:33 -07007450static int __init io_uring_init(void)
7451{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007452#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7453 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7454 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7455} while (0)
7456
7457#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7458 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7459 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7460 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7461 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7462 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7463 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7464 BUILD_BUG_SQE_ELEM(8, __u64, off);
7465 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7466 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007467 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007468 BUILD_BUG_SQE_ELEM(24, __u32, len);
7469 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7470 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7471 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7472 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7473 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7474 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7475 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7476 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7477 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7478 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7479 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7480 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7481 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007482 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007483 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7484 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7485 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007486 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007487
Jens Axboed3656342019-12-18 09:50:26 -07007488 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007489 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7490 return 0;
7491};
7492__initcall(io_uring_init);