blob: 40ca9e6a5acec07bb5dc88da8111677b04a64cc8 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020082#define CREATE_TRACE_POINTS
83#include <trace/events/io_uring.h>
84
Jens Axboe2b188cc2019-01-07 10:46:33 -070085#include <uapi/linux/io_uring.h>
86
87#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060088#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070089
Daniel Xu5277dea2019-09-14 14:23:45 -070090#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060091#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060092
93/*
94 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
95 */
96#define IORING_FILE_TABLE_SHIFT 9
97#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
98#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
99#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700100
101struct io_uring {
102 u32 head ____cacheline_aligned_in_smp;
103 u32 tail ____cacheline_aligned_in_smp;
104};
105
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 * This data is shared with the application through the mmap at offsets
108 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 *
110 * The offsets to the member fields are published through struct
111 * io_sqring_offsets when calling io_uring_setup.
112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
115 * Head and tail offsets into the ring; the offsets need to be
116 * masked to get valid indices.
117 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * The kernel controls head of the sq ring and the tail of the cq ring,
119 * and the application controls tail of the sq ring and the head of the
120 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 * ring_entries - 1)
126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 u32 sq_ring_mask, cq_ring_mask;
128 /* Ring sizes (constant, power of 2) */
129 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 /*
131 * Number of invalid entries dropped by the kernel due to
132 * invalid index stored in array
133 *
134 * Written by the kernel, shouldn't be modified by the
135 * application (i.e. get number of "new events" by comparing to
136 * cached value).
137 *
138 * After a new SQ head value was read by the application this
139 * counter includes all submissions that were dropped reaching
140 * the new SQ head (and possibly more).
141 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000142 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
144 * Runtime flags
145 *
146 * Written by the kernel, shouldn't be modified by the
147 * application.
148 *
149 * The application needs a full memory barrier before checking
150 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
154 * Number of completion events lost because the queue was full;
155 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800156 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 * the completion queue.
158 *
159 * Written by the kernel, shouldn't be modified by the
160 * application (i.e. get number of "new events" by comparing to
161 * cached value).
162 *
163 * As completion events come in out of order this counter is not
164 * ordered with any other data.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 /*
168 * Ring buffer of completion events.
169 *
170 * The kernel writes completion events fresh every time they are
171 * produced, so the application is allowed to modify pending
172 * entries.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700175};
176
Jens Axboeedafcce2019-01-09 09:16:05 -0700177struct io_mapped_ubuf {
178 u64 ubuf;
179 size_t len;
180 struct bio_vec *bvec;
181 unsigned int nr_bvecs;
182};
183
Jens Axboe65e19f52019-10-26 07:20:21 -0600184struct fixed_file_table {
185 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700186};
187
Jens Axboe05f3fb32019-12-09 11:22:50 -0700188struct fixed_file_data {
189 struct fixed_file_table *table;
190 struct io_ring_ctx *ctx;
191
192 struct percpu_ref refs;
193 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe2b188cc2019-01-07 10:46:33 -0700198struct io_ring_ctx {
199 struct {
200 struct percpu_ref refs;
201 } ____cacheline_aligned_in_smp;
202
203 struct {
204 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800205 unsigned int compat: 1;
206 unsigned int account_mem: 1;
207 unsigned int cq_overflow_flushed: 1;
208 unsigned int drain_next: 1;
209 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210
Hristo Venev75b28af2019-08-26 17:23:46 +0000211 /*
212 * Ring buffer of indices into array of io_uring_sqe, which is
213 * mmapped by the application using the IORING_OFF_SQES offset.
214 *
215 * This indirection could e.g. be used to assign fixed
216 * io_uring_sqe entries to operations and only submit them to
217 * the queue when needed.
218 *
219 * The kernel modifies neither the indices array nor the entries
220 * array.
221 */
222 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 unsigned cached_sq_head;
224 unsigned sq_entries;
225 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700226 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600227 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700228 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700229 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600230
231 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600232 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700233 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Jens Axboefcb323c2019-10-24 12:39:47 -0600235 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700236 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 } ____cacheline_aligned_in_smp;
238
Hristo Venev75b28af2019-08-26 17:23:46 +0000239 struct io_rings *rings;
240
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600242 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 struct task_struct *sqo_thread; /* if using sq thread polling */
244 struct mm_struct *sqo_mm;
245 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246
Jens Axboe6b063142019-01-10 22:13:58 -0700247 /*
248 * If used, fixed file set. Writers must ensure that ->refs is dead,
249 * readers must ensure that ->refs is alive as long as the file* is
250 * used. Only updated through io_uring_register(2).
251 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700252 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700253 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300254 int ring_fd;
255 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700256
Jens Axboeedafcce2019-01-09 09:16:05 -0700257 /* if used, fixed mapped user buffers */
258 unsigned nr_user_bufs;
259 struct io_mapped_ubuf *user_bufs;
260
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 struct user_struct *user;
262
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700263 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700264
Jens Axboe206aefd2019-11-07 18:27:42 -0700265 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
266 struct completion *completions;
267
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700268 /* if all else fails... */
269 struct io_kiocb *fallback_req;
270
Jens Axboe206aefd2019-11-07 18:27:42 -0700271#if defined(CONFIG_UNIX)
272 struct socket *ring_sock;
273#endif
274
Jens Axboe071698e2020-01-28 10:04:42 -0700275 struct idr personality_idr;
276
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 struct {
278 unsigned cached_cq_tail;
279 unsigned cq_entries;
280 unsigned cq_mask;
281 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700282 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700283 struct wait_queue_head cq_wait;
284 struct fasync_struct *cq_fasync;
285 struct eventfd_ctx *cq_ev_fd;
286 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700287
288 struct {
289 struct mutex uring_lock;
290 wait_queue_head_t wait;
291 } ____cacheline_aligned_in_smp;
292
293 struct {
294 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700295
Jens Axboedef596e2019-01-09 08:59:42 -0700296 /*
297 * ->poll_list is protected by the ctx->uring_lock for
298 * io_uring instances that don't use IORING_SETUP_SQPOLL.
299 * For SQPOLL, only the single threaded io_sq_thread() will
300 * manipulate the list, hence no extra locking is needed there.
301 */
302 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700303 struct hlist_head *cancel_hash;
304 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700305 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600306
307 spinlock_t inflight_lock;
308 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700310};
311
Jens Axboe09bb8392019-03-13 12:39:28 -0600312/*
313 * First field must be the file pointer in all the
314 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
315 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316struct io_poll_iocb {
317 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700318 union {
319 struct wait_queue_head *head;
320 u64 addr;
321 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700322 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600323 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700324 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700325 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326};
327
Jens Axboeb5dba592019-12-11 14:02:38 -0700328struct io_close {
329 struct file *file;
330 struct file *put_file;
331 int fd;
332};
333
Jens Axboead8a48a2019-11-15 08:49:11 -0700334struct io_timeout_data {
335 struct io_kiocb *req;
336 struct hrtimer timer;
337 struct timespec64 ts;
338 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300339 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700340};
341
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700342struct io_accept {
343 struct file *file;
344 struct sockaddr __user *addr;
345 int __user *addr_len;
346 int flags;
347};
348
349struct io_sync {
350 struct file *file;
351 loff_t len;
352 loff_t off;
353 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700354 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700355};
356
Jens Axboefbf23842019-12-17 18:45:56 -0700357struct io_cancel {
358 struct file *file;
359 u64 addr;
360};
361
Jens Axboeb29472e2019-12-17 18:50:29 -0700362struct io_timeout {
363 struct file *file;
364 u64 addr;
365 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700366 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700367};
368
Jens Axboe9adbd452019-12-20 08:45:55 -0700369struct io_rw {
370 /* NOTE: kiocb has the file as the first member, so don't do it here */
371 struct kiocb kiocb;
372 u64 addr;
373 u64 len;
374};
375
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700376struct io_connect {
377 struct file *file;
378 struct sockaddr __user *addr;
379 int addr_len;
380};
381
Jens Axboee47293f2019-12-20 08:58:21 -0700382struct io_sr_msg {
383 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700384 union {
385 struct user_msghdr __user *msg;
386 void __user *buf;
387 };
Jens Axboee47293f2019-12-20 08:58:21 -0700388 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700389 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700390};
391
Jens Axboe15b71ab2019-12-11 11:20:36 -0700392struct io_open {
393 struct file *file;
394 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 unsigned mask;
397 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700400 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401};
402
Jens Axboe05f3fb32019-12-09 11:22:50 -0700403struct io_files_update {
404 struct file *file;
405 u64 arg;
406 u32 nr_args;
407 u32 offset;
408};
409
Jens Axboe4840e412019-12-25 22:03:45 -0700410struct io_fadvise {
411 struct file *file;
412 u64 offset;
413 u32 len;
414 u32 advice;
415};
416
Jens Axboec1ca7572019-12-25 22:18:28 -0700417struct io_madvise {
418 struct file *file;
419 u64 addr;
420 u32 len;
421 u32 advice;
422};
423
Jens Axboe3e4827b2020-01-08 15:18:09 -0700424struct io_epoll {
425 struct file *file;
426 int epfd;
427 int op;
428 int fd;
429 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430};
431
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300432struct io_splice {
433 struct file *file_out;
434 struct file *file_in;
435 loff_t off_out;
436 loff_t off_in;
437 u64 len;
438 unsigned int flags;
439};
440
Jens Axboef499a022019-12-02 16:28:46 -0700441struct io_async_connect {
442 struct sockaddr_storage address;
443};
444
Jens Axboe03b12302019-12-02 18:50:25 -0700445struct io_async_msghdr {
446 struct iovec fast_iov[UIO_FASTIOV];
447 struct iovec *iov;
448 struct sockaddr __user *uaddr;
449 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700450 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700451};
452
Jens Axboef67676d2019-12-02 11:03:47 -0700453struct io_async_rw {
454 struct iovec fast_iov[UIO_FASTIOV];
455 struct iovec *iov;
456 ssize_t nr_segs;
457 ssize_t size;
458};
459
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700460struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700461 union {
462 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700463 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700464 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700465 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700466 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700467};
468
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300469enum {
470 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
471 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
472 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
473 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
474 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
475
476 REQ_F_LINK_NEXT_BIT,
477 REQ_F_FAIL_LINK_BIT,
478 REQ_F_INFLIGHT_BIT,
479 REQ_F_CUR_POS_BIT,
480 REQ_F_NOWAIT_BIT,
481 REQ_F_IOPOLL_COMPLETED_BIT,
482 REQ_F_LINK_TIMEOUT_BIT,
483 REQ_F_TIMEOUT_BIT,
484 REQ_F_ISREG_BIT,
485 REQ_F_MUST_PUNT_BIT,
486 REQ_F_TIMEOUT_NOSEQ_BIT,
487 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300488 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700489 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700490 REQ_F_POLLED_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300491};
492
493enum {
494 /* ctx owns file */
495 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
496 /* drain existing IO first */
497 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
498 /* linked sqes */
499 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
500 /* doesn't sever on completion < 0 */
501 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
502 /* IOSQE_ASYNC */
503 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
504
505 /* already grabbed next link */
506 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
507 /* fail rest of links */
508 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
509 /* on inflight list */
510 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
511 /* read/write uses file position */
512 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
513 /* must not punt to workers */
514 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
515 /* polled IO has completed */
516 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
517 /* has linked timeout */
518 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
519 /* timeout request */
520 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
521 /* regular file */
522 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
523 /* must be punted even for NONBLOCK */
524 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
525 /* no timeout sequence */
526 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
527 /* completion under lock */
528 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300529 /* needs cleanup */
530 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700531 /* in overflow list */
532 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700533 /* already went through poll handler */
534 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
535};
536
537struct async_poll {
538 struct io_poll_iocb poll;
539 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300540};
541
Jens Axboe09bb8392019-03-13 12:39:28 -0600542/*
543 * NOTE! Each of the iocb union members has the file pointer
544 * as the first entry in their struct definition. So you can
545 * access the file pointer through any of the sub-structs,
546 * or directly as just 'ki_filp' in this struct.
547 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700549 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600550 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700551 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700552 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700553 struct io_accept accept;
554 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700555 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700556 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700557 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700558 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700559 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700560 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700561 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700562 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700563 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700564 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300565 struct io_splice splice;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700566 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700568 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300569 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700570 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571
572 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700573 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700575 refcount_t refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700576 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600578 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600579 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700580
Jens Axboed7718a92020-02-14 22:23:12 -0700581 struct list_head link_list;
582
Jens Axboefcb323c2019-10-24 12:39:47 -0600583 struct list_head inflight_entry;
584
Jens Axboeb41e9852020-02-17 09:52:41 -0700585 union {
586 /*
587 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700588 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
589 * async armed poll handlers for regular commands. The latter
590 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700591 */
592 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700593 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700594 struct hlist_node hash_node;
595 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700596 };
597 struct io_wq_work work;
598 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599};
600
601#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700602#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700603
Jens Axboe9a56a232019-01-09 09:06:50 -0700604struct io_submit_state {
605 struct blk_plug plug;
606
607 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700608 * io_kiocb alloc cache
609 */
610 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300611 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700612
613 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700614 * File reference cache
615 */
616 struct file *file;
617 unsigned int fd;
618 unsigned int has_refs;
619 unsigned int used_refs;
620 unsigned int ios_left;
621};
622
Jens Axboed3656342019-12-18 09:50:26 -0700623struct io_op_def {
624 /* needs req->io allocated for deferral/async */
625 unsigned async_ctx : 1;
626 /* needs current->mm setup, does mm access */
627 unsigned needs_mm : 1;
628 /* needs req->file assigned */
629 unsigned needs_file : 1;
630 /* needs req->file assigned IFF fd is >= 0 */
631 unsigned fd_non_neg : 1;
632 /* hash wq insertion if file is a regular file */
633 unsigned hash_reg_file : 1;
634 /* unbound wq insertion if file is a non-regular file */
635 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700636 /* opcode is not supported by this kernel */
637 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700638 /* needs file table */
639 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700640 /* needs ->fs */
641 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700642 /* set if opcode supports polled "wait" */
643 unsigned pollin : 1;
644 unsigned pollout : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700645};
646
647static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300648 [IORING_OP_NOP] = {},
649 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700650 .async_ctx = 1,
651 .needs_mm = 1,
652 .needs_file = 1,
653 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700654 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700655 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300656 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700657 .async_ctx = 1,
658 .needs_mm = 1,
659 .needs_file = 1,
660 .hash_reg_file = 1,
661 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700662 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700663 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300664 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700665 .needs_file = 1,
666 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300667 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700668 .needs_file = 1,
669 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700670 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700671 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300672 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700673 .needs_file = 1,
674 .hash_reg_file = 1,
675 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700676 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700677 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300678 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700679 .needs_file = 1,
680 .unbound_nonreg_file = 1,
681 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_POLL_REMOVE] = {},
683 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700684 .needs_file = 1,
685 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300686 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700687 .async_ctx = 1,
688 .needs_mm = 1,
689 .needs_file = 1,
690 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700691 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700692 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700695 .async_ctx = 1,
696 .needs_mm = 1,
697 .needs_file = 1,
698 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700699 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700700 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .async_ctx = 1,
704 .needs_mm = 1,
705 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300706 [IORING_OP_TIMEOUT_REMOVE] = {},
707 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_mm = 1,
709 .needs_file = 1,
710 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700711 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700712 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700713 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300714 [IORING_OP_ASYNC_CANCEL] = {},
715 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700716 .async_ctx = 1,
717 .needs_mm = 1,
718 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300719 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700720 .async_ctx = 1,
721 .needs_mm = 1,
722 .needs_file = 1,
723 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700724 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700727 .needs_file = 1,
728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700730 .needs_file = 1,
731 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700732 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700733 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700736 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700737 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700740 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700741 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700742 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300743 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700744 .needs_mm = 1,
745 .needs_file = 1,
746 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700747 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700748 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300749 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700750 .needs_mm = 1,
751 .needs_file = 1,
752 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700753 .pollin = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700756 .needs_mm = 1,
757 .needs_file = 1,
758 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700759 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700760 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300761 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700762 .needs_file = 1,
763 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300764 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700765 .needs_mm = 1,
766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700768 .needs_mm = 1,
769 .needs_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700771 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700774 .needs_mm = 1,
775 .needs_file = 1,
776 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700777 .pollin = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700778 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300779 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700780 .needs_file = 1,
781 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700782 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700783 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700784 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700785 [IORING_OP_EPOLL_CTL] = {
786 .unbound_nonreg_file = 1,
787 .file_table = 1,
788 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300789 [IORING_OP_SPLICE] = {
790 .needs_file = 1,
791 .hash_reg_file = 1,
792 .unbound_nonreg_file = 1,
793 }
Jens Axboed3656342019-12-18 09:50:26 -0700794};
795
Jens Axboe561fb042019-10-24 07:25:42 -0600796static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700797static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800798static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700799static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700800static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
801static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700802static int __io_sqe_files_update(struct io_ring_ctx *ctx,
803 struct io_uring_files_update *ip,
804 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700805static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700806static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300807static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700808static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
809 int fd, struct file **out_file, bool fixed);
810static void __io_queue_sqe(struct io_kiocb *req,
811 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600812
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813static struct kmem_cache *req_cachep;
814
815static const struct file_operations io_uring_fops;
816
817struct sock *io_uring_get_socket(struct file *file)
818{
819#if defined(CONFIG_UNIX)
820 if (file->f_op == &io_uring_fops) {
821 struct io_ring_ctx *ctx = file->private_data;
822
823 return ctx->ring_sock->sk;
824 }
825#endif
826 return NULL;
827}
828EXPORT_SYMBOL(io_uring_get_socket);
829
830static void io_ring_ctx_ref_free(struct percpu_ref *ref)
831{
832 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
833
Jens Axboe206aefd2019-11-07 18:27:42 -0700834 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700835}
836
837static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
838{
839 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700840 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841
842 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
843 if (!ctx)
844 return NULL;
845
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700846 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
847 if (!ctx->fallback_req)
848 goto err;
849
Jens Axboe206aefd2019-11-07 18:27:42 -0700850 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
851 if (!ctx->completions)
852 goto err;
853
Jens Axboe78076bb2019-12-04 19:56:40 -0700854 /*
855 * Use 5 bits less than the max cq entries, that should give us around
856 * 32 entries per hash list if totally full and uniformly spread.
857 */
858 hash_bits = ilog2(p->cq_entries);
859 hash_bits -= 5;
860 if (hash_bits <= 0)
861 hash_bits = 1;
862 ctx->cancel_hash_bits = hash_bits;
863 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
864 GFP_KERNEL);
865 if (!ctx->cancel_hash)
866 goto err;
867 __hash_init(ctx->cancel_hash, 1U << hash_bits);
868
Roman Gushchin21482892019-05-07 10:01:48 -0700869 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700870 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
871 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700872
873 ctx->flags = p->flags;
874 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700875 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700876 init_completion(&ctx->completions[0]);
877 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700878 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700879 mutex_init(&ctx->uring_lock);
880 init_waitqueue_head(&ctx->wait);
881 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700882 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600883 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600884 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600885 init_waitqueue_head(&ctx->inflight_wait);
886 spin_lock_init(&ctx->inflight_lock);
887 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700888 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700889err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700890 if (ctx->fallback_req)
891 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700892 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700893 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700894 kfree(ctx);
895 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896}
897
Bob Liu9d858b22019-11-13 18:06:25 +0800898static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600899{
Jackie Liua197f662019-11-08 08:09:12 -0700900 struct io_ring_ctx *ctx = req->ctx;
901
Jens Axboe498ccd92019-10-25 10:04:25 -0600902 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
903 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600904}
905
Bob Liu9d858b22019-11-13 18:06:25 +0800906static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600907{
Pavel Begunkov87987892020-01-18 01:22:30 +0300908 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800909 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600910
Bob Liu9d858b22019-11-13 18:06:25 +0800911 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600912}
913
914static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600915{
916 struct io_kiocb *req;
917
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600918 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800919 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600920 list_del_init(&req->list);
921 return req;
922 }
923
924 return NULL;
925}
926
Jens Axboe5262f562019-09-17 12:26:57 -0600927static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
928{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600929 struct io_kiocb *req;
930
931 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700932 if (req) {
933 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
934 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800935 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700936 list_del_init(&req->list);
937 return req;
938 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600939 }
940
941 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600942}
943
Jens Axboede0617e2019-04-06 21:51:27 -0600944static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700945{
Hristo Venev75b28af2019-08-26 17:23:46 +0000946 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700947
Pavel Begunkov07910152020-01-17 03:52:46 +0300948 /* order cqe stores with ring update */
949 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700950
Pavel Begunkov07910152020-01-17 03:52:46 +0300951 if (wq_has_sleeper(&ctx->cq_wait)) {
952 wake_up_interruptible(&ctx->cq_wait);
953 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700954 }
955}
956
Jens Axboecccf0ee2020-01-27 16:34:48 -0700957static inline void io_req_work_grab_env(struct io_kiocb *req,
958 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600959{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700960 if (!req->work.mm && def->needs_mm) {
961 mmgrab(current->mm);
962 req->work.mm = current->mm;
963 }
964 if (!req->work.creds)
965 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700966 if (!req->work.fs && def->needs_fs) {
967 spin_lock(&current->fs->lock);
968 if (!current->fs->in_exec) {
969 req->work.fs = current->fs;
970 req->work.fs->users++;
971 } else {
972 req->work.flags |= IO_WQ_WORK_CANCEL;
973 }
974 spin_unlock(&current->fs->lock);
975 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700976 if (!req->work.task_pid)
977 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700978}
979
980static inline void io_req_work_drop_env(struct io_kiocb *req)
981{
982 if (req->work.mm) {
983 mmdrop(req->work.mm);
984 req->work.mm = NULL;
985 }
986 if (req->work.creds) {
987 put_cred(req->work.creds);
988 req->work.creds = NULL;
989 }
Jens Axboeff002b32020-02-07 16:05:21 -0700990 if (req->work.fs) {
991 struct fs_struct *fs = req->work.fs;
992
993 spin_lock(&req->work.fs->lock);
994 if (--fs->users)
995 fs = NULL;
996 spin_unlock(&req->work.fs->lock);
997 if (fs)
998 free_fs_struct(fs);
999 }
Jens Axboe561fb042019-10-24 07:25:42 -06001000}
1001
Jens Axboe94ae5e72019-11-14 19:39:52 -07001002static inline bool io_prep_async_work(struct io_kiocb *req,
1003 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001004{
Jens Axboed3656342019-12-18 09:50:26 -07001005 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001006 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001007
Jens Axboed3656342019-12-18 09:50:26 -07001008 if (req->flags & REQ_F_ISREG) {
1009 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001010 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001011 } else {
1012 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001013 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001014 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001015
1016 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001017
Jens Axboe94ae5e72019-11-14 19:39:52 -07001018 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001019 return do_hashed;
1020}
1021
Jackie Liua197f662019-11-08 08:09:12 -07001022static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001023{
Jackie Liua197f662019-11-08 08:09:12 -07001024 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001025 struct io_kiocb *link;
1026 bool do_hashed;
1027
1028 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001029
1030 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1031 req->flags);
1032 if (!do_hashed) {
1033 io_wq_enqueue(ctx->io_wq, &req->work);
1034 } else {
1035 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1036 file_inode(req->file));
1037 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001038
1039 if (link)
1040 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001041}
1042
Jens Axboe5262f562019-09-17 12:26:57 -06001043static void io_kill_timeout(struct io_kiocb *req)
1044{
1045 int ret;
1046
Jens Axboe2d283902019-12-04 11:08:05 -07001047 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001048 if (ret != -1) {
1049 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001050 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001051 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001052 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001053 }
1054}
1055
1056static void io_kill_timeouts(struct io_ring_ctx *ctx)
1057{
1058 struct io_kiocb *req, *tmp;
1059
1060 spin_lock_irq(&ctx->completion_lock);
1061 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1062 io_kill_timeout(req);
1063 spin_unlock_irq(&ctx->completion_lock);
1064}
1065
Jens Axboede0617e2019-04-06 21:51:27 -06001066static void io_commit_cqring(struct io_ring_ctx *ctx)
1067{
1068 struct io_kiocb *req;
1069
Jens Axboe5262f562019-09-17 12:26:57 -06001070 while ((req = io_get_timeout_req(ctx)) != NULL)
1071 io_kill_timeout(req);
1072
Jens Axboede0617e2019-04-06 21:51:27 -06001073 __io_commit_cqring(ctx);
1074
Pavel Begunkov87987892020-01-18 01:22:30 +03001075 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001076 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001077}
1078
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1080{
Hristo Venev75b28af2019-08-26 17:23:46 +00001081 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082 unsigned tail;
1083
1084 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001085 /*
1086 * writes to the cq entry need to come after reading head; the
1087 * control dependency is enough as we're using WRITE_ONCE to
1088 * fill the cq entry
1089 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001090 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091 return NULL;
1092
1093 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001094 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095}
1096
Jens Axboef2842ab2020-01-08 11:04:00 -07001097static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1098{
Jens Axboef0b493e2020-02-01 21:30:11 -07001099 if (!ctx->cq_ev_fd)
1100 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001101 if (!ctx->eventfd_async)
1102 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001103 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001104}
1105
Jens Axboeb41e9852020-02-17 09:52:41 -07001106static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001107{
1108 if (waitqueue_active(&ctx->wait))
1109 wake_up(&ctx->wait);
1110 if (waitqueue_active(&ctx->sqo_wait))
1111 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001112 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001113 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001114}
1115
Jens Axboec4a2ed72019-11-21 21:01:26 -07001116/* Returns true if there are no backlogged entries after the flush */
1117static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001118{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001119 struct io_rings *rings = ctx->rings;
1120 struct io_uring_cqe *cqe;
1121 struct io_kiocb *req;
1122 unsigned long flags;
1123 LIST_HEAD(list);
1124
1125 if (!force) {
1126 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001127 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001128 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1129 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001130 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001131 }
1132
1133 spin_lock_irqsave(&ctx->completion_lock, flags);
1134
1135 /* if force is set, the ring is going away. always drop after that */
1136 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001137 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001138
Jens Axboec4a2ed72019-11-21 21:01:26 -07001139 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001140 while (!list_empty(&ctx->cq_overflow_list)) {
1141 cqe = io_get_cqring(ctx);
1142 if (!cqe && !force)
1143 break;
1144
1145 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1146 list);
1147 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001148 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001149 if (cqe) {
1150 WRITE_ONCE(cqe->user_data, req->user_data);
1151 WRITE_ONCE(cqe->res, req->result);
1152 WRITE_ONCE(cqe->flags, 0);
1153 } else {
1154 WRITE_ONCE(ctx->rings->cq_overflow,
1155 atomic_inc_return(&ctx->cached_cq_overflow));
1156 }
1157 }
1158
1159 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001160 if (cqe) {
1161 clear_bit(0, &ctx->sq_check_overflow);
1162 clear_bit(0, &ctx->cq_check_overflow);
1163 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001164 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1165 io_cqring_ev_posted(ctx);
1166
1167 while (!list_empty(&list)) {
1168 req = list_first_entry(&list, struct io_kiocb, list);
1169 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001170 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001171 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001172
1173 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001174}
1175
Jens Axboe78e19bb2019-11-06 15:21:34 -07001176static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001179 struct io_uring_cqe *cqe;
1180
Jens Axboe78e19bb2019-11-06 15:21:34 -07001181 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001182
Jens Axboe2b188cc2019-01-07 10:46:33 -07001183 /*
1184 * If we can't get a cq entry, userspace overflowed the
1185 * submission (by quite a lot). Increment the overflow count in
1186 * the ring.
1187 */
1188 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001189 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001190 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001191 WRITE_ONCE(cqe->res, res);
1192 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001193 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001194 WRITE_ONCE(ctx->rings->cq_overflow,
1195 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001196 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001197 if (list_empty(&ctx->cq_overflow_list)) {
1198 set_bit(0, &ctx->sq_check_overflow);
1199 set_bit(0, &ctx->cq_check_overflow);
1200 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001201 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001202 refcount_inc(&req->refs);
1203 req->result = res;
1204 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001205 }
1206}
1207
Jens Axboe78e19bb2019-11-06 15:21:34 -07001208static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001209{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001210 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001211 unsigned long flags;
1212
1213 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001214 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215 io_commit_cqring(ctx);
1216 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1217
Jens Axboe8c838782019-03-12 15:48:16 -06001218 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219}
1220
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001221static inline bool io_is_fallback_req(struct io_kiocb *req)
1222{
1223 return req == (struct io_kiocb *)
1224 ((unsigned long) req->ctx->fallback_req & ~1UL);
1225}
1226
1227static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1228{
1229 struct io_kiocb *req;
1230
1231 req = ctx->fallback_req;
1232 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1233 return req;
1234
1235 return NULL;
1236}
1237
Jens Axboe2579f912019-01-09 09:10:43 -07001238static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1239 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001240{
Jens Axboefd6fab22019-03-14 16:30:06 -06001241 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242 struct io_kiocb *req;
1243
Jens Axboe2579f912019-01-09 09:10:43 -07001244 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001245 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001246 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001247 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001248 } else if (!state->free_reqs) {
1249 size_t sz;
1250 int ret;
1251
1252 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001253 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1254
1255 /*
1256 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1257 * retry single alloc to be on the safe side.
1258 */
1259 if (unlikely(ret <= 0)) {
1260 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1261 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001262 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001263 ret = 1;
1264 }
Jens Axboe2579f912019-01-09 09:10:43 -07001265 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001266 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001267 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001268 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001269 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270 }
1271
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001272got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001273 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001274 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001275 req->ctx = ctx;
1276 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001277 /* one is dropped after submission, the other at completion */
1278 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001279 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001280 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001281 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001282fallback:
1283 req = io_get_fallback_req(ctx);
1284 if (req)
1285 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001286 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287 return NULL;
1288}
1289
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001290static inline void io_put_file(struct io_kiocb *req, struct file *file,
1291 bool fixed)
1292{
1293 if (fixed)
1294 percpu_ref_put(&req->ctx->file_data->refs);
1295 else
1296 fput(file);
1297}
1298
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001299static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001300{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001301 if (likely(!io_is_fallback_req(req)))
1302 kmem_cache_free(req_cachep, req);
1303 else
1304 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1305}
1306
Jens Axboec6ca97b302019-12-28 12:11:08 -07001307static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001309 if (req->flags & REQ_F_NEED_CLEANUP)
1310 io_cleanup_req(req);
1311
YueHaibing96fd84d2020-01-07 22:22:44 +08001312 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001313 if (req->file)
1314 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001315
1316 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001317}
1318
1319static void __io_free_req(struct io_kiocb *req)
1320{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001321 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001322
Jens Axboefcb323c2019-10-24 12:39:47 -06001323 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001324 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001325 unsigned long flags;
1326
1327 spin_lock_irqsave(&ctx->inflight_lock, flags);
1328 list_del(&req->inflight_entry);
1329 if (waitqueue_active(&ctx->inflight_wait))
1330 wake_up(&ctx->inflight_wait);
1331 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1332 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001333
1334 percpu_ref_put(&req->ctx->refs);
1335 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001336}
1337
Jens Axboec6ca97b302019-12-28 12:11:08 -07001338struct req_batch {
1339 void *reqs[IO_IOPOLL_BATCH];
1340 int to_free;
1341 int need_iter;
1342};
1343
1344static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1345{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001346 int fixed_refs = rb->to_free;
1347
Jens Axboec6ca97b302019-12-28 12:11:08 -07001348 if (!rb->to_free)
1349 return;
1350 if (rb->need_iter) {
1351 int i, inflight = 0;
1352 unsigned long flags;
1353
Jens Axboe10fef4b2020-01-09 07:52:28 -07001354 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001355 for (i = 0; i < rb->to_free; i++) {
1356 struct io_kiocb *req = rb->reqs[i];
1357
Jens Axboe10fef4b2020-01-09 07:52:28 -07001358 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001359 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001360 fixed_refs++;
1361 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001362 if (req->flags & REQ_F_INFLIGHT)
1363 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001364 __io_req_aux_free(req);
1365 }
1366 if (!inflight)
1367 goto do_free;
1368
1369 spin_lock_irqsave(&ctx->inflight_lock, flags);
1370 for (i = 0; i < rb->to_free; i++) {
1371 struct io_kiocb *req = rb->reqs[i];
1372
Jens Axboe10fef4b2020-01-09 07:52:28 -07001373 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001374 list_del(&req->inflight_entry);
1375 if (!--inflight)
1376 break;
1377 }
1378 }
1379 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1380
1381 if (waitqueue_active(&ctx->inflight_wait))
1382 wake_up(&ctx->inflight_wait);
1383 }
1384do_free:
1385 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001386 if (fixed_refs)
1387 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001388 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001389 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001390}
1391
Jackie Liua197f662019-11-08 08:09:12 -07001392static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001393{
Jackie Liua197f662019-11-08 08:09:12 -07001394 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001395 int ret;
1396
Jens Axboe2d283902019-12-04 11:08:05 -07001397 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001398 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001399 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001400 io_commit_cqring(ctx);
1401 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001402 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001403 return true;
1404 }
1405
1406 return false;
1407}
1408
Jens Axboeba816ad2019-09-28 11:36:45 -06001409static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001410{
Jens Axboe2665abf2019-11-05 12:40:47 -07001411 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001412 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001413
Jens Axboe4d7dd462019-11-20 13:03:52 -07001414 /* Already got next link */
1415 if (req->flags & REQ_F_LINK_NEXT)
1416 return;
1417
Jens Axboe9e645e112019-05-10 16:07:28 -06001418 /*
1419 * The list should never be empty when we are called here. But could
1420 * potentially happen if the chain is messed up, check to be on the
1421 * safe side.
1422 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001423 while (!list_empty(&req->link_list)) {
1424 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1425 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001426
Pavel Begunkov44932332019-12-05 16:16:35 +03001427 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1428 (nxt->flags & REQ_F_TIMEOUT))) {
1429 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001430 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001431 req->flags &= ~REQ_F_LINK_TIMEOUT;
1432 continue;
1433 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001434
Pavel Begunkov44932332019-12-05 16:16:35 +03001435 list_del_init(&req->link_list);
1436 if (!list_empty(&nxt->link_list))
1437 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001438 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001439 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001440 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001441
Jens Axboe4d7dd462019-11-20 13:03:52 -07001442 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001443 if (wake_ev)
1444 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001445}
1446
1447/*
1448 * Called if REQ_F_LINK is set, and we fail the head request
1449 */
1450static void io_fail_links(struct io_kiocb *req)
1451{
Jens Axboe2665abf2019-11-05 12:40:47 -07001452 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 unsigned long flags;
1454
1455 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001456
1457 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001458 struct io_kiocb *link = list_first_entry(&req->link_list,
1459 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001460
Pavel Begunkov44932332019-12-05 16:16:35 +03001461 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001462 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001463
1464 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001465 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001466 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001467 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001468 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001469 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001470 }
Jens Axboe5d960722019-11-19 15:31:28 -07001471 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001472 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001473
1474 io_commit_cqring(ctx);
1475 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1476 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001477}
1478
Jens Axboe4d7dd462019-11-20 13:03:52 -07001479static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001480{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001481 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001482 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001483
Jens Axboe9e645e112019-05-10 16:07:28 -06001484 /*
1485 * If LINK is set, we have dependent requests in this chain. If we
1486 * didn't fail this request, queue the first one up, moving any other
1487 * dependencies to the next request. In case of failure, fail the rest
1488 * of the chain.
1489 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001490 if (req->flags & REQ_F_FAIL_LINK) {
1491 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001492 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1493 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001494 struct io_ring_ctx *ctx = req->ctx;
1495 unsigned long flags;
1496
1497 /*
1498 * If this is a timeout link, we could be racing with the
1499 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001500 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001501 */
1502 spin_lock_irqsave(&ctx->completion_lock, flags);
1503 io_req_link_next(req, nxt);
1504 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1505 } else {
1506 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001507 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001508}
Jens Axboe9e645e112019-05-10 16:07:28 -06001509
Jackie Liuc69f8db2019-11-09 11:00:08 +08001510static void io_free_req(struct io_kiocb *req)
1511{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001512 struct io_kiocb *nxt = NULL;
1513
1514 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001515 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001516
1517 if (nxt)
1518 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001519}
1520
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001521static void io_link_work_cb(struct io_wq_work **workptr)
1522{
1523 struct io_wq_work *work = *workptr;
1524 struct io_kiocb *link = work->data;
1525
1526 io_queue_linked_timeout(link);
1527 io_wq_submit_work(workptr);
1528}
1529
1530static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1531{
1532 struct io_kiocb *link;
1533
1534 *workptr = &nxt->work;
1535 link = io_prep_linked_timeout(nxt);
1536 if (link) {
1537 nxt->work.func = io_link_work_cb;
1538 nxt->work.data = link;
1539 }
1540}
1541
Jens Axboeba816ad2019-09-28 11:36:45 -06001542/*
1543 * Drop reference to request, return next in chain (if there is one) if this
1544 * was the last reference to this request.
1545 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001546__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001547static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001548{
Jens Axboe2a44f462020-02-25 13:25:41 -07001549 if (refcount_dec_and_test(&req->refs)) {
1550 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001551 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001552 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553}
1554
Jens Axboe2b188cc2019-01-07 10:46:33 -07001555static void io_put_req(struct io_kiocb *req)
1556{
Jens Axboedef596e2019-01-09 08:59:42 -07001557 if (refcount_dec_and_test(&req->refs))
1558 io_free_req(req);
1559}
1560
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001561static void io_put_req_async_completion(struct io_kiocb *req,
1562 struct io_wq_work **workptr)
1563{
1564 /*
1565 * It's in an io-wq worker, so there always should be at least
1566 * one reference, which will be dropped in io_put_work() just
1567 * after the current handler returns.
1568 *
1569 * It also means, that if the counter dropped to 1, then there is
1570 * no asynchronous users left, so it's safe to steal the next work.
1571 */
1572 refcount_dec(&req->refs);
1573 if (refcount_read(&req->refs) == 1) {
1574 struct io_kiocb *nxt = NULL;
1575
1576 io_req_find_next(req, &nxt);
1577 if (nxt)
1578 io_wq_assign_next(workptr, nxt);
1579 }
1580}
1581
Jens Axboe978db572019-11-14 22:39:04 -07001582/*
1583 * Must only be used if we don't need to care about links, usually from
1584 * within the completion handling itself.
1585 */
1586static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001587{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001588 /* drop both submit and complete references */
1589 if (refcount_sub_and_test(2, &req->refs))
1590 __io_free_req(req);
1591}
1592
Jens Axboe978db572019-11-14 22:39:04 -07001593static void io_double_put_req(struct io_kiocb *req)
1594{
1595 /* drop both submit and complete references */
1596 if (refcount_sub_and_test(2, &req->refs))
1597 io_free_req(req);
1598}
1599
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001600static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001601{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001602 struct io_rings *rings = ctx->rings;
1603
Jens Axboead3eb2c2019-12-18 17:12:20 -07001604 if (test_bit(0, &ctx->cq_check_overflow)) {
1605 /*
1606 * noflush == true is from the waitqueue handler, just ensure
1607 * we wake up the task, and the next invocation will flush the
1608 * entries. We cannot safely to it from here.
1609 */
1610 if (noflush && !list_empty(&ctx->cq_overflow_list))
1611 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001612
Jens Axboead3eb2c2019-12-18 17:12:20 -07001613 io_cqring_overflow_flush(ctx, false);
1614 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001615
Jens Axboea3a0e432019-08-20 11:03:11 -06001616 /* See comment at the top of this file */
1617 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001618 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001619}
1620
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001621static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1622{
1623 struct io_rings *rings = ctx->rings;
1624
1625 /* make sure SQ entry isn't read before tail */
1626 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1627}
1628
Jens Axboe8237e042019-12-28 10:48:22 -07001629static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001630{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001631 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1632 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001633
Jens Axboec6ca97b302019-12-28 12:11:08 -07001634 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1635 rb->need_iter++;
1636
1637 rb->reqs[rb->to_free++] = req;
1638 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1639 io_free_req_many(req->ctx, rb);
1640 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001641}
1642
Jens Axboedef596e2019-01-09 08:59:42 -07001643/*
1644 * Find and free completed poll iocbs
1645 */
1646static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1647 struct list_head *done)
1648{
Jens Axboe8237e042019-12-28 10:48:22 -07001649 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001650 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001651
Jens Axboec6ca97b302019-12-28 12:11:08 -07001652 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001653 while (!list_empty(done)) {
1654 req = list_first_entry(done, struct io_kiocb, list);
1655 list_del(&req->list);
1656
Jens Axboe78e19bb2019-11-06 15:21:34 -07001657 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001658 (*nr_events)++;
1659
Jens Axboe8237e042019-12-28 10:48:22 -07001660 if (refcount_dec_and_test(&req->refs) &&
1661 !io_req_multi_free(&rb, req))
1662 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001663 }
Jens Axboedef596e2019-01-09 08:59:42 -07001664
Jens Axboe09bb8392019-03-13 12:39:28 -06001665 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001666 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001667}
1668
1669static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1670 long min)
1671{
1672 struct io_kiocb *req, *tmp;
1673 LIST_HEAD(done);
1674 bool spin;
1675 int ret;
1676
1677 /*
1678 * Only spin for completions if we don't have multiple devices hanging
1679 * off our complete list, and we're under the requested amount.
1680 */
1681 spin = !ctx->poll_multi_file && *nr_events < min;
1682
1683 ret = 0;
1684 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001685 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001686
1687 /*
1688 * Move completed entries to our local list. If we find a
1689 * request that requires polling, break out and complete
1690 * the done list first, if we have entries there.
1691 */
1692 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1693 list_move_tail(&req->list, &done);
1694 continue;
1695 }
1696 if (!list_empty(&done))
1697 break;
1698
1699 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1700 if (ret < 0)
1701 break;
1702
1703 if (ret && spin)
1704 spin = false;
1705 ret = 0;
1706 }
1707
1708 if (!list_empty(&done))
1709 io_iopoll_complete(ctx, nr_events, &done);
1710
1711 return ret;
1712}
1713
1714/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001715 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001716 * non-spinning poll check - we'll still enter the driver poll loop, but only
1717 * as a non-spinning completion check.
1718 */
1719static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1720 long min)
1721{
Jens Axboe08f54392019-08-21 22:19:11 -06001722 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001723 int ret;
1724
1725 ret = io_do_iopoll(ctx, nr_events, min);
1726 if (ret < 0)
1727 return ret;
1728 if (!min || *nr_events >= min)
1729 return 0;
1730 }
1731
1732 return 1;
1733}
1734
1735/*
1736 * We can't just wait for polled events to come to us, we have to actively
1737 * find and complete them.
1738 */
1739static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1740{
1741 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1742 return;
1743
1744 mutex_lock(&ctx->uring_lock);
1745 while (!list_empty(&ctx->poll_list)) {
1746 unsigned int nr_events = 0;
1747
1748 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001749
1750 /*
1751 * Ensure we allow local-to-the-cpu processing to take place,
1752 * in this case we need to ensure that we reap all events.
1753 */
1754 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001755 }
1756 mutex_unlock(&ctx->uring_lock);
1757}
1758
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001759static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1760 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001761{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001762 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001763
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001764 /*
1765 * We disallow the app entering submit/complete with polling, but we
1766 * still need to lock the ring to prevent racing with polled issue
1767 * that got punted to a workqueue.
1768 */
1769 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001770 do {
1771 int tmin = 0;
1772
Jens Axboe500f9fb2019-08-19 12:15:59 -06001773 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001774 * Don't enter poll loop if we already have events pending.
1775 * If we do, we can potentially be spinning for commands that
1776 * already triggered a CQE (eg in error).
1777 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001778 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001779 break;
1780
1781 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001782 * If a submit got punted to a workqueue, we can have the
1783 * application entering polling for a command before it gets
1784 * issued. That app will hold the uring_lock for the duration
1785 * of the poll right here, so we need to take a breather every
1786 * now and then to ensure that the issue has a chance to add
1787 * the poll to the issued list. Otherwise we can spin here
1788 * forever, while the workqueue is stuck trying to acquire the
1789 * very same mutex.
1790 */
1791 if (!(++iters & 7)) {
1792 mutex_unlock(&ctx->uring_lock);
1793 mutex_lock(&ctx->uring_lock);
1794 }
1795
Jens Axboedef596e2019-01-09 08:59:42 -07001796 if (*nr_events < min)
1797 tmin = min - *nr_events;
1798
1799 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1800 if (ret <= 0)
1801 break;
1802 ret = 0;
1803 } while (min && !*nr_events && !need_resched());
1804
Jens Axboe500f9fb2019-08-19 12:15:59 -06001805 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001806 return ret;
1807}
1808
Jens Axboe491381ce2019-10-17 09:20:46 -06001809static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001810{
Jens Axboe491381ce2019-10-17 09:20:46 -06001811 /*
1812 * Tell lockdep we inherited freeze protection from submission
1813 * thread.
1814 */
1815 if (req->flags & REQ_F_ISREG) {
1816 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001817
Jens Axboe491381ce2019-10-17 09:20:46 -06001818 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001819 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001820 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001821}
1822
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001823static inline void req_set_fail_links(struct io_kiocb *req)
1824{
1825 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1826 req->flags |= REQ_F_FAIL_LINK;
1827}
1828
Jens Axboeba816ad2019-09-28 11:36:45 -06001829static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830{
Jens Axboe9adbd452019-12-20 08:45:55 -07001831 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001832
Jens Axboe491381ce2019-10-17 09:20:46 -06001833 if (kiocb->ki_flags & IOCB_WRITE)
1834 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001836 if (res != req->result)
1837 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001838 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001839}
1840
1841static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1842{
Jens Axboe9adbd452019-12-20 08:45:55 -07001843 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001844
1845 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001846 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001847}
1848
Jens Axboedef596e2019-01-09 08:59:42 -07001849static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1850{
Jens Axboe9adbd452019-12-20 08:45:55 -07001851 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001852
Jens Axboe491381ce2019-10-17 09:20:46 -06001853 if (kiocb->ki_flags & IOCB_WRITE)
1854 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001855
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001856 if (res != req->result)
1857 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001858 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001859 if (res != -EAGAIN)
1860 req->flags |= REQ_F_IOPOLL_COMPLETED;
1861}
1862
1863/*
1864 * After the iocb has been issued, it's safe to be found on the poll list.
1865 * Adding the kiocb to the list AFTER submission ensures that we don't
1866 * find it from a io_iopoll_getevents() thread before the issuer is done
1867 * accessing the kiocb cookie.
1868 */
1869static void io_iopoll_req_issued(struct io_kiocb *req)
1870{
1871 struct io_ring_ctx *ctx = req->ctx;
1872
1873 /*
1874 * Track whether we have multiple files in our lists. This will impact
1875 * how we do polling eventually, not spinning if we're on potentially
1876 * different devices.
1877 */
1878 if (list_empty(&ctx->poll_list)) {
1879 ctx->poll_multi_file = false;
1880 } else if (!ctx->poll_multi_file) {
1881 struct io_kiocb *list_req;
1882
1883 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1884 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001885 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001886 ctx->poll_multi_file = true;
1887 }
1888
1889 /*
1890 * For fast devices, IO may have already completed. If it has, add
1891 * it to the front so we find it first.
1892 */
1893 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1894 list_add(&req->list, &ctx->poll_list);
1895 else
1896 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001897
1898 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1899 wq_has_sleeper(&ctx->sqo_wait))
1900 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001901}
1902
Jens Axboe3d6770f2019-04-13 11:50:54 -06001903static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001904{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001905 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001906 int diff = state->has_refs - state->used_refs;
1907
1908 if (diff)
1909 fput_many(state->file, diff);
1910 state->file = NULL;
1911 }
1912}
1913
1914/*
1915 * Get as many references to a file as we have IOs left in this submission,
1916 * assuming most submissions are for one file, or at least that each file
1917 * has more than one submission.
1918 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001919static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001920{
1921 if (!state)
1922 return fget(fd);
1923
1924 if (state->file) {
1925 if (state->fd == fd) {
1926 state->used_refs++;
1927 state->ios_left--;
1928 return state->file;
1929 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001930 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001931 }
1932 state->file = fget_many(fd, state->ios_left);
1933 if (!state->file)
1934 return NULL;
1935
1936 state->fd = fd;
1937 state->has_refs = state->ios_left;
1938 state->used_refs = 1;
1939 state->ios_left--;
1940 return state->file;
1941}
1942
Jens Axboe2b188cc2019-01-07 10:46:33 -07001943/*
1944 * If we tracked the file through the SCM inflight mechanism, we could support
1945 * any file. For now, just ensure that anything potentially problematic is done
1946 * inline.
1947 */
1948static bool io_file_supports_async(struct file *file)
1949{
1950 umode_t mode = file_inode(file)->i_mode;
1951
Jens Axboe10d59342019-12-09 20:16:22 -07001952 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001953 return true;
1954 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1955 return true;
1956
1957 return false;
1958}
1959
Jens Axboe3529d8c2019-12-19 18:24:38 -07001960static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1961 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001962{
Jens Axboedef596e2019-01-09 08:59:42 -07001963 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001964 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001965 unsigned ioprio;
1966 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967
Jens Axboe491381ce2019-10-17 09:20:46 -06001968 if (S_ISREG(file_inode(req->file)->i_mode))
1969 req->flags |= REQ_F_ISREG;
1970
Jens Axboe2b188cc2019-01-07 10:46:33 -07001971 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001972 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1973 req->flags |= REQ_F_CUR_POS;
1974 kiocb->ki_pos = req->file->f_pos;
1975 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001977 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1978 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1979 if (unlikely(ret))
1980 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001981
1982 ioprio = READ_ONCE(sqe->ioprio);
1983 if (ioprio) {
1984 ret = ioprio_check_cap(ioprio);
1985 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001986 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001987
1988 kiocb->ki_ioprio = ioprio;
1989 } else
1990 kiocb->ki_ioprio = get_current_ioprio();
1991
Stefan Bühler8449eed2019-04-27 20:34:19 +02001992 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001993 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1994 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001995 req->flags |= REQ_F_NOWAIT;
1996
1997 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001998 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001999
Jens Axboedef596e2019-01-09 08:59:42 -07002000 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002001 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2002 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002003 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002004
Jens Axboedef596e2019-01-09 08:59:42 -07002005 kiocb->ki_flags |= IOCB_HIPRI;
2006 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002007 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002008 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002009 if (kiocb->ki_flags & IOCB_HIPRI)
2010 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002011 kiocb->ki_complete = io_complete_rw;
2012 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002013
Jens Axboe3529d8c2019-12-19 18:24:38 -07002014 req->rw.addr = READ_ONCE(sqe->addr);
2015 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07002016 /* we own ->private, reuse it for the buffer index */
2017 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002018 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002019 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002020}
2021
2022static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2023{
2024 switch (ret) {
2025 case -EIOCBQUEUED:
2026 break;
2027 case -ERESTARTSYS:
2028 case -ERESTARTNOINTR:
2029 case -ERESTARTNOHAND:
2030 case -ERESTART_RESTARTBLOCK:
2031 /*
2032 * We can't just restart the syscall, since previously
2033 * submitted sqes may already be in progress. Just fail this
2034 * IO with EINTR.
2035 */
2036 ret = -EINTR;
2037 /* fall through */
2038 default:
2039 kiocb->ki_complete(kiocb, ret, 0);
2040 }
2041}
2042
Pavel Begunkov014db002020-03-03 21:33:12 +03002043static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002044{
Jens Axboeba042912019-12-25 16:33:42 -07002045 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2046
2047 if (req->flags & REQ_F_CUR_POS)
2048 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002049 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002050 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002051 else
2052 io_rw_done(kiocb, ret);
2053}
2054
Jens Axboe9adbd452019-12-20 08:45:55 -07002055static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002056 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002057{
Jens Axboe9adbd452019-12-20 08:45:55 -07002058 struct io_ring_ctx *ctx = req->ctx;
2059 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002060 struct io_mapped_ubuf *imu;
2061 unsigned index, buf_index;
2062 size_t offset;
2063 u64 buf_addr;
2064
2065 /* attempt to use fixed buffers without having provided iovecs */
2066 if (unlikely(!ctx->user_bufs))
2067 return -EFAULT;
2068
Jens Axboe9adbd452019-12-20 08:45:55 -07002069 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002070 if (unlikely(buf_index >= ctx->nr_user_bufs))
2071 return -EFAULT;
2072
2073 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2074 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002075 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002076
2077 /* overflow */
2078 if (buf_addr + len < buf_addr)
2079 return -EFAULT;
2080 /* not inside the mapped region */
2081 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2082 return -EFAULT;
2083
2084 /*
2085 * May not be a start of buffer, set size appropriately
2086 * and advance us to the beginning.
2087 */
2088 offset = buf_addr - imu->ubuf;
2089 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002090
2091 if (offset) {
2092 /*
2093 * Don't use iov_iter_advance() here, as it's really slow for
2094 * using the latter parts of a big fixed buffer - it iterates
2095 * over each segment manually. We can cheat a bit here, because
2096 * we know that:
2097 *
2098 * 1) it's a BVEC iter, we set it up
2099 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2100 * first and last bvec
2101 *
2102 * So just find our index, and adjust the iterator afterwards.
2103 * If the offset is within the first bvec (or the whole first
2104 * bvec, just use iov_iter_advance(). This makes it easier
2105 * since we can just skip the first segment, which may not
2106 * be PAGE_SIZE aligned.
2107 */
2108 const struct bio_vec *bvec = imu->bvec;
2109
2110 if (offset <= bvec->bv_len) {
2111 iov_iter_advance(iter, offset);
2112 } else {
2113 unsigned long seg_skip;
2114
2115 /* skip first vec */
2116 offset -= bvec->bv_len;
2117 seg_skip = 1 + (offset >> PAGE_SHIFT);
2118
2119 iter->bvec = bvec + seg_skip;
2120 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002121 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002122 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002123 }
2124 }
2125
Jens Axboe5e559562019-11-13 16:12:46 -07002126 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002127}
2128
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002129static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2130 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002131{
Jens Axboe9adbd452019-12-20 08:45:55 -07002132 void __user *buf = u64_to_user_ptr(req->rw.addr);
2133 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002134 u8 opcode;
2135
Jens Axboed625c6e2019-12-17 19:53:05 -07002136 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002137 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002138 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002139 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002140 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002141
Jens Axboe9adbd452019-12-20 08:45:55 -07002142 /* buffer index only valid with fixed read/write */
2143 if (req->rw.kiocb.private)
2144 return -EINVAL;
2145
Jens Axboe3a6820f2019-12-22 15:19:35 -07002146 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2147 ssize_t ret;
2148 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2149 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002150 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002151 }
2152
Jens Axboef67676d2019-12-02 11:03:47 -07002153 if (req->io) {
2154 struct io_async_rw *iorw = &req->io->rw;
2155
2156 *iovec = iorw->iov;
2157 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2158 if (iorw->iov == iorw->fast_iov)
2159 *iovec = NULL;
2160 return iorw->size;
2161 }
2162
Jens Axboe2b188cc2019-01-07 10:46:33 -07002163#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002164 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002165 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2166 iovec, iter);
2167#endif
2168
2169 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2170}
2171
Jens Axboe32960612019-09-23 11:05:34 -06002172/*
2173 * For files that don't have ->read_iter() and ->write_iter(), handle them
2174 * by looping over ->read() or ->write() manually.
2175 */
2176static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2177 struct iov_iter *iter)
2178{
2179 ssize_t ret = 0;
2180
2181 /*
2182 * Don't support polled IO through this interface, and we can't
2183 * support non-blocking either. For the latter, this just causes
2184 * the kiocb to be handled from an async context.
2185 */
2186 if (kiocb->ki_flags & IOCB_HIPRI)
2187 return -EOPNOTSUPP;
2188 if (kiocb->ki_flags & IOCB_NOWAIT)
2189 return -EAGAIN;
2190
2191 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002192 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002193 ssize_t nr;
2194
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002195 if (!iov_iter_is_bvec(iter)) {
2196 iovec = iov_iter_iovec(iter);
2197 } else {
2198 /* fixed buffers import bvec */
2199 iovec.iov_base = kmap(iter->bvec->bv_page)
2200 + iter->iov_offset;
2201 iovec.iov_len = min(iter->count,
2202 iter->bvec->bv_len - iter->iov_offset);
2203 }
2204
Jens Axboe32960612019-09-23 11:05:34 -06002205 if (rw == READ) {
2206 nr = file->f_op->read(file, iovec.iov_base,
2207 iovec.iov_len, &kiocb->ki_pos);
2208 } else {
2209 nr = file->f_op->write(file, iovec.iov_base,
2210 iovec.iov_len, &kiocb->ki_pos);
2211 }
2212
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002213 if (iov_iter_is_bvec(iter))
2214 kunmap(iter->bvec->bv_page);
2215
Jens Axboe32960612019-09-23 11:05:34 -06002216 if (nr < 0) {
2217 if (!ret)
2218 ret = nr;
2219 break;
2220 }
2221 ret += nr;
2222 if (nr != iovec.iov_len)
2223 break;
2224 iov_iter_advance(iter, nr);
2225 }
2226
2227 return ret;
2228}
2229
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002230static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002231 struct iovec *iovec, struct iovec *fast_iov,
2232 struct iov_iter *iter)
2233{
2234 req->io->rw.nr_segs = iter->nr_segs;
2235 req->io->rw.size = io_size;
2236 req->io->rw.iov = iovec;
2237 if (!req->io->rw.iov) {
2238 req->io->rw.iov = req->io->rw.fast_iov;
2239 memcpy(req->io->rw.iov, fast_iov,
2240 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002241 } else {
2242 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002243 }
2244}
2245
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002246static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002247{
Jens Axboed3656342019-12-18 09:50:26 -07002248 if (!io_op_defs[req->opcode].async_ctx)
2249 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002250 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002251 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002252}
2253
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002254static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2255 struct iovec *iovec, struct iovec *fast_iov,
2256 struct iov_iter *iter)
2257{
Jens Axboe980ad262020-01-24 23:08:54 -07002258 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002259 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002260 if (!req->io) {
2261 if (io_alloc_async_ctx(req))
2262 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002263
Jens Axboe5d204bc2020-01-31 12:06:52 -07002264 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2265 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002266 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002267}
2268
Jens Axboe3529d8c2019-12-19 18:24:38 -07002269static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2270 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002271{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002272 struct io_async_ctx *io;
2273 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002274 ssize_t ret;
2275
Jens Axboe3529d8c2019-12-19 18:24:38 -07002276 ret = io_prep_rw(req, sqe, force_nonblock);
2277 if (ret)
2278 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002279
Jens Axboe3529d8c2019-12-19 18:24:38 -07002280 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2281 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002282
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002283 /* either don't need iovec imported or already have it */
2284 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002285 return 0;
2286
2287 io = req->io;
2288 io->rw.iov = io->rw.fast_iov;
2289 req->io = NULL;
2290 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2291 req->io = io;
2292 if (ret < 0)
2293 return ret;
2294
2295 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2296 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002297}
2298
Pavel Begunkov014db002020-03-03 21:33:12 +03002299static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002300{
2301 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002302 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002304 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002305 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002306
Jens Axboe3529d8c2019-12-19 18:24:38 -07002307 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002308 if (ret < 0)
2309 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002310
Jens Axboefd6c2e42019-12-18 12:19:41 -07002311 /* Ensure we clear previously set non-block flag */
2312 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002313 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002314
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002315 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002316 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002317 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002318 req->result = io_size;
2319
2320 /*
2321 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2322 * we know to async punt it even if it was opened O_NONBLOCK
2323 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002324 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002325 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002326
Jens Axboe31b51512019-01-18 22:56:34 -07002327 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002328 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002329 if (!ret) {
2330 ssize_t ret2;
2331
Jens Axboe9adbd452019-12-20 08:45:55 -07002332 if (req->file->f_op->read_iter)
2333 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002334 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002335 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002336
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002337 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002338 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002339 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002340 } else {
2341copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002342 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002343 inline_vecs, &iter);
2344 if (ret)
2345 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002346 /* any defer here is final, must blocking retry */
2347 if (!(req->flags & REQ_F_NOWAIT))
2348 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002349 return -EAGAIN;
2350 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351 }
Jens Axboef67676d2019-12-02 11:03:47 -07002352out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002353 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002354 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002355 return ret;
2356}
2357
Jens Axboe3529d8c2019-12-19 18:24:38 -07002358static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2359 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002360{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002361 struct io_async_ctx *io;
2362 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002363 ssize_t ret;
2364
Jens Axboe3529d8c2019-12-19 18:24:38 -07002365 ret = io_prep_rw(req, sqe, force_nonblock);
2366 if (ret)
2367 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002368
Jens Axboe3529d8c2019-12-19 18:24:38 -07002369 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2370 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002371
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002372 /* either don't need iovec imported or already have it */
2373 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002374 return 0;
2375
2376 io = req->io;
2377 io->rw.iov = io->rw.fast_iov;
2378 req->io = NULL;
2379 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2380 req->io = io;
2381 if (ret < 0)
2382 return ret;
2383
2384 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2385 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002386}
2387
Pavel Begunkov014db002020-03-03 21:33:12 +03002388static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002389{
2390 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002391 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002393 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002394 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002395
Jens Axboe3529d8c2019-12-19 18:24:38 -07002396 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002397 if (ret < 0)
2398 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002399
Jens Axboefd6c2e42019-12-18 12:19:41 -07002400 /* Ensure we clear previously set non-block flag */
2401 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002402 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002403
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002404 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002405 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002406 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002407 req->result = io_size;
2408
2409 /*
2410 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2411 * we know to async punt it even if it was opened O_NONBLOCK
2412 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002413 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002414 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002415
Jens Axboe10d59342019-12-09 20:16:22 -07002416 /* file path doesn't support NOWAIT for non-direct_IO */
2417 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2418 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002419 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002420
Jens Axboe31b51512019-01-18 22:56:34 -07002421 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002422 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002423 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002424 ssize_t ret2;
2425
Jens Axboe2b188cc2019-01-07 10:46:33 -07002426 /*
2427 * Open-code file_start_write here to grab freeze protection,
2428 * which will be released by another thread in
2429 * io_complete_rw(). Fool lockdep by telling it the lock got
2430 * released so that it doesn't complain about the held lock when
2431 * we return to userspace.
2432 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002433 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002434 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002435 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002436 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002437 SB_FREEZE_WRITE);
2438 }
2439 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002440
Jens Axboe9adbd452019-12-20 08:45:55 -07002441 if (req->file->f_op->write_iter)
2442 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002443 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002444 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002445 /*
2446 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2447 * retry them without IOCB_NOWAIT.
2448 */
2449 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2450 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002451 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002452 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002453 } else {
2454copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002455 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002456 inline_vecs, &iter);
2457 if (ret)
2458 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002459 /* any defer here is final, must blocking retry */
2460 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002461 return -EAGAIN;
2462 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002463 }
Jens Axboe31b51512019-01-18 22:56:34 -07002464out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002465 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002466 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002467 return ret;
2468}
2469
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002470static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2471{
2472 struct io_splice* sp = &req->splice;
2473 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2474 int ret;
2475
2476 if (req->flags & REQ_F_NEED_CLEANUP)
2477 return 0;
2478
2479 sp->file_in = NULL;
2480 sp->off_in = READ_ONCE(sqe->splice_off_in);
2481 sp->off_out = READ_ONCE(sqe->off);
2482 sp->len = READ_ONCE(sqe->len);
2483 sp->flags = READ_ONCE(sqe->splice_flags);
2484
2485 if (unlikely(sp->flags & ~valid_flags))
2486 return -EINVAL;
2487
2488 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2489 (sp->flags & SPLICE_F_FD_IN_FIXED));
2490 if (ret)
2491 return ret;
2492 req->flags |= REQ_F_NEED_CLEANUP;
2493
2494 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2495 req->work.flags |= IO_WQ_WORK_UNBOUND;
2496
2497 return 0;
2498}
2499
2500static bool io_splice_punt(struct file *file)
2501{
2502 if (get_pipe_info(file))
2503 return false;
2504 if (!io_file_supports_async(file))
2505 return true;
2506 return !(file->f_mode & O_NONBLOCK);
2507}
2508
Pavel Begunkov014db002020-03-03 21:33:12 +03002509static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002510{
2511 struct io_splice *sp = &req->splice;
2512 struct file *in = sp->file_in;
2513 struct file *out = sp->file_out;
2514 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2515 loff_t *poff_in, *poff_out;
2516 long ret;
2517
2518 if (force_nonblock) {
2519 if (io_splice_punt(in) || io_splice_punt(out))
2520 return -EAGAIN;
2521 flags |= SPLICE_F_NONBLOCK;
2522 }
2523
2524 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2525 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2526 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2527 if (force_nonblock && ret == -EAGAIN)
2528 return -EAGAIN;
2529
2530 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2531 req->flags &= ~REQ_F_NEED_CLEANUP;
2532
2533 io_cqring_add_event(req, ret);
2534 if (ret != sp->len)
2535 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002536 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002537 return 0;
2538}
2539
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540/*
2541 * IORING_OP_NOP just posts a completion event, nothing else.
2542 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002543static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002544{
2545 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002546
Jens Axboedef596e2019-01-09 08:59:42 -07002547 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2548 return -EINVAL;
2549
Jens Axboe78e19bb2019-11-06 15:21:34 -07002550 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002551 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002552 return 0;
2553}
2554
Jens Axboe3529d8c2019-12-19 18:24:38 -07002555static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002556{
Jens Axboe6b063142019-01-10 22:13:58 -07002557 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002558
Jens Axboe09bb8392019-03-13 12:39:28 -06002559 if (!req->file)
2560 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002561
Jens Axboe6b063142019-01-10 22:13:58 -07002562 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002563 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002564 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002565 return -EINVAL;
2566
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002567 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2568 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2569 return -EINVAL;
2570
2571 req->sync.off = READ_ONCE(sqe->off);
2572 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002573 return 0;
2574}
2575
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002576static bool io_req_cancelled(struct io_kiocb *req)
2577{
2578 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2579 req_set_fail_links(req);
2580 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkov594506f2020-03-03 21:33:11 +03002581 io_double_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002582 return true;
2583 }
2584
2585 return false;
2586}
2587
Pavel Begunkov014db002020-03-03 21:33:12 +03002588static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002589{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002590 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002591 int ret;
2592
Jens Axboe9adbd452019-12-20 08:45:55 -07002593 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002594 end > 0 ? end : LLONG_MAX,
2595 req->sync.flags & IORING_FSYNC_DATASYNC);
2596 if (ret < 0)
2597 req_set_fail_links(req);
2598 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002599 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002600}
2601
2602static void io_fsync_finish(struct io_wq_work **workptr)
2603{
2604 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002605
2606 if (io_req_cancelled(req))
2607 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002608 __io_fsync(req);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002609 io_put_req_async_completion(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002610}
2611
Pavel Begunkov014db002020-03-03 21:33:12 +03002612static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002613{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002614 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002615 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002616 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002617 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002618 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002619 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002620 return 0;
2621}
2622
Pavel Begunkov014db002020-03-03 21:33:12 +03002623static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002624{
Jens Axboed63d1b52019-12-10 10:38:56 -07002625 int ret;
2626
2627 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2628 req->sync.len);
2629 if (ret < 0)
2630 req_set_fail_links(req);
2631 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002632 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002633}
2634
2635static void io_fallocate_finish(struct io_wq_work **workptr)
2636{
2637 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002638
Pavel Begunkov594506f2020-03-03 21:33:11 +03002639 if (io_req_cancelled(req))
2640 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002641 __io_fallocate(req);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002642 io_put_req_async_completion(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002643}
2644
2645static int io_fallocate_prep(struct io_kiocb *req,
2646 const struct io_uring_sqe *sqe)
2647{
2648 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2649 return -EINVAL;
2650
2651 req->sync.off = READ_ONCE(sqe->off);
2652 req->sync.len = READ_ONCE(sqe->addr);
2653 req->sync.mode = READ_ONCE(sqe->len);
2654 return 0;
2655}
2656
Pavel Begunkov014db002020-03-03 21:33:12 +03002657static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002658{
Jens Axboed63d1b52019-12-10 10:38:56 -07002659 /* fallocate always requiring blocking context */
2660 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002661 req->work.func = io_fallocate_finish;
2662 return -EAGAIN;
2663 }
2664
Pavel Begunkov014db002020-03-03 21:33:12 +03002665 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002666 return 0;
2667}
2668
Jens Axboe15b71ab2019-12-11 11:20:36 -07002669static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2670{
Jens Axboef8748882020-01-08 17:47:02 -07002671 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002672 int ret;
2673
2674 if (sqe->ioprio || sqe->buf_index)
2675 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002676 if (sqe->flags & IOSQE_FIXED_FILE)
2677 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002678 if (req->flags & REQ_F_NEED_CLEANUP)
2679 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002680
2681 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002682 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002683 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002684 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002685
Jens Axboef8748882020-01-08 17:47:02 -07002686 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002687 if (IS_ERR(req->open.filename)) {
2688 ret = PTR_ERR(req->open.filename);
2689 req->open.filename = NULL;
2690 return ret;
2691 }
2692
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002693 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002694 return 0;
2695}
2696
Jens Axboecebdb982020-01-08 17:59:24 -07002697static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2698{
2699 struct open_how __user *how;
2700 const char __user *fname;
2701 size_t len;
2702 int ret;
2703
2704 if (sqe->ioprio || sqe->buf_index)
2705 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002706 if (sqe->flags & IOSQE_FIXED_FILE)
2707 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002708 if (req->flags & REQ_F_NEED_CLEANUP)
2709 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002710
2711 req->open.dfd = READ_ONCE(sqe->fd);
2712 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2713 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2714 len = READ_ONCE(sqe->len);
2715
2716 if (len < OPEN_HOW_SIZE_VER0)
2717 return -EINVAL;
2718
2719 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2720 len);
2721 if (ret)
2722 return ret;
2723
2724 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2725 req->open.how.flags |= O_LARGEFILE;
2726
2727 req->open.filename = getname(fname);
2728 if (IS_ERR(req->open.filename)) {
2729 ret = PTR_ERR(req->open.filename);
2730 req->open.filename = NULL;
2731 return ret;
2732 }
2733
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002734 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002735 return 0;
2736}
2737
Pavel Begunkov014db002020-03-03 21:33:12 +03002738static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002739{
2740 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002741 struct file *file;
2742 int ret;
2743
Jens Axboef86cd202020-01-29 13:46:44 -07002744 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002745 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002746
Jens Axboecebdb982020-01-08 17:59:24 -07002747 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002748 if (ret)
2749 goto err;
2750
Jens Axboecebdb982020-01-08 17:59:24 -07002751 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002752 if (ret < 0)
2753 goto err;
2754
2755 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2756 if (IS_ERR(file)) {
2757 put_unused_fd(ret);
2758 ret = PTR_ERR(file);
2759 } else {
2760 fsnotify_open(file);
2761 fd_install(ret, file);
2762 }
2763err:
2764 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002765 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002766 if (ret < 0)
2767 req_set_fail_links(req);
2768 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002769 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002770 return 0;
2771}
2772
Pavel Begunkov014db002020-03-03 21:33:12 +03002773static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07002774{
2775 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03002776 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07002777}
2778
Jens Axboe3e4827b2020-01-08 15:18:09 -07002779static int io_epoll_ctl_prep(struct io_kiocb *req,
2780 const struct io_uring_sqe *sqe)
2781{
2782#if defined(CONFIG_EPOLL)
2783 if (sqe->ioprio || sqe->buf_index)
2784 return -EINVAL;
2785
2786 req->epoll.epfd = READ_ONCE(sqe->fd);
2787 req->epoll.op = READ_ONCE(sqe->len);
2788 req->epoll.fd = READ_ONCE(sqe->off);
2789
2790 if (ep_op_has_event(req->epoll.op)) {
2791 struct epoll_event __user *ev;
2792
2793 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2794 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2795 return -EFAULT;
2796 }
2797
2798 return 0;
2799#else
2800 return -EOPNOTSUPP;
2801#endif
2802}
2803
Pavel Begunkov014db002020-03-03 21:33:12 +03002804static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07002805{
2806#if defined(CONFIG_EPOLL)
2807 struct io_epoll *ie = &req->epoll;
2808 int ret;
2809
2810 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2811 if (force_nonblock && ret == -EAGAIN)
2812 return -EAGAIN;
2813
2814 if (ret < 0)
2815 req_set_fail_links(req);
2816 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002817 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07002818 return 0;
2819#else
2820 return -EOPNOTSUPP;
2821#endif
2822}
2823
Jens Axboec1ca7572019-12-25 22:18:28 -07002824static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2825{
2826#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2827 if (sqe->ioprio || sqe->buf_index || sqe->off)
2828 return -EINVAL;
2829
2830 req->madvise.addr = READ_ONCE(sqe->addr);
2831 req->madvise.len = READ_ONCE(sqe->len);
2832 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2833 return 0;
2834#else
2835 return -EOPNOTSUPP;
2836#endif
2837}
2838
Pavel Begunkov014db002020-03-03 21:33:12 +03002839static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07002840{
2841#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2842 struct io_madvise *ma = &req->madvise;
2843 int ret;
2844
2845 if (force_nonblock)
2846 return -EAGAIN;
2847
2848 ret = do_madvise(ma->addr, ma->len, ma->advice);
2849 if (ret < 0)
2850 req_set_fail_links(req);
2851 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002852 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07002853 return 0;
2854#else
2855 return -EOPNOTSUPP;
2856#endif
2857}
2858
Jens Axboe4840e412019-12-25 22:03:45 -07002859static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2860{
2861 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2862 return -EINVAL;
2863
2864 req->fadvise.offset = READ_ONCE(sqe->off);
2865 req->fadvise.len = READ_ONCE(sqe->len);
2866 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2867 return 0;
2868}
2869
Pavel Begunkov014db002020-03-03 21:33:12 +03002870static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07002871{
2872 struct io_fadvise *fa = &req->fadvise;
2873 int ret;
2874
Jens Axboe3e694262020-02-01 09:22:49 -07002875 if (force_nonblock) {
2876 switch (fa->advice) {
2877 case POSIX_FADV_NORMAL:
2878 case POSIX_FADV_RANDOM:
2879 case POSIX_FADV_SEQUENTIAL:
2880 break;
2881 default:
2882 return -EAGAIN;
2883 }
2884 }
Jens Axboe4840e412019-12-25 22:03:45 -07002885
2886 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2887 if (ret < 0)
2888 req_set_fail_links(req);
2889 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002890 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07002891 return 0;
2892}
2893
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002894static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2895{
Jens Axboef8748882020-01-08 17:47:02 -07002896 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002897 unsigned lookup_flags;
2898 int ret;
2899
2900 if (sqe->ioprio || sqe->buf_index)
2901 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002902 if (sqe->flags & IOSQE_FIXED_FILE)
2903 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002904 if (req->flags & REQ_F_NEED_CLEANUP)
2905 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002906
2907 req->open.dfd = READ_ONCE(sqe->fd);
2908 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002909 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002910 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002911 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002912
Jens Axboec12cedf2020-01-08 17:41:21 -07002913 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002914 return -EINVAL;
2915
Jens Axboef8748882020-01-08 17:47:02 -07002916 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002917 if (IS_ERR(req->open.filename)) {
2918 ret = PTR_ERR(req->open.filename);
2919 req->open.filename = NULL;
2920 return ret;
2921 }
2922
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002923 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002924 return 0;
2925}
2926
Pavel Begunkov014db002020-03-03 21:33:12 +03002927static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002928{
2929 struct io_open *ctx = &req->open;
2930 unsigned lookup_flags;
2931 struct path path;
2932 struct kstat stat;
2933 int ret;
2934
2935 if (force_nonblock)
2936 return -EAGAIN;
2937
Jens Axboec12cedf2020-01-08 17:41:21 -07002938 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002939 return -EINVAL;
2940
2941retry:
2942 /* filename_lookup() drops it, keep a reference */
2943 ctx->filename->refcnt++;
2944
2945 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2946 NULL);
2947 if (ret)
2948 goto err;
2949
Jens Axboec12cedf2020-01-08 17:41:21 -07002950 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002951 path_put(&path);
2952 if (retry_estale(ret, lookup_flags)) {
2953 lookup_flags |= LOOKUP_REVAL;
2954 goto retry;
2955 }
2956 if (!ret)
2957 ret = cp_statx(&stat, ctx->buffer);
2958err:
2959 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002960 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002961 if (ret < 0)
2962 req_set_fail_links(req);
2963 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002964 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002965 return 0;
2966}
2967
Jens Axboeb5dba592019-12-11 14:02:38 -07002968static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2969{
2970 /*
2971 * If we queue this for async, it must not be cancellable. That would
2972 * leave the 'file' in an undeterminate state.
2973 */
2974 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2975
2976 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2977 sqe->rw_flags || sqe->buf_index)
2978 return -EINVAL;
2979 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002980 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002981
2982 req->close.fd = READ_ONCE(sqe->fd);
2983 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002984 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002985 return -EBADF;
2986
2987 return 0;
2988}
2989
Pavel Begunkova93b3332020-02-08 14:04:34 +03002990/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03002991static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03002992{
2993 int ret;
2994
2995 ret = filp_close(req->close.put_file, req->work.files);
2996 if (ret < 0)
2997 req_set_fail_links(req);
2998 io_cqring_add_event(req, ret);
2999 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003000 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003001}
3002
Jens Axboeb5dba592019-12-11 14:02:38 -07003003static void io_close_finish(struct io_wq_work **workptr)
3004{
3005 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003006
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003007 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003008 __io_close_finish(req);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03003009 io_put_req_async_completion(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003010}
3011
Pavel Begunkov014db002020-03-03 21:33:12 +03003012static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003013{
3014 int ret;
3015
3016 req->close.put_file = NULL;
3017 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3018 if (ret < 0)
3019 return ret;
3020
3021 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003022 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003023 /* submission ref will be dropped, take it for async */
3024 refcount_inc(&req->refs);
3025
Pavel Begunkova2100672020-03-02 23:45:16 +03003026 req->work.func = io_close_finish;
3027 /*
3028 * Do manual async queue here to avoid grabbing files - we don't
3029 * need the files, and it'll cause io_close_finish() to close
3030 * the file again and cause a double CQE entry for this request
3031 */
3032 io_queue_async_work(req);
3033 return 0;
3034 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003035
3036 /*
3037 * No ->flush(), safely close from here and just punt the
3038 * fput() to async context.
3039 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003040 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003041 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003042}
3043
Jens Axboe3529d8c2019-12-19 18:24:38 -07003044static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003045{
3046 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003047
3048 if (!req->file)
3049 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003050
3051 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3052 return -EINVAL;
3053 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3054 return -EINVAL;
3055
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003056 req->sync.off = READ_ONCE(sqe->off);
3057 req->sync.len = READ_ONCE(sqe->len);
3058 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003059 return 0;
3060}
3061
Pavel Begunkov014db002020-03-03 21:33:12 +03003062static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003063{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003064 int ret;
3065
Jens Axboe9adbd452019-12-20 08:45:55 -07003066 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003067 req->sync.flags);
3068 if (ret < 0)
3069 req_set_fail_links(req);
3070 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003071 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003072}
3073
3074
3075static void io_sync_file_range_finish(struct io_wq_work **workptr)
3076{
3077 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3078 struct io_kiocb *nxt = NULL;
3079
3080 if (io_req_cancelled(req))
3081 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003082 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003083 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003084 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003085 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003086}
3087
Pavel Begunkov014db002020-03-03 21:33:12 +03003088static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003089{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003090 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003091 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003092 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003093 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003094 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003095
Pavel Begunkov014db002020-03-03 21:33:12 +03003096 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003097 return 0;
3098}
3099
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003100static int io_setup_async_msg(struct io_kiocb *req,
3101 struct io_async_msghdr *kmsg)
3102{
3103 if (req->io)
3104 return -EAGAIN;
3105 if (io_alloc_async_ctx(req)) {
3106 if (kmsg->iov != kmsg->fast_iov)
3107 kfree(kmsg->iov);
3108 return -ENOMEM;
3109 }
3110 req->flags |= REQ_F_NEED_CLEANUP;
3111 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3112 return -EAGAIN;
3113}
3114
Jens Axboe3529d8c2019-12-19 18:24:38 -07003115static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003116{
Jens Axboe03b12302019-12-02 18:50:25 -07003117#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003118 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003119 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003120 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003121
Jens Axboee47293f2019-12-20 08:58:21 -07003122 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3123 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003124 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003125
Jens Axboed8768362020-02-27 14:17:49 -07003126#ifdef CONFIG_COMPAT
3127 if (req->ctx->compat)
3128 sr->msg_flags |= MSG_CMSG_COMPAT;
3129#endif
3130
Jens Axboefddafac2020-01-04 20:19:44 -07003131 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003132 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003133 /* iovec is already imported */
3134 if (req->flags & REQ_F_NEED_CLEANUP)
3135 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003136
Jens Axboed9688562019-12-09 19:35:20 -07003137 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003138 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003139 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003140 if (!ret)
3141 req->flags |= REQ_F_NEED_CLEANUP;
3142 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003143#else
Jens Axboee47293f2019-12-20 08:58:21 -07003144 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003145#endif
3146}
3147
Pavel Begunkov014db002020-03-03 21:33:12 +03003148static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003149{
3150#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003151 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003152 struct socket *sock;
3153 int ret;
3154
3155 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3156 return -EINVAL;
3157
3158 sock = sock_from_file(req->file, &ret);
3159 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003160 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003161 unsigned flags;
3162
Jens Axboe03b12302019-12-02 18:50:25 -07003163 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003164 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003165 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003166 /* if iov is set, it's allocated already */
3167 if (!kmsg->iov)
3168 kmsg->iov = kmsg->fast_iov;
3169 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003170 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003171 struct io_sr_msg *sr = &req->sr_msg;
3172
Jens Axboe0b416c32019-12-15 10:57:46 -07003173 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003174 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003175
3176 io.msg.iov = io.msg.fast_iov;
3177 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3178 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003179 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003180 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003181 }
3182
Jens Axboee47293f2019-12-20 08:58:21 -07003183 flags = req->sr_msg.msg_flags;
3184 if (flags & MSG_DONTWAIT)
3185 req->flags |= REQ_F_NOWAIT;
3186 else if (force_nonblock)
3187 flags |= MSG_DONTWAIT;
3188
Jens Axboe0b416c32019-12-15 10:57:46 -07003189 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003190 if (force_nonblock && ret == -EAGAIN)
3191 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003192 if (ret == -ERESTARTSYS)
3193 ret = -EINTR;
3194 }
3195
Pavel Begunkov1e950812020-02-06 19:51:16 +03003196 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003197 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003198 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003199 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003200 if (ret < 0)
3201 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003202 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003203 return 0;
3204#else
3205 return -EOPNOTSUPP;
3206#endif
3207}
3208
Pavel Begunkov014db002020-03-03 21:33:12 +03003209static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003210{
3211#if defined(CONFIG_NET)
3212 struct socket *sock;
3213 int ret;
3214
3215 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3216 return -EINVAL;
3217
3218 sock = sock_from_file(req->file, &ret);
3219 if (sock) {
3220 struct io_sr_msg *sr = &req->sr_msg;
3221 struct msghdr msg;
3222 struct iovec iov;
3223 unsigned flags;
3224
3225 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3226 &msg.msg_iter);
3227 if (ret)
3228 return ret;
3229
3230 msg.msg_name = NULL;
3231 msg.msg_control = NULL;
3232 msg.msg_controllen = 0;
3233 msg.msg_namelen = 0;
3234
3235 flags = req->sr_msg.msg_flags;
3236 if (flags & MSG_DONTWAIT)
3237 req->flags |= REQ_F_NOWAIT;
3238 else if (force_nonblock)
3239 flags |= MSG_DONTWAIT;
3240
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003241 msg.msg_flags = flags;
3242 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003243 if (force_nonblock && ret == -EAGAIN)
3244 return -EAGAIN;
3245 if (ret == -ERESTARTSYS)
3246 ret = -EINTR;
3247 }
3248
3249 io_cqring_add_event(req, ret);
3250 if (ret < 0)
3251 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003252 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003253 return 0;
3254#else
3255 return -EOPNOTSUPP;
3256#endif
3257}
3258
Jens Axboe3529d8c2019-12-19 18:24:38 -07003259static int io_recvmsg_prep(struct io_kiocb *req,
3260 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003261{
3262#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003263 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003264 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003265 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003266
Jens Axboe3529d8c2019-12-19 18:24:38 -07003267 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3268 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003269 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003270
Jens Axboed8768362020-02-27 14:17:49 -07003271#ifdef CONFIG_COMPAT
3272 if (req->ctx->compat)
3273 sr->msg_flags |= MSG_CMSG_COMPAT;
3274#endif
3275
Jens Axboefddafac2020-01-04 20:19:44 -07003276 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003277 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003278 /* iovec is already imported */
3279 if (req->flags & REQ_F_NEED_CLEANUP)
3280 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003281
Jens Axboed9688562019-12-09 19:35:20 -07003282 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003283 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003284 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003285 if (!ret)
3286 req->flags |= REQ_F_NEED_CLEANUP;
3287 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003288#else
Jens Axboee47293f2019-12-20 08:58:21 -07003289 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003290#endif
3291}
3292
Pavel Begunkov014db002020-03-03 21:33:12 +03003293static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003294{
3295#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003296 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003297 struct socket *sock;
3298 int ret;
3299
3300 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3301 return -EINVAL;
3302
3303 sock = sock_from_file(req->file, &ret);
3304 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003305 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003306 unsigned flags;
3307
Jens Axboe03b12302019-12-02 18:50:25 -07003308 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003309 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003310 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003311 /* if iov is set, it's allocated already */
3312 if (!kmsg->iov)
3313 kmsg->iov = kmsg->fast_iov;
3314 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003315 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003316 struct io_sr_msg *sr = &req->sr_msg;
3317
Jens Axboe0b416c32019-12-15 10:57:46 -07003318 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003319 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003320
3321 io.msg.iov = io.msg.fast_iov;
3322 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3323 sr->msg_flags, &io.msg.uaddr,
3324 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003325 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003326 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003327 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003328
Jens Axboee47293f2019-12-20 08:58:21 -07003329 flags = req->sr_msg.msg_flags;
3330 if (flags & MSG_DONTWAIT)
3331 req->flags |= REQ_F_NOWAIT;
3332 else if (force_nonblock)
3333 flags |= MSG_DONTWAIT;
3334
3335 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3336 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003337 if (force_nonblock && ret == -EAGAIN)
3338 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003339 if (ret == -ERESTARTSYS)
3340 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003341 }
3342
Pavel Begunkov1e950812020-02-06 19:51:16 +03003343 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003344 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003345 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003346 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003347 if (ret < 0)
3348 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003349 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003350 return 0;
3351#else
3352 return -EOPNOTSUPP;
3353#endif
3354}
3355
Pavel Begunkov014db002020-03-03 21:33:12 +03003356static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003357{
3358#if defined(CONFIG_NET)
3359 struct socket *sock;
3360 int ret;
3361
3362 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3363 return -EINVAL;
3364
3365 sock = sock_from_file(req->file, &ret);
3366 if (sock) {
3367 struct io_sr_msg *sr = &req->sr_msg;
3368 struct msghdr msg;
3369 struct iovec iov;
3370 unsigned flags;
3371
3372 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3373 &msg.msg_iter);
3374 if (ret)
3375 return ret;
3376
3377 msg.msg_name = NULL;
3378 msg.msg_control = NULL;
3379 msg.msg_controllen = 0;
3380 msg.msg_namelen = 0;
3381 msg.msg_iocb = NULL;
3382 msg.msg_flags = 0;
3383
3384 flags = req->sr_msg.msg_flags;
3385 if (flags & MSG_DONTWAIT)
3386 req->flags |= REQ_F_NOWAIT;
3387 else if (force_nonblock)
3388 flags |= MSG_DONTWAIT;
3389
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003390 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003391 if (force_nonblock && ret == -EAGAIN)
3392 return -EAGAIN;
3393 if (ret == -ERESTARTSYS)
3394 ret = -EINTR;
3395 }
3396
3397 io_cqring_add_event(req, ret);
3398 if (ret < 0)
3399 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003400 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003401 return 0;
3402#else
3403 return -EOPNOTSUPP;
3404#endif
3405}
3406
3407
Jens Axboe3529d8c2019-12-19 18:24:38 -07003408static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003409{
3410#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003411 struct io_accept *accept = &req->accept;
3412
Jens Axboe17f2fe32019-10-17 14:42:58 -06003413 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3414 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003415 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003416 return -EINVAL;
3417
Jens Axboed55e5f52019-12-11 16:12:15 -07003418 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3419 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003420 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003421 return 0;
3422#else
3423 return -EOPNOTSUPP;
3424#endif
3425}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003426
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003427#if defined(CONFIG_NET)
Pavel Begunkov014db002020-03-03 21:33:12 +03003428static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003429{
3430 struct io_accept *accept = &req->accept;
3431 unsigned file_flags;
3432 int ret;
3433
3434 file_flags = force_nonblock ? O_NONBLOCK : 0;
3435 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3436 accept->addr_len, accept->flags);
3437 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003438 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003439 if (ret == -ERESTARTSYS)
3440 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003441 if (ret < 0)
3442 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003443 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003444 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003445 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003446}
3447
3448static void io_accept_finish(struct io_wq_work **workptr)
3449{
3450 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003451
3452 if (io_req_cancelled(req))
3453 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003454 __io_accept(req, false);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03003455 io_put_req_async_completion(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003456}
3457#endif
3458
Pavel Begunkov014db002020-03-03 21:33:12 +03003459static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003460{
3461#if defined(CONFIG_NET)
3462 int ret;
3463
Pavel Begunkov014db002020-03-03 21:33:12 +03003464 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003465 if (ret == -EAGAIN && force_nonblock) {
3466 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003467 return -EAGAIN;
3468 }
3469 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003470#else
3471 return -EOPNOTSUPP;
3472#endif
3473}
3474
Jens Axboe3529d8c2019-12-19 18:24:38 -07003475static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003476{
3477#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003478 struct io_connect *conn = &req->connect;
3479 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003480
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003481 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3482 return -EINVAL;
3483 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3484 return -EINVAL;
3485
Jens Axboe3529d8c2019-12-19 18:24:38 -07003486 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3487 conn->addr_len = READ_ONCE(sqe->addr2);
3488
3489 if (!io)
3490 return 0;
3491
3492 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003493 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003494#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003495 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003496#endif
3497}
3498
Pavel Begunkov014db002020-03-03 21:33:12 +03003499static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003500{
3501#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003502 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003503 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003504 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003505
Jens Axboef499a022019-12-02 16:28:46 -07003506 if (req->io) {
3507 io = req->io;
3508 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003509 ret = move_addr_to_kernel(req->connect.addr,
3510 req->connect.addr_len,
3511 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003512 if (ret)
3513 goto out;
3514 io = &__io;
3515 }
3516
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003517 file_flags = force_nonblock ? O_NONBLOCK : 0;
3518
3519 ret = __sys_connect_file(req->file, &io->connect.address,
3520 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003521 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003522 if (req->io)
3523 return -EAGAIN;
3524 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003525 ret = -ENOMEM;
3526 goto out;
3527 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003528 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003529 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003530 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003531 if (ret == -ERESTARTSYS)
3532 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003533out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003534 if (ret < 0)
3535 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003536 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003537 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003538 return 0;
3539#else
3540 return -EOPNOTSUPP;
3541#endif
3542}
3543
Jens Axboed7718a92020-02-14 22:23:12 -07003544struct io_poll_table {
3545 struct poll_table_struct pt;
3546 struct io_kiocb *req;
3547 int error;
3548};
3549
3550static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
3551 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003552{
Jens Axboed7718a92020-02-14 22:23:12 -07003553 if (unlikely(poll->head)) {
3554 pt->error = -EINVAL;
3555 return;
3556 }
3557
3558 pt->error = 0;
3559 poll->head = head;
3560 add_wait_queue(head, &poll->wait);
3561}
3562
3563static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
3564 struct poll_table_struct *p)
3565{
3566 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3567
3568 __io_queue_proc(&pt->req->apoll->poll, pt, head);
3569}
3570
3571static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
3572 __poll_t mask, task_work_func_t func)
3573{
3574 struct task_struct *tsk;
3575
3576 /* for instances that support it check for an event match first: */
3577 if (mask && !(mask & poll->events))
3578 return 0;
3579
3580 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
3581
3582 list_del_init(&poll->wait.entry);
3583
3584 tsk = req->task;
3585 req->result = mask;
3586 init_task_work(&req->task_work, func);
3587 /*
3588 * If this fails, then the task is exiting. If that is the case, then
3589 * the exit check will ultimately cancel these work items. Hence we
3590 * don't need to check here and handle it specifically.
3591 */
3592 task_work_add(tsk, &req->task_work, true);
3593 wake_up_process(tsk);
3594 return 1;
3595}
3596
3597static void io_async_task_func(struct callback_head *cb)
3598{
3599 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3600 struct async_poll *apoll = req->apoll;
3601 struct io_ring_ctx *ctx = req->ctx;
3602
3603 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
3604
3605 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
3606
3607 if (hash_hashed(&req->hash_node)) {
3608 spin_lock_irq(&ctx->completion_lock);
3609 hash_del(&req->hash_node);
3610 spin_unlock_irq(&ctx->completion_lock);
3611 }
3612
3613 /* restore ->work in case we need to retry again */
3614 memcpy(&req->work, &apoll->work, sizeof(req->work));
3615
3616 __set_current_state(TASK_RUNNING);
3617 mutex_lock(&ctx->uring_lock);
3618 __io_queue_sqe(req, NULL);
3619 mutex_unlock(&ctx->uring_lock);
3620
3621 kfree(apoll);
3622}
3623
3624static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3625 void *key)
3626{
3627 struct io_kiocb *req = wait->private;
3628 struct io_poll_iocb *poll = &req->apoll->poll;
3629
3630 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
3631 key_to_poll(key));
3632
3633 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
3634}
3635
3636static void io_poll_req_insert(struct io_kiocb *req)
3637{
3638 struct io_ring_ctx *ctx = req->ctx;
3639 struct hlist_head *list;
3640
3641 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3642 hlist_add_head(&req->hash_node, list);
3643}
3644
3645static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
3646 struct io_poll_iocb *poll,
3647 struct io_poll_table *ipt, __poll_t mask,
3648 wait_queue_func_t wake_func)
3649 __acquires(&ctx->completion_lock)
3650{
3651 struct io_ring_ctx *ctx = req->ctx;
3652 bool cancel = false;
3653
3654 poll->file = req->file;
3655 poll->head = NULL;
3656 poll->done = poll->canceled = false;
3657 poll->events = mask;
3658
3659 ipt->pt._key = mask;
3660 ipt->req = req;
3661 ipt->error = -EINVAL;
3662
3663 INIT_LIST_HEAD(&poll->wait.entry);
3664 init_waitqueue_func_entry(&poll->wait, wake_func);
3665 poll->wait.private = req;
3666
3667 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
3668
3669 spin_lock_irq(&ctx->completion_lock);
3670 if (likely(poll->head)) {
3671 spin_lock(&poll->head->lock);
3672 if (unlikely(list_empty(&poll->wait.entry))) {
3673 if (ipt->error)
3674 cancel = true;
3675 ipt->error = 0;
3676 mask = 0;
3677 }
3678 if (mask || ipt->error)
3679 list_del_init(&poll->wait.entry);
3680 else if (cancel)
3681 WRITE_ONCE(poll->canceled, true);
3682 else if (!poll->done) /* actually waiting for an event */
3683 io_poll_req_insert(req);
3684 spin_unlock(&poll->head->lock);
3685 }
3686
3687 return mask;
3688}
3689
3690static bool io_arm_poll_handler(struct io_kiocb *req)
3691{
3692 const struct io_op_def *def = &io_op_defs[req->opcode];
3693 struct io_ring_ctx *ctx = req->ctx;
3694 struct async_poll *apoll;
3695 struct io_poll_table ipt;
3696 __poll_t mask, ret;
3697
3698 if (!req->file || !file_can_poll(req->file))
3699 return false;
3700 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
3701 return false;
3702 if (!def->pollin && !def->pollout)
3703 return false;
3704
3705 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
3706 if (unlikely(!apoll))
3707 return false;
3708
3709 req->flags |= REQ_F_POLLED;
3710 memcpy(&apoll->work, &req->work, sizeof(req->work));
3711
3712 /*
3713 * Don't need a reference here, as we're adding it to the task
3714 * task_works list. If the task exits, the list is pruned.
3715 */
3716 req->task = current;
3717 req->apoll = apoll;
3718 INIT_HLIST_NODE(&req->hash_node);
3719
Nathan Chancellor8755d972020-03-02 16:01:19 -07003720 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07003721 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07003722 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07003723 if (def->pollout)
3724 mask |= POLLOUT | POLLWRNORM;
3725 mask |= POLLERR | POLLPRI;
3726
3727 ipt.pt._qproc = io_async_queue_proc;
3728
3729 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
3730 io_async_wake);
3731 if (ret) {
3732 ipt.error = 0;
3733 apoll->poll.done = true;
3734 spin_unlock_irq(&ctx->completion_lock);
3735 memcpy(&req->work, &apoll->work, sizeof(req->work));
3736 kfree(apoll);
3737 return false;
3738 }
3739 spin_unlock_irq(&ctx->completion_lock);
3740 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
3741 apoll->poll.events);
3742 return true;
3743}
3744
3745static bool __io_poll_remove_one(struct io_kiocb *req,
3746 struct io_poll_iocb *poll)
3747{
Jens Axboeb41e9852020-02-17 09:52:41 -07003748 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003749
3750 spin_lock(&poll->head->lock);
3751 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003752 if (!list_empty(&poll->wait.entry)) {
3753 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07003754 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003755 }
3756 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07003757 return do_complete;
3758}
3759
3760static bool io_poll_remove_one(struct io_kiocb *req)
3761{
3762 bool do_complete;
3763
3764 if (req->opcode == IORING_OP_POLL_ADD) {
3765 do_complete = __io_poll_remove_one(req, &req->poll);
3766 } else {
3767 /* non-poll requests have submit ref still */
3768 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
3769 if (do_complete)
3770 io_put_req(req);
3771 }
3772
Jens Axboe78076bb2019-12-04 19:56:40 -07003773 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07003774
Jens Axboeb41e9852020-02-17 09:52:41 -07003775 if (do_complete) {
3776 io_cqring_fill_event(req, -ECANCELED);
3777 io_commit_cqring(req->ctx);
3778 req->flags |= REQ_F_COMP_LOCKED;
3779 io_put_req(req);
3780 }
3781
3782 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003783}
3784
3785static void io_poll_remove_all(struct io_ring_ctx *ctx)
3786{
Jens Axboe78076bb2019-12-04 19:56:40 -07003787 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003788 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003789 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003790
3791 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003792 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3793 struct hlist_head *list;
3794
3795 list = &ctx->cancel_hash[i];
3796 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3797 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003798 }
3799 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003800
3801 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003802}
3803
Jens Axboe47f46762019-11-09 17:43:02 -07003804static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3805{
Jens Axboe78076bb2019-12-04 19:56:40 -07003806 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003807 struct io_kiocb *req;
3808
Jens Axboe78076bb2019-12-04 19:56:40 -07003809 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3810 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07003811 if (sqe_addr != req->user_data)
3812 continue;
3813 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07003814 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07003815 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07003816 }
3817
3818 return -ENOENT;
3819}
3820
Jens Axboe3529d8c2019-12-19 18:24:38 -07003821static int io_poll_remove_prep(struct io_kiocb *req,
3822 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003823{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003824 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3825 return -EINVAL;
3826 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3827 sqe->poll_events)
3828 return -EINVAL;
3829
Jens Axboe0969e782019-12-17 18:40:57 -07003830 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003831 return 0;
3832}
3833
3834/*
3835 * Find a running poll command that matches one specified in sqe->addr,
3836 * and remove it if found.
3837 */
3838static int io_poll_remove(struct io_kiocb *req)
3839{
3840 struct io_ring_ctx *ctx = req->ctx;
3841 u64 addr;
3842 int ret;
3843
Jens Axboe0969e782019-12-17 18:40:57 -07003844 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003845 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003846 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003847 spin_unlock_irq(&ctx->completion_lock);
3848
Jens Axboe78e19bb2019-11-06 15:21:34 -07003849 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003850 if (ret < 0)
3851 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003852 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003853 return 0;
3854}
3855
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003856static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003857{
Jackie Liua197f662019-11-08 08:09:12 -07003858 struct io_ring_ctx *ctx = req->ctx;
3859
Jens Axboe8c838782019-03-12 15:48:16 -06003860 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03003861 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003862 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003863}
3864
Jens Axboeb41e9852020-02-17 09:52:41 -07003865static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003866{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003867 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003868
Jens Axboe221c5eb2019-01-17 09:41:58 -07003869 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003870 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07003871 io_poll_complete(req, req->result, 0);
3872 req->flags |= REQ_F_COMP_LOCKED;
3873 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003874 spin_unlock_irq(&ctx->completion_lock);
3875
Jens Axboe8c838782019-03-12 15:48:16 -06003876 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07003877}
Jens Axboe89723d02019-11-05 15:32:58 -07003878
Jens Axboeb41e9852020-02-17 09:52:41 -07003879static void io_poll_task_func(struct callback_head *cb)
3880{
3881 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3882 struct io_kiocb *nxt = NULL;
3883
3884 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07003885 if (nxt) {
3886 struct io_ring_ctx *ctx = nxt->ctx;
3887
3888 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003889 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07003890 mutex_unlock(&ctx->uring_lock);
3891 }
Jens Axboef0b493e2020-02-01 21:30:11 -07003892}
3893
Jens Axboe221c5eb2019-01-17 09:41:58 -07003894static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3895 void *key)
3896{
Jens Axboec2f2eb72020-02-10 09:07:05 -07003897 struct io_kiocb *req = wait->private;
3898 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003899
Jens Axboed7718a92020-02-14 22:23:12 -07003900 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003901}
3902
Jens Axboe221c5eb2019-01-17 09:41:58 -07003903static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3904 struct poll_table_struct *p)
3905{
3906 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3907
Jens Axboed7718a92020-02-14 22:23:12 -07003908 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07003909}
3910
Jens Axboe3529d8c2019-12-19 18:24:38 -07003911static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003912{
3913 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003914 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003915
3916 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3917 return -EINVAL;
3918 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3919 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003920 if (!poll->file)
3921 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003922
Jens Axboe221c5eb2019-01-17 09:41:58 -07003923 events = READ_ONCE(sqe->poll_events);
3924 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07003925
Jens Axboed7718a92020-02-14 22:23:12 -07003926 /*
3927 * Don't need a reference here, as we're adding it to the task
3928 * task_works list. If the task exits, the list is pruned.
3929 */
Jens Axboeb41e9852020-02-17 09:52:41 -07003930 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07003931 return 0;
3932}
3933
Pavel Begunkov014db002020-03-03 21:33:12 +03003934static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07003935{
3936 struct io_poll_iocb *poll = &req->poll;
3937 struct io_ring_ctx *ctx = req->ctx;
3938 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07003939 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003940
Jens Axboe78076bb2019-12-04 19:56:40 -07003941 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06003942 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07003943 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06003944
Jens Axboed7718a92020-02-14 22:23:12 -07003945 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
3946 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003947
Jens Axboe8c838782019-03-12 15:48:16 -06003948 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003949 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003950 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003951 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003952 spin_unlock_irq(&ctx->completion_lock);
3953
Jens Axboe8c838782019-03-12 15:48:16 -06003954 if (mask) {
3955 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03003956 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003957 }
Jens Axboe8c838782019-03-12 15:48:16 -06003958 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003959}
3960
Jens Axboe5262f562019-09-17 12:26:57 -06003961static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3962{
Jens Axboead8a48a2019-11-15 08:49:11 -07003963 struct io_timeout_data *data = container_of(timer,
3964 struct io_timeout_data, timer);
3965 struct io_kiocb *req = data->req;
3966 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003967 unsigned long flags;
3968
Jens Axboe5262f562019-09-17 12:26:57 -06003969 atomic_inc(&ctx->cq_timeouts);
3970
3971 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003972 /*
Jens Axboe11365042019-10-16 09:08:32 -06003973 * We could be racing with timeout deletion. If the list is empty,
3974 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003975 */
Jens Axboe842f9612019-10-29 12:34:10 -06003976 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003977 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003978
Jens Axboe11365042019-10-16 09:08:32 -06003979 /*
3980 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003981 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003982 * pointer will be increased, otherwise other timeout reqs may
3983 * return in advance without waiting for enough wait_nr.
3984 */
3985 prev = req;
3986 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3987 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003988 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003989 }
Jens Axboe842f9612019-10-29 12:34:10 -06003990
Jens Axboe78e19bb2019-11-06 15:21:34 -07003991 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003992 io_commit_cqring(ctx);
3993 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3994
3995 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003996 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003997 io_put_req(req);
3998 return HRTIMER_NORESTART;
3999}
4000
Jens Axboe47f46762019-11-09 17:43:02 -07004001static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4002{
4003 struct io_kiocb *req;
4004 int ret = -ENOENT;
4005
4006 list_for_each_entry(req, &ctx->timeout_list, list) {
4007 if (user_data == req->user_data) {
4008 list_del_init(&req->list);
4009 ret = 0;
4010 break;
4011 }
4012 }
4013
4014 if (ret == -ENOENT)
4015 return ret;
4016
Jens Axboe2d283902019-12-04 11:08:05 -07004017 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004018 if (ret == -1)
4019 return -EALREADY;
4020
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004021 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004022 io_cqring_fill_event(req, -ECANCELED);
4023 io_put_req(req);
4024 return 0;
4025}
4026
Jens Axboe3529d8c2019-12-19 18:24:38 -07004027static int io_timeout_remove_prep(struct io_kiocb *req,
4028 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004029{
Jens Axboeb29472e2019-12-17 18:50:29 -07004030 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4031 return -EINVAL;
4032 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4033 return -EINVAL;
4034
4035 req->timeout.addr = READ_ONCE(sqe->addr);
4036 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4037 if (req->timeout.flags)
4038 return -EINVAL;
4039
Jens Axboeb29472e2019-12-17 18:50:29 -07004040 return 0;
4041}
4042
Jens Axboe11365042019-10-16 09:08:32 -06004043/*
4044 * Remove or update an existing timeout command
4045 */
Jens Axboefc4df992019-12-10 14:38:45 -07004046static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004047{
4048 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004049 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004050
Jens Axboe11365042019-10-16 09:08:32 -06004051 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004052 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004053
Jens Axboe47f46762019-11-09 17:43:02 -07004054 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004055 io_commit_cqring(ctx);
4056 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004057 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004058 if (ret < 0)
4059 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004060 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004061 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004062}
4063
Jens Axboe3529d8c2019-12-19 18:24:38 -07004064static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004065 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004066{
Jens Axboead8a48a2019-11-15 08:49:11 -07004067 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004068 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004069
Jens Axboead8a48a2019-11-15 08:49:11 -07004070 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004071 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004072 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004073 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004074 if (sqe->off && is_timeout_link)
4075 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004076 flags = READ_ONCE(sqe->timeout_flags);
4077 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004078 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004079
Jens Axboe26a61672019-12-20 09:02:01 -07004080 req->timeout.count = READ_ONCE(sqe->off);
4081
Jens Axboe3529d8c2019-12-19 18:24:38 -07004082 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004083 return -ENOMEM;
4084
4085 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004086 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004087 req->flags |= REQ_F_TIMEOUT;
4088
4089 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004090 return -EFAULT;
4091
Jens Axboe11365042019-10-16 09:08:32 -06004092 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004093 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004094 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004095 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004096
Jens Axboead8a48a2019-11-15 08:49:11 -07004097 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4098 return 0;
4099}
4100
Jens Axboefc4df992019-12-10 14:38:45 -07004101static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004102{
4103 unsigned count;
4104 struct io_ring_ctx *ctx = req->ctx;
4105 struct io_timeout_data *data;
4106 struct list_head *entry;
4107 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004108
Jens Axboe2d283902019-12-04 11:08:05 -07004109 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004110
Jens Axboe5262f562019-09-17 12:26:57 -06004111 /*
4112 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004113 * timeout event to be satisfied. If it isn't set, then this is
4114 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004115 */
Jens Axboe26a61672019-12-20 09:02:01 -07004116 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004117 if (!count) {
4118 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4119 spin_lock_irq(&ctx->completion_lock);
4120 entry = ctx->timeout_list.prev;
4121 goto add;
4122 }
Jens Axboe5262f562019-09-17 12:26:57 -06004123
4124 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004125 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004126
4127 /*
4128 * Insertion sort, ensuring the first entry in the list is always
4129 * the one we need first.
4130 */
Jens Axboe5262f562019-09-17 12:26:57 -06004131 spin_lock_irq(&ctx->completion_lock);
4132 list_for_each_prev(entry, &ctx->timeout_list) {
4133 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004134 unsigned nxt_sq_head;
4135 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004136 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004137
Jens Axboe93bd25b2019-11-11 23:34:31 -07004138 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4139 continue;
4140
yangerkun5da0fb12019-10-15 21:59:29 +08004141 /*
4142 * Since cached_sq_head + count - 1 can overflow, use type long
4143 * long to store it.
4144 */
4145 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004146 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4147 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004148
4149 /*
4150 * cached_sq_head may overflow, and it will never overflow twice
4151 * once there is some timeout req still be valid.
4152 */
4153 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004154 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004155
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004156 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004157 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004158
4159 /*
4160 * Sequence of reqs after the insert one and itself should
4161 * be adjusted because each timeout req consumes a slot.
4162 */
4163 span++;
4164 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004165 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004166 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004167add:
Jens Axboe5262f562019-09-17 12:26:57 -06004168 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004169 data->timer.function = io_timeout_fn;
4170 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004171 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004172 return 0;
4173}
4174
Jens Axboe62755e32019-10-28 21:49:21 -06004175static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004176{
Jens Axboe62755e32019-10-28 21:49:21 -06004177 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004178
Jens Axboe62755e32019-10-28 21:49:21 -06004179 return req->user_data == (unsigned long) data;
4180}
4181
Jens Axboee977d6d2019-11-05 12:39:45 -07004182static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004183{
Jens Axboe62755e32019-10-28 21:49:21 -06004184 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004185 int ret = 0;
4186
Jens Axboe62755e32019-10-28 21:49:21 -06004187 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4188 switch (cancel_ret) {
4189 case IO_WQ_CANCEL_OK:
4190 ret = 0;
4191 break;
4192 case IO_WQ_CANCEL_RUNNING:
4193 ret = -EALREADY;
4194 break;
4195 case IO_WQ_CANCEL_NOTFOUND:
4196 ret = -ENOENT;
4197 break;
4198 }
4199
Jens Axboee977d6d2019-11-05 12:39:45 -07004200 return ret;
4201}
4202
Jens Axboe47f46762019-11-09 17:43:02 -07004203static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4204 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004205 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004206{
4207 unsigned long flags;
4208 int ret;
4209
4210 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4211 if (ret != -ENOENT) {
4212 spin_lock_irqsave(&ctx->completion_lock, flags);
4213 goto done;
4214 }
4215
4216 spin_lock_irqsave(&ctx->completion_lock, flags);
4217 ret = io_timeout_cancel(ctx, sqe_addr);
4218 if (ret != -ENOENT)
4219 goto done;
4220 ret = io_poll_cancel(ctx, sqe_addr);
4221done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004222 if (!ret)
4223 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004224 io_cqring_fill_event(req, ret);
4225 io_commit_cqring(ctx);
4226 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4227 io_cqring_ev_posted(ctx);
4228
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004229 if (ret < 0)
4230 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004231 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004232}
4233
Jens Axboe3529d8c2019-12-19 18:24:38 -07004234static int io_async_cancel_prep(struct io_kiocb *req,
4235 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004236{
Jens Axboefbf23842019-12-17 18:45:56 -07004237 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004238 return -EINVAL;
4239 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4240 sqe->cancel_flags)
4241 return -EINVAL;
4242
Jens Axboefbf23842019-12-17 18:45:56 -07004243 req->cancel.addr = READ_ONCE(sqe->addr);
4244 return 0;
4245}
4246
Pavel Begunkov014db002020-03-03 21:33:12 +03004247static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004248{
4249 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004250
Pavel Begunkov014db002020-03-03 21:33:12 +03004251 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004252 return 0;
4253}
4254
Jens Axboe05f3fb32019-12-09 11:22:50 -07004255static int io_files_update_prep(struct io_kiocb *req,
4256 const struct io_uring_sqe *sqe)
4257{
4258 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4259 return -EINVAL;
4260
4261 req->files_update.offset = READ_ONCE(sqe->off);
4262 req->files_update.nr_args = READ_ONCE(sqe->len);
4263 if (!req->files_update.nr_args)
4264 return -EINVAL;
4265 req->files_update.arg = READ_ONCE(sqe->addr);
4266 return 0;
4267}
4268
4269static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4270{
4271 struct io_ring_ctx *ctx = req->ctx;
4272 struct io_uring_files_update up;
4273 int ret;
4274
Jens Axboef86cd202020-01-29 13:46:44 -07004275 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004276 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004277
4278 up.offset = req->files_update.offset;
4279 up.fds = req->files_update.arg;
4280
4281 mutex_lock(&ctx->uring_lock);
4282 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4283 mutex_unlock(&ctx->uring_lock);
4284
4285 if (ret < 0)
4286 req_set_fail_links(req);
4287 io_cqring_add_event(req, ret);
4288 io_put_req(req);
4289 return 0;
4290}
4291
Jens Axboe3529d8c2019-12-19 18:24:38 -07004292static int io_req_defer_prep(struct io_kiocb *req,
4293 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004294{
Jens Axboee7815732019-12-17 19:45:06 -07004295 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004296
Jens Axboef86cd202020-01-29 13:46:44 -07004297 if (io_op_defs[req->opcode].file_table) {
4298 ret = io_grab_files(req);
4299 if (unlikely(ret))
4300 return ret;
4301 }
4302
Jens Axboecccf0ee2020-01-27 16:34:48 -07004303 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4304
Jens Axboed625c6e2019-12-17 19:53:05 -07004305 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004306 case IORING_OP_NOP:
4307 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004308 case IORING_OP_READV:
4309 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004310 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004311 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004312 break;
4313 case IORING_OP_WRITEV:
4314 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004315 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004316 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004317 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004318 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004319 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004320 break;
4321 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004322 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004323 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004324 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004325 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004326 break;
4327 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004328 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004329 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004330 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004331 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004332 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004333 break;
4334 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004335 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004337 break;
Jens Axboef499a022019-12-02 16:28:46 -07004338 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004339 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004340 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004341 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004342 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004343 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004344 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004345 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004346 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004347 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004348 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004349 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004350 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004351 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004352 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004353 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004354 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004355 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004356 case IORING_OP_FALLOCATE:
4357 ret = io_fallocate_prep(req, sqe);
4358 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004359 case IORING_OP_OPENAT:
4360 ret = io_openat_prep(req, sqe);
4361 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004362 case IORING_OP_CLOSE:
4363 ret = io_close_prep(req, sqe);
4364 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004365 case IORING_OP_FILES_UPDATE:
4366 ret = io_files_update_prep(req, sqe);
4367 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004368 case IORING_OP_STATX:
4369 ret = io_statx_prep(req, sqe);
4370 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004371 case IORING_OP_FADVISE:
4372 ret = io_fadvise_prep(req, sqe);
4373 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004374 case IORING_OP_MADVISE:
4375 ret = io_madvise_prep(req, sqe);
4376 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004377 case IORING_OP_OPENAT2:
4378 ret = io_openat2_prep(req, sqe);
4379 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004380 case IORING_OP_EPOLL_CTL:
4381 ret = io_epoll_ctl_prep(req, sqe);
4382 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004383 case IORING_OP_SPLICE:
4384 ret = io_splice_prep(req, sqe);
4385 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004386 default:
Jens Axboee7815732019-12-17 19:45:06 -07004387 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4388 req->opcode);
4389 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004390 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004391 }
4392
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004393 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004394}
4395
Jens Axboe3529d8c2019-12-19 18:24:38 -07004396static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004397{
Jackie Liua197f662019-11-08 08:09:12 -07004398 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004399 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004400
Bob Liu9d858b22019-11-13 18:06:25 +08004401 /* Still need defer if there is pending req in defer list. */
4402 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004403 return 0;
4404
Jens Axboe3529d8c2019-12-19 18:24:38 -07004405 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004406 return -EAGAIN;
4407
Jens Axboe3529d8c2019-12-19 18:24:38 -07004408 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004409 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004410 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004411
Jens Axboede0617e2019-04-06 21:51:27 -06004412 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004413 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004414 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004415 return 0;
4416 }
4417
Jens Axboe915967f2019-11-21 09:01:20 -07004418 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004419 list_add_tail(&req->list, &ctx->defer_list);
4420 spin_unlock_irq(&ctx->completion_lock);
4421 return -EIOCBQUEUED;
4422}
4423
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004424static void io_cleanup_req(struct io_kiocb *req)
4425{
4426 struct io_async_ctx *io = req->io;
4427
4428 switch (req->opcode) {
4429 case IORING_OP_READV:
4430 case IORING_OP_READ_FIXED:
4431 case IORING_OP_READ:
4432 case IORING_OP_WRITEV:
4433 case IORING_OP_WRITE_FIXED:
4434 case IORING_OP_WRITE:
4435 if (io->rw.iov != io->rw.fast_iov)
4436 kfree(io->rw.iov);
4437 break;
4438 case IORING_OP_SENDMSG:
4439 case IORING_OP_RECVMSG:
4440 if (io->msg.iov != io->msg.fast_iov)
4441 kfree(io->msg.iov);
4442 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004443 case IORING_OP_OPENAT:
4444 case IORING_OP_OPENAT2:
4445 case IORING_OP_STATX:
4446 putname(req->open.filename);
4447 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004448 case IORING_OP_SPLICE:
4449 io_put_file(req, req->splice.file_in,
4450 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4451 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004452 }
4453
4454 req->flags &= ~REQ_F_NEED_CLEANUP;
4455}
4456
Jens Axboe3529d8c2019-12-19 18:24:38 -07004457static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03004458 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004459{
Jackie Liua197f662019-11-08 08:09:12 -07004460 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004461 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004462
Jens Axboed625c6e2019-12-17 19:53:05 -07004463 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004464 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004465 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004466 break;
4467 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004468 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004469 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004470 if (sqe) {
4471 ret = io_read_prep(req, sqe, force_nonblock);
4472 if (ret < 0)
4473 break;
4474 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004475 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004476 break;
4477 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004478 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004479 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004480 if (sqe) {
4481 ret = io_write_prep(req, sqe, force_nonblock);
4482 if (ret < 0)
4483 break;
4484 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004485 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004486 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004487 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004488 if (sqe) {
4489 ret = io_prep_fsync(req, sqe);
4490 if (ret < 0)
4491 break;
4492 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004493 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004494 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004495 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004496 if (sqe) {
4497 ret = io_poll_add_prep(req, sqe);
4498 if (ret)
4499 break;
4500 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004501 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004502 break;
4503 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004504 if (sqe) {
4505 ret = io_poll_remove_prep(req, sqe);
4506 if (ret < 0)
4507 break;
4508 }
Jens Axboefc4df992019-12-10 14:38:45 -07004509 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004510 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004511 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004512 if (sqe) {
4513 ret = io_prep_sfr(req, sqe);
4514 if (ret < 0)
4515 break;
4516 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004517 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004518 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004519 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004520 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004521 if (sqe) {
4522 ret = io_sendmsg_prep(req, sqe);
4523 if (ret < 0)
4524 break;
4525 }
Jens Axboefddafac2020-01-04 20:19:44 -07004526 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004527 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004528 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004529 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004530 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004531 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004532 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004533 if (sqe) {
4534 ret = io_recvmsg_prep(req, sqe);
4535 if (ret)
4536 break;
4537 }
Jens Axboefddafac2020-01-04 20:19:44 -07004538 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004539 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004540 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004541 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004542 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004543 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004544 if (sqe) {
4545 ret = io_timeout_prep(req, sqe, false);
4546 if (ret)
4547 break;
4548 }
Jens Axboefc4df992019-12-10 14:38:45 -07004549 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004550 break;
Jens Axboe11365042019-10-16 09:08:32 -06004551 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004552 if (sqe) {
4553 ret = io_timeout_remove_prep(req, sqe);
4554 if (ret)
4555 break;
4556 }
Jens Axboefc4df992019-12-10 14:38:45 -07004557 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004558 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004559 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004560 if (sqe) {
4561 ret = io_accept_prep(req, sqe);
4562 if (ret)
4563 break;
4564 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004565 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004566 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004567 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004568 if (sqe) {
4569 ret = io_connect_prep(req, sqe);
4570 if (ret)
4571 break;
4572 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004573 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004574 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004575 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004576 if (sqe) {
4577 ret = io_async_cancel_prep(req, sqe);
4578 if (ret)
4579 break;
4580 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004581 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06004582 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004583 case IORING_OP_FALLOCATE:
4584 if (sqe) {
4585 ret = io_fallocate_prep(req, sqe);
4586 if (ret)
4587 break;
4588 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004589 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07004590 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004591 case IORING_OP_OPENAT:
4592 if (sqe) {
4593 ret = io_openat_prep(req, sqe);
4594 if (ret)
4595 break;
4596 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004597 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004598 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004599 case IORING_OP_CLOSE:
4600 if (sqe) {
4601 ret = io_close_prep(req, sqe);
4602 if (ret)
4603 break;
4604 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004605 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07004606 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004607 case IORING_OP_FILES_UPDATE:
4608 if (sqe) {
4609 ret = io_files_update_prep(req, sqe);
4610 if (ret)
4611 break;
4612 }
4613 ret = io_files_update(req, force_nonblock);
4614 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004615 case IORING_OP_STATX:
4616 if (sqe) {
4617 ret = io_statx_prep(req, sqe);
4618 if (ret)
4619 break;
4620 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004621 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004622 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004623 case IORING_OP_FADVISE:
4624 if (sqe) {
4625 ret = io_fadvise_prep(req, sqe);
4626 if (ret)
4627 break;
4628 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004629 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07004630 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004631 case IORING_OP_MADVISE:
4632 if (sqe) {
4633 ret = io_madvise_prep(req, sqe);
4634 if (ret)
4635 break;
4636 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004637 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07004638 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004639 case IORING_OP_OPENAT2:
4640 if (sqe) {
4641 ret = io_openat2_prep(req, sqe);
4642 if (ret)
4643 break;
4644 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004645 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07004646 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004647 case IORING_OP_EPOLL_CTL:
4648 if (sqe) {
4649 ret = io_epoll_ctl_prep(req, sqe);
4650 if (ret)
4651 break;
4652 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004653 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004654 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004655 case IORING_OP_SPLICE:
4656 if (sqe) {
4657 ret = io_splice_prep(req, sqe);
4658 if (ret < 0)
4659 break;
4660 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004661 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004662 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004663 default:
4664 ret = -EINVAL;
4665 break;
4666 }
4667
Jens Axboedef596e2019-01-09 08:59:42 -07004668 if (ret)
4669 return ret;
4670
4671 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004672 const bool in_async = io_wq_current_is_worker();
4673
Jens Axboe9e645e112019-05-10 16:07:28 -06004674 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004675 return -EAGAIN;
4676
Jens Axboe11ba8202020-01-15 21:51:17 -07004677 /* workqueue context doesn't hold uring_lock, grab it now */
4678 if (in_async)
4679 mutex_lock(&ctx->uring_lock);
4680
Jens Axboedef596e2019-01-09 08:59:42 -07004681 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004682
4683 if (in_async)
4684 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004685 }
4686
4687 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004688}
4689
Jens Axboe561fb042019-10-24 07:25:42 -06004690static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004691{
Jens Axboe561fb042019-10-24 07:25:42 -06004692 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004693 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004694 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004695
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004696 /* if NO_CANCEL is set, we must still run the work */
4697 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4698 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004699 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004700 }
Jens Axboe31b51512019-01-18 22:56:34 -07004701
Jens Axboe561fb042019-10-24 07:25:42 -06004702 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004703 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03004704 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004705 /*
4706 * We can get EAGAIN for polled IO even though we're
4707 * forcing a sync submission from here, since we can't
4708 * wait for request slots on the block side.
4709 */
4710 if (ret != -EAGAIN)
4711 break;
4712 cond_resched();
4713 } while (1);
4714 }
Jens Axboe31b51512019-01-18 22:56:34 -07004715
Jens Axboe561fb042019-10-24 07:25:42 -06004716 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004717 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004718 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004719 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004720 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004721
Pavel Begunkov7a743e22020-03-03 21:33:13 +03004722 io_put_req_async_completion(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07004723}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724
Jens Axboe15b71ab2019-12-11 11:20:36 -07004725static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004726{
Jens Axboed3656342019-12-18 09:50:26 -07004727 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004728 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004729 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004730 return 0;
4731 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004732}
4733
Jens Axboe65e19f52019-10-26 07:20:21 -06004734static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4735 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004736{
Jens Axboe65e19f52019-10-26 07:20:21 -06004737 struct fixed_file_table *table;
4738
Jens Axboe05f3fb32019-12-09 11:22:50 -07004739 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4740 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004741}
4742
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004743static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
4744 int fd, struct file **out_file, bool fixed)
4745{
4746 struct io_ring_ctx *ctx = req->ctx;
4747 struct file *file;
4748
4749 if (fixed) {
4750 if (unlikely(!ctx->file_data ||
4751 (unsigned) fd >= ctx->nr_user_files))
4752 return -EBADF;
4753 fd = array_index_nospec(fd, ctx->nr_user_files);
4754 file = io_file_from_index(ctx, fd);
4755 if (!file)
4756 return -EBADF;
4757 percpu_ref_get(&ctx->file_data->refs);
4758 } else {
4759 trace_io_uring_file_get(ctx, fd);
4760 file = __io_file_get(state, fd);
4761 if (unlikely(!file))
4762 return -EBADF;
4763 }
4764
4765 *out_file = file;
4766 return 0;
4767}
4768
Jens Axboe3529d8c2019-12-19 18:24:38 -07004769static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4770 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004771{
4772 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004773 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004774 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06004775
Jens Axboe3529d8c2019-12-19 18:24:38 -07004776 flags = READ_ONCE(sqe->flags);
4777 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004778
Jens Axboed3656342019-12-18 09:50:26 -07004779 if (!io_req_needs_file(req, fd))
4780 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004781
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004782 fixed = (flags & IOSQE_FIXED_FILE);
4783 if (unlikely(!fixed && req->needs_fixed_file))
4784 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004785
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004786 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06004787}
4788
Jackie Liua197f662019-11-08 08:09:12 -07004789static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004790{
Jens Axboefcb323c2019-10-24 12:39:47 -06004791 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004793
Jens Axboef86cd202020-01-29 13:46:44 -07004794 if (req->work.files)
4795 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004796 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004797 return -EBADF;
4798
Jens Axboefcb323c2019-10-24 12:39:47 -06004799 rcu_read_lock();
4800 spin_lock_irq(&ctx->inflight_lock);
4801 /*
4802 * We use the f_ops->flush() handler to ensure that we can flush
4803 * out work accessing these files if the fd is closed. Check if
4804 * the fd has changed since we started down this path, and disallow
4805 * this operation if it has.
4806 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004807 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004808 list_add(&req->inflight_entry, &ctx->inflight_list);
4809 req->flags |= REQ_F_INFLIGHT;
4810 req->work.files = current->files;
4811 ret = 0;
4812 }
4813 spin_unlock_irq(&ctx->inflight_lock);
4814 rcu_read_unlock();
4815
4816 return ret;
4817}
4818
Jens Axboe2665abf2019-11-05 12:40:47 -07004819static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4820{
Jens Axboead8a48a2019-11-15 08:49:11 -07004821 struct io_timeout_data *data = container_of(timer,
4822 struct io_timeout_data, timer);
4823 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004824 struct io_ring_ctx *ctx = req->ctx;
4825 struct io_kiocb *prev = NULL;
4826 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004827
4828 spin_lock_irqsave(&ctx->completion_lock, flags);
4829
4830 /*
4831 * We don't expect the list to be empty, that will only happen if we
4832 * race with the completion of the linked work.
4833 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004834 if (!list_empty(&req->link_list)) {
4835 prev = list_entry(req->link_list.prev, struct io_kiocb,
4836 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004837 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004838 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004839 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4840 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004841 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004842 }
4843
4844 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4845
4846 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004847 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03004848 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004849 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004850 } else {
4851 io_cqring_add_event(req, -ETIME);
4852 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004853 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004854 return HRTIMER_NORESTART;
4855}
4856
Jens Axboead8a48a2019-11-15 08:49:11 -07004857static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004858{
Jens Axboe76a46e02019-11-10 23:34:16 -07004859 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004860
Jens Axboe76a46e02019-11-10 23:34:16 -07004861 /*
4862 * If the list is now empty, then our linked request finished before
4863 * we got a chance to setup the timer
4864 */
4865 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004866 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004867 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004868
Jens Axboead8a48a2019-11-15 08:49:11 -07004869 data->timer.function = io_link_timeout_fn;
4870 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4871 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004872 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004873 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004874
Jens Axboe2665abf2019-11-05 12:40:47 -07004875 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004876 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004877}
4878
Jens Axboead8a48a2019-11-15 08:49:11 -07004879static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004880{
4881 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004882
Jens Axboe2665abf2019-11-05 12:40:47 -07004883 if (!(req->flags & REQ_F_LINK))
4884 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004885 /* for polled retry, if flag is set, we already went through here */
4886 if (req->flags & REQ_F_POLLED)
4887 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004888
Pavel Begunkov44932332019-12-05 16:16:35 +03004889 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4890 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004891 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004892 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004893
Jens Axboe76a46e02019-11-10 23:34:16 -07004894 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004895 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004896}
4897
Jens Axboe3529d8c2019-12-19 18:24:38 -07004898static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004899{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004900 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004901 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07004902 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004903 int ret;
4904
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004905again:
4906 linked_timeout = io_prep_linked_timeout(req);
4907
Jens Axboe193155c2020-02-22 23:22:19 -07004908 if (req->work.creds && req->work.creds != current_cred()) {
4909 if (old_creds)
4910 revert_creds(old_creds);
4911 if (old_creds == req->work.creds)
4912 old_creds = NULL; /* restored original creds */
4913 else
4914 old_creds = override_creds(req->work.creds);
4915 }
4916
Pavel Begunkov014db002020-03-03 21:33:12 +03004917 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004918
4919 /*
4920 * We async punt it if the file wasn't marked NOWAIT, or if the file
4921 * doesn't support non-blocking read/write attempts
4922 */
4923 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4924 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07004925 if (io_arm_poll_handler(req)) {
4926 if (linked_timeout)
4927 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004928 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07004929 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004930punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004931 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004932 ret = io_grab_files(req);
4933 if (ret)
4934 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004935 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004936
4937 /*
4938 * Queued up for async execution, worker will release
4939 * submit reference when the iocb is actually submitted.
4940 */
4941 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004942 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004943 }
Jens Axboee65ef562019-03-12 10:16:44 -06004944
Jens Axboefcb323c2019-10-24 12:39:47 -06004945err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004946 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06004947 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004948 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004949
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004950 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004951 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004952 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004953 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004954 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004955 }
4956
Jens Axboee65ef562019-03-12 10:16:44 -06004957 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004958 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004959 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004960 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004961 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004962 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004963 if (nxt) {
4964 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004965
4966 if (req->flags & REQ_F_FORCE_ASYNC)
4967 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004968 goto again;
4969 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004970exit:
Jens Axboe193155c2020-02-22 23:22:19 -07004971 if (old_creds)
4972 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004973}
4974
Jens Axboe3529d8c2019-12-19 18:24:38 -07004975static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004976{
4977 int ret;
4978
Jens Axboe3529d8c2019-12-19 18:24:38 -07004979 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004980 if (ret) {
4981 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004982fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004983 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004984 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004985 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004986 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004987 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004988 ret = io_req_defer_prep(req, sqe);
4989 if (unlikely(ret < 0))
4990 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004991 /*
4992 * Never try inline submit of IOSQE_ASYNC is set, go straight
4993 * to async execution.
4994 */
4995 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4996 io_queue_async_work(req);
4997 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004998 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004999 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005000}
5001
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005002static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005003{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005004 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005005 io_cqring_add_event(req, -ECANCELED);
5006 io_double_put_req(req);
5007 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005008 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005009}
5010
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005011#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07005012 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06005013
Jens Axboe3529d8c2019-12-19 18:24:38 -07005014static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5015 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005016{
Jackie Liua197f662019-11-08 08:09:12 -07005017 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005018 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005019 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005020
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005021 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005022
5023 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005024 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005025 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005026 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005027 }
5028
Jens Axboe75c6a032020-01-28 10:15:23 -07005029 id = READ_ONCE(sqe->personality);
5030 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005031 req->work.creds = idr_find(&ctx->personality_idr, id);
5032 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005033 ret = -EINVAL;
5034 goto err_req;
5035 }
Jens Axboe193155c2020-02-22 23:22:19 -07005036 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005037 }
5038
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005039 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005040 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5041 IOSQE_ASYNC | IOSQE_FIXED_FILE);
Jens Axboe9e645e112019-05-10 16:07:28 -06005042
Jens Axboe3529d8c2019-12-19 18:24:38 -07005043 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005044 if (unlikely(ret)) {
5045err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005046 io_cqring_add_event(req, ret);
5047 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005048 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005049 }
5050
Jens Axboe9e645e112019-05-10 16:07:28 -06005051 /*
5052 * If we already have a head request, queue this one for async
5053 * submittal once the head completes. If we don't have a head but
5054 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5055 * submitted sync once the chain is complete. If none of those
5056 * conditions are true (normal request), then just queue it.
5057 */
5058 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005059 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005060
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005061 /*
5062 * Taking sequential execution of a link, draining both sides
5063 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5064 * requests in the link. So, it drains the head and the
5065 * next after the link request. The last one is done via
5066 * drain_next flag to persist the effect across calls.
5067 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005068 if (sqe_flags & IOSQE_IO_DRAIN) {
5069 head->flags |= REQ_F_IO_DRAIN;
5070 ctx->drain_next = 1;
5071 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005072 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005073 ret = -EAGAIN;
5074 goto err_req;
5075 }
5076
Jens Axboe3529d8c2019-12-19 18:24:38 -07005077 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005078 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005079 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005080 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005081 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005082 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005083 trace_io_uring_link(ctx, req, head);
5084 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005085
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005086 /* last request of a link, enqueue the link */
5087 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5088 io_queue_link_head(head);
5089 *link = NULL;
5090 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005091 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005092 if (unlikely(ctx->drain_next)) {
5093 req->flags |= REQ_F_IO_DRAIN;
5094 req->ctx->drain_next = 0;
5095 }
5096 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5097 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005098 INIT_LIST_HEAD(&req->link_list);
5099 ret = io_req_defer_prep(req, sqe);
5100 if (ret)
5101 req->flags |= REQ_F_FAIL_LINK;
5102 *link = req;
5103 } else {
5104 io_queue_sqe(req, sqe);
5105 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005106 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005107
5108 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005109}
5110
Jens Axboe9a56a232019-01-09 09:06:50 -07005111/*
5112 * Batched submission is done, ensure local IO is flushed out.
5113 */
5114static void io_submit_state_end(struct io_submit_state *state)
5115{
5116 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005117 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005118 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005119 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005120}
5121
5122/*
5123 * Start submission side cache.
5124 */
5125static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005126 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005127{
5128 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005129 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005130 state->file = NULL;
5131 state->ios_left = max_ios;
5132}
5133
Jens Axboe2b188cc2019-01-07 10:46:33 -07005134static void io_commit_sqring(struct io_ring_ctx *ctx)
5135{
Hristo Venev75b28af2019-08-26 17:23:46 +00005136 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005137
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005138 /*
5139 * Ensure any loads from the SQEs are done at this point,
5140 * since once we write the new head, the application could
5141 * write new data to them.
5142 */
5143 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005144}
5145
5146/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005147 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005148 * that is mapped by userspace. This means that care needs to be taken to
5149 * ensure that reads are stable, as we cannot rely on userspace always
5150 * being a good citizen. If members of the sqe are validated and then later
5151 * used, it's important that those reads are done through READ_ONCE() to
5152 * prevent a re-load down the line.
5153 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005154static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5155 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005156{
Hristo Venev75b28af2019-08-26 17:23:46 +00005157 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005158 unsigned head;
5159
5160 /*
5161 * The cached sq head (or cq tail) serves two purposes:
5162 *
5163 * 1) allows us to batch the cost of updating the user visible
5164 * head updates.
5165 * 2) allows the kernel side to track the head on its own, even
5166 * though the application is the one updating it.
5167 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005168 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005169 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005170 /*
5171 * All io need record the previous position, if LINK vs DARIN,
5172 * it can be used to mark the position of the first IO in the
5173 * link list.
5174 */
5175 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005176 *sqe_ptr = &ctx->sq_sqes[head];
5177 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5178 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005179 ctx->cached_sq_head++;
5180 return true;
5181 }
5182
5183 /* drop invalid entries */
5184 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005185 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005186 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005187 return false;
5188}
5189
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005190static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005191 struct file *ring_file, int ring_fd,
5192 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005193{
5194 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005195 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005196 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005197 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005198
Jens Axboec4a2ed72019-11-21 21:01:26 -07005199 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005200 if (test_bit(0, &ctx->sq_check_overflow)) {
5201 if (!list_empty(&ctx->cq_overflow_list) &&
5202 !io_cqring_overflow_flush(ctx, false))
5203 return -EBUSY;
5204 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005205
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005206 /* make sure SQ entry isn't read before tail */
5207 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005208
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005209 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5210 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005211
5212 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005213 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005214 statep = &state;
5215 }
5216
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005217 ctx->ring_fd = ring_fd;
5218 ctx->ring_file = ring_file;
5219
Jens Axboe6c271ce2019-01-10 11:22:30 -07005220 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005221 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005222 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005223 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005224
Pavel Begunkov196be952019-11-07 01:41:06 +03005225 req = io_get_req(ctx, statep);
5226 if (unlikely(!req)) {
5227 if (!submitted)
5228 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005229 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005230 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005231 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005232 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005233 break;
5234 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005235
Jens Axboed3656342019-12-18 09:50:26 -07005236 /* will complete beyond this point, count as submitted */
5237 submitted++;
5238
5239 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005240 err = -EINVAL;
5241fail_req:
5242 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005243 io_double_put_req(req);
5244 break;
5245 }
5246
5247 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005248 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005249 if (unlikely(mm_fault)) {
5250 err = -EFAULT;
5251 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005252 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005253 use_mm(ctx->sqo_mm);
5254 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005255 }
5256
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005257 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005258 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5259 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005260 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005261 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005262 }
5263
Pavel Begunkov9466f432020-01-25 22:34:01 +03005264 if (unlikely(submitted != nr)) {
5265 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5266
5267 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5268 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005269 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005270 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005271 if (statep)
5272 io_submit_state_end(&state);
5273
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005274 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5275 io_commit_sqring(ctx);
5276
Jens Axboe6c271ce2019-01-10 11:22:30 -07005277 return submitted;
5278}
5279
5280static int io_sq_thread(void *data)
5281{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005282 struct io_ring_ctx *ctx = data;
5283 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005284 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005285 mm_segment_t old_fs;
5286 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005287 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005288 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005289
Jens Axboe206aefd2019-11-07 18:27:42 -07005290 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005291
Jens Axboe6c271ce2019-01-10 11:22:30 -07005292 old_fs = get_fs();
5293 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005294 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005295
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005296 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005297 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005298 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005299
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005300 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005301 unsigned nr_events = 0;
5302
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005303 mutex_lock(&ctx->uring_lock);
5304 if (!list_empty(&ctx->poll_list))
5305 io_iopoll_getevents(ctx, &nr_events, 0);
5306 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005307 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005308 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005309 }
5310
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005311 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005312
5313 /*
5314 * If submit got -EBUSY, flag us as needing the application
5315 * to enter the kernel to reap and flush events.
5316 */
5317 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005318 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005319 * Drop cur_mm before scheduling, we can't hold it for
5320 * long periods (or over schedule()). Do this before
5321 * adding ourselves to the waitqueue, as the unuse/drop
5322 * may sleep.
5323 */
5324 if (cur_mm) {
5325 unuse_mm(cur_mm);
5326 mmput(cur_mm);
5327 cur_mm = NULL;
5328 }
5329
5330 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005331 * We're polling. If we're within the defined idle
5332 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005333 * to sleep. The exception is if we got EBUSY doing
5334 * more IO, we should wait for the application to
5335 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005336 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005337 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005338 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5339 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005340 if (current->task_works)
5341 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005342 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005343 continue;
5344 }
5345
Jens Axboe6c271ce2019-01-10 11:22:30 -07005346 prepare_to_wait(&ctx->sqo_wait, &wait,
5347 TASK_INTERRUPTIBLE);
5348
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005349 /*
5350 * While doing polled IO, before going to sleep, we need
5351 * to check if there are new reqs added to poll_list, it
5352 * is because reqs may have been punted to io worker and
5353 * will be added to poll_list later, hence check the
5354 * poll_list again.
5355 */
5356 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5357 !list_empty_careful(&ctx->poll_list)) {
5358 finish_wait(&ctx->sqo_wait, &wait);
5359 continue;
5360 }
5361
Jens Axboe6c271ce2019-01-10 11:22:30 -07005362 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005363 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005364 /* make sure to read SQ tail after writing flags */
5365 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005366
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005367 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005368 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005369 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005370 finish_wait(&ctx->sqo_wait, &wait);
5371 break;
5372 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005373 if (current->task_works) {
5374 task_work_run();
5375 continue;
5376 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005377 if (signal_pending(current))
5378 flush_signals(current);
5379 schedule();
5380 finish_wait(&ctx->sqo_wait, &wait);
5381
Hristo Venev75b28af2019-08-26 17:23:46 +00005382 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005383 continue;
5384 }
5385 finish_wait(&ctx->sqo_wait, &wait);
5386
Hristo Venev75b28af2019-08-26 17:23:46 +00005387 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005388 }
5389
Jens Axboe8a4955f2019-12-09 14:52:35 -07005390 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005391 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005392 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005393 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005394 }
5395
Jens Axboeb41e9852020-02-17 09:52:41 -07005396 if (current->task_works)
5397 task_work_run();
5398
Jens Axboe6c271ce2019-01-10 11:22:30 -07005399 set_fs(old_fs);
5400 if (cur_mm) {
5401 unuse_mm(cur_mm);
5402 mmput(cur_mm);
5403 }
Jens Axboe181e4482019-11-25 08:52:30 -07005404 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005405
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005406 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005407
Jens Axboe6c271ce2019-01-10 11:22:30 -07005408 return 0;
5409}
5410
Jens Axboebda52162019-09-24 13:47:15 -06005411struct io_wait_queue {
5412 struct wait_queue_entry wq;
5413 struct io_ring_ctx *ctx;
5414 unsigned to_wait;
5415 unsigned nr_timeouts;
5416};
5417
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005418static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005419{
5420 struct io_ring_ctx *ctx = iowq->ctx;
5421
5422 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005423 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005424 * started waiting. For timeouts, we always want to return to userspace,
5425 * regardless of event count.
5426 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005427 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005428 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5429}
5430
5431static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5432 int wake_flags, void *key)
5433{
5434 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5435 wq);
5436
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005437 /* use noflush == true, as we can't safely rely on locking context */
5438 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005439 return -1;
5440
5441 return autoremove_wake_function(curr, mode, wake_flags, key);
5442}
5443
Jens Axboe2b188cc2019-01-07 10:46:33 -07005444/*
5445 * Wait until events become available, if we don't already have some. The
5446 * application must reap them itself, as they reside on the shared cq ring.
5447 */
5448static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5449 const sigset_t __user *sig, size_t sigsz)
5450{
Jens Axboebda52162019-09-24 13:47:15 -06005451 struct io_wait_queue iowq = {
5452 .wq = {
5453 .private = current,
5454 .func = io_wake_function,
5455 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5456 },
5457 .ctx = ctx,
5458 .to_wait = min_events,
5459 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005460 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005461 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005462
Jens Axboeb41e9852020-02-17 09:52:41 -07005463 do {
5464 if (io_cqring_events(ctx, false) >= min_events)
5465 return 0;
5466 if (!current->task_works)
5467 break;
5468 task_work_run();
5469 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005470
5471 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005472#ifdef CONFIG_COMPAT
5473 if (in_compat_syscall())
5474 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005475 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005476 else
5477#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005478 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005479
Jens Axboe2b188cc2019-01-07 10:46:33 -07005480 if (ret)
5481 return ret;
5482 }
5483
Jens Axboebda52162019-09-24 13:47:15 -06005484 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005485 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005486 do {
5487 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5488 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005489 if (current->task_works)
5490 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005491 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005492 break;
5493 schedule();
5494 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005495 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005496 break;
5497 }
5498 } while (1);
5499 finish_wait(&ctx->wait, &iowq.wq);
5500
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005501 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005502
Hristo Venev75b28af2019-08-26 17:23:46 +00005503 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005504}
5505
Jens Axboe6b063142019-01-10 22:13:58 -07005506static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5507{
5508#if defined(CONFIG_UNIX)
5509 if (ctx->ring_sock) {
5510 struct sock *sock = ctx->ring_sock->sk;
5511 struct sk_buff *skb;
5512
5513 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5514 kfree_skb(skb);
5515 }
5516#else
5517 int i;
5518
Jens Axboe65e19f52019-10-26 07:20:21 -06005519 for (i = 0; i < ctx->nr_user_files; i++) {
5520 struct file *file;
5521
5522 file = io_file_from_index(ctx, i);
5523 if (file)
5524 fput(file);
5525 }
Jens Axboe6b063142019-01-10 22:13:58 -07005526#endif
5527}
5528
Jens Axboe05f3fb32019-12-09 11:22:50 -07005529static void io_file_ref_kill(struct percpu_ref *ref)
5530{
5531 struct fixed_file_data *data;
5532
5533 data = container_of(ref, struct fixed_file_data, refs);
5534 complete(&data->done);
5535}
5536
Jens Axboe6b063142019-01-10 22:13:58 -07005537static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5538{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005539 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005540 unsigned nr_tables, i;
5541
Jens Axboe05f3fb32019-12-09 11:22:50 -07005542 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005543 return -ENXIO;
5544
Jens Axboe05f3fb32019-12-09 11:22:50 -07005545 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005546 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005547 wait_for_completion(&data->done);
5548 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005549 percpu_ref_exit(&data->refs);
5550
Jens Axboe6b063142019-01-10 22:13:58 -07005551 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005552 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5553 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005554 kfree(data->table[i].files);
5555 kfree(data->table);
5556 kfree(data);
5557 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005558 ctx->nr_user_files = 0;
5559 return 0;
5560}
5561
Jens Axboe6c271ce2019-01-10 11:22:30 -07005562static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5563{
5564 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005565 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005566 /*
5567 * The park is a bit of a work-around, without it we get
5568 * warning spews on shutdown with SQPOLL set and affinity
5569 * set to a single CPU.
5570 */
Jens Axboe06058632019-04-13 09:26:03 -06005571 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005572 kthread_stop(ctx->sqo_thread);
5573 ctx->sqo_thread = NULL;
5574 }
5575}
5576
Jens Axboe6b063142019-01-10 22:13:58 -07005577static void io_finish_async(struct io_ring_ctx *ctx)
5578{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005579 io_sq_thread_stop(ctx);
5580
Jens Axboe561fb042019-10-24 07:25:42 -06005581 if (ctx->io_wq) {
5582 io_wq_destroy(ctx->io_wq);
5583 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005584 }
5585}
5586
5587#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005588/*
5589 * Ensure the UNIX gc is aware of our file set, so we are certain that
5590 * the io_uring can be safely unregistered on process exit, even if we have
5591 * loops in the file referencing.
5592 */
5593static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5594{
5595 struct sock *sk = ctx->ring_sock->sk;
5596 struct scm_fp_list *fpl;
5597 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005598 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005599
5600 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5601 unsigned long inflight = ctx->user->unix_inflight + nr;
5602
5603 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5604 return -EMFILE;
5605 }
5606
5607 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5608 if (!fpl)
5609 return -ENOMEM;
5610
5611 skb = alloc_skb(0, GFP_KERNEL);
5612 if (!skb) {
5613 kfree(fpl);
5614 return -ENOMEM;
5615 }
5616
5617 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005618
Jens Axboe08a45172019-10-03 08:11:03 -06005619 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005620 fpl->user = get_uid(ctx->user);
5621 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005622 struct file *file = io_file_from_index(ctx, i + offset);
5623
5624 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005625 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005626 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005627 unix_inflight(fpl->user, fpl->fp[nr_files]);
5628 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005629 }
5630
Jens Axboe08a45172019-10-03 08:11:03 -06005631 if (nr_files) {
5632 fpl->max = SCM_MAX_FD;
5633 fpl->count = nr_files;
5634 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005635 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005636 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5637 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005638
Jens Axboe08a45172019-10-03 08:11:03 -06005639 for (i = 0; i < nr_files; i++)
5640 fput(fpl->fp[i]);
5641 } else {
5642 kfree_skb(skb);
5643 kfree(fpl);
5644 }
Jens Axboe6b063142019-01-10 22:13:58 -07005645
5646 return 0;
5647}
5648
5649/*
5650 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5651 * causes regular reference counting to break down. We rely on the UNIX
5652 * garbage collection to take care of this problem for us.
5653 */
5654static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5655{
5656 unsigned left, total;
5657 int ret = 0;
5658
5659 total = 0;
5660 left = ctx->nr_user_files;
5661 while (left) {
5662 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005663
5664 ret = __io_sqe_files_scm(ctx, this_files, total);
5665 if (ret)
5666 break;
5667 left -= this_files;
5668 total += this_files;
5669 }
5670
5671 if (!ret)
5672 return 0;
5673
5674 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005675 struct file *file = io_file_from_index(ctx, total);
5676
5677 if (file)
5678 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005679 total++;
5680 }
5681
5682 return ret;
5683}
5684#else
5685static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5686{
5687 return 0;
5688}
5689#endif
5690
Jens Axboe65e19f52019-10-26 07:20:21 -06005691static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5692 unsigned nr_files)
5693{
5694 int i;
5695
5696 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005697 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005698 unsigned this_files;
5699
5700 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5701 table->files = kcalloc(this_files, sizeof(struct file *),
5702 GFP_KERNEL);
5703 if (!table->files)
5704 break;
5705 nr_files -= this_files;
5706 }
5707
5708 if (i == nr_tables)
5709 return 0;
5710
5711 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005712 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005713 kfree(table->files);
5714 }
5715 return 1;
5716}
5717
Jens Axboe05f3fb32019-12-09 11:22:50 -07005718static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005719{
5720#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005721 struct sock *sock = ctx->ring_sock->sk;
5722 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5723 struct sk_buff *skb;
5724 int i;
5725
5726 __skb_queue_head_init(&list);
5727
5728 /*
5729 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5730 * remove this entry and rearrange the file array.
5731 */
5732 skb = skb_dequeue(head);
5733 while (skb) {
5734 struct scm_fp_list *fp;
5735
5736 fp = UNIXCB(skb).fp;
5737 for (i = 0; i < fp->count; i++) {
5738 int left;
5739
5740 if (fp->fp[i] != file)
5741 continue;
5742
5743 unix_notinflight(fp->user, fp->fp[i]);
5744 left = fp->count - 1 - i;
5745 if (left) {
5746 memmove(&fp->fp[i], &fp->fp[i + 1],
5747 left * sizeof(struct file *));
5748 }
5749 fp->count--;
5750 if (!fp->count) {
5751 kfree_skb(skb);
5752 skb = NULL;
5753 } else {
5754 __skb_queue_tail(&list, skb);
5755 }
5756 fput(file);
5757 file = NULL;
5758 break;
5759 }
5760
5761 if (!file)
5762 break;
5763
5764 __skb_queue_tail(&list, skb);
5765
5766 skb = skb_dequeue(head);
5767 }
5768
5769 if (skb_peek(&list)) {
5770 spin_lock_irq(&head->lock);
5771 while ((skb = __skb_dequeue(&list)) != NULL)
5772 __skb_queue_tail(head, skb);
5773 spin_unlock_irq(&head->lock);
5774 }
5775#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005776 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005777#endif
5778}
5779
Jens Axboe05f3fb32019-12-09 11:22:50 -07005780struct io_file_put {
5781 struct llist_node llist;
5782 struct file *file;
5783 struct completion *done;
5784};
5785
Jens Axboe2faf8522020-02-04 19:54:55 -07005786static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005787{
5788 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005789 struct llist_node *node;
5790
Jens Axboe05f3fb32019-12-09 11:22:50 -07005791 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5792 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5793 io_ring_file_put(data->ctx, pfile->file);
5794 if (pfile->done)
5795 complete(pfile->done);
5796 else
5797 kfree(pfile);
5798 }
5799 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005800}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005801
Jens Axboe2faf8522020-02-04 19:54:55 -07005802static void io_ring_file_ref_switch(struct work_struct *work)
5803{
5804 struct fixed_file_data *data;
5805
5806 data = container_of(work, struct fixed_file_data, ref_work);
5807 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005808 percpu_ref_switch_to_percpu(&data->refs);
5809}
5810
5811static void io_file_data_ref_zero(struct percpu_ref *ref)
5812{
5813 struct fixed_file_data *data;
5814
5815 data = container_of(ref, struct fixed_file_data, refs);
5816
Jens Axboe2faf8522020-02-04 19:54:55 -07005817 /*
5818 * We can't safely switch from inside this context, punt to wq. If
5819 * the table ref is going away, the table is being unregistered.
5820 * Don't queue up the async work for that case, the caller will
5821 * handle it.
5822 */
5823 if (!percpu_ref_is_dying(&data->refs))
5824 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005825}
5826
5827static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5828 unsigned nr_args)
5829{
5830 __s32 __user *fds = (__s32 __user *) arg;
5831 unsigned nr_tables;
5832 struct file *file;
5833 int fd, ret = 0;
5834 unsigned i;
5835
5836 if (ctx->file_data)
5837 return -EBUSY;
5838 if (!nr_args)
5839 return -EINVAL;
5840 if (nr_args > IORING_MAX_FIXED_FILES)
5841 return -EMFILE;
5842
5843 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5844 if (!ctx->file_data)
5845 return -ENOMEM;
5846 ctx->file_data->ctx = ctx;
5847 init_completion(&ctx->file_data->done);
5848
5849 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5850 ctx->file_data->table = kcalloc(nr_tables,
5851 sizeof(struct fixed_file_table),
5852 GFP_KERNEL);
5853 if (!ctx->file_data->table) {
5854 kfree(ctx->file_data);
5855 ctx->file_data = NULL;
5856 return -ENOMEM;
5857 }
5858
5859 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5860 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5861 kfree(ctx->file_data->table);
5862 kfree(ctx->file_data);
5863 ctx->file_data = NULL;
5864 return -ENOMEM;
5865 }
5866 ctx->file_data->put_llist.first = NULL;
5867 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5868
5869 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5870 percpu_ref_exit(&ctx->file_data->refs);
5871 kfree(ctx->file_data->table);
5872 kfree(ctx->file_data);
5873 ctx->file_data = NULL;
5874 return -ENOMEM;
5875 }
5876
5877 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5878 struct fixed_file_table *table;
5879 unsigned index;
5880
5881 ret = -EFAULT;
5882 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5883 break;
5884 /* allow sparse sets */
5885 if (fd == -1) {
5886 ret = 0;
5887 continue;
5888 }
5889
5890 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5891 index = i & IORING_FILE_TABLE_MASK;
5892 file = fget(fd);
5893
5894 ret = -EBADF;
5895 if (!file)
5896 break;
5897
5898 /*
5899 * Don't allow io_uring instances to be registered. If UNIX
5900 * isn't enabled, then this causes a reference cycle and this
5901 * instance can never get freed. If UNIX is enabled we'll
5902 * handle it just fine, but there's still no point in allowing
5903 * a ring fd as it doesn't support regular read/write anyway.
5904 */
5905 if (file->f_op == &io_uring_fops) {
5906 fput(file);
5907 break;
5908 }
5909 ret = 0;
5910 table->files[index] = file;
5911 }
5912
5913 if (ret) {
5914 for (i = 0; i < ctx->nr_user_files; i++) {
5915 file = io_file_from_index(ctx, i);
5916 if (file)
5917 fput(file);
5918 }
5919 for (i = 0; i < nr_tables; i++)
5920 kfree(ctx->file_data->table[i].files);
5921
5922 kfree(ctx->file_data->table);
5923 kfree(ctx->file_data);
5924 ctx->file_data = NULL;
5925 ctx->nr_user_files = 0;
5926 return ret;
5927 }
5928
5929 ret = io_sqe_files_scm(ctx);
5930 if (ret)
5931 io_sqe_files_unregister(ctx);
5932
5933 return ret;
5934}
5935
Jens Axboec3a31e62019-10-03 13:59:56 -06005936static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5937 int index)
5938{
5939#if defined(CONFIG_UNIX)
5940 struct sock *sock = ctx->ring_sock->sk;
5941 struct sk_buff_head *head = &sock->sk_receive_queue;
5942 struct sk_buff *skb;
5943
5944 /*
5945 * See if we can merge this file into an existing skb SCM_RIGHTS
5946 * file set. If there's no room, fall back to allocating a new skb
5947 * and filling it in.
5948 */
5949 spin_lock_irq(&head->lock);
5950 skb = skb_peek(head);
5951 if (skb) {
5952 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5953
5954 if (fpl->count < SCM_MAX_FD) {
5955 __skb_unlink(skb, head);
5956 spin_unlock_irq(&head->lock);
5957 fpl->fp[fpl->count] = get_file(file);
5958 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5959 fpl->count++;
5960 spin_lock_irq(&head->lock);
5961 __skb_queue_head(head, skb);
5962 } else {
5963 skb = NULL;
5964 }
5965 }
5966 spin_unlock_irq(&head->lock);
5967
5968 if (skb) {
5969 fput(file);
5970 return 0;
5971 }
5972
5973 return __io_sqe_files_scm(ctx, 1, index);
5974#else
5975 return 0;
5976#endif
5977}
5978
Jens Axboe05f3fb32019-12-09 11:22:50 -07005979static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005980{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005981 struct fixed_file_data *data;
5982
Jens Axboedd3db2a2020-02-26 10:23:43 -07005983 /*
5984 * Juggle reference to ensure we hit zero, if needed, so we can
5985 * switch back to percpu mode
5986 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005987 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005988 percpu_ref_put(&data->refs);
5989 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005990}
5991
5992static bool io_queue_file_removal(struct fixed_file_data *data,
5993 struct file *file)
5994{
5995 struct io_file_put *pfile, pfile_stack;
5996 DECLARE_COMPLETION_ONSTACK(done);
5997
5998 /*
5999 * If we fail allocating the struct we need for doing async reomval
6000 * of this file, just punt to sync and wait for it.
6001 */
6002 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6003 if (!pfile) {
6004 pfile = &pfile_stack;
6005 pfile->done = &done;
6006 }
6007
6008 pfile->file = file;
6009 llist_add(&pfile->llist, &data->put_llist);
6010
6011 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006012 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006013 wait_for_completion(&done);
6014 flush_work(&data->ref_work);
6015 return false;
6016 }
6017
6018 return true;
6019}
6020
6021static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6022 struct io_uring_files_update *up,
6023 unsigned nr_args)
6024{
6025 struct fixed_file_data *data = ctx->file_data;
6026 bool ref_switch = false;
6027 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006028 __s32 __user *fds;
6029 int fd, i, err;
6030 __u32 done;
6031
Jens Axboe05f3fb32019-12-09 11:22:50 -07006032 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006033 return -EOVERFLOW;
6034 if (done > ctx->nr_user_files)
6035 return -EINVAL;
6036
6037 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006038 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006039 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006040 struct fixed_file_table *table;
6041 unsigned index;
6042
Jens Axboec3a31e62019-10-03 13:59:56 -06006043 err = 0;
6044 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6045 err = -EFAULT;
6046 break;
6047 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006048 i = array_index_nospec(up->offset, ctx->nr_user_files);
6049 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006050 index = i & IORING_FILE_TABLE_MASK;
6051 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006052 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006053 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006054 if (io_queue_file_removal(data, file))
6055 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006056 }
6057 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006058 file = fget(fd);
6059 if (!file) {
6060 err = -EBADF;
6061 break;
6062 }
6063 /*
6064 * Don't allow io_uring instances to be registered. If
6065 * UNIX isn't enabled, then this causes a reference
6066 * cycle and this instance can never get freed. If UNIX
6067 * is enabled we'll handle it just fine, but there's
6068 * still no point in allowing a ring fd as it doesn't
6069 * support regular read/write anyway.
6070 */
6071 if (file->f_op == &io_uring_fops) {
6072 fput(file);
6073 err = -EBADF;
6074 break;
6075 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006076 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006077 err = io_sqe_file_register(ctx, file, i);
6078 if (err)
6079 break;
6080 }
6081 nr_args--;
6082 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006083 up->offset++;
6084 }
6085
Jens Axboedd3db2a2020-02-26 10:23:43 -07006086 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006087 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006088
6089 return done ? done : err;
6090}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006091static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6092 unsigned nr_args)
6093{
6094 struct io_uring_files_update up;
6095
6096 if (!ctx->file_data)
6097 return -ENXIO;
6098 if (!nr_args)
6099 return -EINVAL;
6100 if (copy_from_user(&up, arg, sizeof(up)))
6101 return -EFAULT;
6102 if (up.resv)
6103 return -EINVAL;
6104
6105 return __io_sqe_files_update(ctx, &up, nr_args);
6106}
Jens Axboec3a31e62019-10-03 13:59:56 -06006107
Jens Axboe7d723062019-11-12 22:31:31 -07006108static void io_put_work(struct io_wq_work *work)
6109{
6110 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6111
Pavel Begunkov7a743e22020-03-03 21:33:13 +03006112 /* Consider that io_put_req_async_completion() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006113 io_put_req(req);
6114}
6115
6116static void io_get_work(struct io_wq_work *work)
6117{
6118 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6119
6120 refcount_inc(&req->refs);
6121}
6122
Pavel Begunkov24369c22020-01-28 03:15:48 +03006123static int io_init_wq_offload(struct io_ring_ctx *ctx,
6124 struct io_uring_params *p)
6125{
6126 struct io_wq_data data;
6127 struct fd f;
6128 struct io_ring_ctx *ctx_attach;
6129 unsigned int concurrency;
6130 int ret = 0;
6131
6132 data.user = ctx->user;
6133 data.get_work = io_get_work;
6134 data.put_work = io_put_work;
6135
6136 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6137 /* Do QD, or 4 * CPUS, whatever is smallest */
6138 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6139
6140 ctx->io_wq = io_wq_create(concurrency, &data);
6141 if (IS_ERR(ctx->io_wq)) {
6142 ret = PTR_ERR(ctx->io_wq);
6143 ctx->io_wq = NULL;
6144 }
6145 return ret;
6146 }
6147
6148 f = fdget(p->wq_fd);
6149 if (!f.file)
6150 return -EBADF;
6151
6152 if (f.file->f_op != &io_uring_fops) {
6153 ret = -EINVAL;
6154 goto out_fput;
6155 }
6156
6157 ctx_attach = f.file->private_data;
6158 /* @io_wq is protected by holding the fd */
6159 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6160 ret = -EINVAL;
6161 goto out_fput;
6162 }
6163
6164 ctx->io_wq = ctx_attach->io_wq;
6165out_fput:
6166 fdput(f);
6167 return ret;
6168}
6169
Jens Axboe6c271ce2019-01-10 11:22:30 -07006170static int io_sq_offload_start(struct io_ring_ctx *ctx,
6171 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006172{
6173 int ret;
6174
Jens Axboe6c271ce2019-01-10 11:22:30 -07006175 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006176 mmgrab(current->mm);
6177 ctx->sqo_mm = current->mm;
6178
Jens Axboe6c271ce2019-01-10 11:22:30 -07006179 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006180 ret = -EPERM;
6181 if (!capable(CAP_SYS_ADMIN))
6182 goto err;
6183
Jens Axboe917257d2019-04-13 09:28:55 -06006184 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6185 if (!ctx->sq_thread_idle)
6186 ctx->sq_thread_idle = HZ;
6187
Jens Axboe6c271ce2019-01-10 11:22:30 -07006188 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006189 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006190
Jens Axboe917257d2019-04-13 09:28:55 -06006191 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006192 if (cpu >= nr_cpu_ids)
6193 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006194 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006195 goto err;
6196
Jens Axboe6c271ce2019-01-10 11:22:30 -07006197 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6198 ctx, cpu,
6199 "io_uring-sq");
6200 } else {
6201 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6202 "io_uring-sq");
6203 }
6204 if (IS_ERR(ctx->sqo_thread)) {
6205 ret = PTR_ERR(ctx->sqo_thread);
6206 ctx->sqo_thread = NULL;
6207 goto err;
6208 }
6209 wake_up_process(ctx->sqo_thread);
6210 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6211 /* Can't have SQ_AFF without SQPOLL */
6212 ret = -EINVAL;
6213 goto err;
6214 }
6215
Pavel Begunkov24369c22020-01-28 03:15:48 +03006216 ret = io_init_wq_offload(ctx, p);
6217 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006218 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006219
6220 return 0;
6221err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006222 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223 mmdrop(ctx->sqo_mm);
6224 ctx->sqo_mm = NULL;
6225 return ret;
6226}
6227
6228static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6229{
6230 atomic_long_sub(nr_pages, &user->locked_vm);
6231}
6232
6233static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6234{
6235 unsigned long page_limit, cur_pages, new_pages;
6236
6237 /* Don't allow more pages than we can safely lock */
6238 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6239
6240 do {
6241 cur_pages = atomic_long_read(&user->locked_vm);
6242 new_pages = cur_pages + nr_pages;
6243 if (new_pages > page_limit)
6244 return -ENOMEM;
6245 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6246 new_pages) != cur_pages);
6247
6248 return 0;
6249}
6250
6251static void io_mem_free(void *ptr)
6252{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006253 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006254
Mark Rutland52e04ef2019-04-30 17:30:21 +01006255 if (!ptr)
6256 return;
6257
6258 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006259 if (put_page_testzero(page))
6260 free_compound_page(page);
6261}
6262
6263static void *io_mem_alloc(size_t size)
6264{
6265 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6266 __GFP_NORETRY;
6267
6268 return (void *) __get_free_pages(gfp_flags, get_order(size));
6269}
6270
Hristo Venev75b28af2019-08-26 17:23:46 +00006271static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6272 size_t *sq_offset)
6273{
6274 struct io_rings *rings;
6275 size_t off, sq_array_size;
6276
6277 off = struct_size(rings, cqes, cq_entries);
6278 if (off == SIZE_MAX)
6279 return SIZE_MAX;
6280
6281#ifdef CONFIG_SMP
6282 off = ALIGN(off, SMP_CACHE_BYTES);
6283 if (off == 0)
6284 return SIZE_MAX;
6285#endif
6286
6287 sq_array_size = array_size(sizeof(u32), sq_entries);
6288 if (sq_array_size == SIZE_MAX)
6289 return SIZE_MAX;
6290
6291 if (check_add_overflow(off, sq_array_size, &off))
6292 return SIZE_MAX;
6293
6294 if (sq_offset)
6295 *sq_offset = off;
6296
6297 return off;
6298}
6299
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6301{
Hristo Venev75b28af2019-08-26 17:23:46 +00006302 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303
Hristo Venev75b28af2019-08-26 17:23:46 +00006304 pages = (size_t)1 << get_order(
6305 rings_size(sq_entries, cq_entries, NULL));
6306 pages += (size_t)1 << get_order(
6307 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006308
Hristo Venev75b28af2019-08-26 17:23:46 +00006309 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006310}
6311
Jens Axboeedafcce2019-01-09 09:16:05 -07006312static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6313{
6314 int i, j;
6315
6316 if (!ctx->user_bufs)
6317 return -ENXIO;
6318
6319 for (i = 0; i < ctx->nr_user_bufs; i++) {
6320 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6321
6322 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006323 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006324
6325 if (ctx->account_mem)
6326 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006327 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006328 imu->nr_bvecs = 0;
6329 }
6330
6331 kfree(ctx->user_bufs);
6332 ctx->user_bufs = NULL;
6333 ctx->nr_user_bufs = 0;
6334 return 0;
6335}
6336
6337static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6338 void __user *arg, unsigned index)
6339{
6340 struct iovec __user *src;
6341
6342#ifdef CONFIG_COMPAT
6343 if (ctx->compat) {
6344 struct compat_iovec __user *ciovs;
6345 struct compat_iovec ciov;
6346
6347 ciovs = (struct compat_iovec __user *) arg;
6348 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6349 return -EFAULT;
6350
Jens Axboed55e5f52019-12-11 16:12:15 -07006351 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006352 dst->iov_len = ciov.iov_len;
6353 return 0;
6354 }
6355#endif
6356 src = (struct iovec __user *) arg;
6357 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6358 return -EFAULT;
6359 return 0;
6360}
6361
6362static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6363 unsigned nr_args)
6364{
6365 struct vm_area_struct **vmas = NULL;
6366 struct page **pages = NULL;
6367 int i, j, got_pages = 0;
6368 int ret = -EINVAL;
6369
6370 if (ctx->user_bufs)
6371 return -EBUSY;
6372 if (!nr_args || nr_args > UIO_MAXIOV)
6373 return -EINVAL;
6374
6375 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6376 GFP_KERNEL);
6377 if (!ctx->user_bufs)
6378 return -ENOMEM;
6379
6380 for (i = 0; i < nr_args; i++) {
6381 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6382 unsigned long off, start, end, ubuf;
6383 int pret, nr_pages;
6384 struct iovec iov;
6385 size_t size;
6386
6387 ret = io_copy_iov(ctx, &iov, arg, i);
6388 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006389 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006390
6391 /*
6392 * Don't impose further limits on the size and buffer
6393 * constraints here, we'll -EINVAL later when IO is
6394 * submitted if they are wrong.
6395 */
6396 ret = -EFAULT;
6397 if (!iov.iov_base || !iov.iov_len)
6398 goto err;
6399
6400 /* arbitrary limit, but we need something */
6401 if (iov.iov_len > SZ_1G)
6402 goto err;
6403
6404 ubuf = (unsigned long) iov.iov_base;
6405 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6406 start = ubuf >> PAGE_SHIFT;
6407 nr_pages = end - start;
6408
6409 if (ctx->account_mem) {
6410 ret = io_account_mem(ctx->user, nr_pages);
6411 if (ret)
6412 goto err;
6413 }
6414
6415 ret = 0;
6416 if (!pages || nr_pages > got_pages) {
6417 kfree(vmas);
6418 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006419 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006420 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006421 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006422 sizeof(struct vm_area_struct *),
6423 GFP_KERNEL);
6424 if (!pages || !vmas) {
6425 ret = -ENOMEM;
6426 if (ctx->account_mem)
6427 io_unaccount_mem(ctx->user, nr_pages);
6428 goto err;
6429 }
6430 got_pages = nr_pages;
6431 }
6432
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006433 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006434 GFP_KERNEL);
6435 ret = -ENOMEM;
6436 if (!imu->bvec) {
6437 if (ctx->account_mem)
6438 io_unaccount_mem(ctx->user, nr_pages);
6439 goto err;
6440 }
6441
6442 ret = 0;
6443 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006444 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006445 FOLL_WRITE | FOLL_LONGTERM,
6446 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006447 if (pret == nr_pages) {
6448 /* don't support file backed memory */
6449 for (j = 0; j < nr_pages; j++) {
6450 struct vm_area_struct *vma = vmas[j];
6451
6452 if (vma->vm_file &&
6453 !is_file_hugepages(vma->vm_file)) {
6454 ret = -EOPNOTSUPP;
6455 break;
6456 }
6457 }
6458 } else {
6459 ret = pret < 0 ? pret : -EFAULT;
6460 }
6461 up_read(&current->mm->mmap_sem);
6462 if (ret) {
6463 /*
6464 * if we did partial map, or found file backed vmas,
6465 * release any pages we did get
6466 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006467 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006468 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006469 if (ctx->account_mem)
6470 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006471 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006472 goto err;
6473 }
6474
6475 off = ubuf & ~PAGE_MASK;
6476 size = iov.iov_len;
6477 for (j = 0; j < nr_pages; j++) {
6478 size_t vec_len;
6479
6480 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6481 imu->bvec[j].bv_page = pages[j];
6482 imu->bvec[j].bv_len = vec_len;
6483 imu->bvec[j].bv_offset = off;
6484 off = 0;
6485 size -= vec_len;
6486 }
6487 /* store original address for later verification */
6488 imu->ubuf = ubuf;
6489 imu->len = iov.iov_len;
6490 imu->nr_bvecs = nr_pages;
6491
6492 ctx->nr_user_bufs++;
6493 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006494 kvfree(pages);
6495 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006496 return 0;
6497err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006498 kvfree(pages);
6499 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006500 io_sqe_buffer_unregister(ctx);
6501 return ret;
6502}
6503
Jens Axboe9b402842019-04-11 11:45:41 -06006504static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6505{
6506 __s32 __user *fds = arg;
6507 int fd;
6508
6509 if (ctx->cq_ev_fd)
6510 return -EBUSY;
6511
6512 if (copy_from_user(&fd, fds, sizeof(*fds)))
6513 return -EFAULT;
6514
6515 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6516 if (IS_ERR(ctx->cq_ev_fd)) {
6517 int ret = PTR_ERR(ctx->cq_ev_fd);
6518 ctx->cq_ev_fd = NULL;
6519 return ret;
6520 }
6521
6522 return 0;
6523}
6524
6525static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6526{
6527 if (ctx->cq_ev_fd) {
6528 eventfd_ctx_put(ctx->cq_ev_fd);
6529 ctx->cq_ev_fd = NULL;
6530 return 0;
6531 }
6532
6533 return -ENXIO;
6534}
6535
Jens Axboe2b188cc2019-01-07 10:46:33 -07006536static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6537{
Jens Axboe6b063142019-01-10 22:13:58 -07006538 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006539 if (ctx->sqo_mm)
6540 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006541
6542 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006543 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006544 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006545 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006546 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006547
Jens Axboe2b188cc2019-01-07 10:46:33 -07006548#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006549 if (ctx->ring_sock) {
6550 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006551 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006552 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006553#endif
6554
Hristo Venev75b28af2019-08-26 17:23:46 +00006555 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006556 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006557
6558 percpu_ref_exit(&ctx->refs);
6559 if (ctx->account_mem)
6560 io_unaccount_mem(ctx->user,
6561 ring_pages(ctx->sq_entries, ctx->cq_entries));
6562 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006563 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006564 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006565 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006566 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006567 kfree(ctx);
6568}
6569
6570static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6571{
6572 struct io_ring_ctx *ctx = file->private_data;
6573 __poll_t mask = 0;
6574
6575 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006576 /*
6577 * synchronizes with barrier from wq_has_sleeper call in
6578 * io_commit_cqring
6579 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006580 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006581 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6582 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006583 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006584 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006585 mask |= EPOLLIN | EPOLLRDNORM;
6586
6587 return mask;
6588}
6589
6590static int io_uring_fasync(int fd, struct file *file, int on)
6591{
6592 struct io_ring_ctx *ctx = file->private_data;
6593
6594 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6595}
6596
Jens Axboe071698e2020-01-28 10:04:42 -07006597static int io_remove_personalities(int id, void *p, void *data)
6598{
6599 struct io_ring_ctx *ctx = data;
6600 const struct cred *cred;
6601
6602 cred = idr_remove(&ctx->personality_idr, id);
6603 if (cred)
6604 put_cred(cred);
6605 return 0;
6606}
6607
Jens Axboe2b188cc2019-01-07 10:46:33 -07006608static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6609{
6610 mutex_lock(&ctx->uring_lock);
6611 percpu_ref_kill(&ctx->refs);
6612 mutex_unlock(&ctx->uring_lock);
6613
Jens Axboedf069d82020-02-04 16:48:34 -07006614 /*
6615 * Wait for sq thread to idle, if we have one. It won't spin on new
6616 * work after we've killed the ctx ref above. This is important to do
6617 * before we cancel existing commands, as the thread could otherwise
6618 * be queueing new work post that. If that's work we need to cancel,
6619 * it could cause shutdown to hang.
6620 */
6621 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6622 cpu_relax();
6623
Jens Axboe5262f562019-09-17 12:26:57 -06006624 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006625 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006626
6627 if (ctx->io_wq)
6628 io_wq_cancel_all(ctx->io_wq);
6629
Jens Axboedef596e2019-01-09 08:59:42 -07006630 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006631 /* if we failed setting up the ctx, we might not have any rings */
6632 if (ctx->rings)
6633 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006634 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006635 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006636 io_ring_ctx_free(ctx);
6637}
6638
6639static int io_uring_release(struct inode *inode, struct file *file)
6640{
6641 struct io_ring_ctx *ctx = file->private_data;
6642
6643 file->private_data = NULL;
6644 io_ring_ctx_wait_and_kill(ctx);
6645 return 0;
6646}
6647
Jens Axboefcb323c2019-10-24 12:39:47 -06006648static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6649 struct files_struct *files)
6650{
6651 struct io_kiocb *req;
6652 DEFINE_WAIT(wait);
6653
6654 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006655 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006656
6657 spin_lock_irq(&ctx->inflight_lock);
6658 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006659 if (req->work.files != files)
6660 continue;
6661 /* req is being completed, ignore */
6662 if (!refcount_inc_not_zero(&req->refs))
6663 continue;
6664 cancel_req = req;
6665 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006666 }
Jens Axboe768134d2019-11-10 20:30:53 -07006667 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006668 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006669 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006670 spin_unlock_irq(&ctx->inflight_lock);
6671
Jens Axboe768134d2019-11-10 20:30:53 -07006672 /* We need to keep going until we don't find a matching req */
6673 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006674 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006675
Jens Axboe2ca10252020-02-13 17:17:35 -07006676 if (cancel_req->flags & REQ_F_OVERFLOW) {
6677 spin_lock_irq(&ctx->completion_lock);
6678 list_del(&cancel_req->list);
6679 cancel_req->flags &= ~REQ_F_OVERFLOW;
6680 if (list_empty(&ctx->cq_overflow_list)) {
6681 clear_bit(0, &ctx->sq_check_overflow);
6682 clear_bit(0, &ctx->cq_check_overflow);
6683 }
6684 spin_unlock_irq(&ctx->completion_lock);
6685
6686 WRITE_ONCE(ctx->rings->cq_overflow,
6687 atomic_inc_return(&ctx->cached_cq_overflow));
6688
6689 /*
6690 * Put inflight ref and overflow ref. If that's
6691 * all we had, then we're done with this request.
6692 */
6693 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6694 io_put_req(cancel_req);
6695 continue;
6696 }
6697 }
6698
Bob Liu2f6d9b92019-11-13 18:06:24 +08006699 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6700 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006701 schedule();
6702 }
Jens Axboe768134d2019-11-10 20:30:53 -07006703 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006704}
6705
6706static int io_uring_flush(struct file *file, void *data)
6707{
6708 struct io_ring_ctx *ctx = file->private_data;
6709
6710 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006711
6712 /*
6713 * If the task is going away, cancel work it may have pending
6714 */
6715 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6716 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6717
Jens Axboefcb323c2019-10-24 12:39:47 -06006718 return 0;
6719}
6720
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006721static void *io_uring_validate_mmap_request(struct file *file,
6722 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006723{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006724 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006725 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006726 struct page *page;
6727 void *ptr;
6728
6729 switch (offset) {
6730 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006731 case IORING_OFF_CQ_RING:
6732 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006733 break;
6734 case IORING_OFF_SQES:
6735 ptr = ctx->sq_sqes;
6736 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006737 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006738 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006739 }
6740
6741 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006742 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006743 return ERR_PTR(-EINVAL);
6744
6745 return ptr;
6746}
6747
6748#ifdef CONFIG_MMU
6749
6750static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6751{
6752 size_t sz = vma->vm_end - vma->vm_start;
6753 unsigned long pfn;
6754 void *ptr;
6755
6756 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6757 if (IS_ERR(ptr))
6758 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006759
6760 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6761 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6762}
6763
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006764#else /* !CONFIG_MMU */
6765
6766static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6767{
6768 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6769}
6770
6771static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6772{
6773 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6774}
6775
6776static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6777 unsigned long addr, unsigned long len,
6778 unsigned long pgoff, unsigned long flags)
6779{
6780 void *ptr;
6781
6782 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6783 if (IS_ERR(ptr))
6784 return PTR_ERR(ptr);
6785
6786 return (unsigned long) ptr;
6787}
6788
6789#endif /* !CONFIG_MMU */
6790
Jens Axboe2b188cc2019-01-07 10:46:33 -07006791SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6792 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6793 size_t, sigsz)
6794{
6795 struct io_ring_ctx *ctx;
6796 long ret = -EBADF;
6797 int submitted = 0;
6798 struct fd f;
6799
Jens Axboeb41e9852020-02-17 09:52:41 -07006800 if (current->task_works)
6801 task_work_run();
6802
Jens Axboe6c271ce2019-01-10 11:22:30 -07006803 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006804 return -EINVAL;
6805
6806 f = fdget(fd);
6807 if (!f.file)
6808 return -EBADF;
6809
6810 ret = -EOPNOTSUPP;
6811 if (f.file->f_op != &io_uring_fops)
6812 goto out_fput;
6813
6814 ret = -ENXIO;
6815 ctx = f.file->private_data;
6816 if (!percpu_ref_tryget(&ctx->refs))
6817 goto out_fput;
6818
Jens Axboe6c271ce2019-01-10 11:22:30 -07006819 /*
6820 * For SQ polling, the thread will do all submissions and completions.
6821 * Just return the requested submit count, and wake the thread if
6822 * we were asked to.
6823 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006824 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006825 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006826 if (!list_empty_careful(&ctx->cq_overflow_list))
6827 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006828 if (flags & IORING_ENTER_SQ_WAKEUP)
6829 wake_up(&ctx->sqo_wait);
6830 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006831 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006832 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006833
6834 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006835 /* already have mm, so io_submit_sqes() won't try to grab it */
6836 cur_mm = ctx->sqo_mm;
6837 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6838 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006839 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006840
6841 if (submitted != to_submit)
6842 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006843 }
6844 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006845 unsigned nr_events = 0;
6846
Jens Axboe2b188cc2019-01-07 10:46:33 -07006847 min_complete = min(min_complete, ctx->cq_entries);
6848
Jens Axboedef596e2019-01-09 08:59:42 -07006849 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006850 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006851 } else {
6852 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6853 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006854 }
6855
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006856out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006857 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006858out_fput:
6859 fdput(f);
6860 return submitted ? submitted : ret;
6861}
6862
Tobias Klauserbebdb652020-02-26 18:38:32 +01006863#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006864static int io_uring_show_cred(int id, void *p, void *data)
6865{
6866 const struct cred *cred = p;
6867 struct seq_file *m = data;
6868 struct user_namespace *uns = seq_user_ns(m);
6869 struct group_info *gi;
6870 kernel_cap_t cap;
6871 unsigned __capi;
6872 int g;
6873
6874 seq_printf(m, "%5d\n", id);
6875 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6876 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6877 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6878 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6879 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6880 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6881 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6882 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6883 seq_puts(m, "\n\tGroups:\t");
6884 gi = cred->group_info;
6885 for (g = 0; g < gi->ngroups; g++) {
6886 seq_put_decimal_ull(m, g ? " " : "",
6887 from_kgid_munged(uns, gi->gid[g]));
6888 }
6889 seq_puts(m, "\n\tCapEff:\t");
6890 cap = cred->cap_effective;
6891 CAP_FOR_EACH_U32(__capi)
6892 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6893 seq_putc(m, '\n');
6894 return 0;
6895}
6896
6897static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6898{
6899 int i;
6900
6901 mutex_lock(&ctx->uring_lock);
6902 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6903 for (i = 0; i < ctx->nr_user_files; i++) {
6904 struct fixed_file_table *table;
6905 struct file *f;
6906
6907 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6908 f = table->files[i & IORING_FILE_TABLE_MASK];
6909 if (f)
6910 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6911 else
6912 seq_printf(m, "%5u: <none>\n", i);
6913 }
6914 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6915 for (i = 0; i < ctx->nr_user_bufs; i++) {
6916 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6917
6918 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6919 (unsigned int) buf->len);
6920 }
6921 if (!idr_is_empty(&ctx->personality_idr)) {
6922 seq_printf(m, "Personalities:\n");
6923 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6924 }
Jens Axboed7718a92020-02-14 22:23:12 -07006925 seq_printf(m, "PollList:\n");
6926 spin_lock_irq(&ctx->completion_lock);
6927 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
6928 struct hlist_head *list = &ctx->cancel_hash[i];
6929 struct io_kiocb *req;
6930
6931 hlist_for_each_entry(req, list, hash_node)
6932 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
6933 req->task->task_works != NULL);
6934 }
6935 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07006936 mutex_unlock(&ctx->uring_lock);
6937}
6938
6939static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6940{
6941 struct io_ring_ctx *ctx = f->private_data;
6942
6943 if (percpu_ref_tryget(&ctx->refs)) {
6944 __io_uring_show_fdinfo(ctx, m);
6945 percpu_ref_put(&ctx->refs);
6946 }
6947}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006948#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006949
Jens Axboe2b188cc2019-01-07 10:46:33 -07006950static const struct file_operations io_uring_fops = {
6951 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006952 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006953 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006954#ifndef CONFIG_MMU
6955 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6956 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6957#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006958 .poll = io_uring_poll,
6959 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006960#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006961 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006962#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006963};
6964
6965static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6966 struct io_uring_params *p)
6967{
Hristo Venev75b28af2019-08-26 17:23:46 +00006968 struct io_rings *rings;
6969 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006970
Hristo Venev75b28af2019-08-26 17:23:46 +00006971 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6972 if (size == SIZE_MAX)
6973 return -EOVERFLOW;
6974
6975 rings = io_mem_alloc(size);
6976 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006977 return -ENOMEM;
6978
Hristo Venev75b28af2019-08-26 17:23:46 +00006979 ctx->rings = rings;
6980 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6981 rings->sq_ring_mask = p->sq_entries - 1;
6982 rings->cq_ring_mask = p->cq_entries - 1;
6983 rings->sq_ring_entries = p->sq_entries;
6984 rings->cq_ring_entries = p->cq_entries;
6985 ctx->sq_mask = rings->sq_ring_mask;
6986 ctx->cq_mask = rings->cq_ring_mask;
6987 ctx->sq_entries = rings->sq_ring_entries;
6988 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006989
6990 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006991 if (size == SIZE_MAX) {
6992 io_mem_free(ctx->rings);
6993 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006994 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006995 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006996
6997 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006998 if (!ctx->sq_sqes) {
6999 io_mem_free(ctx->rings);
7000 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007001 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007002 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007003
Jens Axboe2b188cc2019-01-07 10:46:33 -07007004 return 0;
7005}
7006
7007/*
7008 * Allocate an anonymous fd, this is what constitutes the application
7009 * visible backing of an io_uring instance. The application mmaps this
7010 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7011 * we have to tie this fd to a socket for file garbage collection purposes.
7012 */
7013static int io_uring_get_fd(struct io_ring_ctx *ctx)
7014{
7015 struct file *file;
7016 int ret;
7017
7018#if defined(CONFIG_UNIX)
7019 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7020 &ctx->ring_sock);
7021 if (ret)
7022 return ret;
7023#endif
7024
7025 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7026 if (ret < 0)
7027 goto err;
7028
7029 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7030 O_RDWR | O_CLOEXEC);
7031 if (IS_ERR(file)) {
7032 put_unused_fd(ret);
7033 ret = PTR_ERR(file);
7034 goto err;
7035 }
7036
7037#if defined(CONFIG_UNIX)
7038 ctx->ring_sock->file = file;
7039#endif
7040 fd_install(ret, file);
7041 return ret;
7042err:
7043#if defined(CONFIG_UNIX)
7044 sock_release(ctx->ring_sock);
7045 ctx->ring_sock = NULL;
7046#endif
7047 return ret;
7048}
7049
7050static int io_uring_create(unsigned entries, struct io_uring_params *p)
7051{
7052 struct user_struct *user = NULL;
7053 struct io_ring_ctx *ctx;
7054 bool account_mem;
7055 int ret;
7056
Jens Axboe8110c1a2019-12-28 15:39:54 -07007057 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007058 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007059 if (entries > IORING_MAX_ENTRIES) {
7060 if (!(p->flags & IORING_SETUP_CLAMP))
7061 return -EINVAL;
7062 entries = IORING_MAX_ENTRIES;
7063 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007064
7065 /*
7066 * Use twice as many entries for the CQ ring. It's possible for the
7067 * application to drive a higher depth than the size of the SQ ring,
7068 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007069 * some flexibility in overcommitting a bit. If the application has
7070 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7071 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007072 */
7073 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007074 if (p->flags & IORING_SETUP_CQSIZE) {
7075 /*
7076 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7077 * to a power-of-two, if it isn't already. We do NOT impose
7078 * any cq vs sq ring sizing.
7079 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007080 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007081 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007082 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7083 if (!(p->flags & IORING_SETUP_CLAMP))
7084 return -EINVAL;
7085 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7086 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007087 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7088 } else {
7089 p->cq_entries = 2 * p->sq_entries;
7090 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007091
7092 user = get_uid(current_user());
7093 account_mem = !capable(CAP_IPC_LOCK);
7094
7095 if (account_mem) {
7096 ret = io_account_mem(user,
7097 ring_pages(p->sq_entries, p->cq_entries));
7098 if (ret) {
7099 free_uid(user);
7100 return ret;
7101 }
7102 }
7103
7104 ctx = io_ring_ctx_alloc(p);
7105 if (!ctx) {
7106 if (account_mem)
7107 io_unaccount_mem(user, ring_pages(p->sq_entries,
7108 p->cq_entries));
7109 free_uid(user);
7110 return -ENOMEM;
7111 }
7112 ctx->compat = in_compat_syscall();
7113 ctx->account_mem = account_mem;
7114 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007115 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007116
7117 ret = io_allocate_scq_urings(ctx, p);
7118 if (ret)
7119 goto err;
7120
Jens Axboe6c271ce2019-01-10 11:22:30 -07007121 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007122 if (ret)
7123 goto err;
7124
Jens Axboe2b188cc2019-01-07 10:46:33 -07007125 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007126 p->sq_off.head = offsetof(struct io_rings, sq.head);
7127 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7128 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7129 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7130 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7131 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7132 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007133
7134 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007135 p->cq_off.head = offsetof(struct io_rings, cq.head);
7136 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7137 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7138 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7139 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7140 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007141
Jens Axboe044c1ab2019-10-28 09:15:33 -06007142 /*
7143 * Install ring fd as the very last thing, so we don't risk someone
7144 * having closed it before we finish setup
7145 */
7146 ret = io_uring_get_fd(ctx);
7147 if (ret < 0)
7148 goto err;
7149
Jens Axboeda8c9692019-12-02 18:51:26 -07007150 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007151 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007152 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007153 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007154 return ret;
7155err:
7156 io_ring_ctx_wait_and_kill(ctx);
7157 return ret;
7158}
7159
7160/*
7161 * Sets up an aio uring context, and returns the fd. Applications asks for a
7162 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7163 * params structure passed in.
7164 */
7165static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7166{
7167 struct io_uring_params p;
7168 long ret;
7169 int i;
7170
7171 if (copy_from_user(&p, params, sizeof(p)))
7172 return -EFAULT;
7173 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7174 if (p.resv[i])
7175 return -EINVAL;
7176 }
7177
Jens Axboe6c271ce2019-01-10 11:22:30 -07007178 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007179 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007180 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007181 return -EINVAL;
7182
7183 ret = io_uring_create(entries, &p);
7184 if (ret < 0)
7185 return ret;
7186
7187 if (copy_to_user(params, &p, sizeof(p)))
7188 return -EFAULT;
7189
7190 return ret;
7191}
7192
7193SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7194 struct io_uring_params __user *, params)
7195{
7196 return io_uring_setup(entries, params);
7197}
7198
Jens Axboe66f4af92020-01-16 15:36:52 -07007199static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7200{
7201 struct io_uring_probe *p;
7202 size_t size;
7203 int i, ret;
7204
7205 size = struct_size(p, ops, nr_args);
7206 if (size == SIZE_MAX)
7207 return -EOVERFLOW;
7208 p = kzalloc(size, GFP_KERNEL);
7209 if (!p)
7210 return -ENOMEM;
7211
7212 ret = -EFAULT;
7213 if (copy_from_user(p, arg, size))
7214 goto out;
7215 ret = -EINVAL;
7216 if (memchr_inv(p, 0, size))
7217 goto out;
7218
7219 p->last_op = IORING_OP_LAST - 1;
7220 if (nr_args > IORING_OP_LAST)
7221 nr_args = IORING_OP_LAST;
7222
7223 for (i = 0; i < nr_args; i++) {
7224 p->ops[i].op = i;
7225 if (!io_op_defs[i].not_supported)
7226 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7227 }
7228 p->ops_len = i;
7229
7230 ret = 0;
7231 if (copy_to_user(arg, p, size))
7232 ret = -EFAULT;
7233out:
7234 kfree(p);
7235 return ret;
7236}
7237
Jens Axboe071698e2020-01-28 10:04:42 -07007238static int io_register_personality(struct io_ring_ctx *ctx)
7239{
7240 const struct cred *creds = get_current_cred();
7241 int id;
7242
7243 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7244 USHRT_MAX, GFP_KERNEL);
7245 if (id < 0)
7246 put_cred(creds);
7247 return id;
7248}
7249
7250static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7251{
7252 const struct cred *old_creds;
7253
7254 old_creds = idr_remove(&ctx->personality_idr, id);
7255 if (old_creds) {
7256 put_cred(old_creds);
7257 return 0;
7258 }
7259
7260 return -EINVAL;
7261}
7262
7263static bool io_register_op_must_quiesce(int op)
7264{
7265 switch (op) {
7266 case IORING_UNREGISTER_FILES:
7267 case IORING_REGISTER_FILES_UPDATE:
7268 case IORING_REGISTER_PROBE:
7269 case IORING_REGISTER_PERSONALITY:
7270 case IORING_UNREGISTER_PERSONALITY:
7271 return false;
7272 default:
7273 return true;
7274 }
7275}
7276
Jens Axboeedafcce2019-01-09 09:16:05 -07007277static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7278 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007279 __releases(ctx->uring_lock)
7280 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007281{
7282 int ret;
7283
Jens Axboe35fa71a2019-04-22 10:23:23 -06007284 /*
7285 * We're inside the ring mutex, if the ref is already dying, then
7286 * someone else killed the ctx or is already going through
7287 * io_uring_register().
7288 */
7289 if (percpu_ref_is_dying(&ctx->refs))
7290 return -ENXIO;
7291
Jens Axboe071698e2020-01-28 10:04:42 -07007292 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007293 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007294
Jens Axboe05f3fb32019-12-09 11:22:50 -07007295 /*
7296 * Drop uring mutex before waiting for references to exit. If
7297 * another thread is currently inside io_uring_enter() it might
7298 * need to grab the uring_lock to make progress. If we hold it
7299 * here across the drain wait, then we can deadlock. It's safe
7300 * to drop the mutex here, since no new references will come in
7301 * after we've killed the percpu ref.
7302 */
7303 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007304 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007305 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007306 if (ret) {
7307 percpu_ref_resurrect(&ctx->refs);
7308 ret = -EINTR;
7309 goto out;
7310 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007311 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007312
7313 switch (opcode) {
7314 case IORING_REGISTER_BUFFERS:
7315 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7316 break;
7317 case IORING_UNREGISTER_BUFFERS:
7318 ret = -EINVAL;
7319 if (arg || nr_args)
7320 break;
7321 ret = io_sqe_buffer_unregister(ctx);
7322 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007323 case IORING_REGISTER_FILES:
7324 ret = io_sqe_files_register(ctx, arg, nr_args);
7325 break;
7326 case IORING_UNREGISTER_FILES:
7327 ret = -EINVAL;
7328 if (arg || nr_args)
7329 break;
7330 ret = io_sqe_files_unregister(ctx);
7331 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007332 case IORING_REGISTER_FILES_UPDATE:
7333 ret = io_sqe_files_update(ctx, arg, nr_args);
7334 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007335 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007336 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007337 ret = -EINVAL;
7338 if (nr_args != 1)
7339 break;
7340 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007341 if (ret)
7342 break;
7343 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7344 ctx->eventfd_async = 1;
7345 else
7346 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007347 break;
7348 case IORING_UNREGISTER_EVENTFD:
7349 ret = -EINVAL;
7350 if (arg || nr_args)
7351 break;
7352 ret = io_eventfd_unregister(ctx);
7353 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007354 case IORING_REGISTER_PROBE:
7355 ret = -EINVAL;
7356 if (!arg || nr_args > 256)
7357 break;
7358 ret = io_probe(ctx, arg, nr_args);
7359 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007360 case IORING_REGISTER_PERSONALITY:
7361 ret = -EINVAL;
7362 if (arg || nr_args)
7363 break;
7364 ret = io_register_personality(ctx);
7365 break;
7366 case IORING_UNREGISTER_PERSONALITY:
7367 ret = -EINVAL;
7368 if (arg)
7369 break;
7370 ret = io_unregister_personality(ctx, nr_args);
7371 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007372 default:
7373 ret = -EINVAL;
7374 break;
7375 }
7376
Jens Axboe071698e2020-01-28 10:04:42 -07007377 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007378 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007379 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007380out:
7381 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007382 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007383 return ret;
7384}
7385
7386SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7387 void __user *, arg, unsigned int, nr_args)
7388{
7389 struct io_ring_ctx *ctx;
7390 long ret = -EBADF;
7391 struct fd f;
7392
7393 f = fdget(fd);
7394 if (!f.file)
7395 return -EBADF;
7396
7397 ret = -EOPNOTSUPP;
7398 if (f.file->f_op != &io_uring_fops)
7399 goto out_fput;
7400
7401 ctx = f.file->private_data;
7402
7403 mutex_lock(&ctx->uring_lock);
7404 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7405 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007406 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7407 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007408out_fput:
7409 fdput(f);
7410 return ret;
7411}
7412
Jens Axboe2b188cc2019-01-07 10:46:33 -07007413static int __init io_uring_init(void)
7414{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007415#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7416 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7417 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7418} while (0)
7419
7420#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7421 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7422 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7423 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7424 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7425 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7426 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7427 BUILD_BUG_SQE_ELEM(8, __u64, off);
7428 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7429 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007430 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007431 BUILD_BUG_SQE_ELEM(24, __u32, len);
7432 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7433 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7434 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7435 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7436 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7437 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7438 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7439 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7440 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7441 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7442 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7443 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7444 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007445 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007446 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7447 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7448 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007449 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007450
Jens Axboed3656342019-12-18 09:50:26 -07007451 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007452 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7453 return 0;
7454};
7455__initcall(io_uring_init);