blob: f4faaa2a9a3f8a93d6f4934df7a768c709303553 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020082#define CREATE_TRACE_POINTS
83#include <trace/events/io_uring.h>
84
Jens Axboe2b188cc2019-01-07 10:46:33 -070085#include <uapi/linux/io_uring.h>
86
87#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060088#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070089
Daniel Xu5277dea2019-09-14 14:23:45 -070090#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060091#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060092
93/*
94 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
95 */
96#define IORING_FILE_TABLE_SHIFT 9
97#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
98#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
99#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700100
101struct io_uring {
102 u32 head ____cacheline_aligned_in_smp;
103 u32 tail ____cacheline_aligned_in_smp;
104};
105
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 * This data is shared with the application through the mmap at offsets
108 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 *
110 * The offsets to the member fields are published through struct
111 * io_sqring_offsets when calling io_uring_setup.
112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
115 * Head and tail offsets into the ring; the offsets need to be
116 * masked to get valid indices.
117 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * The kernel controls head of the sq ring and the tail of the cq ring,
119 * and the application controls tail of the sq ring and the head of the
120 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 * ring_entries - 1)
126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 u32 sq_ring_mask, cq_ring_mask;
128 /* Ring sizes (constant, power of 2) */
129 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 /*
131 * Number of invalid entries dropped by the kernel due to
132 * invalid index stored in array
133 *
134 * Written by the kernel, shouldn't be modified by the
135 * application (i.e. get number of "new events" by comparing to
136 * cached value).
137 *
138 * After a new SQ head value was read by the application this
139 * counter includes all submissions that were dropped reaching
140 * the new SQ head (and possibly more).
141 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000142 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
144 * Runtime flags
145 *
146 * Written by the kernel, shouldn't be modified by the
147 * application.
148 *
149 * The application needs a full memory barrier before checking
150 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
154 * Number of completion events lost because the queue was full;
155 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800156 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 * the completion queue.
158 *
159 * Written by the kernel, shouldn't be modified by the
160 * application (i.e. get number of "new events" by comparing to
161 * cached value).
162 *
163 * As completion events come in out of order this counter is not
164 * ordered with any other data.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 /*
168 * Ring buffer of completion events.
169 *
170 * The kernel writes completion events fresh every time they are
171 * produced, so the application is allowed to modify pending
172 * entries.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700175};
176
Jens Axboeedafcce2019-01-09 09:16:05 -0700177struct io_mapped_ubuf {
178 u64 ubuf;
179 size_t len;
180 struct bio_vec *bvec;
181 unsigned int nr_bvecs;
182};
183
Jens Axboe65e19f52019-10-26 07:20:21 -0600184struct fixed_file_table {
185 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700186};
187
Jens Axboe05f3fb32019-12-09 11:22:50 -0700188struct fixed_file_data {
189 struct fixed_file_table *table;
190 struct io_ring_ctx *ctx;
191
192 struct percpu_ref refs;
193 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe2b188cc2019-01-07 10:46:33 -0700198struct io_ring_ctx {
199 struct {
200 struct percpu_ref refs;
201 } ____cacheline_aligned_in_smp;
202
203 struct {
204 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800205 unsigned int compat: 1;
206 unsigned int account_mem: 1;
207 unsigned int cq_overflow_flushed: 1;
208 unsigned int drain_next: 1;
209 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210
Hristo Venev75b28af2019-08-26 17:23:46 +0000211 /*
212 * Ring buffer of indices into array of io_uring_sqe, which is
213 * mmapped by the application using the IORING_OFF_SQES offset.
214 *
215 * This indirection could e.g. be used to assign fixed
216 * io_uring_sqe entries to operations and only submit them to
217 * the queue when needed.
218 *
219 * The kernel modifies neither the indices array nor the entries
220 * array.
221 */
222 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 unsigned cached_sq_head;
224 unsigned sq_entries;
225 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700226 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600227 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700228 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700229 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600230
231 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600232 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700233 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Jens Axboefcb323c2019-10-24 12:39:47 -0600235 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700236 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 } ____cacheline_aligned_in_smp;
238
Hristo Venev75b28af2019-08-26 17:23:46 +0000239 struct io_rings *rings;
240
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600242 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 struct task_struct *sqo_thread; /* if using sq thread polling */
244 struct mm_struct *sqo_mm;
245 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246
Jens Axboe6b063142019-01-10 22:13:58 -0700247 /*
248 * If used, fixed file set. Writers must ensure that ->refs is dead,
249 * readers must ensure that ->refs is alive as long as the file* is
250 * used. Only updated through io_uring_register(2).
251 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700252 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700253 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300254 int ring_fd;
255 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700256
Jens Axboeedafcce2019-01-09 09:16:05 -0700257 /* if used, fixed mapped user buffers */
258 unsigned nr_user_bufs;
259 struct io_mapped_ubuf *user_bufs;
260
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 struct user_struct *user;
262
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700263 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700264
Jens Axboe206aefd2019-11-07 18:27:42 -0700265 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
266 struct completion *completions;
267
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700268 /* if all else fails... */
269 struct io_kiocb *fallback_req;
270
Jens Axboe206aefd2019-11-07 18:27:42 -0700271#if defined(CONFIG_UNIX)
272 struct socket *ring_sock;
273#endif
274
Jens Axboe071698e2020-01-28 10:04:42 -0700275 struct idr personality_idr;
276
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 struct {
278 unsigned cached_cq_tail;
279 unsigned cq_entries;
280 unsigned cq_mask;
281 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700282 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700283 struct wait_queue_head cq_wait;
284 struct fasync_struct *cq_fasync;
285 struct eventfd_ctx *cq_ev_fd;
286 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700287
288 struct {
289 struct mutex uring_lock;
290 wait_queue_head_t wait;
291 } ____cacheline_aligned_in_smp;
292
293 struct {
294 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700295
Jens Axboedef596e2019-01-09 08:59:42 -0700296 /*
297 * ->poll_list is protected by the ctx->uring_lock for
298 * io_uring instances that don't use IORING_SETUP_SQPOLL.
299 * For SQPOLL, only the single threaded io_sq_thread() will
300 * manipulate the list, hence no extra locking is needed there.
301 */
302 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700303 struct hlist_head *cancel_hash;
304 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700305 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600306
307 spinlock_t inflight_lock;
308 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700310};
311
Jens Axboe09bb8392019-03-13 12:39:28 -0600312/*
313 * First field must be the file pointer in all the
314 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
315 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316struct io_poll_iocb {
317 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700318 union {
319 struct wait_queue_head *head;
320 u64 addr;
321 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700322 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600323 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700324 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700325 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326};
327
Jens Axboeb5dba592019-12-11 14:02:38 -0700328struct io_close {
329 struct file *file;
330 struct file *put_file;
331 int fd;
332};
333
Jens Axboead8a48a2019-11-15 08:49:11 -0700334struct io_timeout_data {
335 struct io_kiocb *req;
336 struct hrtimer timer;
337 struct timespec64 ts;
338 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300339 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700340};
341
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700342struct io_accept {
343 struct file *file;
344 struct sockaddr __user *addr;
345 int __user *addr_len;
346 int flags;
347};
348
349struct io_sync {
350 struct file *file;
351 loff_t len;
352 loff_t off;
353 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700354 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700355};
356
Jens Axboefbf23842019-12-17 18:45:56 -0700357struct io_cancel {
358 struct file *file;
359 u64 addr;
360};
361
Jens Axboeb29472e2019-12-17 18:50:29 -0700362struct io_timeout {
363 struct file *file;
364 u64 addr;
365 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700366 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700367};
368
Jens Axboe9adbd452019-12-20 08:45:55 -0700369struct io_rw {
370 /* NOTE: kiocb has the file as the first member, so don't do it here */
371 struct kiocb kiocb;
372 u64 addr;
373 u64 len;
374};
375
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700376struct io_connect {
377 struct file *file;
378 struct sockaddr __user *addr;
379 int addr_len;
380};
381
Jens Axboee47293f2019-12-20 08:58:21 -0700382struct io_sr_msg {
383 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700384 union {
385 struct user_msghdr __user *msg;
386 void __user *buf;
387 };
Jens Axboee47293f2019-12-20 08:58:21 -0700388 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700389 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700390};
391
Jens Axboe15b71ab2019-12-11 11:20:36 -0700392struct io_open {
393 struct file *file;
394 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 unsigned mask;
397 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700400 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401};
402
Jens Axboe05f3fb32019-12-09 11:22:50 -0700403struct io_files_update {
404 struct file *file;
405 u64 arg;
406 u32 nr_args;
407 u32 offset;
408};
409
Jens Axboe4840e412019-12-25 22:03:45 -0700410struct io_fadvise {
411 struct file *file;
412 u64 offset;
413 u32 len;
414 u32 advice;
415};
416
Jens Axboec1ca7572019-12-25 22:18:28 -0700417struct io_madvise {
418 struct file *file;
419 u64 addr;
420 u32 len;
421 u32 advice;
422};
423
Jens Axboe3e4827b2020-01-08 15:18:09 -0700424struct io_epoll {
425 struct file *file;
426 int epfd;
427 int op;
428 int fd;
429 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430};
431
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300432struct io_splice {
433 struct file *file_out;
434 struct file *file_in;
435 loff_t off_out;
436 loff_t off_in;
437 u64 len;
438 unsigned int flags;
439};
440
Jens Axboef499a022019-12-02 16:28:46 -0700441struct io_async_connect {
442 struct sockaddr_storage address;
443};
444
Jens Axboe03b12302019-12-02 18:50:25 -0700445struct io_async_msghdr {
446 struct iovec fast_iov[UIO_FASTIOV];
447 struct iovec *iov;
448 struct sockaddr __user *uaddr;
449 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700450 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700451};
452
Jens Axboef67676d2019-12-02 11:03:47 -0700453struct io_async_rw {
454 struct iovec fast_iov[UIO_FASTIOV];
455 struct iovec *iov;
456 ssize_t nr_segs;
457 ssize_t size;
458};
459
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700460struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700461 union {
462 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700463 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700464 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700465 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700466 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700467};
468
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300469enum {
470 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
471 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
472 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
473 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
474 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
475
476 REQ_F_LINK_NEXT_BIT,
477 REQ_F_FAIL_LINK_BIT,
478 REQ_F_INFLIGHT_BIT,
479 REQ_F_CUR_POS_BIT,
480 REQ_F_NOWAIT_BIT,
481 REQ_F_IOPOLL_COMPLETED_BIT,
482 REQ_F_LINK_TIMEOUT_BIT,
483 REQ_F_TIMEOUT_BIT,
484 REQ_F_ISREG_BIT,
485 REQ_F_MUST_PUNT_BIT,
486 REQ_F_TIMEOUT_NOSEQ_BIT,
487 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300488 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700489 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700490 REQ_F_POLLED_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300491};
492
493enum {
494 /* ctx owns file */
495 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
496 /* drain existing IO first */
497 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
498 /* linked sqes */
499 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
500 /* doesn't sever on completion < 0 */
501 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
502 /* IOSQE_ASYNC */
503 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
504
505 /* already grabbed next link */
506 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
507 /* fail rest of links */
508 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
509 /* on inflight list */
510 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
511 /* read/write uses file position */
512 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
513 /* must not punt to workers */
514 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
515 /* polled IO has completed */
516 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
517 /* has linked timeout */
518 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
519 /* timeout request */
520 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
521 /* regular file */
522 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
523 /* must be punted even for NONBLOCK */
524 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
525 /* no timeout sequence */
526 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
527 /* completion under lock */
528 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300529 /* needs cleanup */
530 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700531 /* in overflow list */
532 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700533 /* already went through poll handler */
534 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
535};
536
537struct async_poll {
538 struct io_poll_iocb poll;
539 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300540};
541
Jens Axboe09bb8392019-03-13 12:39:28 -0600542/*
543 * NOTE! Each of the iocb union members has the file pointer
544 * as the first entry in their struct definition. So you can
545 * access the file pointer through any of the sub-structs,
546 * or directly as just 'ki_filp' in this struct.
547 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700549 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600550 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700551 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700552 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700553 struct io_accept accept;
554 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700555 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700556 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700557 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700558 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700559 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700560 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700561 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700562 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700563 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700564 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300565 struct io_splice splice;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700566 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700568 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300569 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700570 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571
572 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700573 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700575 refcount_t refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700576 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600578 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600579 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700580
Jens Axboed7718a92020-02-14 22:23:12 -0700581 struct list_head link_list;
582
Jens Axboefcb323c2019-10-24 12:39:47 -0600583 struct list_head inflight_entry;
584
Jens Axboeb41e9852020-02-17 09:52:41 -0700585 union {
586 /*
587 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700588 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
589 * async armed poll handlers for regular commands. The latter
590 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700591 */
592 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700593 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700594 struct hlist_node hash_node;
595 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700596 };
597 struct io_wq_work work;
598 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599};
600
601#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700602#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700603
Jens Axboe9a56a232019-01-09 09:06:50 -0700604struct io_submit_state {
605 struct blk_plug plug;
606
607 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700608 * io_kiocb alloc cache
609 */
610 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300611 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700612
613 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700614 * File reference cache
615 */
616 struct file *file;
617 unsigned int fd;
618 unsigned int has_refs;
619 unsigned int used_refs;
620 unsigned int ios_left;
621};
622
Jens Axboed3656342019-12-18 09:50:26 -0700623struct io_op_def {
624 /* needs req->io allocated for deferral/async */
625 unsigned async_ctx : 1;
626 /* needs current->mm setup, does mm access */
627 unsigned needs_mm : 1;
628 /* needs req->file assigned */
629 unsigned needs_file : 1;
630 /* needs req->file assigned IFF fd is >= 0 */
631 unsigned fd_non_neg : 1;
632 /* hash wq insertion if file is a regular file */
633 unsigned hash_reg_file : 1;
634 /* unbound wq insertion if file is a non-regular file */
635 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700636 /* opcode is not supported by this kernel */
637 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700638 /* needs file table */
639 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700640 /* needs ->fs */
641 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700642 /* set if opcode supports polled "wait" */
643 unsigned pollin : 1;
644 unsigned pollout : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700645};
646
647static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300648 [IORING_OP_NOP] = {},
649 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700650 .async_ctx = 1,
651 .needs_mm = 1,
652 .needs_file = 1,
653 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700654 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700655 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300656 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700657 .async_ctx = 1,
658 .needs_mm = 1,
659 .needs_file = 1,
660 .hash_reg_file = 1,
661 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700662 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700663 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300664 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700665 .needs_file = 1,
666 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300667 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700668 .needs_file = 1,
669 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700670 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700671 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300672 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700673 .needs_file = 1,
674 .hash_reg_file = 1,
675 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700676 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700677 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300678 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700679 .needs_file = 1,
680 .unbound_nonreg_file = 1,
681 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_POLL_REMOVE] = {},
683 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700684 .needs_file = 1,
685 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300686 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700687 .async_ctx = 1,
688 .needs_mm = 1,
689 .needs_file = 1,
690 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700691 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700692 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700695 .async_ctx = 1,
696 .needs_mm = 1,
697 .needs_file = 1,
698 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700699 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700700 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .async_ctx = 1,
704 .needs_mm = 1,
705 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300706 [IORING_OP_TIMEOUT_REMOVE] = {},
707 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_mm = 1,
709 .needs_file = 1,
710 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700711 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700712 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700713 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300714 [IORING_OP_ASYNC_CANCEL] = {},
715 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700716 .async_ctx = 1,
717 .needs_mm = 1,
718 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300719 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700720 .async_ctx = 1,
721 .needs_mm = 1,
722 .needs_file = 1,
723 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700724 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700727 .needs_file = 1,
728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700730 .needs_file = 1,
731 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700732 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700733 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700736 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700737 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700740 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700741 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700742 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300743 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700744 .needs_mm = 1,
745 .needs_file = 1,
746 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700747 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700748 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300749 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700750 .needs_mm = 1,
751 .needs_file = 1,
752 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700753 .pollin = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700756 .needs_mm = 1,
757 .needs_file = 1,
758 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700759 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700760 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300761 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700762 .needs_file = 1,
763 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300764 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700765 .needs_mm = 1,
766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700768 .needs_mm = 1,
769 .needs_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700771 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700774 .needs_mm = 1,
775 .needs_file = 1,
776 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700777 .pollin = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700778 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300779 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700780 .needs_file = 1,
781 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700782 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700783 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700784 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700785 [IORING_OP_EPOLL_CTL] = {
786 .unbound_nonreg_file = 1,
787 .file_table = 1,
788 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300789 [IORING_OP_SPLICE] = {
790 .needs_file = 1,
791 .hash_reg_file = 1,
792 .unbound_nonreg_file = 1,
793 }
Jens Axboed3656342019-12-18 09:50:26 -0700794};
795
Jens Axboe561fb042019-10-24 07:25:42 -0600796static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700797static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800798static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700799static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700800static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
801static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700802static int __io_sqe_files_update(struct io_ring_ctx *ctx,
803 struct io_uring_files_update *ip,
804 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700805static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700806static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300807static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700808static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
809 int fd, struct file **out_file, bool fixed);
810static void __io_queue_sqe(struct io_kiocb *req,
811 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600812
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813static struct kmem_cache *req_cachep;
814
815static const struct file_operations io_uring_fops;
816
817struct sock *io_uring_get_socket(struct file *file)
818{
819#if defined(CONFIG_UNIX)
820 if (file->f_op == &io_uring_fops) {
821 struct io_ring_ctx *ctx = file->private_data;
822
823 return ctx->ring_sock->sk;
824 }
825#endif
826 return NULL;
827}
828EXPORT_SYMBOL(io_uring_get_socket);
829
830static void io_ring_ctx_ref_free(struct percpu_ref *ref)
831{
832 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
833
Jens Axboe206aefd2019-11-07 18:27:42 -0700834 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700835}
836
837static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
838{
839 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700840 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841
842 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
843 if (!ctx)
844 return NULL;
845
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700846 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
847 if (!ctx->fallback_req)
848 goto err;
849
Jens Axboe206aefd2019-11-07 18:27:42 -0700850 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
851 if (!ctx->completions)
852 goto err;
853
Jens Axboe78076bb2019-12-04 19:56:40 -0700854 /*
855 * Use 5 bits less than the max cq entries, that should give us around
856 * 32 entries per hash list if totally full and uniformly spread.
857 */
858 hash_bits = ilog2(p->cq_entries);
859 hash_bits -= 5;
860 if (hash_bits <= 0)
861 hash_bits = 1;
862 ctx->cancel_hash_bits = hash_bits;
863 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
864 GFP_KERNEL);
865 if (!ctx->cancel_hash)
866 goto err;
867 __hash_init(ctx->cancel_hash, 1U << hash_bits);
868
Roman Gushchin21482892019-05-07 10:01:48 -0700869 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700870 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
871 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700872
873 ctx->flags = p->flags;
874 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700875 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700876 init_completion(&ctx->completions[0]);
877 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700878 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700879 mutex_init(&ctx->uring_lock);
880 init_waitqueue_head(&ctx->wait);
881 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700882 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600883 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600884 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600885 init_waitqueue_head(&ctx->inflight_wait);
886 spin_lock_init(&ctx->inflight_lock);
887 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700888 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700889err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700890 if (ctx->fallback_req)
891 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700892 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700893 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700894 kfree(ctx);
895 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896}
897
Bob Liu9d858b22019-11-13 18:06:25 +0800898static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600899{
Jackie Liua197f662019-11-08 08:09:12 -0700900 struct io_ring_ctx *ctx = req->ctx;
901
Jens Axboe498ccd92019-10-25 10:04:25 -0600902 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
903 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600904}
905
Bob Liu9d858b22019-11-13 18:06:25 +0800906static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600907{
Pavel Begunkov87987892020-01-18 01:22:30 +0300908 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800909 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600910
Bob Liu9d858b22019-11-13 18:06:25 +0800911 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600912}
913
914static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600915{
916 struct io_kiocb *req;
917
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600918 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800919 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600920 list_del_init(&req->list);
921 return req;
922 }
923
924 return NULL;
925}
926
Jens Axboe5262f562019-09-17 12:26:57 -0600927static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
928{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600929 struct io_kiocb *req;
930
931 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700932 if (req) {
933 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
934 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800935 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700936 list_del_init(&req->list);
937 return req;
938 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600939 }
940
941 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600942}
943
Jens Axboede0617e2019-04-06 21:51:27 -0600944static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700945{
Hristo Venev75b28af2019-08-26 17:23:46 +0000946 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700947
Pavel Begunkov07910152020-01-17 03:52:46 +0300948 /* order cqe stores with ring update */
949 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700950
Pavel Begunkov07910152020-01-17 03:52:46 +0300951 if (wq_has_sleeper(&ctx->cq_wait)) {
952 wake_up_interruptible(&ctx->cq_wait);
953 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700954 }
955}
956
Jens Axboecccf0ee2020-01-27 16:34:48 -0700957static inline void io_req_work_grab_env(struct io_kiocb *req,
958 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600959{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700960 if (!req->work.mm && def->needs_mm) {
961 mmgrab(current->mm);
962 req->work.mm = current->mm;
963 }
964 if (!req->work.creds)
965 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700966 if (!req->work.fs && def->needs_fs) {
967 spin_lock(&current->fs->lock);
968 if (!current->fs->in_exec) {
969 req->work.fs = current->fs;
970 req->work.fs->users++;
971 } else {
972 req->work.flags |= IO_WQ_WORK_CANCEL;
973 }
974 spin_unlock(&current->fs->lock);
975 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700976 if (!req->work.task_pid)
977 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700978}
979
980static inline void io_req_work_drop_env(struct io_kiocb *req)
981{
982 if (req->work.mm) {
983 mmdrop(req->work.mm);
984 req->work.mm = NULL;
985 }
986 if (req->work.creds) {
987 put_cred(req->work.creds);
988 req->work.creds = NULL;
989 }
Jens Axboeff002b32020-02-07 16:05:21 -0700990 if (req->work.fs) {
991 struct fs_struct *fs = req->work.fs;
992
993 spin_lock(&req->work.fs->lock);
994 if (--fs->users)
995 fs = NULL;
996 spin_unlock(&req->work.fs->lock);
997 if (fs)
998 free_fs_struct(fs);
999 }
Jens Axboe561fb042019-10-24 07:25:42 -06001000}
1001
Jens Axboe94ae5e72019-11-14 19:39:52 -07001002static inline bool io_prep_async_work(struct io_kiocb *req,
1003 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001004{
Jens Axboed3656342019-12-18 09:50:26 -07001005 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001006 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001007
Jens Axboed3656342019-12-18 09:50:26 -07001008 if (req->flags & REQ_F_ISREG) {
1009 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001010 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001011 } else {
1012 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001013 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001014 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001015
1016 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001017
Jens Axboe94ae5e72019-11-14 19:39:52 -07001018 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001019 return do_hashed;
1020}
1021
Jackie Liua197f662019-11-08 08:09:12 -07001022static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001023{
Jackie Liua197f662019-11-08 08:09:12 -07001024 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001025 struct io_kiocb *link;
1026 bool do_hashed;
1027
1028 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001029
1030 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1031 req->flags);
1032 if (!do_hashed) {
1033 io_wq_enqueue(ctx->io_wq, &req->work);
1034 } else {
1035 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1036 file_inode(req->file));
1037 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001038
1039 if (link)
1040 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001041}
1042
Jens Axboe5262f562019-09-17 12:26:57 -06001043static void io_kill_timeout(struct io_kiocb *req)
1044{
1045 int ret;
1046
Jens Axboe2d283902019-12-04 11:08:05 -07001047 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001048 if (ret != -1) {
1049 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001050 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001051 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001052 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001053 }
1054}
1055
1056static void io_kill_timeouts(struct io_ring_ctx *ctx)
1057{
1058 struct io_kiocb *req, *tmp;
1059
1060 spin_lock_irq(&ctx->completion_lock);
1061 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1062 io_kill_timeout(req);
1063 spin_unlock_irq(&ctx->completion_lock);
1064}
1065
Jens Axboede0617e2019-04-06 21:51:27 -06001066static void io_commit_cqring(struct io_ring_ctx *ctx)
1067{
1068 struct io_kiocb *req;
1069
Jens Axboe5262f562019-09-17 12:26:57 -06001070 while ((req = io_get_timeout_req(ctx)) != NULL)
1071 io_kill_timeout(req);
1072
Jens Axboede0617e2019-04-06 21:51:27 -06001073 __io_commit_cqring(ctx);
1074
Pavel Begunkov87987892020-01-18 01:22:30 +03001075 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001076 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001077}
1078
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1080{
Hristo Venev75b28af2019-08-26 17:23:46 +00001081 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082 unsigned tail;
1083
1084 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001085 /*
1086 * writes to the cq entry need to come after reading head; the
1087 * control dependency is enough as we're using WRITE_ONCE to
1088 * fill the cq entry
1089 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001090 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091 return NULL;
1092
1093 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001094 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095}
1096
Jens Axboef2842ab2020-01-08 11:04:00 -07001097static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1098{
Jens Axboef0b493e2020-02-01 21:30:11 -07001099 if (!ctx->cq_ev_fd)
1100 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001101 if (!ctx->eventfd_async)
1102 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001103 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001104}
1105
Jens Axboeb41e9852020-02-17 09:52:41 -07001106static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001107{
1108 if (waitqueue_active(&ctx->wait))
1109 wake_up(&ctx->wait);
1110 if (waitqueue_active(&ctx->sqo_wait))
1111 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001112 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001113 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001114}
1115
Jens Axboec4a2ed72019-11-21 21:01:26 -07001116/* Returns true if there are no backlogged entries after the flush */
1117static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001118{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001119 struct io_rings *rings = ctx->rings;
1120 struct io_uring_cqe *cqe;
1121 struct io_kiocb *req;
1122 unsigned long flags;
1123 LIST_HEAD(list);
1124
1125 if (!force) {
1126 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001127 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001128 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1129 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001130 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001131 }
1132
1133 spin_lock_irqsave(&ctx->completion_lock, flags);
1134
1135 /* if force is set, the ring is going away. always drop after that */
1136 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001137 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001138
Jens Axboec4a2ed72019-11-21 21:01:26 -07001139 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001140 while (!list_empty(&ctx->cq_overflow_list)) {
1141 cqe = io_get_cqring(ctx);
1142 if (!cqe && !force)
1143 break;
1144
1145 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1146 list);
1147 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001148 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001149 if (cqe) {
1150 WRITE_ONCE(cqe->user_data, req->user_data);
1151 WRITE_ONCE(cqe->res, req->result);
1152 WRITE_ONCE(cqe->flags, 0);
1153 } else {
1154 WRITE_ONCE(ctx->rings->cq_overflow,
1155 atomic_inc_return(&ctx->cached_cq_overflow));
1156 }
1157 }
1158
1159 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001160 if (cqe) {
1161 clear_bit(0, &ctx->sq_check_overflow);
1162 clear_bit(0, &ctx->cq_check_overflow);
1163 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001164 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1165 io_cqring_ev_posted(ctx);
1166
1167 while (!list_empty(&list)) {
1168 req = list_first_entry(&list, struct io_kiocb, list);
1169 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001170 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001171 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001172
1173 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001174}
1175
Jens Axboe78e19bb2019-11-06 15:21:34 -07001176static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001179 struct io_uring_cqe *cqe;
1180
Jens Axboe78e19bb2019-11-06 15:21:34 -07001181 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001182
Jens Axboe2b188cc2019-01-07 10:46:33 -07001183 /*
1184 * If we can't get a cq entry, userspace overflowed the
1185 * submission (by quite a lot). Increment the overflow count in
1186 * the ring.
1187 */
1188 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001189 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001190 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001191 WRITE_ONCE(cqe->res, res);
1192 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001193 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001194 WRITE_ONCE(ctx->rings->cq_overflow,
1195 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001196 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001197 if (list_empty(&ctx->cq_overflow_list)) {
1198 set_bit(0, &ctx->sq_check_overflow);
1199 set_bit(0, &ctx->cq_check_overflow);
1200 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001201 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001202 refcount_inc(&req->refs);
1203 req->result = res;
1204 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001205 }
1206}
1207
Jens Axboe78e19bb2019-11-06 15:21:34 -07001208static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001209{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001210 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001211 unsigned long flags;
1212
1213 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001214 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215 io_commit_cqring(ctx);
1216 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1217
Jens Axboe8c838782019-03-12 15:48:16 -06001218 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219}
1220
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001221static inline bool io_is_fallback_req(struct io_kiocb *req)
1222{
1223 return req == (struct io_kiocb *)
1224 ((unsigned long) req->ctx->fallback_req & ~1UL);
1225}
1226
1227static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1228{
1229 struct io_kiocb *req;
1230
1231 req = ctx->fallback_req;
1232 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1233 return req;
1234
1235 return NULL;
1236}
1237
Jens Axboe2579f912019-01-09 09:10:43 -07001238static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1239 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001240{
Jens Axboefd6fab22019-03-14 16:30:06 -06001241 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242 struct io_kiocb *req;
1243
Jens Axboe2579f912019-01-09 09:10:43 -07001244 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001245 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001246 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001247 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001248 } else if (!state->free_reqs) {
1249 size_t sz;
1250 int ret;
1251
1252 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001253 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1254
1255 /*
1256 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1257 * retry single alloc to be on the safe side.
1258 */
1259 if (unlikely(ret <= 0)) {
1260 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1261 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001262 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001263 ret = 1;
1264 }
Jens Axboe2579f912019-01-09 09:10:43 -07001265 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001266 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001267 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001268 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001269 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270 }
1271
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001272got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001273 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001274 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001275 req->ctx = ctx;
1276 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001277 /* one is dropped after submission, the other at completion */
1278 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001279 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001280 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001281 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001282fallback:
1283 req = io_get_fallback_req(ctx);
1284 if (req)
1285 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001286 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287 return NULL;
1288}
1289
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001290static inline void io_put_file(struct io_kiocb *req, struct file *file,
1291 bool fixed)
1292{
1293 if (fixed)
1294 percpu_ref_put(&req->ctx->file_data->refs);
1295 else
1296 fput(file);
1297}
1298
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001299static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001300{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001301 if (likely(!io_is_fallback_req(req)))
1302 kmem_cache_free(req_cachep, req);
1303 else
1304 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1305}
1306
Jens Axboec6ca97b302019-12-28 12:11:08 -07001307static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001309 if (req->flags & REQ_F_NEED_CLEANUP)
1310 io_cleanup_req(req);
1311
YueHaibing96fd84d2020-01-07 22:22:44 +08001312 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001313 if (req->file)
1314 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001315
1316 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001317}
1318
1319static void __io_free_req(struct io_kiocb *req)
1320{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001321 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001322
Jens Axboefcb323c2019-10-24 12:39:47 -06001323 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001324 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001325 unsigned long flags;
1326
1327 spin_lock_irqsave(&ctx->inflight_lock, flags);
1328 list_del(&req->inflight_entry);
1329 if (waitqueue_active(&ctx->inflight_wait))
1330 wake_up(&ctx->inflight_wait);
1331 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1332 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001333
1334 percpu_ref_put(&req->ctx->refs);
1335 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001336}
1337
Jens Axboec6ca97b302019-12-28 12:11:08 -07001338struct req_batch {
1339 void *reqs[IO_IOPOLL_BATCH];
1340 int to_free;
1341 int need_iter;
1342};
1343
1344static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1345{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001346 int fixed_refs = rb->to_free;
1347
Jens Axboec6ca97b302019-12-28 12:11:08 -07001348 if (!rb->to_free)
1349 return;
1350 if (rb->need_iter) {
1351 int i, inflight = 0;
1352 unsigned long flags;
1353
Jens Axboe10fef4b2020-01-09 07:52:28 -07001354 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001355 for (i = 0; i < rb->to_free; i++) {
1356 struct io_kiocb *req = rb->reqs[i];
1357
Jens Axboe10fef4b2020-01-09 07:52:28 -07001358 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001359 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001360 fixed_refs++;
1361 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001362 if (req->flags & REQ_F_INFLIGHT)
1363 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001364 __io_req_aux_free(req);
1365 }
1366 if (!inflight)
1367 goto do_free;
1368
1369 spin_lock_irqsave(&ctx->inflight_lock, flags);
1370 for (i = 0; i < rb->to_free; i++) {
1371 struct io_kiocb *req = rb->reqs[i];
1372
Jens Axboe10fef4b2020-01-09 07:52:28 -07001373 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001374 list_del(&req->inflight_entry);
1375 if (!--inflight)
1376 break;
1377 }
1378 }
1379 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1380
1381 if (waitqueue_active(&ctx->inflight_wait))
1382 wake_up(&ctx->inflight_wait);
1383 }
1384do_free:
1385 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001386 if (fixed_refs)
1387 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001388 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001389 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001390}
1391
Jackie Liua197f662019-11-08 08:09:12 -07001392static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001393{
Jackie Liua197f662019-11-08 08:09:12 -07001394 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001395 int ret;
1396
Jens Axboe2d283902019-12-04 11:08:05 -07001397 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001398 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001399 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001400 io_commit_cqring(ctx);
1401 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001402 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001403 return true;
1404 }
1405
1406 return false;
1407}
1408
Jens Axboeba816ad2019-09-28 11:36:45 -06001409static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001410{
Jens Axboe2665abf2019-11-05 12:40:47 -07001411 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001412 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001413
Jens Axboe4d7dd462019-11-20 13:03:52 -07001414 /* Already got next link */
1415 if (req->flags & REQ_F_LINK_NEXT)
1416 return;
1417
Jens Axboe9e645e112019-05-10 16:07:28 -06001418 /*
1419 * The list should never be empty when we are called here. But could
1420 * potentially happen if the chain is messed up, check to be on the
1421 * safe side.
1422 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001423 while (!list_empty(&req->link_list)) {
1424 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1425 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001426
Pavel Begunkov44932332019-12-05 16:16:35 +03001427 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1428 (nxt->flags & REQ_F_TIMEOUT))) {
1429 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001430 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001431 req->flags &= ~REQ_F_LINK_TIMEOUT;
1432 continue;
1433 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001434
Pavel Begunkov44932332019-12-05 16:16:35 +03001435 list_del_init(&req->link_list);
1436 if (!list_empty(&nxt->link_list))
1437 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001438 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001439 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001440 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001441
Jens Axboe4d7dd462019-11-20 13:03:52 -07001442 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001443 if (wake_ev)
1444 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001445}
1446
1447/*
1448 * Called if REQ_F_LINK is set, and we fail the head request
1449 */
1450static void io_fail_links(struct io_kiocb *req)
1451{
Jens Axboe2665abf2019-11-05 12:40:47 -07001452 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 unsigned long flags;
1454
1455 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001456
1457 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001458 struct io_kiocb *link = list_first_entry(&req->link_list,
1459 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001460
Pavel Begunkov44932332019-12-05 16:16:35 +03001461 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001462 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001463
1464 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001465 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001466 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001467 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001468 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001469 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001470 }
Jens Axboe5d960722019-11-19 15:31:28 -07001471 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001472 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001473
1474 io_commit_cqring(ctx);
1475 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1476 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001477}
1478
Jens Axboe4d7dd462019-11-20 13:03:52 -07001479static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001480{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001481 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001482 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001483
Jens Axboe9e645e112019-05-10 16:07:28 -06001484 /*
1485 * If LINK is set, we have dependent requests in this chain. If we
1486 * didn't fail this request, queue the first one up, moving any other
1487 * dependencies to the next request. In case of failure, fail the rest
1488 * of the chain.
1489 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001490 if (req->flags & REQ_F_FAIL_LINK) {
1491 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001492 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1493 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001494 struct io_ring_ctx *ctx = req->ctx;
1495 unsigned long flags;
1496
1497 /*
1498 * If this is a timeout link, we could be racing with the
1499 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001500 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001501 */
1502 spin_lock_irqsave(&ctx->completion_lock, flags);
1503 io_req_link_next(req, nxt);
1504 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1505 } else {
1506 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001507 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001508}
Jens Axboe9e645e112019-05-10 16:07:28 -06001509
Jackie Liuc69f8db2019-11-09 11:00:08 +08001510static void io_free_req(struct io_kiocb *req)
1511{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001512 struct io_kiocb *nxt = NULL;
1513
1514 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001515 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001516
1517 if (nxt)
1518 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001519}
1520
Jens Axboeba816ad2019-09-28 11:36:45 -06001521/*
1522 * Drop reference to request, return next in chain (if there is one) if this
1523 * was the last reference to this request.
1524 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001525__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001526static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001527{
Jens Axboe2a44f462020-02-25 13:25:41 -07001528 if (refcount_dec_and_test(&req->refs)) {
1529 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001530 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001531 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001532}
1533
Jens Axboe2b188cc2019-01-07 10:46:33 -07001534static void io_put_req(struct io_kiocb *req)
1535{
Jens Axboedef596e2019-01-09 08:59:42 -07001536 if (refcount_dec_and_test(&req->refs))
1537 io_free_req(req);
1538}
1539
Jens Axboe978db572019-11-14 22:39:04 -07001540/*
1541 * Must only be used if we don't need to care about links, usually from
1542 * within the completion handling itself.
1543 */
1544static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001545{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001546 /* drop both submit and complete references */
1547 if (refcount_sub_and_test(2, &req->refs))
1548 __io_free_req(req);
1549}
1550
Jens Axboe978db572019-11-14 22:39:04 -07001551static void io_double_put_req(struct io_kiocb *req)
1552{
1553 /* drop both submit and complete references */
1554 if (refcount_sub_and_test(2, &req->refs))
1555 io_free_req(req);
1556}
1557
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001558static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001559{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001560 struct io_rings *rings = ctx->rings;
1561
Jens Axboead3eb2c2019-12-18 17:12:20 -07001562 if (test_bit(0, &ctx->cq_check_overflow)) {
1563 /*
1564 * noflush == true is from the waitqueue handler, just ensure
1565 * we wake up the task, and the next invocation will flush the
1566 * entries. We cannot safely to it from here.
1567 */
1568 if (noflush && !list_empty(&ctx->cq_overflow_list))
1569 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001570
Jens Axboead3eb2c2019-12-18 17:12:20 -07001571 io_cqring_overflow_flush(ctx, false);
1572 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001573
Jens Axboea3a0e432019-08-20 11:03:11 -06001574 /* See comment at the top of this file */
1575 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001576 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001577}
1578
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001579static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1580{
1581 struct io_rings *rings = ctx->rings;
1582
1583 /* make sure SQ entry isn't read before tail */
1584 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1585}
1586
Jens Axboe8237e042019-12-28 10:48:22 -07001587static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001588{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001589 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1590 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001591
Jens Axboec6ca97b302019-12-28 12:11:08 -07001592 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1593 rb->need_iter++;
1594
1595 rb->reqs[rb->to_free++] = req;
1596 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1597 io_free_req_many(req->ctx, rb);
1598 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001599}
1600
Jens Axboedef596e2019-01-09 08:59:42 -07001601/*
1602 * Find and free completed poll iocbs
1603 */
1604static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1605 struct list_head *done)
1606{
Jens Axboe8237e042019-12-28 10:48:22 -07001607 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001608 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001609
Jens Axboec6ca97b302019-12-28 12:11:08 -07001610 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001611 while (!list_empty(done)) {
1612 req = list_first_entry(done, struct io_kiocb, list);
1613 list_del(&req->list);
1614
Jens Axboe78e19bb2019-11-06 15:21:34 -07001615 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001616 (*nr_events)++;
1617
Jens Axboe8237e042019-12-28 10:48:22 -07001618 if (refcount_dec_and_test(&req->refs) &&
1619 !io_req_multi_free(&rb, req))
1620 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001621 }
Jens Axboedef596e2019-01-09 08:59:42 -07001622
Jens Axboe09bb8392019-03-13 12:39:28 -06001623 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001624 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001625}
1626
1627static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1628 long min)
1629{
1630 struct io_kiocb *req, *tmp;
1631 LIST_HEAD(done);
1632 bool spin;
1633 int ret;
1634
1635 /*
1636 * Only spin for completions if we don't have multiple devices hanging
1637 * off our complete list, and we're under the requested amount.
1638 */
1639 spin = !ctx->poll_multi_file && *nr_events < min;
1640
1641 ret = 0;
1642 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001643 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001644
1645 /*
1646 * Move completed entries to our local list. If we find a
1647 * request that requires polling, break out and complete
1648 * the done list first, if we have entries there.
1649 */
1650 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1651 list_move_tail(&req->list, &done);
1652 continue;
1653 }
1654 if (!list_empty(&done))
1655 break;
1656
1657 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1658 if (ret < 0)
1659 break;
1660
1661 if (ret && spin)
1662 spin = false;
1663 ret = 0;
1664 }
1665
1666 if (!list_empty(&done))
1667 io_iopoll_complete(ctx, nr_events, &done);
1668
1669 return ret;
1670}
1671
1672/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001673 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001674 * non-spinning poll check - we'll still enter the driver poll loop, but only
1675 * as a non-spinning completion check.
1676 */
1677static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1678 long min)
1679{
Jens Axboe08f54392019-08-21 22:19:11 -06001680 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001681 int ret;
1682
1683 ret = io_do_iopoll(ctx, nr_events, min);
1684 if (ret < 0)
1685 return ret;
1686 if (!min || *nr_events >= min)
1687 return 0;
1688 }
1689
1690 return 1;
1691}
1692
1693/*
1694 * We can't just wait for polled events to come to us, we have to actively
1695 * find and complete them.
1696 */
1697static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1698{
1699 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1700 return;
1701
1702 mutex_lock(&ctx->uring_lock);
1703 while (!list_empty(&ctx->poll_list)) {
1704 unsigned int nr_events = 0;
1705
1706 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001707
1708 /*
1709 * Ensure we allow local-to-the-cpu processing to take place,
1710 * in this case we need to ensure that we reap all events.
1711 */
1712 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001713 }
1714 mutex_unlock(&ctx->uring_lock);
1715}
1716
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001717static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1718 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001719{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001720 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001721
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001722 /*
1723 * We disallow the app entering submit/complete with polling, but we
1724 * still need to lock the ring to prevent racing with polled issue
1725 * that got punted to a workqueue.
1726 */
1727 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001728 do {
1729 int tmin = 0;
1730
Jens Axboe500f9fb2019-08-19 12:15:59 -06001731 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001732 * Don't enter poll loop if we already have events pending.
1733 * If we do, we can potentially be spinning for commands that
1734 * already triggered a CQE (eg in error).
1735 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001736 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001737 break;
1738
1739 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001740 * If a submit got punted to a workqueue, we can have the
1741 * application entering polling for a command before it gets
1742 * issued. That app will hold the uring_lock for the duration
1743 * of the poll right here, so we need to take a breather every
1744 * now and then to ensure that the issue has a chance to add
1745 * the poll to the issued list. Otherwise we can spin here
1746 * forever, while the workqueue is stuck trying to acquire the
1747 * very same mutex.
1748 */
1749 if (!(++iters & 7)) {
1750 mutex_unlock(&ctx->uring_lock);
1751 mutex_lock(&ctx->uring_lock);
1752 }
1753
Jens Axboedef596e2019-01-09 08:59:42 -07001754 if (*nr_events < min)
1755 tmin = min - *nr_events;
1756
1757 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1758 if (ret <= 0)
1759 break;
1760 ret = 0;
1761 } while (min && !*nr_events && !need_resched());
1762
Jens Axboe500f9fb2019-08-19 12:15:59 -06001763 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001764 return ret;
1765}
1766
Jens Axboe491381ce2019-10-17 09:20:46 -06001767static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001768{
Jens Axboe491381ce2019-10-17 09:20:46 -06001769 /*
1770 * Tell lockdep we inherited freeze protection from submission
1771 * thread.
1772 */
1773 if (req->flags & REQ_F_ISREG) {
1774 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775
Jens Axboe491381ce2019-10-17 09:20:46 -06001776 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001778 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779}
1780
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001781static inline void req_set_fail_links(struct io_kiocb *req)
1782{
1783 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1784 req->flags |= REQ_F_FAIL_LINK;
1785}
1786
Jens Axboeba816ad2019-09-28 11:36:45 -06001787static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788{
Jens Axboe9adbd452019-12-20 08:45:55 -07001789 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790
Jens Axboe491381ce2019-10-17 09:20:46 -06001791 if (kiocb->ki_flags & IOCB_WRITE)
1792 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001794 if (res != req->result)
1795 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001796 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001797}
1798
1799static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1800{
Jens Axboe9adbd452019-12-20 08:45:55 -07001801 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001802
1803 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001804 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001805}
1806
Jens Axboedef596e2019-01-09 08:59:42 -07001807static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1808{
Jens Axboe9adbd452019-12-20 08:45:55 -07001809 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001810
Jens Axboe491381ce2019-10-17 09:20:46 -06001811 if (kiocb->ki_flags & IOCB_WRITE)
1812 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001813
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001814 if (res != req->result)
1815 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001816 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001817 if (res != -EAGAIN)
1818 req->flags |= REQ_F_IOPOLL_COMPLETED;
1819}
1820
1821/*
1822 * After the iocb has been issued, it's safe to be found on the poll list.
1823 * Adding the kiocb to the list AFTER submission ensures that we don't
1824 * find it from a io_iopoll_getevents() thread before the issuer is done
1825 * accessing the kiocb cookie.
1826 */
1827static void io_iopoll_req_issued(struct io_kiocb *req)
1828{
1829 struct io_ring_ctx *ctx = req->ctx;
1830
1831 /*
1832 * Track whether we have multiple files in our lists. This will impact
1833 * how we do polling eventually, not spinning if we're on potentially
1834 * different devices.
1835 */
1836 if (list_empty(&ctx->poll_list)) {
1837 ctx->poll_multi_file = false;
1838 } else if (!ctx->poll_multi_file) {
1839 struct io_kiocb *list_req;
1840
1841 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1842 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001843 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001844 ctx->poll_multi_file = true;
1845 }
1846
1847 /*
1848 * For fast devices, IO may have already completed. If it has, add
1849 * it to the front so we find it first.
1850 */
1851 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1852 list_add(&req->list, &ctx->poll_list);
1853 else
1854 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001855
1856 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1857 wq_has_sleeper(&ctx->sqo_wait))
1858 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001859}
1860
Jens Axboe3d6770f2019-04-13 11:50:54 -06001861static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001862{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001863 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001864 int diff = state->has_refs - state->used_refs;
1865
1866 if (diff)
1867 fput_many(state->file, diff);
1868 state->file = NULL;
1869 }
1870}
1871
1872/*
1873 * Get as many references to a file as we have IOs left in this submission,
1874 * assuming most submissions are for one file, or at least that each file
1875 * has more than one submission.
1876 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001877static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001878{
1879 if (!state)
1880 return fget(fd);
1881
1882 if (state->file) {
1883 if (state->fd == fd) {
1884 state->used_refs++;
1885 state->ios_left--;
1886 return state->file;
1887 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001888 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001889 }
1890 state->file = fget_many(fd, state->ios_left);
1891 if (!state->file)
1892 return NULL;
1893
1894 state->fd = fd;
1895 state->has_refs = state->ios_left;
1896 state->used_refs = 1;
1897 state->ios_left--;
1898 return state->file;
1899}
1900
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901/*
1902 * If we tracked the file through the SCM inflight mechanism, we could support
1903 * any file. For now, just ensure that anything potentially problematic is done
1904 * inline.
1905 */
1906static bool io_file_supports_async(struct file *file)
1907{
1908 umode_t mode = file_inode(file)->i_mode;
1909
Jens Axboe10d59342019-12-09 20:16:22 -07001910 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911 return true;
1912 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1913 return true;
1914
1915 return false;
1916}
1917
Jens Axboe3529d8c2019-12-19 18:24:38 -07001918static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1919 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001920{
Jens Axboedef596e2019-01-09 08:59:42 -07001921 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001922 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001923 unsigned ioprio;
1924 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001925
Jens Axboe491381ce2019-10-17 09:20:46 -06001926 if (S_ISREG(file_inode(req->file)->i_mode))
1927 req->flags |= REQ_F_ISREG;
1928
Jens Axboe2b188cc2019-01-07 10:46:33 -07001929 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001930 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1931 req->flags |= REQ_F_CUR_POS;
1932 kiocb->ki_pos = req->file->f_pos;
1933 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001934 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001935 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1936 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1937 if (unlikely(ret))
1938 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001939
1940 ioprio = READ_ONCE(sqe->ioprio);
1941 if (ioprio) {
1942 ret = ioprio_check_cap(ioprio);
1943 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001944 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001945
1946 kiocb->ki_ioprio = ioprio;
1947 } else
1948 kiocb->ki_ioprio = get_current_ioprio();
1949
Stefan Bühler8449eed2019-04-27 20:34:19 +02001950 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001951 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1952 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001953 req->flags |= REQ_F_NOWAIT;
1954
1955 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001957
Jens Axboedef596e2019-01-09 08:59:42 -07001958 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001959 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1960 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001961 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001962
Jens Axboedef596e2019-01-09 08:59:42 -07001963 kiocb->ki_flags |= IOCB_HIPRI;
1964 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001965 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001966 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001967 if (kiocb->ki_flags & IOCB_HIPRI)
1968 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001969 kiocb->ki_complete = io_complete_rw;
1970 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001971
Jens Axboe3529d8c2019-12-19 18:24:38 -07001972 req->rw.addr = READ_ONCE(sqe->addr);
1973 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001974 /* we own ->private, reuse it for the buffer index */
1975 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001976 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001977 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001978}
1979
1980static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1981{
1982 switch (ret) {
1983 case -EIOCBQUEUED:
1984 break;
1985 case -ERESTARTSYS:
1986 case -ERESTARTNOINTR:
1987 case -ERESTARTNOHAND:
1988 case -ERESTART_RESTARTBLOCK:
1989 /*
1990 * We can't just restart the syscall, since previously
1991 * submitted sqes may already be in progress. Just fail this
1992 * IO with EINTR.
1993 */
1994 ret = -EINTR;
1995 /* fall through */
1996 default:
1997 kiocb->ki_complete(kiocb, ret, 0);
1998 }
1999}
2000
Pavel Begunkov014db002020-03-03 21:33:12 +03002001static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002002{
Jens Axboeba042912019-12-25 16:33:42 -07002003 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2004
2005 if (req->flags & REQ_F_CUR_POS)
2006 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002007 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002008 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002009 else
2010 io_rw_done(kiocb, ret);
2011}
2012
Jens Axboe9adbd452019-12-20 08:45:55 -07002013static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002014 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002015{
Jens Axboe9adbd452019-12-20 08:45:55 -07002016 struct io_ring_ctx *ctx = req->ctx;
2017 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002018 struct io_mapped_ubuf *imu;
2019 unsigned index, buf_index;
2020 size_t offset;
2021 u64 buf_addr;
2022
2023 /* attempt to use fixed buffers without having provided iovecs */
2024 if (unlikely(!ctx->user_bufs))
2025 return -EFAULT;
2026
Jens Axboe9adbd452019-12-20 08:45:55 -07002027 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002028 if (unlikely(buf_index >= ctx->nr_user_bufs))
2029 return -EFAULT;
2030
2031 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2032 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002033 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002034
2035 /* overflow */
2036 if (buf_addr + len < buf_addr)
2037 return -EFAULT;
2038 /* not inside the mapped region */
2039 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2040 return -EFAULT;
2041
2042 /*
2043 * May not be a start of buffer, set size appropriately
2044 * and advance us to the beginning.
2045 */
2046 offset = buf_addr - imu->ubuf;
2047 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002048
2049 if (offset) {
2050 /*
2051 * Don't use iov_iter_advance() here, as it's really slow for
2052 * using the latter parts of a big fixed buffer - it iterates
2053 * over each segment manually. We can cheat a bit here, because
2054 * we know that:
2055 *
2056 * 1) it's a BVEC iter, we set it up
2057 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2058 * first and last bvec
2059 *
2060 * So just find our index, and adjust the iterator afterwards.
2061 * If the offset is within the first bvec (or the whole first
2062 * bvec, just use iov_iter_advance(). This makes it easier
2063 * since we can just skip the first segment, which may not
2064 * be PAGE_SIZE aligned.
2065 */
2066 const struct bio_vec *bvec = imu->bvec;
2067
2068 if (offset <= bvec->bv_len) {
2069 iov_iter_advance(iter, offset);
2070 } else {
2071 unsigned long seg_skip;
2072
2073 /* skip first vec */
2074 offset -= bvec->bv_len;
2075 seg_skip = 1 + (offset >> PAGE_SHIFT);
2076
2077 iter->bvec = bvec + seg_skip;
2078 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002079 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002080 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002081 }
2082 }
2083
Jens Axboe5e559562019-11-13 16:12:46 -07002084 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002085}
2086
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002087static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2088 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002089{
Jens Axboe9adbd452019-12-20 08:45:55 -07002090 void __user *buf = u64_to_user_ptr(req->rw.addr);
2091 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002092 u8 opcode;
2093
Jens Axboed625c6e2019-12-17 19:53:05 -07002094 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002095 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002096 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002097 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002098 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002099
Jens Axboe9adbd452019-12-20 08:45:55 -07002100 /* buffer index only valid with fixed read/write */
2101 if (req->rw.kiocb.private)
2102 return -EINVAL;
2103
Jens Axboe3a6820f2019-12-22 15:19:35 -07002104 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2105 ssize_t ret;
2106 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2107 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002108 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002109 }
2110
Jens Axboef67676d2019-12-02 11:03:47 -07002111 if (req->io) {
2112 struct io_async_rw *iorw = &req->io->rw;
2113
2114 *iovec = iorw->iov;
2115 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2116 if (iorw->iov == iorw->fast_iov)
2117 *iovec = NULL;
2118 return iorw->size;
2119 }
2120
Jens Axboe2b188cc2019-01-07 10:46:33 -07002121#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002122 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002123 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2124 iovec, iter);
2125#endif
2126
2127 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2128}
2129
Jens Axboe32960612019-09-23 11:05:34 -06002130/*
2131 * For files that don't have ->read_iter() and ->write_iter(), handle them
2132 * by looping over ->read() or ->write() manually.
2133 */
2134static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2135 struct iov_iter *iter)
2136{
2137 ssize_t ret = 0;
2138
2139 /*
2140 * Don't support polled IO through this interface, and we can't
2141 * support non-blocking either. For the latter, this just causes
2142 * the kiocb to be handled from an async context.
2143 */
2144 if (kiocb->ki_flags & IOCB_HIPRI)
2145 return -EOPNOTSUPP;
2146 if (kiocb->ki_flags & IOCB_NOWAIT)
2147 return -EAGAIN;
2148
2149 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002150 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002151 ssize_t nr;
2152
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002153 if (!iov_iter_is_bvec(iter)) {
2154 iovec = iov_iter_iovec(iter);
2155 } else {
2156 /* fixed buffers import bvec */
2157 iovec.iov_base = kmap(iter->bvec->bv_page)
2158 + iter->iov_offset;
2159 iovec.iov_len = min(iter->count,
2160 iter->bvec->bv_len - iter->iov_offset);
2161 }
2162
Jens Axboe32960612019-09-23 11:05:34 -06002163 if (rw == READ) {
2164 nr = file->f_op->read(file, iovec.iov_base,
2165 iovec.iov_len, &kiocb->ki_pos);
2166 } else {
2167 nr = file->f_op->write(file, iovec.iov_base,
2168 iovec.iov_len, &kiocb->ki_pos);
2169 }
2170
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002171 if (iov_iter_is_bvec(iter))
2172 kunmap(iter->bvec->bv_page);
2173
Jens Axboe32960612019-09-23 11:05:34 -06002174 if (nr < 0) {
2175 if (!ret)
2176 ret = nr;
2177 break;
2178 }
2179 ret += nr;
2180 if (nr != iovec.iov_len)
2181 break;
2182 iov_iter_advance(iter, nr);
2183 }
2184
2185 return ret;
2186}
2187
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002188static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002189 struct iovec *iovec, struct iovec *fast_iov,
2190 struct iov_iter *iter)
2191{
2192 req->io->rw.nr_segs = iter->nr_segs;
2193 req->io->rw.size = io_size;
2194 req->io->rw.iov = iovec;
2195 if (!req->io->rw.iov) {
2196 req->io->rw.iov = req->io->rw.fast_iov;
2197 memcpy(req->io->rw.iov, fast_iov,
2198 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002199 } else {
2200 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002201 }
2202}
2203
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002204static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002205{
Jens Axboed3656342019-12-18 09:50:26 -07002206 if (!io_op_defs[req->opcode].async_ctx)
2207 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002208 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002209 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002210}
2211
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002212static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2213 struct iovec *iovec, struct iovec *fast_iov,
2214 struct iov_iter *iter)
2215{
Jens Axboe980ad262020-01-24 23:08:54 -07002216 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002217 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002218 if (!req->io) {
2219 if (io_alloc_async_ctx(req))
2220 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002221
Jens Axboe5d204bc2020-01-31 12:06:52 -07002222 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2223 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002224 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002225}
2226
Jens Axboe3529d8c2019-12-19 18:24:38 -07002227static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2228 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002229{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002230 struct io_async_ctx *io;
2231 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002232 ssize_t ret;
2233
Jens Axboe3529d8c2019-12-19 18:24:38 -07002234 ret = io_prep_rw(req, sqe, force_nonblock);
2235 if (ret)
2236 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002237
Jens Axboe3529d8c2019-12-19 18:24:38 -07002238 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2239 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002240
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002241 /* either don't need iovec imported or already have it */
2242 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002243 return 0;
2244
2245 io = req->io;
2246 io->rw.iov = io->rw.fast_iov;
2247 req->io = NULL;
2248 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2249 req->io = io;
2250 if (ret < 0)
2251 return ret;
2252
2253 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2254 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002255}
2256
Pavel Begunkov014db002020-03-03 21:33:12 +03002257static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002258{
2259 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002260 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002261 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002262 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002263 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002264
Jens Axboe3529d8c2019-12-19 18:24:38 -07002265 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002266 if (ret < 0)
2267 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002268
Jens Axboefd6c2e42019-12-18 12:19:41 -07002269 /* Ensure we clear previously set non-block flag */
2270 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002271 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002272
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002273 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002274 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002275 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002276 req->result = io_size;
2277
2278 /*
2279 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2280 * we know to async punt it even if it was opened O_NONBLOCK
2281 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002282 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002283 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002284
Jens Axboe31b51512019-01-18 22:56:34 -07002285 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002286 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002287 if (!ret) {
2288 ssize_t ret2;
2289
Jens Axboe9adbd452019-12-20 08:45:55 -07002290 if (req->file->f_op->read_iter)
2291 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002292 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002293 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002294
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002295 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002296 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002297 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002298 } else {
2299copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002300 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002301 inline_vecs, &iter);
2302 if (ret)
2303 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002304 /* any defer here is final, must blocking retry */
2305 if (!(req->flags & REQ_F_NOWAIT))
2306 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002307 return -EAGAIN;
2308 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002309 }
Jens Axboef67676d2019-12-02 11:03:47 -07002310out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002311 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002312 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002313 return ret;
2314}
2315
Jens Axboe3529d8c2019-12-19 18:24:38 -07002316static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2317 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002318{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002319 struct io_async_ctx *io;
2320 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002321 ssize_t ret;
2322
Jens Axboe3529d8c2019-12-19 18:24:38 -07002323 ret = io_prep_rw(req, sqe, force_nonblock);
2324 if (ret)
2325 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002326
Jens Axboe3529d8c2019-12-19 18:24:38 -07002327 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2328 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002329
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002330 /* either don't need iovec imported or already have it */
2331 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002332 return 0;
2333
2334 io = req->io;
2335 io->rw.iov = io->rw.fast_iov;
2336 req->io = NULL;
2337 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2338 req->io = io;
2339 if (ret < 0)
2340 return ret;
2341
2342 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2343 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002344}
2345
Pavel Begunkov014db002020-03-03 21:33:12 +03002346static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002347{
2348 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002349 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002350 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002351 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002352 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002353
Jens Axboe3529d8c2019-12-19 18:24:38 -07002354 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002355 if (ret < 0)
2356 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002357
Jens Axboefd6c2e42019-12-18 12:19:41 -07002358 /* Ensure we clear previously set non-block flag */
2359 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002360 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002361
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002362 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002363 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002364 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002365 req->result = io_size;
2366
2367 /*
2368 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2369 * we know to async punt it even if it was opened O_NONBLOCK
2370 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002371 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002372 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002373
Jens Axboe10d59342019-12-09 20:16:22 -07002374 /* file path doesn't support NOWAIT for non-direct_IO */
2375 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2376 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002377 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002378
Jens Axboe31b51512019-01-18 22:56:34 -07002379 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002380 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002381 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002382 ssize_t ret2;
2383
Jens Axboe2b188cc2019-01-07 10:46:33 -07002384 /*
2385 * Open-code file_start_write here to grab freeze protection,
2386 * which will be released by another thread in
2387 * io_complete_rw(). Fool lockdep by telling it the lock got
2388 * released so that it doesn't complain about the held lock when
2389 * we return to userspace.
2390 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002391 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002392 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002393 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002394 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002395 SB_FREEZE_WRITE);
2396 }
2397 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002398
Jens Axboe9adbd452019-12-20 08:45:55 -07002399 if (req->file->f_op->write_iter)
2400 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002401 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002402 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002403 /*
2404 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2405 * retry them without IOCB_NOWAIT.
2406 */
2407 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2408 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002409 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002410 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002411 } else {
2412copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002413 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002414 inline_vecs, &iter);
2415 if (ret)
2416 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002417 /* any defer here is final, must blocking retry */
2418 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002419 return -EAGAIN;
2420 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002421 }
Jens Axboe31b51512019-01-18 22:56:34 -07002422out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002423 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002424 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002425 return ret;
2426}
2427
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002428static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2429{
2430 struct io_splice* sp = &req->splice;
2431 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2432 int ret;
2433
2434 if (req->flags & REQ_F_NEED_CLEANUP)
2435 return 0;
2436
2437 sp->file_in = NULL;
2438 sp->off_in = READ_ONCE(sqe->splice_off_in);
2439 sp->off_out = READ_ONCE(sqe->off);
2440 sp->len = READ_ONCE(sqe->len);
2441 sp->flags = READ_ONCE(sqe->splice_flags);
2442
2443 if (unlikely(sp->flags & ~valid_flags))
2444 return -EINVAL;
2445
2446 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2447 (sp->flags & SPLICE_F_FD_IN_FIXED));
2448 if (ret)
2449 return ret;
2450 req->flags |= REQ_F_NEED_CLEANUP;
2451
2452 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2453 req->work.flags |= IO_WQ_WORK_UNBOUND;
2454
2455 return 0;
2456}
2457
2458static bool io_splice_punt(struct file *file)
2459{
2460 if (get_pipe_info(file))
2461 return false;
2462 if (!io_file_supports_async(file))
2463 return true;
2464 return !(file->f_mode & O_NONBLOCK);
2465}
2466
Pavel Begunkov014db002020-03-03 21:33:12 +03002467static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002468{
2469 struct io_splice *sp = &req->splice;
2470 struct file *in = sp->file_in;
2471 struct file *out = sp->file_out;
2472 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2473 loff_t *poff_in, *poff_out;
2474 long ret;
2475
2476 if (force_nonblock) {
2477 if (io_splice_punt(in) || io_splice_punt(out))
2478 return -EAGAIN;
2479 flags |= SPLICE_F_NONBLOCK;
2480 }
2481
2482 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2483 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2484 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2485 if (force_nonblock && ret == -EAGAIN)
2486 return -EAGAIN;
2487
2488 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2489 req->flags &= ~REQ_F_NEED_CLEANUP;
2490
2491 io_cqring_add_event(req, ret);
2492 if (ret != sp->len)
2493 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002494 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002495 return 0;
2496}
2497
Jens Axboe2b188cc2019-01-07 10:46:33 -07002498/*
2499 * IORING_OP_NOP just posts a completion event, nothing else.
2500 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002501static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002502{
2503 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002504
Jens Axboedef596e2019-01-09 08:59:42 -07002505 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2506 return -EINVAL;
2507
Jens Axboe78e19bb2019-11-06 15:21:34 -07002508 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002509 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002510 return 0;
2511}
2512
Jens Axboe3529d8c2019-12-19 18:24:38 -07002513static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002514{
Jens Axboe6b063142019-01-10 22:13:58 -07002515 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002516
Jens Axboe09bb8392019-03-13 12:39:28 -06002517 if (!req->file)
2518 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002519
Jens Axboe6b063142019-01-10 22:13:58 -07002520 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002521 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002522 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002523 return -EINVAL;
2524
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002525 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2526 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2527 return -EINVAL;
2528
2529 req->sync.off = READ_ONCE(sqe->off);
2530 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002531 return 0;
2532}
2533
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002534static bool io_req_cancelled(struct io_kiocb *req)
2535{
2536 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2537 req_set_fail_links(req);
2538 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkov594506f2020-03-03 21:33:11 +03002539 io_double_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002540 return true;
2541 }
2542
2543 return false;
2544}
2545
Jens Axboe78912932020-01-14 22:09:06 -07002546static void io_link_work_cb(struct io_wq_work **workptr)
2547{
2548 struct io_wq_work *work = *workptr;
2549 struct io_kiocb *link = work->data;
2550
2551 io_queue_linked_timeout(link);
Pavel Begunkov5eae8612020-02-28 10:36:38 +03002552 io_wq_submit_work(workptr);
Jens Axboe78912932020-01-14 22:09:06 -07002553}
2554
2555static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2556{
2557 struct io_kiocb *link;
2558
Jens Axboe78912932020-01-14 22:09:06 -07002559 *workptr = &nxt->work;
Pavel Begunkov3b17cf52020-02-29 22:56:10 +03002560 link = io_prep_linked_timeout(nxt);
Jens Axboe78912932020-01-14 22:09:06 -07002561 if (link) {
Jens Axboe78912932020-01-14 22:09:06 -07002562 nxt->work.func = io_link_work_cb;
2563 nxt->work.data = link;
2564 }
2565}
2566
Pavel Begunkov014db002020-03-03 21:33:12 +03002567static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002568{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002569 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002570 int ret;
2571
Jens Axboe9adbd452019-12-20 08:45:55 -07002572 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002573 end > 0 ? end : LLONG_MAX,
2574 req->sync.flags & IORING_FSYNC_DATASYNC);
2575 if (ret < 0)
2576 req_set_fail_links(req);
2577 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002578 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002579}
2580
2581static void io_fsync_finish(struct io_wq_work **workptr)
2582{
2583 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2584 struct io_kiocb *nxt = NULL;
2585
2586 if (io_req_cancelled(req))
2587 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002588 __io_fsync(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03002589 io_put_req(req); /* drop submission reference */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002590 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002591 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002592}
2593
Pavel Begunkov014db002020-03-03 21:33:12 +03002594static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002595{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002596 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002597 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002598 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002599 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002600 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002601 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002602 return 0;
2603}
2604
Pavel Begunkov014db002020-03-03 21:33:12 +03002605static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002606{
Jens Axboed63d1b52019-12-10 10:38:56 -07002607 int ret;
2608
2609 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2610 req->sync.len);
2611 if (ret < 0)
2612 req_set_fail_links(req);
2613 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002614 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002615}
2616
2617static void io_fallocate_finish(struct io_wq_work **workptr)
2618{
2619 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2620 struct io_kiocb *nxt = NULL;
2621
Pavel Begunkov594506f2020-03-03 21:33:11 +03002622 if (io_req_cancelled(req))
2623 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002624 __io_fallocate(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03002625 io_put_req(req); /* drop submission reference */
Jens Axboed63d1b52019-12-10 10:38:56 -07002626 if (nxt)
2627 io_wq_assign_next(workptr, nxt);
2628}
2629
2630static int io_fallocate_prep(struct io_kiocb *req,
2631 const struct io_uring_sqe *sqe)
2632{
2633 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2634 return -EINVAL;
2635
2636 req->sync.off = READ_ONCE(sqe->off);
2637 req->sync.len = READ_ONCE(sqe->addr);
2638 req->sync.mode = READ_ONCE(sqe->len);
2639 return 0;
2640}
2641
Pavel Begunkov014db002020-03-03 21:33:12 +03002642static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002643{
Jens Axboed63d1b52019-12-10 10:38:56 -07002644 /* fallocate always requiring blocking context */
2645 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002646 req->work.func = io_fallocate_finish;
2647 return -EAGAIN;
2648 }
2649
Pavel Begunkov014db002020-03-03 21:33:12 +03002650 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002651 return 0;
2652}
2653
Jens Axboe15b71ab2019-12-11 11:20:36 -07002654static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2655{
Jens Axboef8748882020-01-08 17:47:02 -07002656 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002657 int ret;
2658
2659 if (sqe->ioprio || sqe->buf_index)
2660 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002661 if (sqe->flags & IOSQE_FIXED_FILE)
2662 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002663 if (req->flags & REQ_F_NEED_CLEANUP)
2664 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002665
2666 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002667 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002668 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002669 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002670
Jens Axboef8748882020-01-08 17:47:02 -07002671 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002672 if (IS_ERR(req->open.filename)) {
2673 ret = PTR_ERR(req->open.filename);
2674 req->open.filename = NULL;
2675 return ret;
2676 }
2677
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002678 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002679 return 0;
2680}
2681
Jens Axboecebdb982020-01-08 17:59:24 -07002682static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2683{
2684 struct open_how __user *how;
2685 const char __user *fname;
2686 size_t len;
2687 int ret;
2688
2689 if (sqe->ioprio || sqe->buf_index)
2690 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002691 if (sqe->flags & IOSQE_FIXED_FILE)
2692 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002693 if (req->flags & REQ_F_NEED_CLEANUP)
2694 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002695
2696 req->open.dfd = READ_ONCE(sqe->fd);
2697 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2698 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2699 len = READ_ONCE(sqe->len);
2700
2701 if (len < OPEN_HOW_SIZE_VER0)
2702 return -EINVAL;
2703
2704 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2705 len);
2706 if (ret)
2707 return ret;
2708
2709 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2710 req->open.how.flags |= O_LARGEFILE;
2711
2712 req->open.filename = getname(fname);
2713 if (IS_ERR(req->open.filename)) {
2714 ret = PTR_ERR(req->open.filename);
2715 req->open.filename = NULL;
2716 return ret;
2717 }
2718
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002719 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002720 return 0;
2721}
2722
Pavel Begunkov014db002020-03-03 21:33:12 +03002723static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002724{
2725 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002726 struct file *file;
2727 int ret;
2728
Jens Axboef86cd202020-01-29 13:46:44 -07002729 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002730 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002731
Jens Axboecebdb982020-01-08 17:59:24 -07002732 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002733 if (ret)
2734 goto err;
2735
Jens Axboecebdb982020-01-08 17:59:24 -07002736 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002737 if (ret < 0)
2738 goto err;
2739
2740 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2741 if (IS_ERR(file)) {
2742 put_unused_fd(ret);
2743 ret = PTR_ERR(file);
2744 } else {
2745 fsnotify_open(file);
2746 fd_install(ret, file);
2747 }
2748err:
2749 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002750 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002751 if (ret < 0)
2752 req_set_fail_links(req);
2753 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002754 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002755 return 0;
2756}
2757
Pavel Begunkov014db002020-03-03 21:33:12 +03002758static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07002759{
2760 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03002761 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07002762}
2763
Jens Axboe3e4827b2020-01-08 15:18:09 -07002764static int io_epoll_ctl_prep(struct io_kiocb *req,
2765 const struct io_uring_sqe *sqe)
2766{
2767#if defined(CONFIG_EPOLL)
2768 if (sqe->ioprio || sqe->buf_index)
2769 return -EINVAL;
2770
2771 req->epoll.epfd = READ_ONCE(sqe->fd);
2772 req->epoll.op = READ_ONCE(sqe->len);
2773 req->epoll.fd = READ_ONCE(sqe->off);
2774
2775 if (ep_op_has_event(req->epoll.op)) {
2776 struct epoll_event __user *ev;
2777
2778 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2779 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2780 return -EFAULT;
2781 }
2782
2783 return 0;
2784#else
2785 return -EOPNOTSUPP;
2786#endif
2787}
2788
Pavel Begunkov014db002020-03-03 21:33:12 +03002789static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07002790{
2791#if defined(CONFIG_EPOLL)
2792 struct io_epoll *ie = &req->epoll;
2793 int ret;
2794
2795 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2796 if (force_nonblock && ret == -EAGAIN)
2797 return -EAGAIN;
2798
2799 if (ret < 0)
2800 req_set_fail_links(req);
2801 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002802 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07002803 return 0;
2804#else
2805 return -EOPNOTSUPP;
2806#endif
2807}
2808
Jens Axboec1ca7572019-12-25 22:18:28 -07002809static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2810{
2811#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2812 if (sqe->ioprio || sqe->buf_index || sqe->off)
2813 return -EINVAL;
2814
2815 req->madvise.addr = READ_ONCE(sqe->addr);
2816 req->madvise.len = READ_ONCE(sqe->len);
2817 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2818 return 0;
2819#else
2820 return -EOPNOTSUPP;
2821#endif
2822}
2823
Pavel Begunkov014db002020-03-03 21:33:12 +03002824static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07002825{
2826#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2827 struct io_madvise *ma = &req->madvise;
2828 int ret;
2829
2830 if (force_nonblock)
2831 return -EAGAIN;
2832
2833 ret = do_madvise(ma->addr, ma->len, ma->advice);
2834 if (ret < 0)
2835 req_set_fail_links(req);
2836 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002837 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07002838 return 0;
2839#else
2840 return -EOPNOTSUPP;
2841#endif
2842}
2843
Jens Axboe4840e412019-12-25 22:03:45 -07002844static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2845{
2846 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2847 return -EINVAL;
2848
2849 req->fadvise.offset = READ_ONCE(sqe->off);
2850 req->fadvise.len = READ_ONCE(sqe->len);
2851 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2852 return 0;
2853}
2854
Pavel Begunkov014db002020-03-03 21:33:12 +03002855static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07002856{
2857 struct io_fadvise *fa = &req->fadvise;
2858 int ret;
2859
Jens Axboe3e694262020-02-01 09:22:49 -07002860 if (force_nonblock) {
2861 switch (fa->advice) {
2862 case POSIX_FADV_NORMAL:
2863 case POSIX_FADV_RANDOM:
2864 case POSIX_FADV_SEQUENTIAL:
2865 break;
2866 default:
2867 return -EAGAIN;
2868 }
2869 }
Jens Axboe4840e412019-12-25 22:03:45 -07002870
2871 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2872 if (ret < 0)
2873 req_set_fail_links(req);
2874 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002875 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07002876 return 0;
2877}
2878
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002879static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2880{
Jens Axboef8748882020-01-08 17:47:02 -07002881 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002882 unsigned lookup_flags;
2883 int ret;
2884
2885 if (sqe->ioprio || sqe->buf_index)
2886 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002887 if (sqe->flags & IOSQE_FIXED_FILE)
2888 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002889 if (req->flags & REQ_F_NEED_CLEANUP)
2890 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002891
2892 req->open.dfd = READ_ONCE(sqe->fd);
2893 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002894 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002895 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002896 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002897
Jens Axboec12cedf2020-01-08 17:41:21 -07002898 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002899 return -EINVAL;
2900
Jens Axboef8748882020-01-08 17:47:02 -07002901 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002902 if (IS_ERR(req->open.filename)) {
2903 ret = PTR_ERR(req->open.filename);
2904 req->open.filename = NULL;
2905 return ret;
2906 }
2907
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002908 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002909 return 0;
2910}
2911
Pavel Begunkov014db002020-03-03 21:33:12 +03002912static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002913{
2914 struct io_open *ctx = &req->open;
2915 unsigned lookup_flags;
2916 struct path path;
2917 struct kstat stat;
2918 int ret;
2919
2920 if (force_nonblock)
2921 return -EAGAIN;
2922
Jens Axboec12cedf2020-01-08 17:41:21 -07002923 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002924 return -EINVAL;
2925
2926retry:
2927 /* filename_lookup() drops it, keep a reference */
2928 ctx->filename->refcnt++;
2929
2930 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2931 NULL);
2932 if (ret)
2933 goto err;
2934
Jens Axboec12cedf2020-01-08 17:41:21 -07002935 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002936 path_put(&path);
2937 if (retry_estale(ret, lookup_flags)) {
2938 lookup_flags |= LOOKUP_REVAL;
2939 goto retry;
2940 }
2941 if (!ret)
2942 ret = cp_statx(&stat, ctx->buffer);
2943err:
2944 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002945 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002946 if (ret < 0)
2947 req_set_fail_links(req);
2948 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002949 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002950 return 0;
2951}
2952
Jens Axboeb5dba592019-12-11 14:02:38 -07002953static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2954{
2955 /*
2956 * If we queue this for async, it must not be cancellable. That would
2957 * leave the 'file' in an undeterminate state.
2958 */
2959 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2960
2961 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2962 sqe->rw_flags || sqe->buf_index)
2963 return -EINVAL;
2964 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002965 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002966
2967 req->close.fd = READ_ONCE(sqe->fd);
2968 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002969 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002970 return -EBADF;
2971
2972 return 0;
2973}
2974
Pavel Begunkova93b3332020-02-08 14:04:34 +03002975/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03002976static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03002977{
2978 int ret;
2979
2980 ret = filp_close(req->close.put_file, req->work.files);
2981 if (ret < 0)
2982 req_set_fail_links(req);
2983 io_cqring_add_event(req, ret);
2984 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03002985 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03002986}
2987
Jens Axboeb5dba592019-12-11 14:02:38 -07002988static void io_close_finish(struct io_wq_work **workptr)
2989{
2990 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2991 struct io_kiocb *nxt = NULL;
2992
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002993 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03002994 __io_close_finish(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03002995 io_put_req(req); /* drop submission reference */
Jens Axboeb5dba592019-12-11 14:02:38 -07002996 if (nxt)
2997 io_wq_assign_next(workptr, nxt);
2998}
2999
Pavel Begunkov014db002020-03-03 21:33:12 +03003000static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003001{
3002 int ret;
3003
3004 req->close.put_file = NULL;
3005 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3006 if (ret < 0)
3007 return ret;
3008
3009 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003010 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003011 /* submission ref will be dropped, take it for async */
3012 refcount_inc(&req->refs);
3013
Pavel Begunkova2100672020-03-02 23:45:16 +03003014 req->work.func = io_close_finish;
3015 /*
3016 * Do manual async queue here to avoid grabbing files - we don't
3017 * need the files, and it'll cause io_close_finish() to close
3018 * the file again and cause a double CQE entry for this request
3019 */
3020 io_queue_async_work(req);
3021 return 0;
3022 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003023
3024 /*
3025 * No ->flush(), safely close from here and just punt the
3026 * fput() to async context.
3027 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003028 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003029 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003030}
3031
Jens Axboe3529d8c2019-12-19 18:24:38 -07003032static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003033{
3034 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003035
3036 if (!req->file)
3037 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003038
3039 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3040 return -EINVAL;
3041 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3042 return -EINVAL;
3043
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003044 req->sync.off = READ_ONCE(sqe->off);
3045 req->sync.len = READ_ONCE(sqe->len);
3046 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003047 return 0;
3048}
3049
Pavel Begunkov014db002020-03-03 21:33:12 +03003050static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003051{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003052 int ret;
3053
Jens Axboe9adbd452019-12-20 08:45:55 -07003054 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003055 req->sync.flags);
3056 if (ret < 0)
3057 req_set_fail_links(req);
3058 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003059 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003060}
3061
3062
3063static void io_sync_file_range_finish(struct io_wq_work **workptr)
3064{
3065 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3066 struct io_kiocb *nxt = NULL;
3067
3068 if (io_req_cancelled(req))
3069 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003070 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003071 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003072 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003073 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003074}
3075
Pavel Begunkov014db002020-03-03 21:33:12 +03003076static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003077{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003078 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003079 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003080 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003081 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003082 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003083
Pavel Begunkov014db002020-03-03 21:33:12 +03003084 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003085 return 0;
3086}
3087
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003088static int io_setup_async_msg(struct io_kiocb *req,
3089 struct io_async_msghdr *kmsg)
3090{
3091 if (req->io)
3092 return -EAGAIN;
3093 if (io_alloc_async_ctx(req)) {
3094 if (kmsg->iov != kmsg->fast_iov)
3095 kfree(kmsg->iov);
3096 return -ENOMEM;
3097 }
3098 req->flags |= REQ_F_NEED_CLEANUP;
3099 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3100 return -EAGAIN;
3101}
3102
Jens Axboe3529d8c2019-12-19 18:24:38 -07003103static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003104{
Jens Axboe03b12302019-12-02 18:50:25 -07003105#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003106 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003107 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003108 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003109
Jens Axboee47293f2019-12-20 08:58:21 -07003110 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3111 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003112 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003113
Jens Axboed8768362020-02-27 14:17:49 -07003114#ifdef CONFIG_COMPAT
3115 if (req->ctx->compat)
3116 sr->msg_flags |= MSG_CMSG_COMPAT;
3117#endif
3118
Jens Axboefddafac2020-01-04 20:19:44 -07003119 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003120 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003121 /* iovec is already imported */
3122 if (req->flags & REQ_F_NEED_CLEANUP)
3123 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003124
Jens Axboed9688562019-12-09 19:35:20 -07003125 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003126 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003127 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003128 if (!ret)
3129 req->flags |= REQ_F_NEED_CLEANUP;
3130 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003131#else
Jens Axboee47293f2019-12-20 08:58:21 -07003132 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003133#endif
3134}
3135
Pavel Begunkov014db002020-03-03 21:33:12 +03003136static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003137{
3138#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003139 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003140 struct socket *sock;
3141 int ret;
3142
3143 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3144 return -EINVAL;
3145
3146 sock = sock_from_file(req->file, &ret);
3147 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003148 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003149 unsigned flags;
3150
Jens Axboe03b12302019-12-02 18:50:25 -07003151 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003152 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003153 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003154 /* if iov is set, it's allocated already */
3155 if (!kmsg->iov)
3156 kmsg->iov = kmsg->fast_iov;
3157 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003158 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003159 struct io_sr_msg *sr = &req->sr_msg;
3160
Jens Axboe0b416c32019-12-15 10:57:46 -07003161 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003162 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003163
3164 io.msg.iov = io.msg.fast_iov;
3165 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3166 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003167 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003168 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003169 }
3170
Jens Axboee47293f2019-12-20 08:58:21 -07003171 flags = req->sr_msg.msg_flags;
3172 if (flags & MSG_DONTWAIT)
3173 req->flags |= REQ_F_NOWAIT;
3174 else if (force_nonblock)
3175 flags |= MSG_DONTWAIT;
3176
Jens Axboe0b416c32019-12-15 10:57:46 -07003177 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003178 if (force_nonblock && ret == -EAGAIN)
3179 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003180 if (ret == -ERESTARTSYS)
3181 ret = -EINTR;
3182 }
3183
Pavel Begunkov1e950812020-02-06 19:51:16 +03003184 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003185 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003186 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003187 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003188 if (ret < 0)
3189 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003190 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003191 return 0;
3192#else
3193 return -EOPNOTSUPP;
3194#endif
3195}
3196
Pavel Begunkov014db002020-03-03 21:33:12 +03003197static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003198{
3199#if defined(CONFIG_NET)
3200 struct socket *sock;
3201 int ret;
3202
3203 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3204 return -EINVAL;
3205
3206 sock = sock_from_file(req->file, &ret);
3207 if (sock) {
3208 struct io_sr_msg *sr = &req->sr_msg;
3209 struct msghdr msg;
3210 struct iovec iov;
3211 unsigned flags;
3212
3213 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3214 &msg.msg_iter);
3215 if (ret)
3216 return ret;
3217
3218 msg.msg_name = NULL;
3219 msg.msg_control = NULL;
3220 msg.msg_controllen = 0;
3221 msg.msg_namelen = 0;
3222
3223 flags = req->sr_msg.msg_flags;
3224 if (flags & MSG_DONTWAIT)
3225 req->flags |= REQ_F_NOWAIT;
3226 else if (force_nonblock)
3227 flags |= MSG_DONTWAIT;
3228
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003229 msg.msg_flags = flags;
3230 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003231 if (force_nonblock && ret == -EAGAIN)
3232 return -EAGAIN;
3233 if (ret == -ERESTARTSYS)
3234 ret = -EINTR;
3235 }
3236
3237 io_cqring_add_event(req, ret);
3238 if (ret < 0)
3239 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003240 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003241 return 0;
3242#else
3243 return -EOPNOTSUPP;
3244#endif
3245}
3246
Jens Axboe3529d8c2019-12-19 18:24:38 -07003247static int io_recvmsg_prep(struct io_kiocb *req,
3248 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003249{
3250#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003251 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003252 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003253 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003254
Jens Axboe3529d8c2019-12-19 18:24:38 -07003255 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3256 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003257 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003258
Jens Axboed8768362020-02-27 14:17:49 -07003259#ifdef CONFIG_COMPAT
3260 if (req->ctx->compat)
3261 sr->msg_flags |= MSG_CMSG_COMPAT;
3262#endif
3263
Jens Axboefddafac2020-01-04 20:19:44 -07003264 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003265 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003266 /* iovec is already imported */
3267 if (req->flags & REQ_F_NEED_CLEANUP)
3268 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003269
Jens Axboed9688562019-12-09 19:35:20 -07003270 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003271 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003272 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003273 if (!ret)
3274 req->flags |= REQ_F_NEED_CLEANUP;
3275 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003276#else
Jens Axboee47293f2019-12-20 08:58:21 -07003277 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003278#endif
3279}
3280
Pavel Begunkov014db002020-03-03 21:33:12 +03003281static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003282{
3283#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003284 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003285 struct socket *sock;
3286 int ret;
3287
3288 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3289 return -EINVAL;
3290
3291 sock = sock_from_file(req->file, &ret);
3292 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003293 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003294 unsigned flags;
3295
Jens Axboe03b12302019-12-02 18:50:25 -07003296 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003297 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003298 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003299 /* if iov is set, it's allocated already */
3300 if (!kmsg->iov)
3301 kmsg->iov = kmsg->fast_iov;
3302 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003303 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003304 struct io_sr_msg *sr = &req->sr_msg;
3305
Jens Axboe0b416c32019-12-15 10:57:46 -07003306 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003307 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003308
3309 io.msg.iov = io.msg.fast_iov;
3310 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3311 sr->msg_flags, &io.msg.uaddr,
3312 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003313 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003314 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003315 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003316
Jens Axboee47293f2019-12-20 08:58:21 -07003317 flags = req->sr_msg.msg_flags;
3318 if (flags & MSG_DONTWAIT)
3319 req->flags |= REQ_F_NOWAIT;
3320 else if (force_nonblock)
3321 flags |= MSG_DONTWAIT;
3322
3323 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3324 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003325 if (force_nonblock && ret == -EAGAIN)
3326 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003327 if (ret == -ERESTARTSYS)
3328 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003329 }
3330
Pavel Begunkov1e950812020-02-06 19:51:16 +03003331 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003332 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003333 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003334 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003335 if (ret < 0)
3336 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003337 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003338 return 0;
3339#else
3340 return -EOPNOTSUPP;
3341#endif
3342}
3343
Pavel Begunkov014db002020-03-03 21:33:12 +03003344static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003345{
3346#if defined(CONFIG_NET)
3347 struct socket *sock;
3348 int ret;
3349
3350 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3351 return -EINVAL;
3352
3353 sock = sock_from_file(req->file, &ret);
3354 if (sock) {
3355 struct io_sr_msg *sr = &req->sr_msg;
3356 struct msghdr msg;
3357 struct iovec iov;
3358 unsigned flags;
3359
3360 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3361 &msg.msg_iter);
3362 if (ret)
3363 return ret;
3364
3365 msg.msg_name = NULL;
3366 msg.msg_control = NULL;
3367 msg.msg_controllen = 0;
3368 msg.msg_namelen = 0;
3369 msg.msg_iocb = NULL;
3370 msg.msg_flags = 0;
3371
3372 flags = req->sr_msg.msg_flags;
3373 if (flags & MSG_DONTWAIT)
3374 req->flags |= REQ_F_NOWAIT;
3375 else if (force_nonblock)
3376 flags |= MSG_DONTWAIT;
3377
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003378 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003379 if (force_nonblock && ret == -EAGAIN)
3380 return -EAGAIN;
3381 if (ret == -ERESTARTSYS)
3382 ret = -EINTR;
3383 }
3384
3385 io_cqring_add_event(req, ret);
3386 if (ret < 0)
3387 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003388 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003389 return 0;
3390#else
3391 return -EOPNOTSUPP;
3392#endif
3393}
3394
3395
Jens Axboe3529d8c2019-12-19 18:24:38 -07003396static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003397{
3398#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003399 struct io_accept *accept = &req->accept;
3400
Jens Axboe17f2fe32019-10-17 14:42:58 -06003401 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3402 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003403 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003404 return -EINVAL;
3405
Jens Axboed55e5f52019-12-11 16:12:15 -07003406 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3407 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003408 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003409 return 0;
3410#else
3411 return -EOPNOTSUPP;
3412#endif
3413}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003414
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003415#if defined(CONFIG_NET)
Pavel Begunkov014db002020-03-03 21:33:12 +03003416static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003417{
3418 struct io_accept *accept = &req->accept;
3419 unsigned file_flags;
3420 int ret;
3421
3422 file_flags = force_nonblock ? O_NONBLOCK : 0;
3423 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3424 accept->addr_len, accept->flags);
3425 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003426 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003427 if (ret == -ERESTARTSYS)
3428 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003429 if (ret < 0)
3430 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003431 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003432 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003433 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003434}
3435
3436static void io_accept_finish(struct io_wq_work **workptr)
3437{
3438 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3439 struct io_kiocb *nxt = NULL;
3440
3441 if (io_req_cancelled(req))
3442 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003443 __io_accept(req, false);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003444 io_put_req(req); /* drop submission reference */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003445 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003446 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003447}
3448#endif
3449
Pavel Begunkov014db002020-03-03 21:33:12 +03003450static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003451{
3452#if defined(CONFIG_NET)
3453 int ret;
3454
Pavel Begunkov014db002020-03-03 21:33:12 +03003455 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003456 if (ret == -EAGAIN && force_nonblock) {
3457 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003458 return -EAGAIN;
3459 }
3460 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003461#else
3462 return -EOPNOTSUPP;
3463#endif
3464}
3465
Jens Axboe3529d8c2019-12-19 18:24:38 -07003466static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003467{
3468#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003469 struct io_connect *conn = &req->connect;
3470 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003471
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003472 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3473 return -EINVAL;
3474 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3475 return -EINVAL;
3476
Jens Axboe3529d8c2019-12-19 18:24:38 -07003477 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3478 conn->addr_len = READ_ONCE(sqe->addr2);
3479
3480 if (!io)
3481 return 0;
3482
3483 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003484 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003485#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003486 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003487#endif
3488}
3489
Pavel Begunkov014db002020-03-03 21:33:12 +03003490static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003491{
3492#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003493 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003494 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003495 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003496
Jens Axboef499a022019-12-02 16:28:46 -07003497 if (req->io) {
3498 io = req->io;
3499 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003500 ret = move_addr_to_kernel(req->connect.addr,
3501 req->connect.addr_len,
3502 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003503 if (ret)
3504 goto out;
3505 io = &__io;
3506 }
3507
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003508 file_flags = force_nonblock ? O_NONBLOCK : 0;
3509
3510 ret = __sys_connect_file(req->file, &io->connect.address,
3511 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003512 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003513 if (req->io)
3514 return -EAGAIN;
3515 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003516 ret = -ENOMEM;
3517 goto out;
3518 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003519 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003520 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003521 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003522 if (ret == -ERESTARTSYS)
3523 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003524out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003525 if (ret < 0)
3526 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003527 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003528 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003529 return 0;
3530#else
3531 return -EOPNOTSUPP;
3532#endif
3533}
3534
Jens Axboed7718a92020-02-14 22:23:12 -07003535struct io_poll_table {
3536 struct poll_table_struct pt;
3537 struct io_kiocb *req;
3538 int error;
3539};
3540
3541static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
3542 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003543{
Jens Axboed7718a92020-02-14 22:23:12 -07003544 if (unlikely(poll->head)) {
3545 pt->error = -EINVAL;
3546 return;
3547 }
3548
3549 pt->error = 0;
3550 poll->head = head;
3551 add_wait_queue(head, &poll->wait);
3552}
3553
3554static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
3555 struct poll_table_struct *p)
3556{
3557 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3558
3559 __io_queue_proc(&pt->req->apoll->poll, pt, head);
3560}
3561
3562static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
3563 __poll_t mask, task_work_func_t func)
3564{
3565 struct task_struct *tsk;
3566
3567 /* for instances that support it check for an event match first: */
3568 if (mask && !(mask & poll->events))
3569 return 0;
3570
3571 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
3572
3573 list_del_init(&poll->wait.entry);
3574
3575 tsk = req->task;
3576 req->result = mask;
3577 init_task_work(&req->task_work, func);
3578 /*
3579 * If this fails, then the task is exiting. If that is the case, then
3580 * the exit check will ultimately cancel these work items. Hence we
3581 * don't need to check here and handle it specifically.
3582 */
3583 task_work_add(tsk, &req->task_work, true);
3584 wake_up_process(tsk);
3585 return 1;
3586}
3587
3588static void io_async_task_func(struct callback_head *cb)
3589{
3590 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3591 struct async_poll *apoll = req->apoll;
3592 struct io_ring_ctx *ctx = req->ctx;
3593
3594 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
3595
3596 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
3597
3598 if (hash_hashed(&req->hash_node)) {
3599 spin_lock_irq(&ctx->completion_lock);
3600 hash_del(&req->hash_node);
3601 spin_unlock_irq(&ctx->completion_lock);
3602 }
3603
3604 /* restore ->work in case we need to retry again */
3605 memcpy(&req->work, &apoll->work, sizeof(req->work));
3606
3607 __set_current_state(TASK_RUNNING);
3608 mutex_lock(&ctx->uring_lock);
3609 __io_queue_sqe(req, NULL);
3610 mutex_unlock(&ctx->uring_lock);
3611
3612 kfree(apoll);
3613}
3614
3615static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3616 void *key)
3617{
3618 struct io_kiocb *req = wait->private;
3619 struct io_poll_iocb *poll = &req->apoll->poll;
3620
3621 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
3622 key_to_poll(key));
3623
3624 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
3625}
3626
3627static void io_poll_req_insert(struct io_kiocb *req)
3628{
3629 struct io_ring_ctx *ctx = req->ctx;
3630 struct hlist_head *list;
3631
3632 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3633 hlist_add_head(&req->hash_node, list);
3634}
3635
3636static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
3637 struct io_poll_iocb *poll,
3638 struct io_poll_table *ipt, __poll_t mask,
3639 wait_queue_func_t wake_func)
3640 __acquires(&ctx->completion_lock)
3641{
3642 struct io_ring_ctx *ctx = req->ctx;
3643 bool cancel = false;
3644
3645 poll->file = req->file;
3646 poll->head = NULL;
3647 poll->done = poll->canceled = false;
3648 poll->events = mask;
3649
3650 ipt->pt._key = mask;
3651 ipt->req = req;
3652 ipt->error = -EINVAL;
3653
3654 INIT_LIST_HEAD(&poll->wait.entry);
3655 init_waitqueue_func_entry(&poll->wait, wake_func);
3656 poll->wait.private = req;
3657
3658 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
3659
3660 spin_lock_irq(&ctx->completion_lock);
3661 if (likely(poll->head)) {
3662 spin_lock(&poll->head->lock);
3663 if (unlikely(list_empty(&poll->wait.entry))) {
3664 if (ipt->error)
3665 cancel = true;
3666 ipt->error = 0;
3667 mask = 0;
3668 }
3669 if (mask || ipt->error)
3670 list_del_init(&poll->wait.entry);
3671 else if (cancel)
3672 WRITE_ONCE(poll->canceled, true);
3673 else if (!poll->done) /* actually waiting for an event */
3674 io_poll_req_insert(req);
3675 spin_unlock(&poll->head->lock);
3676 }
3677
3678 return mask;
3679}
3680
3681static bool io_arm_poll_handler(struct io_kiocb *req)
3682{
3683 const struct io_op_def *def = &io_op_defs[req->opcode];
3684 struct io_ring_ctx *ctx = req->ctx;
3685 struct async_poll *apoll;
3686 struct io_poll_table ipt;
3687 __poll_t mask, ret;
3688
3689 if (!req->file || !file_can_poll(req->file))
3690 return false;
3691 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
3692 return false;
3693 if (!def->pollin && !def->pollout)
3694 return false;
3695
3696 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
3697 if (unlikely(!apoll))
3698 return false;
3699
3700 req->flags |= REQ_F_POLLED;
3701 memcpy(&apoll->work, &req->work, sizeof(req->work));
3702
3703 /*
3704 * Don't need a reference here, as we're adding it to the task
3705 * task_works list. If the task exits, the list is pruned.
3706 */
3707 req->task = current;
3708 req->apoll = apoll;
3709 INIT_HLIST_NODE(&req->hash_node);
3710
Nathan Chancellor8755d972020-03-02 16:01:19 -07003711 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07003712 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07003713 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07003714 if (def->pollout)
3715 mask |= POLLOUT | POLLWRNORM;
3716 mask |= POLLERR | POLLPRI;
3717
3718 ipt.pt._qproc = io_async_queue_proc;
3719
3720 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
3721 io_async_wake);
3722 if (ret) {
3723 ipt.error = 0;
3724 apoll->poll.done = true;
3725 spin_unlock_irq(&ctx->completion_lock);
3726 memcpy(&req->work, &apoll->work, sizeof(req->work));
3727 kfree(apoll);
3728 return false;
3729 }
3730 spin_unlock_irq(&ctx->completion_lock);
3731 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
3732 apoll->poll.events);
3733 return true;
3734}
3735
3736static bool __io_poll_remove_one(struct io_kiocb *req,
3737 struct io_poll_iocb *poll)
3738{
Jens Axboeb41e9852020-02-17 09:52:41 -07003739 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003740
3741 spin_lock(&poll->head->lock);
3742 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003743 if (!list_empty(&poll->wait.entry)) {
3744 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07003745 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003746 }
3747 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07003748 return do_complete;
3749}
3750
3751static bool io_poll_remove_one(struct io_kiocb *req)
3752{
3753 bool do_complete;
3754
3755 if (req->opcode == IORING_OP_POLL_ADD) {
3756 do_complete = __io_poll_remove_one(req, &req->poll);
3757 } else {
3758 /* non-poll requests have submit ref still */
3759 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
3760 if (do_complete)
3761 io_put_req(req);
3762 }
3763
Jens Axboe78076bb2019-12-04 19:56:40 -07003764 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07003765
Jens Axboeb41e9852020-02-17 09:52:41 -07003766 if (do_complete) {
3767 io_cqring_fill_event(req, -ECANCELED);
3768 io_commit_cqring(req->ctx);
3769 req->flags |= REQ_F_COMP_LOCKED;
3770 io_put_req(req);
3771 }
3772
3773 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003774}
3775
3776static void io_poll_remove_all(struct io_ring_ctx *ctx)
3777{
Jens Axboe78076bb2019-12-04 19:56:40 -07003778 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003779 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003780 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003781
3782 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003783 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3784 struct hlist_head *list;
3785
3786 list = &ctx->cancel_hash[i];
3787 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3788 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003789 }
3790 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003791
3792 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003793}
3794
Jens Axboe47f46762019-11-09 17:43:02 -07003795static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3796{
Jens Axboe78076bb2019-12-04 19:56:40 -07003797 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003798 struct io_kiocb *req;
3799
Jens Axboe78076bb2019-12-04 19:56:40 -07003800 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3801 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07003802 if (sqe_addr != req->user_data)
3803 continue;
3804 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07003805 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07003806 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07003807 }
3808
3809 return -ENOENT;
3810}
3811
Jens Axboe3529d8c2019-12-19 18:24:38 -07003812static int io_poll_remove_prep(struct io_kiocb *req,
3813 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003814{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003815 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3816 return -EINVAL;
3817 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3818 sqe->poll_events)
3819 return -EINVAL;
3820
Jens Axboe0969e782019-12-17 18:40:57 -07003821 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003822 return 0;
3823}
3824
3825/*
3826 * Find a running poll command that matches one specified in sqe->addr,
3827 * and remove it if found.
3828 */
3829static int io_poll_remove(struct io_kiocb *req)
3830{
3831 struct io_ring_ctx *ctx = req->ctx;
3832 u64 addr;
3833 int ret;
3834
Jens Axboe0969e782019-12-17 18:40:57 -07003835 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003836 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003837 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003838 spin_unlock_irq(&ctx->completion_lock);
3839
Jens Axboe78e19bb2019-11-06 15:21:34 -07003840 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003841 if (ret < 0)
3842 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003843 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003844 return 0;
3845}
3846
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003847static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003848{
Jackie Liua197f662019-11-08 08:09:12 -07003849 struct io_ring_ctx *ctx = req->ctx;
3850
Jens Axboe8c838782019-03-12 15:48:16 -06003851 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03003852 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003853 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003854}
3855
Jens Axboeb41e9852020-02-17 09:52:41 -07003856static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003857{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003858 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003859
Jens Axboe221c5eb2019-01-17 09:41:58 -07003860 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003861 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07003862 io_poll_complete(req, req->result, 0);
3863 req->flags |= REQ_F_COMP_LOCKED;
3864 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003865 spin_unlock_irq(&ctx->completion_lock);
3866
Jens Axboe8c838782019-03-12 15:48:16 -06003867 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07003868}
Jens Axboe89723d02019-11-05 15:32:58 -07003869
Jens Axboeb41e9852020-02-17 09:52:41 -07003870static void io_poll_task_func(struct callback_head *cb)
3871{
3872 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3873 struct io_kiocb *nxt = NULL;
3874
3875 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07003876 if (nxt) {
3877 struct io_ring_ctx *ctx = nxt->ctx;
3878
3879 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003880 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07003881 mutex_unlock(&ctx->uring_lock);
3882 }
Jens Axboef0b493e2020-02-01 21:30:11 -07003883}
3884
Jens Axboe221c5eb2019-01-17 09:41:58 -07003885static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3886 void *key)
3887{
Jens Axboec2f2eb72020-02-10 09:07:05 -07003888 struct io_kiocb *req = wait->private;
3889 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003890
Jens Axboed7718a92020-02-14 22:23:12 -07003891 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003892}
3893
Jens Axboe221c5eb2019-01-17 09:41:58 -07003894static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3895 struct poll_table_struct *p)
3896{
3897 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3898
Jens Axboed7718a92020-02-14 22:23:12 -07003899 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07003900}
3901
Jens Axboe3529d8c2019-12-19 18:24:38 -07003902static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003903{
3904 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003905 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003906
3907 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3908 return -EINVAL;
3909 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3910 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003911 if (!poll->file)
3912 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003913
Jens Axboe221c5eb2019-01-17 09:41:58 -07003914 events = READ_ONCE(sqe->poll_events);
3915 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07003916
Jens Axboed7718a92020-02-14 22:23:12 -07003917 /*
3918 * Don't need a reference here, as we're adding it to the task
3919 * task_works list. If the task exits, the list is pruned.
3920 */
Jens Axboeb41e9852020-02-17 09:52:41 -07003921 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07003922 return 0;
3923}
3924
Pavel Begunkov014db002020-03-03 21:33:12 +03003925static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07003926{
3927 struct io_poll_iocb *poll = &req->poll;
3928 struct io_ring_ctx *ctx = req->ctx;
3929 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07003930 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003931
Jens Axboe78076bb2019-12-04 19:56:40 -07003932 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06003933 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07003934 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06003935
Jens Axboed7718a92020-02-14 22:23:12 -07003936 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
3937 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003938
Jens Axboe8c838782019-03-12 15:48:16 -06003939 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003940 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003941 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003942 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003943 spin_unlock_irq(&ctx->completion_lock);
3944
Jens Axboe8c838782019-03-12 15:48:16 -06003945 if (mask) {
3946 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03003947 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003948 }
Jens Axboe8c838782019-03-12 15:48:16 -06003949 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003950}
3951
Jens Axboe5262f562019-09-17 12:26:57 -06003952static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3953{
Jens Axboead8a48a2019-11-15 08:49:11 -07003954 struct io_timeout_data *data = container_of(timer,
3955 struct io_timeout_data, timer);
3956 struct io_kiocb *req = data->req;
3957 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003958 unsigned long flags;
3959
Jens Axboe5262f562019-09-17 12:26:57 -06003960 atomic_inc(&ctx->cq_timeouts);
3961
3962 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003963 /*
Jens Axboe11365042019-10-16 09:08:32 -06003964 * We could be racing with timeout deletion. If the list is empty,
3965 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003966 */
Jens Axboe842f9612019-10-29 12:34:10 -06003967 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003968 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003969
Jens Axboe11365042019-10-16 09:08:32 -06003970 /*
3971 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003972 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003973 * pointer will be increased, otherwise other timeout reqs may
3974 * return in advance without waiting for enough wait_nr.
3975 */
3976 prev = req;
3977 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3978 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003979 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003980 }
Jens Axboe842f9612019-10-29 12:34:10 -06003981
Jens Axboe78e19bb2019-11-06 15:21:34 -07003982 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003983 io_commit_cqring(ctx);
3984 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3985
3986 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003987 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003988 io_put_req(req);
3989 return HRTIMER_NORESTART;
3990}
3991
Jens Axboe47f46762019-11-09 17:43:02 -07003992static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3993{
3994 struct io_kiocb *req;
3995 int ret = -ENOENT;
3996
3997 list_for_each_entry(req, &ctx->timeout_list, list) {
3998 if (user_data == req->user_data) {
3999 list_del_init(&req->list);
4000 ret = 0;
4001 break;
4002 }
4003 }
4004
4005 if (ret == -ENOENT)
4006 return ret;
4007
Jens Axboe2d283902019-12-04 11:08:05 -07004008 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004009 if (ret == -1)
4010 return -EALREADY;
4011
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004012 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004013 io_cqring_fill_event(req, -ECANCELED);
4014 io_put_req(req);
4015 return 0;
4016}
4017
Jens Axboe3529d8c2019-12-19 18:24:38 -07004018static int io_timeout_remove_prep(struct io_kiocb *req,
4019 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004020{
Jens Axboeb29472e2019-12-17 18:50:29 -07004021 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4022 return -EINVAL;
4023 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4024 return -EINVAL;
4025
4026 req->timeout.addr = READ_ONCE(sqe->addr);
4027 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4028 if (req->timeout.flags)
4029 return -EINVAL;
4030
Jens Axboeb29472e2019-12-17 18:50:29 -07004031 return 0;
4032}
4033
Jens Axboe11365042019-10-16 09:08:32 -06004034/*
4035 * Remove or update an existing timeout command
4036 */
Jens Axboefc4df992019-12-10 14:38:45 -07004037static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004038{
4039 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004040 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004041
Jens Axboe11365042019-10-16 09:08:32 -06004042 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004043 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004044
Jens Axboe47f46762019-11-09 17:43:02 -07004045 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004046 io_commit_cqring(ctx);
4047 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004048 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004049 if (ret < 0)
4050 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004051 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004052 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004053}
4054
Jens Axboe3529d8c2019-12-19 18:24:38 -07004055static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004056 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004057{
Jens Axboead8a48a2019-11-15 08:49:11 -07004058 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004059 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004060
Jens Axboead8a48a2019-11-15 08:49:11 -07004061 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004062 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004063 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004064 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004065 if (sqe->off && is_timeout_link)
4066 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004067 flags = READ_ONCE(sqe->timeout_flags);
4068 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004069 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004070
Jens Axboe26a61672019-12-20 09:02:01 -07004071 req->timeout.count = READ_ONCE(sqe->off);
4072
Jens Axboe3529d8c2019-12-19 18:24:38 -07004073 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004074 return -ENOMEM;
4075
4076 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004077 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004078 req->flags |= REQ_F_TIMEOUT;
4079
4080 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004081 return -EFAULT;
4082
Jens Axboe11365042019-10-16 09:08:32 -06004083 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004084 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004085 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004086 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004087
Jens Axboead8a48a2019-11-15 08:49:11 -07004088 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4089 return 0;
4090}
4091
Jens Axboefc4df992019-12-10 14:38:45 -07004092static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004093{
4094 unsigned count;
4095 struct io_ring_ctx *ctx = req->ctx;
4096 struct io_timeout_data *data;
4097 struct list_head *entry;
4098 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004099
Jens Axboe2d283902019-12-04 11:08:05 -07004100 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004101
Jens Axboe5262f562019-09-17 12:26:57 -06004102 /*
4103 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004104 * timeout event to be satisfied. If it isn't set, then this is
4105 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004106 */
Jens Axboe26a61672019-12-20 09:02:01 -07004107 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004108 if (!count) {
4109 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4110 spin_lock_irq(&ctx->completion_lock);
4111 entry = ctx->timeout_list.prev;
4112 goto add;
4113 }
Jens Axboe5262f562019-09-17 12:26:57 -06004114
4115 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004116 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004117
4118 /*
4119 * Insertion sort, ensuring the first entry in the list is always
4120 * the one we need first.
4121 */
Jens Axboe5262f562019-09-17 12:26:57 -06004122 spin_lock_irq(&ctx->completion_lock);
4123 list_for_each_prev(entry, &ctx->timeout_list) {
4124 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004125 unsigned nxt_sq_head;
4126 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004127 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004128
Jens Axboe93bd25b2019-11-11 23:34:31 -07004129 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4130 continue;
4131
yangerkun5da0fb12019-10-15 21:59:29 +08004132 /*
4133 * Since cached_sq_head + count - 1 can overflow, use type long
4134 * long to store it.
4135 */
4136 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004137 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4138 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004139
4140 /*
4141 * cached_sq_head may overflow, and it will never overflow twice
4142 * once there is some timeout req still be valid.
4143 */
4144 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004145 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004146
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004147 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004148 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004149
4150 /*
4151 * Sequence of reqs after the insert one and itself should
4152 * be adjusted because each timeout req consumes a slot.
4153 */
4154 span++;
4155 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004156 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004157 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004158add:
Jens Axboe5262f562019-09-17 12:26:57 -06004159 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004160 data->timer.function = io_timeout_fn;
4161 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004162 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004163 return 0;
4164}
4165
Jens Axboe62755e32019-10-28 21:49:21 -06004166static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004167{
Jens Axboe62755e32019-10-28 21:49:21 -06004168 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004169
Jens Axboe62755e32019-10-28 21:49:21 -06004170 return req->user_data == (unsigned long) data;
4171}
4172
Jens Axboee977d6d2019-11-05 12:39:45 -07004173static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004174{
Jens Axboe62755e32019-10-28 21:49:21 -06004175 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004176 int ret = 0;
4177
Jens Axboe62755e32019-10-28 21:49:21 -06004178 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4179 switch (cancel_ret) {
4180 case IO_WQ_CANCEL_OK:
4181 ret = 0;
4182 break;
4183 case IO_WQ_CANCEL_RUNNING:
4184 ret = -EALREADY;
4185 break;
4186 case IO_WQ_CANCEL_NOTFOUND:
4187 ret = -ENOENT;
4188 break;
4189 }
4190
Jens Axboee977d6d2019-11-05 12:39:45 -07004191 return ret;
4192}
4193
Jens Axboe47f46762019-11-09 17:43:02 -07004194static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4195 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004196 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004197{
4198 unsigned long flags;
4199 int ret;
4200
4201 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4202 if (ret != -ENOENT) {
4203 spin_lock_irqsave(&ctx->completion_lock, flags);
4204 goto done;
4205 }
4206
4207 spin_lock_irqsave(&ctx->completion_lock, flags);
4208 ret = io_timeout_cancel(ctx, sqe_addr);
4209 if (ret != -ENOENT)
4210 goto done;
4211 ret = io_poll_cancel(ctx, sqe_addr);
4212done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004213 if (!ret)
4214 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004215 io_cqring_fill_event(req, ret);
4216 io_commit_cqring(ctx);
4217 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4218 io_cqring_ev_posted(ctx);
4219
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004220 if (ret < 0)
4221 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004222 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004223}
4224
Jens Axboe3529d8c2019-12-19 18:24:38 -07004225static int io_async_cancel_prep(struct io_kiocb *req,
4226 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004227{
Jens Axboefbf23842019-12-17 18:45:56 -07004228 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004229 return -EINVAL;
4230 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4231 sqe->cancel_flags)
4232 return -EINVAL;
4233
Jens Axboefbf23842019-12-17 18:45:56 -07004234 req->cancel.addr = READ_ONCE(sqe->addr);
4235 return 0;
4236}
4237
Pavel Begunkov014db002020-03-03 21:33:12 +03004238static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004239{
4240 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004241
Pavel Begunkov014db002020-03-03 21:33:12 +03004242 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004243 return 0;
4244}
4245
Jens Axboe05f3fb32019-12-09 11:22:50 -07004246static int io_files_update_prep(struct io_kiocb *req,
4247 const struct io_uring_sqe *sqe)
4248{
4249 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4250 return -EINVAL;
4251
4252 req->files_update.offset = READ_ONCE(sqe->off);
4253 req->files_update.nr_args = READ_ONCE(sqe->len);
4254 if (!req->files_update.nr_args)
4255 return -EINVAL;
4256 req->files_update.arg = READ_ONCE(sqe->addr);
4257 return 0;
4258}
4259
4260static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4261{
4262 struct io_ring_ctx *ctx = req->ctx;
4263 struct io_uring_files_update up;
4264 int ret;
4265
Jens Axboef86cd202020-01-29 13:46:44 -07004266 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004267 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004268
4269 up.offset = req->files_update.offset;
4270 up.fds = req->files_update.arg;
4271
4272 mutex_lock(&ctx->uring_lock);
4273 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4274 mutex_unlock(&ctx->uring_lock);
4275
4276 if (ret < 0)
4277 req_set_fail_links(req);
4278 io_cqring_add_event(req, ret);
4279 io_put_req(req);
4280 return 0;
4281}
4282
Jens Axboe3529d8c2019-12-19 18:24:38 -07004283static int io_req_defer_prep(struct io_kiocb *req,
4284 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004285{
Jens Axboee7815732019-12-17 19:45:06 -07004286 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004287
Jens Axboef86cd202020-01-29 13:46:44 -07004288 if (io_op_defs[req->opcode].file_table) {
4289 ret = io_grab_files(req);
4290 if (unlikely(ret))
4291 return ret;
4292 }
4293
Jens Axboecccf0ee2020-01-27 16:34:48 -07004294 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4295
Jens Axboed625c6e2019-12-17 19:53:05 -07004296 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004297 case IORING_OP_NOP:
4298 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004299 case IORING_OP_READV:
4300 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004301 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004302 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004303 break;
4304 case IORING_OP_WRITEV:
4305 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004306 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004307 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004308 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004309 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004310 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004311 break;
4312 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004313 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004314 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004315 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004316 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004317 break;
4318 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004319 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004320 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004321 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004322 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004323 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004324 break;
4325 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004326 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004327 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004328 break;
Jens Axboef499a022019-12-02 16:28:46 -07004329 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004330 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004331 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004332 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004333 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004334 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004335 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004337 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004338 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004339 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004340 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004341 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004342 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004343 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004344 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004345 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004346 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004347 case IORING_OP_FALLOCATE:
4348 ret = io_fallocate_prep(req, sqe);
4349 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004350 case IORING_OP_OPENAT:
4351 ret = io_openat_prep(req, sqe);
4352 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004353 case IORING_OP_CLOSE:
4354 ret = io_close_prep(req, sqe);
4355 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004356 case IORING_OP_FILES_UPDATE:
4357 ret = io_files_update_prep(req, sqe);
4358 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004359 case IORING_OP_STATX:
4360 ret = io_statx_prep(req, sqe);
4361 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004362 case IORING_OP_FADVISE:
4363 ret = io_fadvise_prep(req, sqe);
4364 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004365 case IORING_OP_MADVISE:
4366 ret = io_madvise_prep(req, sqe);
4367 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004368 case IORING_OP_OPENAT2:
4369 ret = io_openat2_prep(req, sqe);
4370 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004371 case IORING_OP_EPOLL_CTL:
4372 ret = io_epoll_ctl_prep(req, sqe);
4373 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004374 case IORING_OP_SPLICE:
4375 ret = io_splice_prep(req, sqe);
4376 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004377 default:
Jens Axboee7815732019-12-17 19:45:06 -07004378 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4379 req->opcode);
4380 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004381 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004382 }
4383
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004384 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004385}
4386
Jens Axboe3529d8c2019-12-19 18:24:38 -07004387static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004388{
Jackie Liua197f662019-11-08 08:09:12 -07004389 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004390 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004391
Bob Liu9d858b22019-11-13 18:06:25 +08004392 /* Still need defer if there is pending req in defer list. */
4393 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004394 return 0;
4395
Jens Axboe3529d8c2019-12-19 18:24:38 -07004396 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004397 return -EAGAIN;
4398
Jens Axboe3529d8c2019-12-19 18:24:38 -07004399 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004400 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004401 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004402
Jens Axboede0617e2019-04-06 21:51:27 -06004403 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004404 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004405 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004406 return 0;
4407 }
4408
Jens Axboe915967f2019-11-21 09:01:20 -07004409 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004410 list_add_tail(&req->list, &ctx->defer_list);
4411 spin_unlock_irq(&ctx->completion_lock);
4412 return -EIOCBQUEUED;
4413}
4414
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004415static void io_cleanup_req(struct io_kiocb *req)
4416{
4417 struct io_async_ctx *io = req->io;
4418
4419 switch (req->opcode) {
4420 case IORING_OP_READV:
4421 case IORING_OP_READ_FIXED:
4422 case IORING_OP_READ:
4423 case IORING_OP_WRITEV:
4424 case IORING_OP_WRITE_FIXED:
4425 case IORING_OP_WRITE:
4426 if (io->rw.iov != io->rw.fast_iov)
4427 kfree(io->rw.iov);
4428 break;
4429 case IORING_OP_SENDMSG:
4430 case IORING_OP_RECVMSG:
4431 if (io->msg.iov != io->msg.fast_iov)
4432 kfree(io->msg.iov);
4433 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004434 case IORING_OP_OPENAT:
4435 case IORING_OP_OPENAT2:
4436 case IORING_OP_STATX:
4437 putname(req->open.filename);
4438 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004439 case IORING_OP_SPLICE:
4440 io_put_file(req, req->splice.file_in,
4441 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4442 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004443 }
4444
4445 req->flags &= ~REQ_F_NEED_CLEANUP;
4446}
4447
Jens Axboe3529d8c2019-12-19 18:24:38 -07004448static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03004449 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004450{
Jackie Liua197f662019-11-08 08:09:12 -07004451 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004452 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004453
Jens Axboed625c6e2019-12-17 19:53:05 -07004454 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004455 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004456 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004457 break;
4458 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004459 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004460 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004461 if (sqe) {
4462 ret = io_read_prep(req, sqe, force_nonblock);
4463 if (ret < 0)
4464 break;
4465 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004466 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004467 break;
4468 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004469 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004470 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004471 if (sqe) {
4472 ret = io_write_prep(req, sqe, force_nonblock);
4473 if (ret < 0)
4474 break;
4475 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004476 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004477 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004478 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004479 if (sqe) {
4480 ret = io_prep_fsync(req, sqe);
4481 if (ret < 0)
4482 break;
4483 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004484 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004485 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004486 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004487 if (sqe) {
4488 ret = io_poll_add_prep(req, sqe);
4489 if (ret)
4490 break;
4491 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004492 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004493 break;
4494 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004495 if (sqe) {
4496 ret = io_poll_remove_prep(req, sqe);
4497 if (ret < 0)
4498 break;
4499 }
Jens Axboefc4df992019-12-10 14:38:45 -07004500 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004501 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004502 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004503 if (sqe) {
4504 ret = io_prep_sfr(req, sqe);
4505 if (ret < 0)
4506 break;
4507 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004508 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004509 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004510 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004511 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004512 if (sqe) {
4513 ret = io_sendmsg_prep(req, sqe);
4514 if (ret < 0)
4515 break;
4516 }
Jens Axboefddafac2020-01-04 20:19:44 -07004517 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004518 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004519 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004520 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004521 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004522 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004523 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004524 if (sqe) {
4525 ret = io_recvmsg_prep(req, sqe);
4526 if (ret)
4527 break;
4528 }
Jens Axboefddafac2020-01-04 20:19:44 -07004529 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004530 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004531 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004532 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004533 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004534 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004535 if (sqe) {
4536 ret = io_timeout_prep(req, sqe, false);
4537 if (ret)
4538 break;
4539 }
Jens Axboefc4df992019-12-10 14:38:45 -07004540 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004541 break;
Jens Axboe11365042019-10-16 09:08:32 -06004542 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004543 if (sqe) {
4544 ret = io_timeout_remove_prep(req, sqe);
4545 if (ret)
4546 break;
4547 }
Jens Axboefc4df992019-12-10 14:38:45 -07004548 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004549 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004550 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004551 if (sqe) {
4552 ret = io_accept_prep(req, sqe);
4553 if (ret)
4554 break;
4555 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004556 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004557 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004558 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004559 if (sqe) {
4560 ret = io_connect_prep(req, sqe);
4561 if (ret)
4562 break;
4563 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004564 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004565 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004566 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004567 if (sqe) {
4568 ret = io_async_cancel_prep(req, sqe);
4569 if (ret)
4570 break;
4571 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004572 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06004573 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004574 case IORING_OP_FALLOCATE:
4575 if (sqe) {
4576 ret = io_fallocate_prep(req, sqe);
4577 if (ret)
4578 break;
4579 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004580 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07004581 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004582 case IORING_OP_OPENAT:
4583 if (sqe) {
4584 ret = io_openat_prep(req, sqe);
4585 if (ret)
4586 break;
4587 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004588 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004589 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004590 case IORING_OP_CLOSE:
4591 if (sqe) {
4592 ret = io_close_prep(req, sqe);
4593 if (ret)
4594 break;
4595 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004596 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07004597 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004598 case IORING_OP_FILES_UPDATE:
4599 if (sqe) {
4600 ret = io_files_update_prep(req, sqe);
4601 if (ret)
4602 break;
4603 }
4604 ret = io_files_update(req, force_nonblock);
4605 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004606 case IORING_OP_STATX:
4607 if (sqe) {
4608 ret = io_statx_prep(req, sqe);
4609 if (ret)
4610 break;
4611 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004612 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004613 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004614 case IORING_OP_FADVISE:
4615 if (sqe) {
4616 ret = io_fadvise_prep(req, sqe);
4617 if (ret)
4618 break;
4619 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004620 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07004621 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004622 case IORING_OP_MADVISE:
4623 if (sqe) {
4624 ret = io_madvise_prep(req, sqe);
4625 if (ret)
4626 break;
4627 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004628 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07004629 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004630 case IORING_OP_OPENAT2:
4631 if (sqe) {
4632 ret = io_openat2_prep(req, sqe);
4633 if (ret)
4634 break;
4635 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004636 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07004637 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004638 case IORING_OP_EPOLL_CTL:
4639 if (sqe) {
4640 ret = io_epoll_ctl_prep(req, sqe);
4641 if (ret)
4642 break;
4643 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004644 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004645 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004646 case IORING_OP_SPLICE:
4647 if (sqe) {
4648 ret = io_splice_prep(req, sqe);
4649 if (ret < 0)
4650 break;
4651 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004652 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004653 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004654 default:
4655 ret = -EINVAL;
4656 break;
4657 }
4658
Jens Axboedef596e2019-01-09 08:59:42 -07004659 if (ret)
4660 return ret;
4661
4662 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004663 const bool in_async = io_wq_current_is_worker();
4664
Jens Axboe9e645e112019-05-10 16:07:28 -06004665 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004666 return -EAGAIN;
4667
Jens Axboe11ba8202020-01-15 21:51:17 -07004668 /* workqueue context doesn't hold uring_lock, grab it now */
4669 if (in_async)
4670 mutex_lock(&ctx->uring_lock);
4671
Jens Axboedef596e2019-01-09 08:59:42 -07004672 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004673
4674 if (in_async)
4675 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004676 }
4677
4678 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004679}
4680
Jens Axboe561fb042019-10-24 07:25:42 -06004681static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004682{
Jens Axboe561fb042019-10-24 07:25:42 -06004683 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004684 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004685 struct io_kiocb *nxt = NULL;
4686 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004687
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004688 /* if NO_CANCEL is set, we must still run the work */
4689 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4690 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004691 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004692 }
Jens Axboe31b51512019-01-18 22:56:34 -07004693
Jens Axboe561fb042019-10-24 07:25:42 -06004694 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004695 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03004696 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004697 /*
4698 * We can get EAGAIN for polled IO even though we're
4699 * forcing a sync submission from here, since we can't
4700 * wait for request slots on the block side.
4701 */
4702 if (ret != -EAGAIN)
4703 break;
4704 cond_resched();
4705 } while (1);
4706 }
Jens Axboe31b51512019-01-18 22:56:34 -07004707
Jens Axboe561fb042019-10-24 07:25:42 -06004708 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004709 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004710 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004711 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004712 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004713
Pavel Begunkov594506f2020-03-03 21:33:11 +03004714 io_put_req(req); /* drop submission reference */
4715 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07004716 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004717}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004718
Jens Axboe15b71ab2019-12-11 11:20:36 -07004719static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004720{
Jens Axboed3656342019-12-18 09:50:26 -07004721 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004722 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004723 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004724 return 0;
4725 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004726}
4727
Jens Axboe65e19f52019-10-26 07:20:21 -06004728static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4729 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004730{
Jens Axboe65e19f52019-10-26 07:20:21 -06004731 struct fixed_file_table *table;
4732
Jens Axboe05f3fb32019-12-09 11:22:50 -07004733 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4734 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004735}
4736
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004737static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
4738 int fd, struct file **out_file, bool fixed)
4739{
4740 struct io_ring_ctx *ctx = req->ctx;
4741 struct file *file;
4742
4743 if (fixed) {
4744 if (unlikely(!ctx->file_data ||
4745 (unsigned) fd >= ctx->nr_user_files))
4746 return -EBADF;
4747 fd = array_index_nospec(fd, ctx->nr_user_files);
4748 file = io_file_from_index(ctx, fd);
4749 if (!file)
4750 return -EBADF;
4751 percpu_ref_get(&ctx->file_data->refs);
4752 } else {
4753 trace_io_uring_file_get(ctx, fd);
4754 file = __io_file_get(state, fd);
4755 if (unlikely(!file))
4756 return -EBADF;
4757 }
4758
4759 *out_file = file;
4760 return 0;
4761}
4762
Jens Axboe3529d8c2019-12-19 18:24:38 -07004763static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4764 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004765{
4766 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004767 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004768 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06004769
Jens Axboe3529d8c2019-12-19 18:24:38 -07004770 flags = READ_ONCE(sqe->flags);
4771 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004772
Jens Axboed3656342019-12-18 09:50:26 -07004773 if (!io_req_needs_file(req, fd))
4774 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004775
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004776 fixed = (flags & IOSQE_FIXED_FILE);
4777 if (unlikely(!fixed && req->needs_fixed_file))
4778 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004779
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004780 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06004781}
4782
Jackie Liua197f662019-11-08 08:09:12 -07004783static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004784{
Jens Axboefcb323c2019-10-24 12:39:47 -06004785 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004786 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004787
Jens Axboef86cd202020-01-29 13:46:44 -07004788 if (req->work.files)
4789 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004790 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004791 return -EBADF;
4792
Jens Axboefcb323c2019-10-24 12:39:47 -06004793 rcu_read_lock();
4794 spin_lock_irq(&ctx->inflight_lock);
4795 /*
4796 * We use the f_ops->flush() handler to ensure that we can flush
4797 * out work accessing these files if the fd is closed. Check if
4798 * the fd has changed since we started down this path, and disallow
4799 * this operation if it has.
4800 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004801 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004802 list_add(&req->inflight_entry, &ctx->inflight_list);
4803 req->flags |= REQ_F_INFLIGHT;
4804 req->work.files = current->files;
4805 ret = 0;
4806 }
4807 spin_unlock_irq(&ctx->inflight_lock);
4808 rcu_read_unlock();
4809
4810 return ret;
4811}
4812
Jens Axboe2665abf2019-11-05 12:40:47 -07004813static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4814{
Jens Axboead8a48a2019-11-15 08:49:11 -07004815 struct io_timeout_data *data = container_of(timer,
4816 struct io_timeout_data, timer);
4817 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004818 struct io_ring_ctx *ctx = req->ctx;
4819 struct io_kiocb *prev = NULL;
4820 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004821
4822 spin_lock_irqsave(&ctx->completion_lock, flags);
4823
4824 /*
4825 * We don't expect the list to be empty, that will only happen if we
4826 * race with the completion of the linked work.
4827 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004828 if (!list_empty(&req->link_list)) {
4829 prev = list_entry(req->link_list.prev, struct io_kiocb,
4830 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004831 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004832 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004833 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4834 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004835 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004836 }
4837
4838 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4839
4840 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004841 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03004842 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004843 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004844 } else {
4845 io_cqring_add_event(req, -ETIME);
4846 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004847 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004848 return HRTIMER_NORESTART;
4849}
4850
Jens Axboead8a48a2019-11-15 08:49:11 -07004851static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004852{
Jens Axboe76a46e02019-11-10 23:34:16 -07004853 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004854
Jens Axboe76a46e02019-11-10 23:34:16 -07004855 /*
4856 * If the list is now empty, then our linked request finished before
4857 * we got a chance to setup the timer
4858 */
4859 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004860 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004861 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004862
Jens Axboead8a48a2019-11-15 08:49:11 -07004863 data->timer.function = io_link_timeout_fn;
4864 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4865 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004866 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004867 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004868
Jens Axboe2665abf2019-11-05 12:40:47 -07004869 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004870 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004871}
4872
Jens Axboead8a48a2019-11-15 08:49:11 -07004873static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004874{
4875 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004876
Jens Axboe2665abf2019-11-05 12:40:47 -07004877 if (!(req->flags & REQ_F_LINK))
4878 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004879 /* for polled retry, if flag is set, we already went through here */
4880 if (req->flags & REQ_F_POLLED)
4881 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004882
Pavel Begunkov44932332019-12-05 16:16:35 +03004883 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4884 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004885 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004886 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004887
Jens Axboe76a46e02019-11-10 23:34:16 -07004888 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004889 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004890}
4891
Jens Axboe3529d8c2019-12-19 18:24:38 -07004892static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004893{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004894 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004895 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07004896 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004897 int ret;
4898
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004899again:
4900 linked_timeout = io_prep_linked_timeout(req);
4901
Jens Axboe193155c2020-02-22 23:22:19 -07004902 if (req->work.creds && req->work.creds != current_cred()) {
4903 if (old_creds)
4904 revert_creds(old_creds);
4905 if (old_creds == req->work.creds)
4906 old_creds = NULL; /* restored original creds */
4907 else
4908 old_creds = override_creds(req->work.creds);
4909 }
4910
Pavel Begunkov014db002020-03-03 21:33:12 +03004911 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004912
4913 /*
4914 * We async punt it if the file wasn't marked NOWAIT, or if the file
4915 * doesn't support non-blocking read/write attempts
4916 */
4917 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4918 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07004919 if (io_arm_poll_handler(req)) {
4920 if (linked_timeout)
4921 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004922 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07004923 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004924punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004925 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004926 ret = io_grab_files(req);
4927 if (ret)
4928 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004929 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004930
4931 /*
4932 * Queued up for async execution, worker will release
4933 * submit reference when the iocb is actually submitted.
4934 */
4935 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004936 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004937 }
Jens Axboee65ef562019-03-12 10:16:44 -06004938
Jens Axboefcb323c2019-10-24 12:39:47 -06004939err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004940 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06004941 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004942 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004943
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004944 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004945 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004946 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004947 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004948 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004949 }
4950
Jens Axboee65ef562019-03-12 10:16:44 -06004951 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004952 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004953 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004954 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004955 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004956 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004957 if (nxt) {
4958 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004959
4960 if (req->flags & REQ_F_FORCE_ASYNC)
4961 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004962 goto again;
4963 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004964exit:
Jens Axboe193155c2020-02-22 23:22:19 -07004965 if (old_creds)
4966 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004967}
4968
Jens Axboe3529d8c2019-12-19 18:24:38 -07004969static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004970{
4971 int ret;
4972
Jens Axboe3529d8c2019-12-19 18:24:38 -07004973 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004974 if (ret) {
4975 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004976fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004977 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004978 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004979 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004980 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004981 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004982 ret = io_req_defer_prep(req, sqe);
4983 if (unlikely(ret < 0))
4984 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004985 /*
4986 * Never try inline submit of IOSQE_ASYNC is set, go straight
4987 * to async execution.
4988 */
4989 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4990 io_queue_async_work(req);
4991 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004992 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004993 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004994}
4995
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004996static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004997{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004998 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004999 io_cqring_add_event(req, -ECANCELED);
5000 io_double_put_req(req);
5001 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005002 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005003}
5004
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005005#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07005006 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06005007
Jens Axboe3529d8c2019-12-19 18:24:38 -07005008static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5009 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005010{
Jackie Liua197f662019-11-08 08:09:12 -07005011 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005012 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005013 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005014
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005015 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005016
5017 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005018 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005019 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005020 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005021 }
5022
Jens Axboe75c6a032020-01-28 10:15:23 -07005023 id = READ_ONCE(sqe->personality);
5024 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005025 req->work.creds = idr_find(&ctx->personality_idr, id);
5026 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005027 ret = -EINVAL;
5028 goto err_req;
5029 }
Jens Axboe193155c2020-02-22 23:22:19 -07005030 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005031 }
5032
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005033 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005034 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5035 IOSQE_ASYNC | IOSQE_FIXED_FILE);
Jens Axboe9e645e112019-05-10 16:07:28 -06005036
Jens Axboe3529d8c2019-12-19 18:24:38 -07005037 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005038 if (unlikely(ret)) {
5039err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005040 io_cqring_add_event(req, ret);
5041 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005042 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005043 }
5044
Jens Axboe9e645e112019-05-10 16:07:28 -06005045 /*
5046 * If we already have a head request, queue this one for async
5047 * submittal once the head completes. If we don't have a head but
5048 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5049 * submitted sync once the chain is complete. If none of those
5050 * conditions are true (normal request), then just queue it.
5051 */
5052 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005053 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005054
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005055 /*
5056 * Taking sequential execution of a link, draining both sides
5057 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5058 * requests in the link. So, it drains the head and the
5059 * next after the link request. The last one is done via
5060 * drain_next flag to persist the effect across calls.
5061 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005062 if (sqe_flags & IOSQE_IO_DRAIN) {
5063 head->flags |= REQ_F_IO_DRAIN;
5064 ctx->drain_next = 1;
5065 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005066 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005067 ret = -EAGAIN;
5068 goto err_req;
5069 }
5070
Jens Axboe3529d8c2019-12-19 18:24:38 -07005071 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005072 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005073 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005074 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005075 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005076 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005077 trace_io_uring_link(ctx, req, head);
5078 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005079
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005080 /* last request of a link, enqueue the link */
5081 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5082 io_queue_link_head(head);
5083 *link = NULL;
5084 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005085 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005086 if (unlikely(ctx->drain_next)) {
5087 req->flags |= REQ_F_IO_DRAIN;
5088 req->ctx->drain_next = 0;
5089 }
5090 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5091 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005092 INIT_LIST_HEAD(&req->link_list);
5093 ret = io_req_defer_prep(req, sqe);
5094 if (ret)
5095 req->flags |= REQ_F_FAIL_LINK;
5096 *link = req;
5097 } else {
5098 io_queue_sqe(req, sqe);
5099 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005100 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005101
5102 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005103}
5104
Jens Axboe9a56a232019-01-09 09:06:50 -07005105/*
5106 * Batched submission is done, ensure local IO is flushed out.
5107 */
5108static void io_submit_state_end(struct io_submit_state *state)
5109{
5110 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005111 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005112 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005113 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005114}
5115
5116/*
5117 * Start submission side cache.
5118 */
5119static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005120 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005121{
5122 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005123 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005124 state->file = NULL;
5125 state->ios_left = max_ios;
5126}
5127
Jens Axboe2b188cc2019-01-07 10:46:33 -07005128static void io_commit_sqring(struct io_ring_ctx *ctx)
5129{
Hristo Venev75b28af2019-08-26 17:23:46 +00005130 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005131
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005132 /*
5133 * Ensure any loads from the SQEs are done at this point,
5134 * since once we write the new head, the application could
5135 * write new data to them.
5136 */
5137 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005138}
5139
5140/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005141 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005142 * that is mapped by userspace. This means that care needs to be taken to
5143 * ensure that reads are stable, as we cannot rely on userspace always
5144 * being a good citizen. If members of the sqe are validated and then later
5145 * used, it's important that those reads are done through READ_ONCE() to
5146 * prevent a re-load down the line.
5147 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005148static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5149 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005150{
Hristo Venev75b28af2019-08-26 17:23:46 +00005151 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005152 unsigned head;
5153
5154 /*
5155 * The cached sq head (or cq tail) serves two purposes:
5156 *
5157 * 1) allows us to batch the cost of updating the user visible
5158 * head updates.
5159 * 2) allows the kernel side to track the head on its own, even
5160 * though the application is the one updating it.
5161 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005162 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005163 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005164 /*
5165 * All io need record the previous position, if LINK vs DARIN,
5166 * it can be used to mark the position of the first IO in the
5167 * link list.
5168 */
5169 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005170 *sqe_ptr = &ctx->sq_sqes[head];
5171 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5172 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005173 ctx->cached_sq_head++;
5174 return true;
5175 }
5176
5177 /* drop invalid entries */
5178 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005179 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005180 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005181 return false;
5182}
5183
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005184static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005185 struct file *ring_file, int ring_fd,
5186 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005187{
5188 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005189 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005190 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005191 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005192
Jens Axboec4a2ed72019-11-21 21:01:26 -07005193 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005194 if (test_bit(0, &ctx->sq_check_overflow)) {
5195 if (!list_empty(&ctx->cq_overflow_list) &&
5196 !io_cqring_overflow_flush(ctx, false))
5197 return -EBUSY;
5198 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005199
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005200 /* make sure SQ entry isn't read before tail */
5201 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005202
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005203 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5204 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005205
5206 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005207 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005208 statep = &state;
5209 }
5210
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005211 ctx->ring_fd = ring_fd;
5212 ctx->ring_file = ring_file;
5213
Jens Axboe6c271ce2019-01-10 11:22:30 -07005214 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005215 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005216 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005217 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005218
Pavel Begunkov196be952019-11-07 01:41:06 +03005219 req = io_get_req(ctx, statep);
5220 if (unlikely(!req)) {
5221 if (!submitted)
5222 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005223 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005224 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005225 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005226 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005227 break;
5228 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005229
Jens Axboed3656342019-12-18 09:50:26 -07005230 /* will complete beyond this point, count as submitted */
5231 submitted++;
5232
5233 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005234 err = -EINVAL;
5235fail_req:
5236 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005237 io_double_put_req(req);
5238 break;
5239 }
5240
5241 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005242 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005243 if (unlikely(mm_fault)) {
5244 err = -EFAULT;
5245 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005246 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005247 use_mm(ctx->sqo_mm);
5248 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005249 }
5250
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005251 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005252 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5253 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005254 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005255 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005256 }
5257
Pavel Begunkov9466f432020-01-25 22:34:01 +03005258 if (unlikely(submitted != nr)) {
5259 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5260
5261 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5262 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005263 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005264 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005265 if (statep)
5266 io_submit_state_end(&state);
5267
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005268 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5269 io_commit_sqring(ctx);
5270
Jens Axboe6c271ce2019-01-10 11:22:30 -07005271 return submitted;
5272}
5273
5274static int io_sq_thread(void *data)
5275{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005276 struct io_ring_ctx *ctx = data;
5277 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005278 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005279 mm_segment_t old_fs;
5280 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005281 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005282 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005283
Jens Axboe206aefd2019-11-07 18:27:42 -07005284 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005285
Jens Axboe6c271ce2019-01-10 11:22:30 -07005286 old_fs = get_fs();
5287 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005288 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005289
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005290 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005291 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005292 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005293
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005294 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005295 unsigned nr_events = 0;
5296
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005297 mutex_lock(&ctx->uring_lock);
5298 if (!list_empty(&ctx->poll_list))
5299 io_iopoll_getevents(ctx, &nr_events, 0);
5300 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005301 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005302 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005303 }
5304
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005305 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005306
5307 /*
5308 * If submit got -EBUSY, flag us as needing the application
5309 * to enter the kernel to reap and flush events.
5310 */
5311 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005312 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005313 * Drop cur_mm before scheduling, we can't hold it for
5314 * long periods (or over schedule()). Do this before
5315 * adding ourselves to the waitqueue, as the unuse/drop
5316 * may sleep.
5317 */
5318 if (cur_mm) {
5319 unuse_mm(cur_mm);
5320 mmput(cur_mm);
5321 cur_mm = NULL;
5322 }
5323
5324 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005325 * We're polling. If we're within the defined idle
5326 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005327 * to sleep. The exception is if we got EBUSY doing
5328 * more IO, we should wait for the application to
5329 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005330 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005331 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005332 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5333 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005334 if (current->task_works)
5335 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005336 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005337 continue;
5338 }
5339
Jens Axboe6c271ce2019-01-10 11:22:30 -07005340 prepare_to_wait(&ctx->sqo_wait, &wait,
5341 TASK_INTERRUPTIBLE);
5342
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005343 /*
5344 * While doing polled IO, before going to sleep, we need
5345 * to check if there are new reqs added to poll_list, it
5346 * is because reqs may have been punted to io worker and
5347 * will be added to poll_list later, hence check the
5348 * poll_list again.
5349 */
5350 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5351 !list_empty_careful(&ctx->poll_list)) {
5352 finish_wait(&ctx->sqo_wait, &wait);
5353 continue;
5354 }
5355
Jens Axboe6c271ce2019-01-10 11:22:30 -07005356 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005357 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005358 /* make sure to read SQ tail after writing flags */
5359 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005360
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005361 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005362 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005363 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005364 finish_wait(&ctx->sqo_wait, &wait);
5365 break;
5366 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005367 if (current->task_works) {
5368 task_work_run();
5369 continue;
5370 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005371 if (signal_pending(current))
5372 flush_signals(current);
5373 schedule();
5374 finish_wait(&ctx->sqo_wait, &wait);
5375
Hristo Venev75b28af2019-08-26 17:23:46 +00005376 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005377 continue;
5378 }
5379 finish_wait(&ctx->sqo_wait, &wait);
5380
Hristo Venev75b28af2019-08-26 17:23:46 +00005381 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005382 }
5383
Jens Axboe8a4955f2019-12-09 14:52:35 -07005384 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005385 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005386 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005387 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005388 }
5389
Jens Axboeb41e9852020-02-17 09:52:41 -07005390 if (current->task_works)
5391 task_work_run();
5392
Jens Axboe6c271ce2019-01-10 11:22:30 -07005393 set_fs(old_fs);
5394 if (cur_mm) {
5395 unuse_mm(cur_mm);
5396 mmput(cur_mm);
5397 }
Jens Axboe181e4482019-11-25 08:52:30 -07005398 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005399
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005400 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005401
Jens Axboe6c271ce2019-01-10 11:22:30 -07005402 return 0;
5403}
5404
Jens Axboebda52162019-09-24 13:47:15 -06005405struct io_wait_queue {
5406 struct wait_queue_entry wq;
5407 struct io_ring_ctx *ctx;
5408 unsigned to_wait;
5409 unsigned nr_timeouts;
5410};
5411
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005412static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005413{
5414 struct io_ring_ctx *ctx = iowq->ctx;
5415
5416 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005417 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005418 * started waiting. For timeouts, we always want to return to userspace,
5419 * regardless of event count.
5420 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005421 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005422 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5423}
5424
5425static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5426 int wake_flags, void *key)
5427{
5428 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5429 wq);
5430
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005431 /* use noflush == true, as we can't safely rely on locking context */
5432 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005433 return -1;
5434
5435 return autoremove_wake_function(curr, mode, wake_flags, key);
5436}
5437
Jens Axboe2b188cc2019-01-07 10:46:33 -07005438/*
5439 * Wait until events become available, if we don't already have some. The
5440 * application must reap them itself, as they reside on the shared cq ring.
5441 */
5442static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5443 const sigset_t __user *sig, size_t sigsz)
5444{
Jens Axboebda52162019-09-24 13:47:15 -06005445 struct io_wait_queue iowq = {
5446 .wq = {
5447 .private = current,
5448 .func = io_wake_function,
5449 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5450 },
5451 .ctx = ctx,
5452 .to_wait = min_events,
5453 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005454 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005455 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005456
Jens Axboeb41e9852020-02-17 09:52:41 -07005457 do {
5458 if (io_cqring_events(ctx, false) >= min_events)
5459 return 0;
5460 if (!current->task_works)
5461 break;
5462 task_work_run();
5463 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005464
5465 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005466#ifdef CONFIG_COMPAT
5467 if (in_compat_syscall())
5468 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005469 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005470 else
5471#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005472 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005473
Jens Axboe2b188cc2019-01-07 10:46:33 -07005474 if (ret)
5475 return ret;
5476 }
5477
Jens Axboebda52162019-09-24 13:47:15 -06005478 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005479 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005480 do {
5481 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5482 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005483 if (current->task_works)
5484 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005485 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005486 break;
5487 schedule();
5488 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005489 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005490 break;
5491 }
5492 } while (1);
5493 finish_wait(&ctx->wait, &iowq.wq);
5494
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005495 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005496
Hristo Venev75b28af2019-08-26 17:23:46 +00005497 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005498}
5499
Jens Axboe6b063142019-01-10 22:13:58 -07005500static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5501{
5502#if defined(CONFIG_UNIX)
5503 if (ctx->ring_sock) {
5504 struct sock *sock = ctx->ring_sock->sk;
5505 struct sk_buff *skb;
5506
5507 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5508 kfree_skb(skb);
5509 }
5510#else
5511 int i;
5512
Jens Axboe65e19f52019-10-26 07:20:21 -06005513 for (i = 0; i < ctx->nr_user_files; i++) {
5514 struct file *file;
5515
5516 file = io_file_from_index(ctx, i);
5517 if (file)
5518 fput(file);
5519 }
Jens Axboe6b063142019-01-10 22:13:58 -07005520#endif
5521}
5522
Jens Axboe05f3fb32019-12-09 11:22:50 -07005523static void io_file_ref_kill(struct percpu_ref *ref)
5524{
5525 struct fixed_file_data *data;
5526
5527 data = container_of(ref, struct fixed_file_data, refs);
5528 complete(&data->done);
5529}
5530
Jens Axboe6b063142019-01-10 22:13:58 -07005531static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5532{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005533 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005534 unsigned nr_tables, i;
5535
Jens Axboe05f3fb32019-12-09 11:22:50 -07005536 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005537 return -ENXIO;
5538
Jens Axboe05f3fb32019-12-09 11:22:50 -07005539 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005540 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005541 wait_for_completion(&data->done);
5542 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005543 percpu_ref_exit(&data->refs);
5544
Jens Axboe6b063142019-01-10 22:13:58 -07005545 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005546 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5547 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005548 kfree(data->table[i].files);
5549 kfree(data->table);
5550 kfree(data);
5551 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005552 ctx->nr_user_files = 0;
5553 return 0;
5554}
5555
Jens Axboe6c271ce2019-01-10 11:22:30 -07005556static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5557{
5558 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005559 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005560 /*
5561 * The park is a bit of a work-around, without it we get
5562 * warning spews on shutdown with SQPOLL set and affinity
5563 * set to a single CPU.
5564 */
Jens Axboe06058632019-04-13 09:26:03 -06005565 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005566 kthread_stop(ctx->sqo_thread);
5567 ctx->sqo_thread = NULL;
5568 }
5569}
5570
Jens Axboe6b063142019-01-10 22:13:58 -07005571static void io_finish_async(struct io_ring_ctx *ctx)
5572{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005573 io_sq_thread_stop(ctx);
5574
Jens Axboe561fb042019-10-24 07:25:42 -06005575 if (ctx->io_wq) {
5576 io_wq_destroy(ctx->io_wq);
5577 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005578 }
5579}
5580
5581#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005582/*
5583 * Ensure the UNIX gc is aware of our file set, so we are certain that
5584 * the io_uring can be safely unregistered on process exit, even if we have
5585 * loops in the file referencing.
5586 */
5587static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5588{
5589 struct sock *sk = ctx->ring_sock->sk;
5590 struct scm_fp_list *fpl;
5591 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005592 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005593
5594 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5595 unsigned long inflight = ctx->user->unix_inflight + nr;
5596
5597 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5598 return -EMFILE;
5599 }
5600
5601 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5602 if (!fpl)
5603 return -ENOMEM;
5604
5605 skb = alloc_skb(0, GFP_KERNEL);
5606 if (!skb) {
5607 kfree(fpl);
5608 return -ENOMEM;
5609 }
5610
5611 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005612
Jens Axboe08a45172019-10-03 08:11:03 -06005613 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005614 fpl->user = get_uid(ctx->user);
5615 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005616 struct file *file = io_file_from_index(ctx, i + offset);
5617
5618 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005619 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005620 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005621 unix_inflight(fpl->user, fpl->fp[nr_files]);
5622 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005623 }
5624
Jens Axboe08a45172019-10-03 08:11:03 -06005625 if (nr_files) {
5626 fpl->max = SCM_MAX_FD;
5627 fpl->count = nr_files;
5628 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005629 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005630 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5631 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005632
Jens Axboe08a45172019-10-03 08:11:03 -06005633 for (i = 0; i < nr_files; i++)
5634 fput(fpl->fp[i]);
5635 } else {
5636 kfree_skb(skb);
5637 kfree(fpl);
5638 }
Jens Axboe6b063142019-01-10 22:13:58 -07005639
5640 return 0;
5641}
5642
5643/*
5644 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5645 * causes regular reference counting to break down. We rely on the UNIX
5646 * garbage collection to take care of this problem for us.
5647 */
5648static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5649{
5650 unsigned left, total;
5651 int ret = 0;
5652
5653 total = 0;
5654 left = ctx->nr_user_files;
5655 while (left) {
5656 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005657
5658 ret = __io_sqe_files_scm(ctx, this_files, total);
5659 if (ret)
5660 break;
5661 left -= this_files;
5662 total += this_files;
5663 }
5664
5665 if (!ret)
5666 return 0;
5667
5668 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005669 struct file *file = io_file_from_index(ctx, total);
5670
5671 if (file)
5672 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005673 total++;
5674 }
5675
5676 return ret;
5677}
5678#else
5679static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5680{
5681 return 0;
5682}
5683#endif
5684
Jens Axboe65e19f52019-10-26 07:20:21 -06005685static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5686 unsigned nr_files)
5687{
5688 int i;
5689
5690 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005691 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005692 unsigned this_files;
5693
5694 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5695 table->files = kcalloc(this_files, sizeof(struct file *),
5696 GFP_KERNEL);
5697 if (!table->files)
5698 break;
5699 nr_files -= this_files;
5700 }
5701
5702 if (i == nr_tables)
5703 return 0;
5704
5705 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005706 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005707 kfree(table->files);
5708 }
5709 return 1;
5710}
5711
Jens Axboe05f3fb32019-12-09 11:22:50 -07005712static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005713{
5714#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005715 struct sock *sock = ctx->ring_sock->sk;
5716 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5717 struct sk_buff *skb;
5718 int i;
5719
5720 __skb_queue_head_init(&list);
5721
5722 /*
5723 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5724 * remove this entry and rearrange the file array.
5725 */
5726 skb = skb_dequeue(head);
5727 while (skb) {
5728 struct scm_fp_list *fp;
5729
5730 fp = UNIXCB(skb).fp;
5731 for (i = 0; i < fp->count; i++) {
5732 int left;
5733
5734 if (fp->fp[i] != file)
5735 continue;
5736
5737 unix_notinflight(fp->user, fp->fp[i]);
5738 left = fp->count - 1 - i;
5739 if (left) {
5740 memmove(&fp->fp[i], &fp->fp[i + 1],
5741 left * sizeof(struct file *));
5742 }
5743 fp->count--;
5744 if (!fp->count) {
5745 kfree_skb(skb);
5746 skb = NULL;
5747 } else {
5748 __skb_queue_tail(&list, skb);
5749 }
5750 fput(file);
5751 file = NULL;
5752 break;
5753 }
5754
5755 if (!file)
5756 break;
5757
5758 __skb_queue_tail(&list, skb);
5759
5760 skb = skb_dequeue(head);
5761 }
5762
5763 if (skb_peek(&list)) {
5764 spin_lock_irq(&head->lock);
5765 while ((skb = __skb_dequeue(&list)) != NULL)
5766 __skb_queue_tail(head, skb);
5767 spin_unlock_irq(&head->lock);
5768 }
5769#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005770 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005771#endif
5772}
5773
Jens Axboe05f3fb32019-12-09 11:22:50 -07005774struct io_file_put {
5775 struct llist_node llist;
5776 struct file *file;
5777 struct completion *done;
5778};
5779
Jens Axboe2faf8522020-02-04 19:54:55 -07005780static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005781{
5782 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005783 struct llist_node *node;
5784
Jens Axboe05f3fb32019-12-09 11:22:50 -07005785 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5786 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5787 io_ring_file_put(data->ctx, pfile->file);
5788 if (pfile->done)
5789 complete(pfile->done);
5790 else
5791 kfree(pfile);
5792 }
5793 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005794}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005795
Jens Axboe2faf8522020-02-04 19:54:55 -07005796static void io_ring_file_ref_switch(struct work_struct *work)
5797{
5798 struct fixed_file_data *data;
5799
5800 data = container_of(work, struct fixed_file_data, ref_work);
5801 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005802 percpu_ref_switch_to_percpu(&data->refs);
5803}
5804
5805static void io_file_data_ref_zero(struct percpu_ref *ref)
5806{
5807 struct fixed_file_data *data;
5808
5809 data = container_of(ref, struct fixed_file_data, refs);
5810
Jens Axboe2faf8522020-02-04 19:54:55 -07005811 /*
5812 * We can't safely switch from inside this context, punt to wq. If
5813 * the table ref is going away, the table is being unregistered.
5814 * Don't queue up the async work for that case, the caller will
5815 * handle it.
5816 */
5817 if (!percpu_ref_is_dying(&data->refs))
5818 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005819}
5820
5821static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5822 unsigned nr_args)
5823{
5824 __s32 __user *fds = (__s32 __user *) arg;
5825 unsigned nr_tables;
5826 struct file *file;
5827 int fd, ret = 0;
5828 unsigned i;
5829
5830 if (ctx->file_data)
5831 return -EBUSY;
5832 if (!nr_args)
5833 return -EINVAL;
5834 if (nr_args > IORING_MAX_FIXED_FILES)
5835 return -EMFILE;
5836
5837 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5838 if (!ctx->file_data)
5839 return -ENOMEM;
5840 ctx->file_data->ctx = ctx;
5841 init_completion(&ctx->file_data->done);
5842
5843 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5844 ctx->file_data->table = kcalloc(nr_tables,
5845 sizeof(struct fixed_file_table),
5846 GFP_KERNEL);
5847 if (!ctx->file_data->table) {
5848 kfree(ctx->file_data);
5849 ctx->file_data = NULL;
5850 return -ENOMEM;
5851 }
5852
5853 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5854 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5855 kfree(ctx->file_data->table);
5856 kfree(ctx->file_data);
5857 ctx->file_data = NULL;
5858 return -ENOMEM;
5859 }
5860 ctx->file_data->put_llist.first = NULL;
5861 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5862
5863 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5864 percpu_ref_exit(&ctx->file_data->refs);
5865 kfree(ctx->file_data->table);
5866 kfree(ctx->file_data);
5867 ctx->file_data = NULL;
5868 return -ENOMEM;
5869 }
5870
5871 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5872 struct fixed_file_table *table;
5873 unsigned index;
5874
5875 ret = -EFAULT;
5876 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5877 break;
5878 /* allow sparse sets */
5879 if (fd == -1) {
5880 ret = 0;
5881 continue;
5882 }
5883
5884 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5885 index = i & IORING_FILE_TABLE_MASK;
5886 file = fget(fd);
5887
5888 ret = -EBADF;
5889 if (!file)
5890 break;
5891
5892 /*
5893 * Don't allow io_uring instances to be registered. If UNIX
5894 * isn't enabled, then this causes a reference cycle and this
5895 * instance can never get freed. If UNIX is enabled we'll
5896 * handle it just fine, but there's still no point in allowing
5897 * a ring fd as it doesn't support regular read/write anyway.
5898 */
5899 if (file->f_op == &io_uring_fops) {
5900 fput(file);
5901 break;
5902 }
5903 ret = 0;
5904 table->files[index] = file;
5905 }
5906
5907 if (ret) {
5908 for (i = 0; i < ctx->nr_user_files; i++) {
5909 file = io_file_from_index(ctx, i);
5910 if (file)
5911 fput(file);
5912 }
5913 for (i = 0; i < nr_tables; i++)
5914 kfree(ctx->file_data->table[i].files);
5915
5916 kfree(ctx->file_data->table);
5917 kfree(ctx->file_data);
5918 ctx->file_data = NULL;
5919 ctx->nr_user_files = 0;
5920 return ret;
5921 }
5922
5923 ret = io_sqe_files_scm(ctx);
5924 if (ret)
5925 io_sqe_files_unregister(ctx);
5926
5927 return ret;
5928}
5929
Jens Axboec3a31e62019-10-03 13:59:56 -06005930static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5931 int index)
5932{
5933#if defined(CONFIG_UNIX)
5934 struct sock *sock = ctx->ring_sock->sk;
5935 struct sk_buff_head *head = &sock->sk_receive_queue;
5936 struct sk_buff *skb;
5937
5938 /*
5939 * See if we can merge this file into an existing skb SCM_RIGHTS
5940 * file set. If there's no room, fall back to allocating a new skb
5941 * and filling it in.
5942 */
5943 spin_lock_irq(&head->lock);
5944 skb = skb_peek(head);
5945 if (skb) {
5946 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5947
5948 if (fpl->count < SCM_MAX_FD) {
5949 __skb_unlink(skb, head);
5950 spin_unlock_irq(&head->lock);
5951 fpl->fp[fpl->count] = get_file(file);
5952 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5953 fpl->count++;
5954 spin_lock_irq(&head->lock);
5955 __skb_queue_head(head, skb);
5956 } else {
5957 skb = NULL;
5958 }
5959 }
5960 spin_unlock_irq(&head->lock);
5961
5962 if (skb) {
5963 fput(file);
5964 return 0;
5965 }
5966
5967 return __io_sqe_files_scm(ctx, 1, index);
5968#else
5969 return 0;
5970#endif
5971}
5972
Jens Axboe05f3fb32019-12-09 11:22:50 -07005973static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005974{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005975 struct fixed_file_data *data;
5976
Jens Axboedd3db2a2020-02-26 10:23:43 -07005977 /*
5978 * Juggle reference to ensure we hit zero, if needed, so we can
5979 * switch back to percpu mode
5980 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005981 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005982 percpu_ref_put(&data->refs);
5983 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005984}
5985
5986static bool io_queue_file_removal(struct fixed_file_data *data,
5987 struct file *file)
5988{
5989 struct io_file_put *pfile, pfile_stack;
5990 DECLARE_COMPLETION_ONSTACK(done);
5991
5992 /*
5993 * If we fail allocating the struct we need for doing async reomval
5994 * of this file, just punt to sync and wait for it.
5995 */
5996 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5997 if (!pfile) {
5998 pfile = &pfile_stack;
5999 pfile->done = &done;
6000 }
6001
6002 pfile->file = file;
6003 llist_add(&pfile->llist, &data->put_llist);
6004
6005 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006006 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006007 wait_for_completion(&done);
6008 flush_work(&data->ref_work);
6009 return false;
6010 }
6011
6012 return true;
6013}
6014
6015static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6016 struct io_uring_files_update *up,
6017 unsigned nr_args)
6018{
6019 struct fixed_file_data *data = ctx->file_data;
6020 bool ref_switch = false;
6021 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006022 __s32 __user *fds;
6023 int fd, i, err;
6024 __u32 done;
6025
Jens Axboe05f3fb32019-12-09 11:22:50 -07006026 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006027 return -EOVERFLOW;
6028 if (done > ctx->nr_user_files)
6029 return -EINVAL;
6030
6031 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006032 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006033 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006034 struct fixed_file_table *table;
6035 unsigned index;
6036
Jens Axboec3a31e62019-10-03 13:59:56 -06006037 err = 0;
6038 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6039 err = -EFAULT;
6040 break;
6041 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006042 i = array_index_nospec(up->offset, ctx->nr_user_files);
6043 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006044 index = i & IORING_FILE_TABLE_MASK;
6045 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006046 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006047 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006048 if (io_queue_file_removal(data, file))
6049 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006050 }
6051 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006052 file = fget(fd);
6053 if (!file) {
6054 err = -EBADF;
6055 break;
6056 }
6057 /*
6058 * Don't allow io_uring instances to be registered. If
6059 * UNIX isn't enabled, then this causes a reference
6060 * cycle and this instance can never get freed. If UNIX
6061 * is enabled we'll handle it just fine, but there's
6062 * still no point in allowing a ring fd as it doesn't
6063 * support regular read/write anyway.
6064 */
6065 if (file->f_op == &io_uring_fops) {
6066 fput(file);
6067 err = -EBADF;
6068 break;
6069 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006070 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006071 err = io_sqe_file_register(ctx, file, i);
6072 if (err)
6073 break;
6074 }
6075 nr_args--;
6076 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006077 up->offset++;
6078 }
6079
Jens Axboedd3db2a2020-02-26 10:23:43 -07006080 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006081 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006082
6083 return done ? done : err;
6084}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006085static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6086 unsigned nr_args)
6087{
6088 struct io_uring_files_update up;
6089
6090 if (!ctx->file_data)
6091 return -ENXIO;
6092 if (!nr_args)
6093 return -EINVAL;
6094 if (copy_from_user(&up, arg, sizeof(up)))
6095 return -EFAULT;
6096 if (up.resv)
6097 return -EINVAL;
6098
6099 return __io_sqe_files_update(ctx, &up, nr_args);
6100}
Jens Axboec3a31e62019-10-03 13:59:56 -06006101
Jens Axboe7d723062019-11-12 22:31:31 -07006102static void io_put_work(struct io_wq_work *work)
6103{
6104 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6105
6106 io_put_req(req);
6107}
6108
6109static void io_get_work(struct io_wq_work *work)
6110{
6111 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6112
6113 refcount_inc(&req->refs);
6114}
6115
Pavel Begunkov24369c22020-01-28 03:15:48 +03006116static int io_init_wq_offload(struct io_ring_ctx *ctx,
6117 struct io_uring_params *p)
6118{
6119 struct io_wq_data data;
6120 struct fd f;
6121 struct io_ring_ctx *ctx_attach;
6122 unsigned int concurrency;
6123 int ret = 0;
6124
6125 data.user = ctx->user;
6126 data.get_work = io_get_work;
6127 data.put_work = io_put_work;
6128
6129 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6130 /* Do QD, or 4 * CPUS, whatever is smallest */
6131 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6132
6133 ctx->io_wq = io_wq_create(concurrency, &data);
6134 if (IS_ERR(ctx->io_wq)) {
6135 ret = PTR_ERR(ctx->io_wq);
6136 ctx->io_wq = NULL;
6137 }
6138 return ret;
6139 }
6140
6141 f = fdget(p->wq_fd);
6142 if (!f.file)
6143 return -EBADF;
6144
6145 if (f.file->f_op != &io_uring_fops) {
6146 ret = -EINVAL;
6147 goto out_fput;
6148 }
6149
6150 ctx_attach = f.file->private_data;
6151 /* @io_wq is protected by holding the fd */
6152 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6153 ret = -EINVAL;
6154 goto out_fput;
6155 }
6156
6157 ctx->io_wq = ctx_attach->io_wq;
6158out_fput:
6159 fdput(f);
6160 return ret;
6161}
6162
Jens Axboe6c271ce2019-01-10 11:22:30 -07006163static int io_sq_offload_start(struct io_ring_ctx *ctx,
6164 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006165{
6166 int ret;
6167
Jens Axboe6c271ce2019-01-10 11:22:30 -07006168 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006169 mmgrab(current->mm);
6170 ctx->sqo_mm = current->mm;
6171
Jens Axboe6c271ce2019-01-10 11:22:30 -07006172 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006173 ret = -EPERM;
6174 if (!capable(CAP_SYS_ADMIN))
6175 goto err;
6176
Jens Axboe917257d2019-04-13 09:28:55 -06006177 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6178 if (!ctx->sq_thread_idle)
6179 ctx->sq_thread_idle = HZ;
6180
Jens Axboe6c271ce2019-01-10 11:22:30 -07006181 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006182 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006183
Jens Axboe917257d2019-04-13 09:28:55 -06006184 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006185 if (cpu >= nr_cpu_ids)
6186 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006187 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006188 goto err;
6189
Jens Axboe6c271ce2019-01-10 11:22:30 -07006190 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6191 ctx, cpu,
6192 "io_uring-sq");
6193 } else {
6194 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6195 "io_uring-sq");
6196 }
6197 if (IS_ERR(ctx->sqo_thread)) {
6198 ret = PTR_ERR(ctx->sqo_thread);
6199 ctx->sqo_thread = NULL;
6200 goto err;
6201 }
6202 wake_up_process(ctx->sqo_thread);
6203 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6204 /* Can't have SQ_AFF without SQPOLL */
6205 ret = -EINVAL;
6206 goto err;
6207 }
6208
Pavel Begunkov24369c22020-01-28 03:15:48 +03006209 ret = io_init_wq_offload(ctx, p);
6210 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006211 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006212
6213 return 0;
6214err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006215 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006216 mmdrop(ctx->sqo_mm);
6217 ctx->sqo_mm = NULL;
6218 return ret;
6219}
6220
6221static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6222{
6223 atomic_long_sub(nr_pages, &user->locked_vm);
6224}
6225
6226static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6227{
6228 unsigned long page_limit, cur_pages, new_pages;
6229
6230 /* Don't allow more pages than we can safely lock */
6231 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6232
6233 do {
6234 cur_pages = atomic_long_read(&user->locked_vm);
6235 new_pages = cur_pages + nr_pages;
6236 if (new_pages > page_limit)
6237 return -ENOMEM;
6238 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6239 new_pages) != cur_pages);
6240
6241 return 0;
6242}
6243
6244static void io_mem_free(void *ptr)
6245{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006246 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006247
Mark Rutland52e04ef2019-04-30 17:30:21 +01006248 if (!ptr)
6249 return;
6250
6251 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006252 if (put_page_testzero(page))
6253 free_compound_page(page);
6254}
6255
6256static void *io_mem_alloc(size_t size)
6257{
6258 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6259 __GFP_NORETRY;
6260
6261 return (void *) __get_free_pages(gfp_flags, get_order(size));
6262}
6263
Hristo Venev75b28af2019-08-26 17:23:46 +00006264static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6265 size_t *sq_offset)
6266{
6267 struct io_rings *rings;
6268 size_t off, sq_array_size;
6269
6270 off = struct_size(rings, cqes, cq_entries);
6271 if (off == SIZE_MAX)
6272 return SIZE_MAX;
6273
6274#ifdef CONFIG_SMP
6275 off = ALIGN(off, SMP_CACHE_BYTES);
6276 if (off == 0)
6277 return SIZE_MAX;
6278#endif
6279
6280 sq_array_size = array_size(sizeof(u32), sq_entries);
6281 if (sq_array_size == SIZE_MAX)
6282 return SIZE_MAX;
6283
6284 if (check_add_overflow(off, sq_array_size, &off))
6285 return SIZE_MAX;
6286
6287 if (sq_offset)
6288 *sq_offset = off;
6289
6290 return off;
6291}
6292
Jens Axboe2b188cc2019-01-07 10:46:33 -07006293static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6294{
Hristo Venev75b28af2019-08-26 17:23:46 +00006295 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006296
Hristo Venev75b28af2019-08-26 17:23:46 +00006297 pages = (size_t)1 << get_order(
6298 rings_size(sq_entries, cq_entries, NULL));
6299 pages += (size_t)1 << get_order(
6300 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301
Hristo Venev75b28af2019-08-26 17:23:46 +00006302 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303}
6304
Jens Axboeedafcce2019-01-09 09:16:05 -07006305static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6306{
6307 int i, j;
6308
6309 if (!ctx->user_bufs)
6310 return -ENXIO;
6311
6312 for (i = 0; i < ctx->nr_user_bufs; i++) {
6313 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6314
6315 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006316 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006317
6318 if (ctx->account_mem)
6319 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006320 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006321 imu->nr_bvecs = 0;
6322 }
6323
6324 kfree(ctx->user_bufs);
6325 ctx->user_bufs = NULL;
6326 ctx->nr_user_bufs = 0;
6327 return 0;
6328}
6329
6330static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6331 void __user *arg, unsigned index)
6332{
6333 struct iovec __user *src;
6334
6335#ifdef CONFIG_COMPAT
6336 if (ctx->compat) {
6337 struct compat_iovec __user *ciovs;
6338 struct compat_iovec ciov;
6339
6340 ciovs = (struct compat_iovec __user *) arg;
6341 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6342 return -EFAULT;
6343
Jens Axboed55e5f52019-12-11 16:12:15 -07006344 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006345 dst->iov_len = ciov.iov_len;
6346 return 0;
6347 }
6348#endif
6349 src = (struct iovec __user *) arg;
6350 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6351 return -EFAULT;
6352 return 0;
6353}
6354
6355static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6356 unsigned nr_args)
6357{
6358 struct vm_area_struct **vmas = NULL;
6359 struct page **pages = NULL;
6360 int i, j, got_pages = 0;
6361 int ret = -EINVAL;
6362
6363 if (ctx->user_bufs)
6364 return -EBUSY;
6365 if (!nr_args || nr_args > UIO_MAXIOV)
6366 return -EINVAL;
6367
6368 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6369 GFP_KERNEL);
6370 if (!ctx->user_bufs)
6371 return -ENOMEM;
6372
6373 for (i = 0; i < nr_args; i++) {
6374 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6375 unsigned long off, start, end, ubuf;
6376 int pret, nr_pages;
6377 struct iovec iov;
6378 size_t size;
6379
6380 ret = io_copy_iov(ctx, &iov, arg, i);
6381 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006382 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006383
6384 /*
6385 * Don't impose further limits on the size and buffer
6386 * constraints here, we'll -EINVAL later when IO is
6387 * submitted if they are wrong.
6388 */
6389 ret = -EFAULT;
6390 if (!iov.iov_base || !iov.iov_len)
6391 goto err;
6392
6393 /* arbitrary limit, but we need something */
6394 if (iov.iov_len > SZ_1G)
6395 goto err;
6396
6397 ubuf = (unsigned long) iov.iov_base;
6398 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6399 start = ubuf >> PAGE_SHIFT;
6400 nr_pages = end - start;
6401
6402 if (ctx->account_mem) {
6403 ret = io_account_mem(ctx->user, nr_pages);
6404 if (ret)
6405 goto err;
6406 }
6407
6408 ret = 0;
6409 if (!pages || nr_pages > got_pages) {
6410 kfree(vmas);
6411 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006412 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006413 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006414 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006415 sizeof(struct vm_area_struct *),
6416 GFP_KERNEL);
6417 if (!pages || !vmas) {
6418 ret = -ENOMEM;
6419 if (ctx->account_mem)
6420 io_unaccount_mem(ctx->user, nr_pages);
6421 goto err;
6422 }
6423 got_pages = nr_pages;
6424 }
6425
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006426 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006427 GFP_KERNEL);
6428 ret = -ENOMEM;
6429 if (!imu->bvec) {
6430 if (ctx->account_mem)
6431 io_unaccount_mem(ctx->user, nr_pages);
6432 goto err;
6433 }
6434
6435 ret = 0;
6436 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006437 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006438 FOLL_WRITE | FOLL_LONGTERM,
6439 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006440 if (pret == nr_pages) {
6441 /* don't support file backed memory */
6442 for (j = 0; j < nr_pages; j++) {
6443 struct vm_area_struct *vma = vmas[j];
6444
6445 if (vma->vm_file &&
6446 !is_file_hugepages(vma->vm_file)) {
6447 ret = -EOPNOTSUPP;
6448 break;
6449 }
6450 }
6451 } else {
6452 ret = pret < 0 ? pret : -EFAULT;
6453 }
6454 up_read(&current->mm->mmap_sem);
6455 if (ret) {
6456 /*
6457 * if we did partial map, or found file backed vmas,
6458 * release any pages we did get
6459 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006460 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006461 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006462 if (ctx->account_mem)
6463 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006464 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006465 goto err;
6466 }
6467
6468 off = ubuf & ~PAGE_MASK;
6469 size = iov.iov_len;
6470 for (j = 0; j < nr_pages; j++) {
6471 size_t vec_len;
6472
6473 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6474 imu->bvec[j].bv_page = pages[j];
6475 imu->bvec[j].bv_len = vec_len;
6476 imu->bvec[j].bv_offset = off;
6477 off = 0;
6478 size -= vec_len;
6479 }
6480 /* store original address for later verification */
6481 imu->ubuf = ubuf;
6482 imu->len = iov.iov_len;
6483 imu->nr_bvecs = nr_pages;
6484
6485 ctx->nr_user_bufs++;
6486 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006487 kvfree(pages);
6488 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006489 return 0;
6490err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006491 kvfree(pages);
6492 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006493 io_sqe_buffer_unregister(ctx);
6494 return ret;
6495}
6496
Jens Axboe9b402842019-04-11 11:45:41 -06006497static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6498{
6499 __s32 __user *fds = arg;
6500 int fd;
6501
6502 if (ctx->cq_ev_fd)
6503 return -EBUSY;
6504
6505 if (copy_from_user(&fd, fds, sizeof(*fds)))
6506 return -EFAULT;
6507
6508 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6509 if (IS_ERR(ctx->cq_ev_fd)) {
6510 int ret = PTR_ERR(ctx->cq_ev_fd);
6511 ctx->cq_ev_fd = NULL;
6512 return ret;
6513 }
6514
6515 return 0;
6516}
6517
6518static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6519{
6520 if (ctx->cq_ev_fd) {
6521 eventfd_ctx_put(ctx->cq_ev_fd);
6522 ctx->cq_ev_fd = NULL;
6523 return 0;
6524 }
6525
6526 return -ENXIO;
6527}
6528
Jens Axboe2b188cc2019-01-07 10:46:33 -07006529static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6530{
Jens Axboe6b063142019-01-10 22:13:58 -07006531 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006532 if (ctx->sqo_mm)
6533 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006534
6535 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006536 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006537 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006538 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006539 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006540
Jens Axboe2b188cc2019-01-07 10:46:33 -07006541#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006542 if (ctx->ring_sock) {
6543 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006544 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006545 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006546#endif
6547
Hristo Venev75b28af2019-08-26 17:23:46 +00006548 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006549 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006550
6551 percpu_ref_exit(&ctx->refs);
6552 if (ctx->account_mem)
6553 io_unaccount_mem(ctx->user,
6554 ring_pages(ctx->sq_entries, ctx->cq_entries));
6555 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006556 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006557 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006558 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006559 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006560 kfree(ctx);
6561}
6562
6563static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6564{
6565 struct io_ring_ctx *ctx = file->private_data;
6566 __poll_t mask = 0;
6567
6568 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006569 /*
6570 * synchronizes with barrier from wq_has_sleeper call in
6571 * io_commit_cqring
6572 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006573 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006574 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6575 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006576 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006577 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006578 mask |= EPOLLIN | EPOLLRDNORM;
6579
6580 return mask;
6581}
6582
6583static int io_uring_fasync(int fd, struct file *file, int on)
6584{
6585 struct io_ring_ctx *ctx = file->private_data;
6586
6587 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6588}
6589
Jens Axboe071698e2020-01-28 10:04:42 -07006590static int io_remove_personalities(int id, void *p, void *data)
6591{
6592 struct io_ring_ctx *ctx = data;
6593 const struct cred *cred;
6594
6595 cred = idr_remove(&ctx->personality_idr, id);
6596 if (cred)
6597 put_cred(cred);
6598 return 0;
6599}
6600
Jens Axboe2b188cc2019-01-07 10:46:33 -07006601static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6602{
6603 mutex_lock(&ctx->uring_lock);
6604 percpu_ref_kill(&ctx->refs);
6605 mutex_unlock(&ctx->uring_lock);
6606
Jens Axboedf069d82020-02-04 16:48:34 -07006607 /*
6608 * Wait for sq thread to idle, if we have one. It won't spin on new
6609 * work after we've killed the ctx ref above. This is important to do
6610 * before we cancel existing commands, as the thread could otherwise
6611 * be queueing new work post that. If that's work we need to cancel,
6612 * it could cause shutdown to hang.
6613 */
6614 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6615 cpu_relax();
6616
Jens Axboe5262f562019-09-17 12:26:57 -06006617 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006618 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006619
6620 if (ctx->io_wq)
6621 io_wq_cancel_all(ctx->io_wq);
6622
Jens Axboedef596e2019-01-09 08:59:42 -07006623 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006624 /* if we failed setting up the ctx, we might not have any rings */
6625 if (ctx->rings)
6626 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006627 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006628 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006629 io_ring_ctx_free(ctx);
6630}
6631
6632static int io_uring_release(struct inode *inode, struct file *file)
6633{
6634 struct io_ring_ctx *ctx = file->private_data;
6635
6636 file->private_data = NULL;
6637 io_ring_ctx_wait_and_kill(ctx);
6638 return 0;
6639}
6640
Jens Axboefcb323c2019-10-24 12:39:47 -06006641static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6642 struct files_struct *files)
6643{
6644 struct io_kiocb *req;
6645 DEFINE_WAIT(wait);
6646
6647 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006648 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006649
6650 spin_lock_irq(&ctx->inflight_lock);
6651 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006652 if (req->work.files != files)
6653 continue;
6654 /* req is being completed, ignore */
6655 if (!refcount_inc_not_zero(&req->refs))
6656 continue;
6657 cancel_req = req;
6658 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006659 }
Jens Axboe768134d2019-11-10 20:30:53 -07006660 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006661 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006662 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006663 spin_unlock_irq(&ctx->inflight_lock);
6664
Jens Axboe768134d2019-11-10 20:30:53 -07006665 /* We need to keep going until we don't find a matching req */
6666 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006667 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006668
Jens Axboe2ca10252020-02-13 17:17:35 -07006669 if (cancel_req->flags & REQ_F_OVERFLOW) {
6670 spin_lock_irq(&ctx->completion_lock);
6671 list_del(&cancel_req->list);
6672 cancel_req->flags &= ~REQ_F_OVERFLOW;
6673 if (list_empty(&ctx->cq_overflow_list)) {
6674 clear_bit(0, &ctx->sq_check_overflow);
6675 clear_bit(0, &ctx->cq_check_overflow);
6676 }
6677 spin_unlock_irq(&ctx->completion_lock);
6678
6679 WRITE_ONCE(ctx->rings->cq_overflow,
6680 atomic_inc_return(&ctx->cached_cq_overflow));
6681
6682 /*
6683 * Put inflight ref and overflow ref. If that's
6684 * all we had, then we're done with this request.
6685 */
6686 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6687 io_put_req(cancel_req);
6688 continue;
6689 }
6690 }
6691
Bob Liu2f6d9b92019-11-13 18:06:24 +08006692 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6693 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006694 schedule();
6695 }
Jens Axboe768134d2019-11-10 20:30:53 -07006696 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006697}
6698
6699static int io_uring_flush(struct file *file, void *data)
6700{
6701 struct io_ring_ctx *ctx = file->private_data;
6702
6703 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006704
6705 /*
6706 * If the task is going away, cancel work it may have pending
6707 */
6708 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6709 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6710
Jens Axboefcb323c2019-10-24 12:39:47 -06006711 return 0;
6712}
6713
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006714static void *io_uring_validate_mmap_request(struct file *file,
6715 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006716{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006717 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006718 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006719 struct page *page;
6720 void *ptr;
6721
6722 switch (offset) {
6723 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006724 case IORING_OFF_CQ_RING:
6725 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006726 break;
6727 case IORING_OFF_SQES:
6728 ptr = ctx->sq_sqes;
6729 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006730 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006731 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006732 }
6733
6734 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006735 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006736 return ERR_PTR(-EINVAL);
6737
6738 return ptr;
6739}
6740
6741#ifdef CONFIG_MMU
6742
6743static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6744{
6745 size_t sz = vma->vm_end - vma->vm_start;
6746 unsigned long pfn;
6747 void *ptr;
6748
6749 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6750 if (IS_ERR(ptr))
6751 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006752
6753 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6754 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6755}
6756
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006757#else /* !CONFIG_MMU */
6758
6759static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6760{
6761 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6762}
6763
6764static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6765{
6766 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6767}
6768
6769static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6770 unsigned long addr, unsigned long len,
6771 unsigned long pgoff, unsigned long flags)
6772{
6773 void *ptr;
6774
6775 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6776 if (IS_ERR(ptr))
6777 return PTR_ERR(ptr);
6778
6779 return (unsigned long) ptr;
6780}
6781
6782#endif /* !CONFIG_MMU */
6783
Jens Axboe2b188cc2019-01-07 10:46:33 -07006784SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6785 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6786 size_t, sigsz)
6787{
6788 struct io_ring_ctx *ctx;
6789 long ret = -EBADF;
6790 int submitted = 0;
6791 struct fd f;
6792
Jens Axboeb41e9852020-02-17 09:52:41 -07006793 if (current->task_works)
6794 task_work_run();
6795
Jens Axboe6c271ce2019-01-10 11:22:30 -07006796 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797 return -EINVAL;
6798
6799 f = fdget(fd);
6800 if (!f.file)
6801 return -EBADF;
6802
6803 ret = -EOPNOTSUPP;
6804 if (f.file->f_op != &io_uring_fops)
6805 goto out_fput;
6806
6807 ret = -ENXIO;
6808 ctx = f.file->private_data;
6809 if (!percpu_ref_tryget(&ctx->refs))
6810 goto out_fput;
6811
Jens Axboe6c271ce2019-01-10 11:22:30 -07006812 /*
6813 * For SQ polling, the thread will do all submissions and completions.
6814 * Just return the requested submit count, and wake the thread if
6815 * we were asked to.
6816 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006817 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006818 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006819 if (!list_empty_careful(&ctx->cq_overflow_list))
6820 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006821 if (flags & IORING_ENTER_SQ_WAKEUP)
6822 wake_up(&ctx->sqo_wait);
6823 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006824 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006825 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006826
6827 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006828 /* already have mm, so io_submit_sqes() won't try to grab it */
6829 cur_mm = ctx->sqo_mm;
6830 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6831 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006832 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006833
6834 if (submitted != to_submit)
6835 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006836 }
6837 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006838 unsigned nr_events = 0;
6839
Jens Axboe2b188cc2019-01-07 10:46:33 -07006840 min_complete = min(min_complete, ctx->cq_entries);
6841
Jens Axboedef596e2019-01-09 08:59:42 -07006842 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006843 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006844 } else {
6845 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6846 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006847 }
6848
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006849out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006850 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006851out_fput:
6852 fdput(f);
6853 return submitted ? submitted : ret;
6854}
6855
Tobias Klauserbebdb652020-02-26 18:38:32 +01006856#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006857static int io_uring_show_cred(int id, void *p, void *data)
6858{
6859 const struct cred *cred = p;
6860 struct seq_file *m = data;
6861 struct user_namespace *uns = seq_user_ns(m);
6862 struct group_info *gi;
6863 kernel_cap_t cap;
6864 unsigned __capi;
6865 int g;
6866
6867 seq_printf(m, "%5d\n", id);
6868 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6869 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6870 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6871 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6872 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6873 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6874 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6875 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6876 seq_puts(m, "\n\tGroups:\t");
6877 gi = cred->group_info;
6878 for (g = 0; g < gi->ngroups; g++) {
6879 seq_put_decimal_ull(m, g ? " " : "",
6880 from_kgid_munged(uns, gi->gid[g]));
6881 }
6882 seq_puts(m, "\n\tCapEff:\t");
6883 cap = cred->cap_effective;
6884 CAP_FOR_EACH_U32(__capi)
6885 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6886 seq_putc(m, '\n');
6887 return 0;
6888}
6889
6890static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6891{
6892 int i;
6893
6894 mutex_lock(&ctx->uring_lock);
6895 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6896 for (i = 0; i < ctx->nr_user_files; i++) {
6897 struct fixed_file_table *table;
6898 struct file *f;
6899
6900 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6901 f = table->files[i & IORING_FILE_TABLE_MASK];
6902 if (f)
6903 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6904 else
6905 seq_printf(m, "%5u: <none>\n", i);
6906 }
6907 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6908 for (i = 0; i < ctx->nr_user_bufs; i++) {
6909 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6910
6911 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6912 (unsigned int) buf->len);
6913 }
6914 if (!idr_is_empty(&ctx->personality_idr)) {
6915 seq_printf(m, "Personalities:\n");
6916 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6917 }
Jens Axboed7718a92020-02-14 22:23:12 -07006918 seq_printf(m, "PollList:\n");
6919 spin_lock_irq(&ctx->completion_lock);
6920 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
6921 struct hlist_head *list = &ctx->cancel_hash[i];
6922 struct io_kiocb *req;
6923
6924 hlist_for_each_entry(req, list, hash_node)
6925 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
6926 req->task->task_works != NULL);
6927 }
6928 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07006929 mutex_unlock(&ctx->uring_lock);
6930}
6931
6932static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6933{
6934 struct io_ring_ctx *ctx = f->private_data;
6935
6936 if (percpu_ref_tryget(&ctx->refs)) {
6937 __io_uring_show_fdinfo(ctx, m);
6938 percpu_ref_put(&ctx->refs);
6939 }
6940}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006941#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006942
Jens Axboe2b188cc2019-01-07 10:46:33 -07006943static const struct file_operations io_uring_fops = {
6944 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006945 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006946 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006947#ifndef CONFIG_MMU
6948 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6949 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6950#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006951 .poll = io_uring_poll,
6952 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006953#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006954 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006955#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006956};
6957
6958static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6959 struct io_uring_params *p)
6960{
Hristo Venev75b28af2019-08-26 17:23:46 +00006961 struct io_rings *rings;
6962 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006963
Hristo Venev75b28af2019-08-26 17:23:46 +00006964 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6965 if (size == SIZE_MAX)
6966 return -EOVERFLOW;
6967
6968 rings = io_mem_alloc(size);
6969 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006970 return -ENOMEM;
6971
Hristo Venev75b28af2019-08-26 17:23:46 +00006972 ctx->rings = rings;
6973 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6974 rings->sq_ring_mask = p->sq_entries - 1;
6975 rings->cq_ring_mask = p->cq_entries - 1;
6976 rings->sq_ring_entries = p->sq_entries;
6977 rings->cq_ring_entries = p->cq_entries;
6978 ctx->sq_mask = rings->sq_ring_mask;
6979 ctx->cq_mask = rings->cq_ring_mask;
6980 ctx->sq_entries = rings->sq_ring_entries;
6981 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006982
6983 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006984 if (size == SIZE_MAX) {
6985 io_mem_free(ctx->rings);
6986 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006987 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006988 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006989
6990 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006991 if (!ctx->sq_sqes) {
6992 io_mem_free(ctx->rings);
6993 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006994 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006995 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006996
Jens Axboe2b188cc2019-01-07 10:46:33 -07006997 return 0;
6998}
6999
7000/*
7001 * Allocate an anonymous fd, this is what constitutes the application
7002 * visible backing of an io_uring instance. The application mmaps this
7003 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7004 * we have to tie this fd to a socket for file garbage collection purposes.
7005 */
7006static int io_uring_get_fd(struct io_ring_ctx *ctx)
7007{
7008 struct file *file;
7009 int ret;
7010
7011#if defined(CONFIG_UNIX)
7012 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7013 &ctx->ring_sock);
7014 if (ret)
7015 return ret;
7016#endif
7017
7018 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7019 if (ret < 0)
7020 goto err;
7021
7022 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7023 O_RDWR | O_CLOEXEC);
7024 if (IS_ERR(file)) {
7025 put_unused_fd(ret);
7026 ret = PTR_ERR(file);
7027 goto err;
7028 }
7029
7030#if defined(CONFIG_UNIX)
7031 ctx->ring_sock->file = file;
7032#endif
7033 fd_install(ret, file);
7034 return ret;
7035err:
7036#if defined(CONFIG_UNIX)
7037 sock_release(ctx->ring_sock);
7038 ctx->ring_sock = NULL;
7039#endif
7040 return ret;
7041}
7042
7043static int io_uring_create(unsigned entries, struct io_uring_params *p)
7044{
7045 struct user_struct *user = NULL;
7046 struct io_ring_ctx *ctx;
7047 bool account_mem;
7048 int ret;
7049
Jens Axboe8110c1a2019-12-28 15:39:54 -07007050 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007051 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007052 if (entries > IORING_MAX_ENTRIES) {
7053 if (!(p->flags & IORING_SETUP_CLAMP))
7054 return -EINVAL;
7055 entries = IORING_MAX_ENTRIES;
7056 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007057
7058 /*
7059 * Use twice as many entries for the CQ ring. It's possible for the
7060 * application to drive a higher depth than the size of the SQ ring,
7061 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007062 * some flexibility in overcommitting a bit. If the application has
7063 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7064 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007065 */
7066 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007067 if (p->flags & IORING_SETUP_CQSIZE) {
7068 /*
7069 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7070 * to a power-of-two, if it isn't already. We do NOT impose
7071 * any cq vs sq ring sizing.
7072 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007073 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007074 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007075 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7076 if (!(p->flags & IORING_SETUP_CLAMP))
7077 return -EINVAL;
7078 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7079 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007080 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7081 } else {
7082 p->cq_entries = 2 * p->sq_entries;
7083 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007084
7085 user = get_uid(current_user());
7086 account_mem = !capable(CAP_IPC_LOCK);
7087
7088 if (account_mem) {
7089 ret = io_account_mem(user,
7090 ring_pages(p->sq_entries, p->cq_entries));
7091 if (ret) {
7092 free_uid(user);
7093 return ret;
7094 }
7095 }
7096
7097 ctx = io_ring_ctx_alloc(p);
7098 if (!ctx) {
7099 if (account_mem)
7100 io_unaccount_mem(user, ring_pages(p->sq_entries,
7101 p->cq_entries));
7102 free_uid(user);
7103 return -ENOMEM;
7104 }
7105 ctx->compat = in_compat_syscall();
7106 ctx->account_mem = account_mem;
7107 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007108 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007109
7110 ret = io_allocate_scq_urings(ctx, p);
7111 if (ret)
7112 goto err;
7113
Jens Axboe6c271ce2019-01-10 11:22:30 -07007114 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007115 if (ret)
7116 goto err;
7117
Jens Axboe2b188cc2019-01-07 10:46:33 -07007118 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007119 p->sq_off.head = offsetof(struct io_rings, sq.head);
7120 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7121 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7122 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7123 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7124 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7125 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007126
7127 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007128 p->cq_off.head = offsetof(struct io_rings, cq.head);
7129 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7130 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7131 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7132 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7133 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007134
Jens Axboe044c1ab2019-10-28 09:15:33 -06007135 /*
7136 * Install ring fd as the very last thing, so we don't risk someone
7137 * having closed it before we finish setup
7138 */
7139 ret = io_uring_get_fd(ctx);
7140 if (ret < 0)
7141 goto err;
7142
Jens Axboeda8c9692019-12-02 18:51:26 -07007143 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007144 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007145 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007146 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007147 return ret;
7148err:
7149 io_ring_ctx_wait_and_kill(ctx);
7150 return ret;
7151}
7152
7153/*
7154 * Sets up an aio uring context, and returns the fd. Applications asks for a
7155 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7156 * params structure passed in.
7157 */
7158static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7159{
7160 struct io_uring_params p;
7161 long ret;
7162 int i;
7163
7164 if (copy_from_user(&p, params, sizeof(p)))
7165 return -EFAULT;
7166 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7167 if (p.resv[i])
7168 return -EINVAL;
7169 }
7170
Jens Axboe6c271ce2019-01-10 11:22:30 -07007171 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007172 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007173 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007174 return -EINVAL;
7175
7176 ret = io_uring_create(entries, &p);
7177 if (ret < 0)
7178 return ret;
7179
7180 if (copy_to_user(params, &p, sizeof(p)))
7181 return -EFAULT;
7182
7183 return ret;
7184}
7185
7186SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7187 struct io_uring_params __user *, params)
7188{
7189 return io_uring_setup(entries, params);
7190}
7191
Jens Axboe66f4af92020-01-16 15:36:52 -07007192static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7193{
7194 struct io_uring_probe *p;
7195 size_t size;
7196 int i, ret;
7197
7198 size = struct_size(p, ops, nr_args);
7199 if (size == SIZE_MAX)
7200 return -EOVERFLOW;
7201 p = kzalloc(size, GFP_KERNEL);
7202 if (!p)
7203 return -ENOMEM;
7204
7205 ret = -EFAULT;
7206 if (copy_from_user(p, arg, size))
7207 goto out;
7208 ret = -EINVAL;
7209 if (memchr_inv(p, 0, size))
7210 goto out;
7211
7212 p->last_op = IORING_OP_LAST - 1;
7213 if (nr_args > IORING_OP_LAST)
7214 nr_args = IORING_OP_LAST;
7215
7216 for (i = 0; i < nr_args; i++) {
7217 p->ops[i].op = i;
7218 if (!io_op_defs[i].not_supported)
7219 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7220 }
7221 p->ops_len = i;
7222
7223 ret = 0;
7224 if (copy_to_user(arg, p, size))
7225 ret = -EFAULT;
7226out:
7227 kfree(p);
7228 return ret;
7229}
7230
Jens Axboe071698e2020-01-28 10:04:42 -07007231static int io_register_personality(struct io_ring_ctx *ctx)
7232{
7233 const struct cred *creds = get_current_cred();
7234 int id;
7235
7236 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7237 USHRT_MAX, GFP_KERNEL);
7238 if (id < 0)
7239 put_cred(creds);
7240 return id;
7241}
7242
7243static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7244{
7245 const struct cred *old_creds;
7246
7247 old_creds = idr_remove(&ctx->personality_idr, id);
7248 if (old_creds) {
7249 put_cred(old_creds);
7250 return 0;
7251 }
7252
7253 return -EINVAL;
7254}
7255
7256static bool io_register_op_must_quiesce(int op)
7257{
7258 switch (op) {
7259 case IORING_UNREGISTER_FILES:
7260 case IORING_REGISTER_FILES_UPDATE:
7261 case IORING_REGISTER_PROBE:
7262 case IORING_REGISTER_PERSONALITY:
7263 case IORING_UNREGISTER_PERSONALITY:
7264 return false;
7265 default:
7266 return true;
7267 }
7268}
7269
Jens Axboeedafcce2019-01-09 09:16:05 -07007270static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7271 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007272 __releases(ctx->uring_lock)
7273 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007274{
7275 int ret;
7276
Jens Axboe35fa71a2019-04-22 10:23:23 -06007277 /*
7278 * We're inside the ring mutex, if the ref is already dying, then
7279 * someone else killed the ctx or is already going through
7280 * io_uring_register().
7281 */
7282 if (percpu_ref_is_dying(&ctx->refs))
7283 return -ENXIO;
7284
Jens Axboe071698e2020-01-28 10:04:42 -07007285 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007286 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007287
Jens Axboe05f3fb32019-12-09 11:22:50 -07007288 /*
7289 * Drop uring mutex before waiting for references to exit. If
7290 * another thread is currently inside io_uring_enter() it might
7291 * need to grab the uring_lock to make progress. If we hold it
7292 * here across the drain wait, then we can deadlock. It's safe
7293 * to drop the mutex here, since no new references will come in
7294 * after we've killed the percpu ref.
7295 */
7296 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007297 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007298 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007299 if (ret) {
7300 percpu_ref_resurrect(&ctx->refs);
7301 ret = -EINTR;
7302 goto out;
7303 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007304 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007305
7306 switch (opcode) {
7307 case IORING_REGISTER_BUFFERS:
7308 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7309 break;
7310 case IORING_UNREGISTER_BUFFERS:
7311 ret = -EINVAL;
7312 if (arg || nr_args)
7313 break;
7314 ret = io_sqe_buffer_unregister(ctx);
7315 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007316 case IORING_REGISTER_FILES:
7317 ret = io_sqe_files_register(ctx, arg, nr_args);
7318 break;
7319 case IORING_UNREGISTER_FILES:
7320 ret = -EINVAL;
7321 if (arg || nr_args)
7322 break;
7323 ret = io_sqe_files_unregister(ctx);
7324 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007325 case IORING_REGISTER_FILES_UPDATE:
7326 ret = io_sqe_files_update(ctx, arg, nr_args);
7327 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007328 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007329 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007330 ret = -EINVAL;
7331 if (nr_args != 1)
7332 break;
7333 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007334 if (ret)
7335 break;
7336 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7337 ctx->eventfd_async = 1;
7338 else
7339 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007340 break;
7341 case IORING_UNREGISTER_EVENTFD:
7342 ret = -EINVAL;
7343 if (arg || nr_args)
7344 break;
7345 ret = io_eventfd_unregister(ctx);
7346 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007347 case IORING_REGISTER_PROBE:
7348 ret = -EINVAL;
7349 if (!arg || nr_args > 256)
7350 break;
7351 ret = io_probe(ctx, arg, nr_args);
7352 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007353 case IORING_REGISTER_PERSONALITY:
7354 ret = -EINVAL;
7355 if (arg || nr_args)
7356 break;
7357 ret = io_register_personality(ctx);
7358 break;
7359 case IORING_UNREGISTER_PERSONALITY:
7360 ret = -EINVAL;
7361 if (arg)
7362 break;
7363 ret = io_unregister_personality(ctx, nr_args);
7364 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007365 default:
7366 ret = -EINVAL;
7367 break;
7368 }
7369
Jens Axboe071698e2020-01-28 10:04:42 -07007370 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007371 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007372 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007373out:
7374 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007375 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007376 return ret;
7377}
7378
7379SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7380 void __user *, arg, unsigned int, nr_args)
7381{
7382 struct io_ring_ctx *ctx;
7383 long ret = -EBADF;
7384 struct fd f;
7385
7386 f = fdget(fd);
7387 if (!f.file)
7388 return -EBADF;
7389
7390 ret = -EOPNOTSUPP;
7391 if (f.file->f_op != &io_uring_fops)
7392 goto out_fput;
7393
7394 ctx = f.file->private_data;
7395
7396 mutex_lock(&ctx->uring_lock);
7397 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7398 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007399 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7400 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007401out_fput:
7402 fdput(f);
7403 return ret;
7404}
7405
Jens Axboe2b188cc2019-01-07 10:46:33 -07007406static int __init io_uring_init(void)
7407{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007408#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7409 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7410 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7411} while (0)
7412
7413#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7414 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7415 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7416 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7417 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7418 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7419 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7420 BUILD_BUG_SQE_ELEM(8, __u64, off);
7421 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7422 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007423 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007424 BUILD_BUG_SQE_ELEM(24, __u32, len);
7425 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7426 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7427 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7428 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7429 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7430 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7431 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7432 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7433 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7434 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7435 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7436 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7437 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007438 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007439 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7440 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7441 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007442 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007443
Jens Axboed3656342019-12-18 09:50:26 -07007444 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007445 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7446 return 0;
7447};
7448__initcall(io_uring_init);