blob: 0d973de7512776691cadf739530c75bebbf05859 [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,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300490};
491
492enum {
493 /* ctx owns file */
494 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
495 /* drain existing IO first */
496 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
497 /* linked sqes */
498 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
499 /* doesn't sever on completion < 0 */
500 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
501 /* IOSQE_ASYNC */
502 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
503
504 /* already grabbed next link */
505 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
506 /* fail rest of links */
507 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
508 /* on inflight list */
509 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
510 /* read/write uses file position */
511 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
512 /* must not punt to workers */
513 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
514 /* polled IO has completed */
515 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
516 /* has linked timeout */
517 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
518 /* timeout request */
519 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
520 /* regular file */
521 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
522 /* must be punted even for NONBLOCK */
523 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
524 /* no timeout sequence */
525 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
526 /* completion under lock */
527 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300528 /* needs cleanup */
529 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700530 /* in overflow list */
531 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300532};
533
Jens Axboe09bb8392019-03-13 12:39:28 -0600534/*
535 * NOTE! Each of the iocb union members has the file pointer
536 * as the first entry in their struct definition. So you can
537 * access the file pointer through any of the sub-structs,
538 * or directly as just 'ki_filp' in this struct.
539 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700540struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700541 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600542 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700543 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700544 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700545 struct io_accept accept;
546 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700547 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700548 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700549 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700550 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700551 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700552 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700553 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700554 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700555 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700556 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300557 struct io_splice splice;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700558 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700560 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300561 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700562 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700563
564 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700565 union {
566 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700567 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700568 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600569 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700570 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700571 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700572 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600573 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600574 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575
Jens Axboefcb323c2019-10-24 12:39:47 -0600576 struct list_head inflight_entry;
577
Jens Axboeb41e9852020-02-17 09:52:41 -0700578 union {
579 /*
580 * Only commands that never go async can use the below fields,
581 * obviously. Right now only IORING_OP_POLL_ADD uses them.
582 */
583 struct {
584 struct task_struct *task;
585 struct callback_head task_work;
586 };
587 struct io_wq_work work;
588 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700589};
590
591#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700592#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700593
Jens Axboe9a56a232019-01-09 09:06:50 -0700594struct io_submit_state {
595 struct blk_plug plug;
596
597 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700598 * io_kiocb alloc cache
599 */
600 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300601 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700602
603 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700604 * File reference cache
605 */
606 struct file *file;
607 unsigned int fd;
608 unsigned int has_refs;
609 unsigned int used_refs;
610 unsigned int ios_left;
611};
612
Jens Axboed3656342019-12-18 09:50:26 -0700613struct io_op_def {
614 /* needs req->io allocated for deferral/async */
615 unsigned async_ctx : 1;
616 /* needs current->mm setup, does mm access */
617 unsigned needs_mm : 1;
618 /* needs req->file assigned */
619 unsigned needs_file : 1;
620 /* needs req->file assigned IFF fd is >= 0 */
621 unsigned fd_non_neg : 1;
622 /* hash wq insertion if file is a regular file */
623 unsigned hash_reg_file : 1;
624 /* unbound wq insertion if file is a non-regular file */
625 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700626 /* opcode is not supported by this kernel */
627 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700628 /* needs file table */
629 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700630 /* needs ->fs */
631 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700632 /* set if opcode supports polled "wait" */
633 unsigned pollin : 1;
634 unsigned pollout : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700635};
636
637static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300638 [IORING_OP_NOP] = {},
639 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700640 .async_ctx = 1,
641 .needs_mm = 1,
642 .needs_file = 1,
643 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700644 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700645 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300646 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700647 .async_ctx = 1,
648 .needs_mm = 1,
649 .needs_file = 1,
650 .hash_reg_file = 1,
651 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700652 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700653 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300654 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700655 .needs_file = 1,
656 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300657 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700658 .needs_file = 1,
659 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700660 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700661 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300662 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700663 .needs_file = 1,
664 .hash_reg_file = 1,
665 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700666 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700667 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300668 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700669 .needs_file = 1,
670 .unbound_nonreg_file = 1,
671 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300672 [IORING_OP_POLL_REMOVE] = {},
673 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700674 .needs_file = 1,
675 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300676 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700677 .async_ctx = 1,
678 .needs_mm = 1,
679 .needs_file = 1,
680 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700681 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700682 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700683 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300684 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700685 .async_ctx = 1,
686 .needs_mm = 1,
687 .needs_file = 1,
688 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700689 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700690 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700691 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300692 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700693 .async_ctx = 1,
694 .needs_mm = 1,
695 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300696 [IORING_OP_TIMEOUT_REMOVE] = {},
697 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700698 .needs_mm = 1,
699 .needs_file = 1,
700 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700701 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700702 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700703 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300704 [IORING_OP_ASYNC_CANCEL] = {},
705 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700706 .async_ctx = 1,
707 .needs_mm = 1,
708 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300709 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700710 .async_ctx = 1,
711 .needs_mm = 1,
712 .needs_file = 1,
713 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700714 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700717 .needs_file = 1,
718 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300719 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700720 .needs_file = 1,
721 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700722 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700723 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700724 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300725 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700726 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700727 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700730 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700731 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700732 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300733 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700734 .needs_mm = 1,
735 .needs_file = 1,
736 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700737 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700740 .needs_mm = 1,
741 .needs_file = 1,
742 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700743 .pollin = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700744 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300745 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700746 .needs_mm = 1,
747 .needs_file = 1,
748 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700749 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700750 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300751 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700752 .needs_file = 1,
753 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300754 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700755 .needs_mm = 1,
756 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300757 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700758 .needs_mm = 1,
759 .needs_file = 1,
760 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700761 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700762 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700764 .needs_mm = 1,
765 .needs_file = 1,
766 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700767 .pollin = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700770 .needs_file = 1,
771 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700772 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700773 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700774 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700775 [IORING_OP_EPOLL_CTL] = {
776 .unbound_nonreg_file = 1,
777 .file_table = 1,
778 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300779 [IORING_OP_SPLICE] = {
780 .needs_file = 1,
781 .hash_reg_file = 1,
782 .unbound_nonreg_file = 1,
783 }
Jens Axboed3656342019-12-18 09:50:26 -0700784};
785
Jens Axboe561fb042019-10-24 07:25:42 -0600786static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700787static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800788static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700789static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700790static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
791static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700792static int __io_sqe_files_update(struct io_ring_ctx *ctx,
793 struct io_uring_files_update *ip,
794 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700795static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700796static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300797static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700798static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
799 int fd, struct file **out_file, bool fixed);
800static void __io_queue_sqe(struct io_kiocb *req,
801 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600802
Jens Axboe2b188cc2019-01-07 10:46:33 -0700803static struct kmem_cache *req_cachep;
804
805static const struct file_operations io_uring_fops;
806
807struct sock *io_uring_get_socket(struct file *file)
808{
809#if defined(CONFIG_UNIX)
810 if (file->f_op == &io_uring_fops) {
811 struct io_ring_ctx *ctx = file->private_data;
812
813 return ctx->ring_sock->sk;
814 }
815#endif
816 return NULL;
817}
818EXPORT_SYMBOL(io_uring_get_socket);
819
820static void io_ring_ctx_ref_free(struct percpu_ref *ref)
821{
822 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
823
Jens Axboe206aefd2019-11-07 18:27:42 -0700824 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700825}
826
827static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
828{
829 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700830 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700831
832 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
833 if (!ctx)
834 return NULL;
835
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700836 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
837 if (!ctx->fallback_req)
838 goto err;
839
Jens Axboe206aefd2019-11-07 18:27:42 -0700840 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
841 if (!ctx->completions)
842 goto err;
843
Jens Axboe78076bb2019-12-04 19:56:40 -0700844 /*
845 * Use 5 bits less than the max cq entries, that should give us around
846 * 32 entries per hash list if totally full and uniformly spread.
847 */
848 hash_bits = ilog2(p->cq_entries);
849 hash_bits -= 5;
850 if (hash_bits <= 0)
851 hash_bits = 1;
852 ctx->cancel_hash_bits = hash_bits;
853 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
854 GFP_KERNEL);
855 if (!ctx->cancel_hash)
856 goto err;
857 __hash_init(ctx->cancel_hash, 1U << hash_bits);
858
Roman Gushchin21482892019-05-07 10:01:48 -0700859 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700860 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
861 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700862
863 ctx->flags = p->flags;
864 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700865 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700866 init_completion(&ctx->completions[0]);
867 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700868 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700869 mutex_init(&ctx->uring_lock);
870 init_waitqueue_head(&ctx->wait);
871 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700872 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600873 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600874 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600875 init_waitqueue_head(&ctx->inflight_wait);
876 spin_lock_init(&ctx->inflight_lock);
877 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700878 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700879err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700880 if (ctx->fallback_req)
881 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700882 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700883 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700884 kfree(ctx);
885 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700886}
887
Bob Liu9d858b22019-11-13 18:06:25 +0800888static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600889{
Jackie Liua197f662019-11-08 08:09:12 -0700890 struct io_ring_ctx *ctx = req->ctx;
891
Jens Axboe498ccd92019-10-25 10:04:25 -0600892 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
893 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600894}
895
Bob Liu9d858b22019-11-13 18:06:25 +0800896static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600897{
Pavel Begunkov87987892020-01-18 01:22:30 +0300898 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800899 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600900
Bob Liu9d858b22019-11-13 18:06:25 +0800901 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600902}
903
904static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600905{
906 struct io_kiocb *req;
907
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600908 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800909 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600910 list_del_init(&req->list);
911 return req;
912 }
913
914 return NULL;
915}
916
Jens Axboe5262f562019-09-17 12:26:57 -0600917static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
918{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600919 struct io_kiocb *req;
920
921 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700922 if (req) {
923 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
924 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800925 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700926 list_del_init(&req->list);
927 return req;
928 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600929 }
930
931 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600932}
933
Jens Axboede0617e2019-04-06 21:51:27 -0600934static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700935{
Hristo Venev75b28af2019-08-26 17:23:46 +0000936 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937
Pavel Begunkov07910152020-01-17 03:52:46 +0300938 /* order cqe stores with ring update */
939 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700940
Pavel Begunkov07910152020-01-17 03:52:46 +0300941 if (wq_has_sleeper(&ctx->cq_wait)) {
942 wake_up_interruptible(&ctx->cq_wait);
943 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700944 }
945}
946
Jens Axboecccf0ee2020-01-27 16:34:48 -0700947static inline void io_req_work_grab_env(struct io_kiocb *req,
948 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600949{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700950 if (!req->work.mm && def->needs_mm) {
951 mmgrab(current->mm);
952 req->work.mm = current->mm;
953 }
954 if (!req->work.creds)
955 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700956 if (!req->work.fs && def->needs_fs) {
957 spin_lock(&current->fs->lock);
958 if (!current->fs->in_exec) {
959 req->work.fs = current->fs;
960 req->work.fs->users++;
961 } else {
962 req->work.flags |= IO_WQ_WORK_CANCEL;
963 }
964 spin_unlock(&current->fs->lock);
965 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700966 if (!req->work.task_pid)
967 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700968}
969
970static inline void io_req_work_drop_env(struct io_kiocb *req)
971{
972 if (req->work.mm) {
973 mmdrop(req->work.mm);
974 req->work.mm = NULL;
975 }
976 if (req->work.creds) {
977 put_cred(req->work.creds);
978 req->work.creds = NULL;
979 }
Jens Axboeff002b32020-02-07 16:05:21 -0700980 if (req->work.fs) {
981 struct fs_struct *fs = req->work.fs;
982
983 spin_lock(&req->work.fs->lock);
984 if (--fs->users)
985 fs = NULL;
986 spin_unlock(&req->work.fs->lock);
987 if (fs)
988 free_fs_struct(fs);
989 }
Jens Axboe561fb042019-10-24 07:25:42 -0600990}
991
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +0300992static inline void io_prep_next_work(struct io_kiocb *req,
993 struct io_kiocb **link)
994{
995 const struct io_op_def *def = &io_op_defs[req->opcode];
996
997 if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file)
998 req->work.flags |= IO_WQ_WORK_UNBOUND;
999
1000 *link = io_prep_linked_timeout(req);
1001}
1002
Jens Axboe94ae5e72019-11-14 19:39:52 -07001003static inline bool io_prep_async_work(struct io_kiocb *req,
1004 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001005{
Jens Axboed3656342019-12-18 09:50:26 -07001006 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001007 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001008
Jens Axboed3656342019-12-18 09:50:26 -07001009 if (req->flags & REQ_F_ISREG) {
1010 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001011 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001012 } else {
1013 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001014 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001015 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001016
1017 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001018
Jens Axboe94ae5e72019-11-14 19:39:52 -07001019 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001020 return do_hashed;
1021}
1022
Jackie Liua197f662019-11-08 08:09:12 -07001023static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001024{
Jackie Liua197f662019-11-08 08:09:12 -07001025 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001026 struct io_kiocb *link;
1027 bool do_hashed;
1028
1029 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001030
1031 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1032 req->flags);
1033 if (!do_hashed) {
1034 io_wq_enqueue(ctx->io_wq, &req->work);
1035 } else {
1036 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1037 file_inode(req->file));
1038 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001039
1040 if (link)
1041 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001042}
1043
Jens Axboe5262f562019-09-17 12:26:57 -06001044static void io_kill_timeout(struct io_kiocb *req)
1045{
1046 int ret;
1047
Jens Axboe2d283902019-12-04 11:08:05 -07001048 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001049 if (ret != -1) {
1050 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001051 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001052 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001053 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001054 }
1055}
1056
1057static void io_kill_timeouts(struct io_ring_ctx *ctx)
1058{
1059 struct io_kiocb *req, *tmp;
1060
1061 spin_lock_irq(&ctx->completion_lock);
1062 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1063 io_kill_timeout(req);
1064 spin_unlock_irq(&ctx->completion_lock);
1065}
1066
Jens Axboede0617e2019-04-06 21:51:27 -06001067static void io_commit_cqring(struct io_ring_ctx *ctx)
1068{
1069 struct io_kiocb *req;
1070
Jens Axboe5262f562019-09-17 12:26:57 -06001071 while ((req = io_get_timeout_req(ctx)) != NULL)
1072 io_kill_timeout(req);
1073
Jens Axboede0617e2019-04-06 21:51:27 -06001074 __io_commit_cqring(ctx);
1075
Pavel Begunkov87987892020-01-18 01:22:30 +03001076 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001077 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001078}
1079
Jens Axboe2b188cc2019-01-07 10:46:33 -07001080static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1081{
Hristo Venev75b28af2019-08-26 17:23:46 +00001082 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001083 unsigned tail;
1084
1085 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001086 /*
1087 * writes to the cq entry need to come after reading head; the
1088 * control dependency is enough as we're using WRITE_ONCE to
1089 * fill the cq entry
1090 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001091 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001092 return NULL;
1093
1094 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001095 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001096}
1097
Jens Axboef2842ab2020-01-08 11:04:00 -07001098static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1099{
Jens Axboef0b493e2020-02-01 21:30:11 -07001100 if (!ctx->cq_ev_fd)
1101 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001102 if (!ctx->eventfd_async)
1103 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001104 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001105}
1106
Jens Axboeb41e9852020-02-17 09:52:41 -07001107static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001108{
1109 if (waitqueue_active(&ctx->wait))
1110 wake_up(&ctx->wait);
1111 if (waitqueue_active(&ctx->sqo_wait))
1112 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001113 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001114 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001115}
1116
Jens Axboec4a2ed72019-11-21 21:01:26 -07001117/* Returns true if there are no backlogged entries after the flush */
1118static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001120 struct io_rings *rings = ctx->rings;
1121 struct io_uring_cqe *cqe;
1122 struct io_kiocb *req;
1123 unsigned long flags;
1124 LIST_HEAD(list);
1125
1126 if (!force) {
1127 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001128 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001129 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1130 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001131 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001132 }
1133
1134 spin_lock_irqsave(&ctx->completion_lock, flags);
1135
1136 /* if force is set, the ring is going away. always drop after that */
1137 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001138 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001139
Jens Axboec4a2ed72019-11-21 21:01:26 -07001140 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001141 while (!list_empty(&ctx->cq_overflow_list)) {
1142 cqe = io_get_cqring(ctx);
1143 if (!cqe && !force)
1144 break;
1145
1146 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1147 list);
1148 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001149 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001150 if (cqe) {
1151 WRITE_ONCE(cqe->user_data, req->user_data);
1152 WRITE_ONCE(cqe->res, req->result);
1153 WRITE_ONCE(cqe->flags, 0);
1154 } else {
1155 WRITE_ONCE(ctx->rings->cq_overflow,
1156 atomic_inc_return(&ctx->cached_cq_overflow));
1157 }
1158 }
1159
1160 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001161 if (cqe) {
1162 clear_bit(0, &ctx->sq_check_overflow);
1163 clear_bit(0, &ctx->cq_check_overflow);
1164 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001165 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1166 io_cqring_ev_posted(ctx);
1167
1168 while (!list_empty(&list)) {
1169 req = list_first_entry(&list, struct io_kiocb, list);
1170 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001171 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001172 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001173
1174 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001175}
1176
Jens Axboe78e19bb2019-11-06 15:21:34 -07001177static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001178{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001179 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180 struct io_uring_cqe *cqe;
1181
Jens Axboe78e19bb2019-11-06 15:21:34 -07001182 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001183
Jens Axboe2b188cc2019-01-07 10:46:33 -07001184 /*
1185 * If we can't get a cq entry, userspace overflowed the
1186 * submission (by quite a lot). Increment the overflow count in
1187 * the ring.
1188 */
1189 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001190 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001191 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001192 WRITE_ONCE(cqe->res, res);
1193 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001194 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001195 WRITE_ONCE(ctx->rings->cq_overflow,
1196 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001197 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001198 if (list_empty(&ctx->cq_overflow_list)) {
1199 set_bit(0, &ctx->sq_check_overflow);
1200 set_bit(0, &ctx->cq_check_overflow);
1201 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001202 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001203 refcount_inc(&req->refs);
1204 req->result = res;
1205 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206 }
1207}
1208
Jens Axboe78e19bb2019-11-06 15:21:34 -07001209static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001210{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001211 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001212 unsigned long flags;
1213
1214 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001215 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216 io_commit_cqring(ctx);
1217 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1218
Jens Axboe8c838782019-03-12 15:48:16 -06001219 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220}
1221
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001222static inline bool io_is_fallback_req(struct io_kiocb *req)
1223{
1224 return req == (struct io_kiocb *)
1225 ((unsigned long) req->ctx->fallback_req & ~1UL);
1226}
1227
1228static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1229{
1230 struct io_kiocb *req;
1231
1232 req = ctx->fallback_req;
1233 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1234 return req;
1235
1236 return NULL;
1237}
1238
Jens Axboe2579f912019-01-09 09:10:43 -07001239static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1240 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001241{
Jens Axboefd6fab22019-03-14 16:30:06 -06001242 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001243 struct io_kiocb *req;
1244
Jens Axboe2579f912019-01-09 09:10:43 -07001245 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001246 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001247 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001248 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001249 } else if (!state->free_reqs) {
1250 size_t sz;
1251 int ret;
1252
1253 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001254 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1255
1256 /*
1257 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1258 * retry single alloc to be on the safe side.
1259 */
1260 if (unlikely(ret <= 0)) {
1261 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1262 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001263 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001264 ret = 1;
1265 }
Jens Axboe2579f912019-01-09 09:10:43 -07001266 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001267 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001268 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001269 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001270 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001271 }
1272
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001273got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001274 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001275 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001276 req->ctx = ctx;
1277 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001278 /* one is dropped after submission, the other at completion */
1279 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001280 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001281 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001282 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001283fallback:
1284 req = io_get_fallback_req(ctx);
1285 if (req)
1286 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001287 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288 return NULL;
1289}
1290
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001291static inline void io_put_file(struct io_kiocb *req, struct file *file,
1292 bool fixed)
1293{
1294 if (fixed)
1295 percpu_ref_put(&req->ctx->file_data->refs);
1296 else
1297 fput(file);
1298}
1299
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001300static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001301{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001302 if (likely(!io_is_fallback_req(req)))
1303 kmem_cache_free(req_cachep, req);
1304 else
1305 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1306}
1307
Jens Axboec6ca97b302019-12-28 12:11:08 -07001308static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001309{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001310 if (req->flags & REQ_F_NEED_CLEANUP)
1311 io_cleanup_req(req);
1312
YueHaibing96fd84d2020-01-07 22:22:44 +08001313 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001314 if (req->file)
1315 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001316
1317 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318}
1319
1320static void __io_free_req(struct io_kiocb *req)
1321{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001322 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001323
Jens Axboefcb323c2019-10-24 12:39:47 -06001324 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001325 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001326 unsigned long flags;
1327
1328 spin_lock_irqsave(&ctx->inflight_lock, flags);
1329 list_del(&req->inflight_entry);
1330 if (waitqueue_active(&ctx->inflight_wait))
1331 wake_up(&ctx->inflight_wait);
1332 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1333 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001334
1335 percpu_ref_put(&req->ctx->refs);
1336 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001337}
1338
Jens Axboec6ca97b302019-12-28 12:11:08 -07001339struct req_batch {
1340 void *reqs[IO_IOPOLL_BATCH];
1341 int to_free;
1342 int need_iter;
1343};
1344
1345static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1346{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001347 int fixed_refs = rb->to_free;
1348
Jens Axboec6ca97b302019-12-28 12:11:08 -07001349 if (!rb->to_free)
1350 return;
1351 if (rb->need_iter) {
1352 int i, inflight = 0;
1353 unsigned long flags;
1354
Jens Axboe10fef4b2020-01-09 07:52:28 -07001355 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001356 for (i = 0; i < rb->to_free; i++) {
1357 struct io_kiocb *req = rb->reqs[i];
1358
Jens Axboe10fef4b2020-01-09 07:52:28 -07001359 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001360 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001361 fixed_refs++;
1362 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001363 if (req->flags & REQ_F_INFLIGHT)
1364 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001365 __io_req_aux_free(req);
1366 }
1367 if (!inflight)
1368 goto do_free;
1369
1370 spin_lock_irqsave(&ctx->inflight_lock, flags);
1371 for (i = 0; i < rb->to_free; i++) {
1372 struct io_kiocb *req = rb->reqs[i];
1373
Jens Axboe10fef4b2020-01-09 07:52:28 -07001374 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001375 list_del(&req->inflight_entry);
1376 if (!--inflight)
1377 break;
1378 }
1379 }
1380 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1381
1382 if (waitqueue_active(&ctx->inflight_wait))
1383 wake_up(&ctx->inflight_wait);
1384 }
1385do_free:
1386 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001387 if (fixed_refs)
1388 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001389 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001390 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001391}
1392
Jackie Liua197f662019-11-08 08:09:12 -07001393static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001394{
Jackie Liua197f662019-11-08 08:09:12 -07001395 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001396 int ret;
1397
Jens Axboe2d283902019-12-04 11:08:05 -07001398 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001399 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001400 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001401 io_commit_cqring(ctx);
1402 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001403 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001404 return true;
1405 }
1406
1407 return false;
1408}
1409
Jens Axboeba816ad2019-09-28 11:36:45 -06001410static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001411{
Jens Axboe2665abf2019-11-05 12:40:47 -07001412 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001413 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001414
Jens Axboe4d7dd462019-11-20 13:03:52 -07001415 /* Already got next link */
1416 if (req->flags & REQ_F_LINK_NEXT)
1417 return;
1418
Jens Axboe9e645e112019-05-10 16:07:28 -06001419 /*
1420 * The list should never be empty when we are called here. But could
1421 * potentially happen if the chain is messed up, check to be on the
1422 * safe side.
1423 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001424 while (!list_empty(&req->link_list)) {
1425 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1426 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001427
Pavel Begunkov44932332019-12-05 16:16:35 +03001428 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1429 (nxt->flags & REQ_F_TIMEOUT))) {
1430 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001431 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001432 req->flags &= ~REQ_F_LINK_TIMEOUT;
1433 continue;
1434 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001435
Pavel Begunkov44932332019-12-05 16:16:35 +03001436 list_del_init(&req->link_list);
1437 if (!list_empty(&nxt->link_list))
1438 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001439 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001440 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001441 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001442
Jens Axboe4d7dd462019-11-20 13:03:52 -07001443 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001444 if (wake_ev)
1445 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001446}
1447
1448/*
1449 * Called if REQ_F_LINK is set, and we fail the head request
1450 */
1451static void io_fail_links(struct io_kiocb *req)
1452{
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001454 unsigned long flags;
1455
1456 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001457
1458 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001459 struct io_kiocb *link = list_first_entry(&req->link_list,
1460 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001461
Pavel Begunkov44932332019-12-05 16:16:35 +03001462 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001463 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001464
1465 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001466 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001467 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001468 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001469 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001470 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001471 }
Jens Axboe5d960722019-11-19 15:31:28 -07001472 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001473 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001474
1475 io_commit_cqring(ctx);
1476 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1477 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001478}
1479
Jens Axboe4d7dd462019-11-20 13:03:52 -07001480static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001481{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001482 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001483 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001484
Jens Axboe9e645e112019-05-10 16:07:28 -06001485 /*
1486 * If LINK is set, we have dependent requests in this chain. If we
1487 * didn't fail this request, queue the first one up, moving any other
1488 * dependencies to the next request. In case of failure, fail the rest
1489 * of the chain.
1490 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001491 if (req->flags & REQ_F_FAIL_LINK) {
1492 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001493 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1494 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001495 struct io_ring_ctx *ctx = req->ctx;
1496 unsigned long flags;
1497
1498 /*
1499 * If this is a timeout link, we could be racing with the
1500 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001501 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001502 */
1503 spin_lock_irqsave(&ctx->completion_lock, flags);
1504 io_req_link_next(req, nxt);
1505 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1506 } else {
1507 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001508 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001509}
Jens Axboe9e645e112019-05-10 16:07:28 -06001510
Jackie Liuc69f8db2019-11-09 11:00:08 +08001511static void io_free_req(struct io_kiocb *req)
1512{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001513 struct io_kiocb *nxt = NULL;
1514
1515 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001516 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001517
1518 if (nxt)
1519 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001520}
1521
Jens Axboeba816ad2019-09-28 11:36:45 -06001522/*
1523 * Drop reference to request, return next in chain (if there is one) if this
1524 * was the last reference to this request.
1525 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001526__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001527static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001528{
Jens Axboe2a44f462020-02-25 13:25:41 -07001529 if (refcount_dec_and_test(&req->refs)) {
1530 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001531 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001532 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001533}
1534
Jens Axboe2b188cc2019-01-07 10:46:33 -07001535static void io_put_req(struct io_kiocb *req)
1536{
Jens Axboedef596e2019-01-09 08:59:42 -07001537 if (refcount_dec_and_test(&req->refs))
1538 io_free_req(req);
1539}
1540
Jens Axboe978db572019-11-14 22:39:04 -07001541/*
1542 * Must only be used if we don't need to care about links, usually from
1543 * within the completion handling itself.
1544 */
1545static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001546{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001547 /* drop both submit and complete references */
1548 if (refcount_sub_and_test(2, &req->refs))
1549 __io_free_req(req);
1550}
1551
Jens Axboe978db572019-11-14 22:39:04 -07001552static void io_double_put_req(struct io_kiocb *req)
1553{
1554 /* drop both submit and complete references */
1555 if (refcount_sub_and_test(2, &req->refs))
1556 io_free_req(req);
1557}
1558
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001559static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001560{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001561 struct io_rings *rings = ctx->rings;
1562
Jens Axboead3eb2c2019-12-18 17:12:20 -07001563 if (test_bit(0, &ctx->cq_check_overflow)) {
1564 /*
1565 * noflush == true is from the waitqueue handler, just ensure
1566 * we wake up the task, and the next invocation will flush the
1567 * entries. We cannot safely to it from here.
1568 */
1569 if (noflush && !list_empty(&ctx->cq_overflow_list))
1570 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001571
Jens Axboead3eb2c2019-12-18 17:12:20 -07001572 io_cqring_overflow_flush(ctx, false);
1573 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001574
Jens Axboea3a0e432019-08-20 11:03:11 -06001575 /* See comment at the top of this file */
1576 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001577 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001578}
1579
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001580static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1581{
1582 struct io_rings *rings = ctx->rings;
1583
1584 /* make sure SQ entry isn't read before tail */
1585 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1586}
1587
Jens Axboe8237e042019-12-28 10:48:22 -07001588static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001589{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001590 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1591 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001592
Jens Axboec6ca97b302019-12-28 12:11:08 -07001593 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1594 rb->need_iter++;
1595
1596 rb->reqs[rb->to_free++] = req;
1597 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1598 io_free_req_many(req->ctx, rb);
1599 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001600}
1601
Jens Axboedef596e2019-01-09 08:59:42 -07001602/*
1603 * Find and free completed poll iocbs
1604 */
1605static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1606 struct list_head *done)
1607{
Jens Axboe8237e042019-12-28 10:48:22 -07001608 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001609 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001610
Jens Axboec6ca97b302019-12-28 12:11:08 -07001611 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001612 while (!list_empty(done)) {
1613 req = list_first_entry(done, struct io_kiocb, list);
1614 list_del(&req->list);
1615
Jens Axboe78e19bb2019-11-06 15:21:34 -07001616 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001617 (*nr_events)++;
1618
Jens Axboe8237e042019-12-28 10:48:22 -07001619 if (refcount_dec_and_test(&req->refs) &&
1620 !io_req_multi_free(&rb, req))
1621 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001622 }
Jens Axboedef596e2019-01-09 08:59:42 -07001623
Jens Axboe09bb8392019-03-13 12:39:28 -06001624 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001625 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001626}
1627
1628static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1629 long min)
1630{
1631 struct io_kiocb *req, *tmp;
1632 LIST_HEAD(done);
1633 bool spin;
1634 int ret;
1635
1636 /*
1637 * Only spin for completions if we don't have multiple devices hanging
1638 * off our complete list, and we're under the requested amount.
1639 */
1640 spin = !ctx->poll_multi_file && *nr_events < min;
1641
1642 ret = 0;
1643 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001644 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001645
1646 /*
1647 * Move completed entries to our local list. If we find a
1648 * request that requires polling, break out and complete
1649 * the done list first, if we have entries there.
1650 */
1651 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1652 list_move_tail(&req->list, &done);
1653 continue;
1654 }
1655 if (!list_empty(&done))
1656 break;
1657
1658 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1659 if (ret < 0)
1660 break;
1661
1662 if (ret && spin)
1663 spin = false;
1664 ret = 0;
1665 }
1666
1667 if (!list_empty(&done))
1668 io_iopoll_complete(ctx, nr_events, &done);
1669
1670 return ret;
1671}
1672
1673/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001674 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001675 * non-spinning poll check - we'll still enter the driver poll loop, but only
1676 * as a non-spinning completion check.
1677 */
1678static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1679 long min)
1680{
Jens Axboe08f54392019-08-21 22:19:11 -06001681 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001682 int ret;
1683
1684 ret = io_do_iopoll(ctx, nr_events, min);
1685 if (ret < 0)
1686 return ret;
1687 if (!min || *nr_events >= min)
1688 return 0;
1689 }
1690
1691 return 1;
1692}
1693
1694/*
1695 * We can't just wait for polled events to come to us, we have to actively
1696 * find and complete them.
1697 */
1698static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1699{
1700 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1701 return;
1702
1703 mutex_lock(&ctx->uring_lock);
1704 while (!list_empty(&ctx->poll_list)) {
1705 unsigned int nr_events = 0;
1706
1707 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001708
1709 /*
1710 * Ensure we allow local-to-the-cpu processing to take place,
1711 * in this case we need to ensure that we reap all events.
1712 */
1713 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001714 }
1715 mutex_unlock(&ctx->uring_lock);
1716}
1717
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001718static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1719 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001720{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001721 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001722
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001723 /*
1724 * We disallow the app entering submit/complete with polling, but we
1725 * still need to lock the ring to prevent racing with polled issue
1726 * that got punted to a workqueue.
1727 */
1728 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001729 do {
1730 int tmin = 0;
1731
Jens Axboe500f9fb2019-08-19 12:15:59 -06001732 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001733 * Don't enter poll loop if we already have events pending.
1734 * If we do, we can potentially be spinning for commands that
1735 * already triggered a CQE (eg in error).
1736 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001737 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001738 break;
1739
1740 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001741 * If a submit got punted to a workqueue, we can have the
1742 * application entering polling for a command before it gets
1743 * issued. That app will hold the uring_lock for the duration
1744 * of the poll right here, so we need to take a breather every
1745 * now and then to ensure that the issue has a chance to add
1746 * the poll to the issued list. Otherwise we can spin here
1747 * forever, while the workqueue is stuck trying to acquire the
1748 * very same mutex.
1749 */
1750 if (!(++iters & 7)) {
1751 mutex_unlock(&ctx->uring_lock);
1752 mutex_lock(&ctx->uring_lock);
1753 }
1754
Jens Axboedef596e2019-01-09 08:59:42 -07001755 if (*nr_events < min)
1756 tmin = min - *nr_events;
1757
1758 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1759 if (ret <= 0)
1760 break;
1761 ret = 0;
1762 } while (min && !*nr_events && !need_resched());
1763
Jens Axboe500f9fb2019-08-19 12:15:59 -06001764 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001765 return ret;
1766}
1767
Jens Axboe491381ce2019-10-17 09:20:46 -06001768static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001769{
Jens Axboe491381ce2019-10-17 09:20:46 -06001770 /*
1771 * Tell lockdep we inherited freeze protection from submission
1772 * thread.
1773 */
1774 if (req->flags & REQ_F_ISREG) {
1775 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776
Jens Axboe491381ce2019-10-17 09:20:46 -06001777 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001778 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001779 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001780}
1781
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001782static inline void req_set_fail_links(struct io_kiocb *req)
1783{
1784 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1785 req->flags |= REQ_F_FAIL_LINK;
1786}
1787
Jens Axboeba816ad2019-09-28 11:36:45 -06001788static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001789{
Jens Axboe9adbd452019-12-20 08:45:55 -07001790 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791
Jens Axboe491381ce2019-10-17 09:20:46 -06001792 if (kiocb->ki_flags & IOCB_WRITE)
1793 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001795 if (res != req->result)
1796 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001797 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001798}
1799
1800static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1801{
Jens Axboe9adbd452019-12-20 08:45:55 -07001802 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001803
1804 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001805 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001806}
1807
Jens Axboeba816ad2019-09-28 11:36:45 -06001808static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1809{
Jens Axboe9adbd452019-12-20 08:45:55 -07001810 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001811 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001812
1813 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001814 io_put_req_find_next(req, &nxt);
1815
1816 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001817}
1818
Jens Axboedef596e2019-01-09 08:59:42 -07001819static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1820{
Jens Axboe9adbd452019-12-20 08:45:55 -07001821 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001822
Jens Axboe491381ce2019-10-17 09:20:46 -06001823 if (kiocb->ki_flags & IOCB_WRITE)
1824 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001825
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001826 if (res != req->result)
1827 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001828 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001829 if (res != -EAGAIN)
1830 req->flags |= REQ_F_IOPOLL_COMPLETED;
1831}
1832
1833/*
1834 * After the iocb has been issued, it's safe to be found on the poll list.
1835 * Adding the kiocb to the list AFTER submission ensures that we don't
1836 * find it from a io_iopoll_getevents() thread before the issuer is done
1837 * accessing the kiocb cookie.
1838 */
1839static void io_iopoll_req_issued(struct io_kiocb *req)
1840{
1841 struct io_ring_ctx *ctx = req->ctx;
1842
1843 /*
1844 * Track whether we have multiple files in our lists. This will impact
1845 * how we do polling eventually, not spinning if we're on potentially
1846 * different devices.
1847 */
1848 if (list_empty(&ctx->poll_list)) {
1849 ctx->poll_multi_file = false;
1850 } else if (!ctx->poll_multi_file) {
1851 struct io_kiocb *list_req;
1852
1853 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1854 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001855 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001856 ctx->poll_multi_file = true;
1857 }
1858
1859 /*
1860 * For fast devices, IO may have already completed. If it has, add
1861 * it to the front so we find it first.
1862 */
1863 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1864 list_add(&req->list, &ctx->poll_list);
1865 else
1866 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001867
1868 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1869 wq_has_sleeper(&ctx->sqo_wait))
1870 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001871}
1872
Jens Axboe3d6770f2019-04-13 11:50:54 -06001873static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001874{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001875 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001876 int diff = state->has_refs - state->used_refs;
1877
1878 if (diff)
1879 fput_many(state->file, diff);
1880 state->file = NULL;
1881 }
1882}
1883
1884/*
1885 * Get as many references to a file as we have IOs left in this submission,
1886 * assuming most submissions are for one file, or at least that each file
1887 * has more than one submission.
1888 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001889static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001890{
1891 if (!state)
1892 return fget(fd);
1893
1894 if (state->file) {
1895 if (state->fd == fd) {
1896 state->used_refs++;
1897 state->ios_left--;
1898 return state->file;
1899 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001900 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001901 }
1902 state->file = fget_many(fd, state->ios_left);
1903 if (!state->file)
1904 return NULL;
1905
1906 state->fd = fd;
1907 state->has_refs = state->ios_left;
1908 state->used_refs = 1;
1909 state->ios_left--;
1910 return state->file;
1911}
1912
Jens Axboe2b188cc2019-01-07 10:46:33 -07001913/*
1914 * If we tracked the file through the SCM inflight mechanism, we could support
1915 * any file. For now, just ensure that anything potentially problematic is done
1916 * inline.
1917 */
1918static bool io_file_supports_async(struct file *file)
1919{
1920 umode_t mode = file_inode(file)->i_mode;
1921
Jens Axboe10d59342019-12-09 20:16:22 -07001922 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923 return true;
1924 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1925 return true;
1926
1927 return false;
1928}
1929
Jens Axboe3529d8c2019-12-19 18:24:38 -07001930static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1931 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001932{
Jens Axboedef596e2019-01-09 08:59:42 -07001933 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001934 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001935 unsigned ioprio;
1936 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001937
Jens Axboe491381ce2019-10-17 09:20:46 -06001938 if (S_ISREG(file_inode(req->file)->i_mode))
1939 req->flags |= REQ_F_ISREG;
1940
Jens Axboe2b188cc2019-01-07 10:46:33 -07001941 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001942 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1943 req->flags |= REQ_F_CUR_POS;
1944 kiocb->ki_pos = req->file->f_pos;
1945 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001946 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001947 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1948 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1949 if (unlikely(ret))
1950 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001951
1952 ioprio = READ_ONCE(sqe->ioprio);
1953 if (ioprio) {
1954 ret = ioprio_check_cap(ioprio);
1955 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001956 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001957
1958 kiocb->ki_ioprio = ioprio;
1959 } else
1960 kiocb->ki_ioprio = get_current_ioprio();
1961
Stefan Bühler8449eed2019-04-27 20:34:19 +02001962 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001963 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1964 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001965 req->flags |= REQ_F_NOWAIT;
1966
1967 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001968 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001969
Jens Axboedef596e2019-01-09 08:59:42 -07001970 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001971 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1972 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001973 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001974
Jens Axboedef596e2019-01-09 08:59:42 -07001975 kiocb->ki_flags |= IOCB_HIPRI;
1976 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001977 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001978 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001979 if (kiocb->ki_flags & IOCB_HIPRI)
1980 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001981 kiocb->ki_complete = io_complete_rw;
1982 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001983
Jens Axboe3529d8c2019-12-19 18:24:38 -07001984 req->rw.addr = READ_ONCE(sqe->addr);
1985 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001986 /* we own ->private, reuse it for the buffer index */
1987 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001988 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001989 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001990}
1991
1992static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1993{
1994 switch (ret) {
1995 case -EIOCBQUEUED:
1996 break;
1997 case -ERESTARTSYS:
1998 case -ERESTARTNOINTR:
1999 case -ERESTARTNOHAND:
2000 case -ERESTART_RESTARTBLOCK:
2001 /*
2002 * We can't just restart the syscall, since previously
2003 * submitted sqes may already be in progress. Just fail this
2004 * IO with EINTR.
2005 */
2006 ret = -EINTR;
2007 /* fall through */
2008 default:
2009 kiocb->ki_complete(kiocb, ret, 0);
2010 }
2011}
2012
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002013static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt)
Jens Axboeba816ad2019-09-28 11:36:45 -06002014{
Jens Axboeba042912019-12-25 16:33:42 -07002015 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2016
2017 if (req->flags & REQ_F_CUR_POS)
2018 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002019 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06002020 *nxt = __io_complete_rw(kiocb, ret);
2021 else
2022 io_rw_done(kiocb, ret);
2023}
2024
Jens Axboe9adbd452019-12-20 08:45:55 -07002025static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002026 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002027{
Jens Axboe9adbd452019-12-20 08:45:55 -07002028 struct io_ring_ctx *ctx = req->ctx;
2029 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002030 struct io_mapped_ubuf *imu;
2031 unsigned index, buf_index;
2032 size_t offset;
2033 u64 buf_addr;
2034
2035 /* attempt to use fixed buffers without having provided iovecs */
2036 if (unlikely(!ctx->user_bufs))
2037 return -EFAULT;
2038
Jens Axboe9adbd452019-12-20 08:45:55 -07002039 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002040 if (unlikely(buf_index >= ctx->nr_user_bufs))
2041 return -EFAULT;
2042
2043 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2044 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002045 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002046
2047 /* overflow */
2048 if (buf_addr + len < buf_addr)
2049 return -EFAULT;
2050 /* not inside the mapped region */
2051 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2052 return -EFAULT;
2053
2054 /*
2055 * May not be a start of buffer, set size appropriately
2056 * and advance us to the beginning.
2057 */
2058 offset = buf_addr - imu->ubuf;
2059 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002060
2061 if (offset) {
2062 /*
2063 * Don't use iov_iter_advance() here, as it's really slow for
2064 * using the latter parts of a big fixed buffer - it iterates
2065 * over each segment manually. We can cheat a bit here, because
2066 * we know that:
2067 *
2068 * 1) it's a BVEC iter, we set it up
2069 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2070 * first and last bvec
2071 *
2072 * So just find our index, and adjust the iterator afterwards.
2073 * If the offset is within the first bvec (or the whole first
2074 * bvec, just use iov_iter_advance(). This makes it easier
2075 * since we can just skip the first segment, which may not
2076 * be PAGE_SIZE aligned.
2077 */
2078 const struct bio_vec *bvec = imu->bvec;
2079
2080 if (offset <= bvec->bv_len) {
2081 iov_iter_advance(iter, offset);
2082 } else {
2083 unsigned long seg_skip;
2084
2085 /* skip first vec */
2086 offset -= bvec->bv_len;
2087 seg_skip = 1 + (offset >> PAGE_SHIFT);
2088
2089 iter->bvec = bvec + seg_skip;
2090 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002091 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002092 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002093 }
2094 }
2095
Jens Axboe5e559562019-11-13 16:12:46 -07002096 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002097}
2098
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002099static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2100 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002101{
Jens Axboe9adbd452019-12-20 08:45:55 -07002102 void __user *buf = u64_to_user_ptr(req->rw.addr);
2103 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002104 u8 opcode;
2105
Jens Axboed625c6e2019-12-17 19:53:05 -07002106 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002107 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002108 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002109 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002110 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002111
Jens Axboe9adbd452019-12-20 08:45:55 -07002112 /* buffer index only valid with fixed read/write */
2113 if (req->rw.kiocb.private)
2114 return -EINVAL;
2115
Jens Axboe3a6820f2019-12-22 15:19:35 -07002116 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2117 ssize_t ret;
2118 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2119 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002120 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002121 }
2122
Jens Axboef67676d2019-12-02 11:03:47 -07002123 if (req->io) {
2124 struct io_async_rw *iorw = &req->io->rw;
2125
2126 *iovec = iorw->iov;
2127 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2128 if (iorw->iov == iorw->fast_iov)
2129 *iovec = NULL;
2130 return iorw->size;
2131 }
2132
Jens Axboe2b188cc2019-01-07 10:46:33 -07002133#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002134 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002135 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2136 iovec, iter);
2137#endif
2138
2139 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2140}
2141
Jens Axboe32960612019-09-23 11:05:34 -06002142/*
2143 * For files that don't have ->read_iter() and ->write_iter(), handle them
2144 * by looping over ->read() or ->write() manually.
2145 */
2146static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2147 struct iov_iter *iter)
2148{
2149 ssize_t ret = 0;
2150
2151 /*
2152 * Don't support polled IO through this interface, and we can't
2153 * support non-blocking either. For the latter, this just causes
2154 * the kiocb to be handled from an async context.
2155 */
2156 if (kiocb->ki_flags & IOCB_HIPRI)
2157 return -EOPNOTSUPP;
2158 if (kiocb->ki_flags & IOCB_NOWAIT)
2159 return -EAGAIN;
2160
2161 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002162 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002163 ssize_t nr;
2164
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002165 if (!iov_iter_is_bvec(iter)) {
2166 iovec = iov_iter_iovec(iter);
2167 } else {
2168 /* fixed buffers import bvec */
2169 iovec.iov_base = kmap(iter->bvec->bv_page)
2170 + iter->iov_offset;
2171 iovec.iov_len = min(iter->count,
2172 iter->bvec->bv_len - iter->iov_offset);
2173 }
2174
Jens Axboe32960612019-09-23 11:05:34 -06002175 if (rw == READ) {
2176 nr = file->f_op->read(file, iovec.iov_base,
2177 iovec.iov_len, &kiocb->ki_pos);
2178 } else {
2179 nr = file->f_op->write(file, iovec.iov_base,
2180 iovec.iov_len, &kiocb->ki_pos);
2181 }
2182
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002183 if (iov_iter_is_bvec(iter))
2184 kunmap(iter->bvec->bv_page);
2185
Jens Axboe32960612019-09-23 11:05:34 -06002186 if (nr < 0) {
2187 if (!ret)
2188 ret = nr;
2189 break;
2190 }
2191 ret += nr;
2192 if (nr != iovec.iov_len)
2193 break;
2194 iov_iter_advance(iter, nr);
2195 }
2196
2197 return ret;
2198}
2199
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002200static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002201 struct iovec *iovec, struct iovec *fast_iov,
2202 struct iov_iter *iter)
2203{
2204 req->io->rw.nr_segs = iter->nr_segs;
2205 req->io->rw.size = io_size;
2206 req->io->rw.iov = iovec;
2207 if (!req->io->rw.iov) {
2208 req->io->rw.iov = req->io->rw.fast_iov;
2209 memcpy(req->io->rw.iov, fast_iov,
2210 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002211 } else {
2212 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002213 }
2214}
2215
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002216static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002217{
Jens Axboed3656342019-12-18 09:50:26 -07002218 if (!io_op_defs[req->opcode].async_ctx)
2219 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002220 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002221 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002222}
2223
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002224static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2225 struct iovec *iovec, struct iovec *fast_iov,
2226 struct iov_iter *iter)
2227{
Jens Axboe980ad262020-01-24 23:08:54 -07002228 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002229 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002230 if (!req->io) {
2231 if (io_alloc_async_ctx(req))
2232 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002233
Jens Axboe5d204bc2020-01-31 12:06:52 -07002234 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2235 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002236 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002237}
2238
Jens Axboe3529d8c2019-12-19 18:24:38 -07002239static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2240 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002241{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002242 struct io_async_ctx *io;
2243 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002244 ssize_t ret;
2245
Jens Axboe3529d8c2019-12-19 18:24:38 -07002246 ret = io_prep_rw(req, sqe, force_nonblock);
2247 if (ret)
2248 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002249
Jens Axboe3529d8c2019-12-19 18:24:38 -07002250 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2251 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002252
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002253 /* either don't need iovec imported or already have it */
2254 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002255 return 0;
2256
2257 io = req->io;
2258 io->rw.iov = io->rw.fast_iov;
2259 req->io = NULL;
2260 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2261 req->io = io;
2262 if (ret < 0)
2263 return ret;
2264
2265 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2266 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002267}
2268
Pavel Begunkov267bc902019-11-07 01:41:08 +03002269static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002270 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002271{
2272 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002273 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002274 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002275 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002276 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002277
Jens Axboe3529d8c2019-12-19 18:24:38 -07002278 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002279 if (ret < 0)
2280 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281
Jens Axboefd6c2e42019-12-18 12:19:41 -07002282 /* Ensure we clear previously set non-block flag */
2283 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002284 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002285
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002286 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002287 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002288 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002289 req->result = io_size;
2290
2291 /*
2292 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2293 * we know to async punt it even if it was opened O_NONBLOCK
2294 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002295 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002296 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002297
Jens Axboe31b51512019-01-18 22:56:34 -07002298 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002299 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002300 if (!ret) {
2301 ssize_t ret2;
2302
Jens Axboe9adbd452019-12-20 08:45:55 -07002303 if (req->file->f_op->read_iter)
2304 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002305 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002306 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002307
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002308 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002309 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002310 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002311 } else {
2312copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002313 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002314 inline_vecs, &iter);
2315 if (ret)
2316 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002317 /* any defer here is final, must blocking retry */
2318 if (!(req->flags & REQ_F_NOWAIT))
2319 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002320 return -EAGAIN;
2321 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002322 }
Jens Axboef67676d2019-12-02 11:03:47 -07002323out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002324 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002325 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326 return ret;
2327}
2328
Jens Axboe3529d8c2019-12-19 18:24:38 -07002329static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2330 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002331{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002332 struct io_async_ctx *io;
2333 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002334 ssize_t ret;
2335
Jens Axboe3529d8c2019-12-19 18:24:38 -07002336 ret = io_prep_rw(req, sqe, force_nonblock);
2337 if (ret)
2338 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002339
Jens Axboe3529d8c2019-12-19 18:24:38 -07002340 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2341 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002342
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002343 /* either don't need iovec imported or already have it */
2344 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002345 return 0;
2346
2347 io = req->io;
2348 io->rw.iov = io->rw.fast_iov;
2349 req->io = NULL;
2350 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2351 req->io = io;
2352 if (ret < 0)
2353 return ret;
2354
2355 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2356 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002357}
2358
Pavel Begunkov267bc902019-11-07 01:41:08 +03002359static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002360 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002361{
2362 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002363 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002364 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002365 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002366 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367
Jens Axboe3529d8c2019-12-19 18:24:38 -07002368 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002369 if (ret < 0)
2370 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002371
Jens Axboefd6c2e42019-12-18 12:19:41 -07002372 /* Ensure we clear previously set non-block flag */
2373 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002374 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002375
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002376 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002377 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002378 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002379 req->result = io_size;
2380
2381 /*
2382 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2383 * we know to async punt it even if it was opened O_NONBLOCK
2384 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002385 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002386 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002387
Jens Axboe10d59342019-12-09 20:16:22 -07002388 /* file path doesn't support NOWAIT for non-direct_IO */
2389 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2390 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002391 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002392
Jens Axboe31b51512019-01-18 22:56:34 -07002393 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002394 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002395 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002396 ssize_t ret2;
2397
Jens Axboe2b188cc2019-01-07 10:46:33 -07002398 /*
2399 * Open-code file_start_write here to grab freeze protection,
2400 * which will be released by another thread in
2401 * io_complete_rw(). Fool lockdep by telling it the lock got
2402 * released so that it doesn't complain about the held lock when
2403 * we return to userspace.
2404 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002405 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002406 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002407 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002408 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002409 SB_FREEZE_WRITE);
2410 }
2411 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002412
Jens Axboe9adbd452019-12-20 08:45:55 -07002413 if (req->file->f_op->write_iter)
2414 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002415 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002416 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002417 /*
2418 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2419 * retry them without IOCB_NOWAIT.
2420 */
2421 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2422 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002423 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002424 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002425 } else {
2426copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002427 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002428 inline_vecs, &iter);
2429 if (ret)
2430 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002431 /* any defer here is final, must blocking retry */
2432 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002433 return -EAGAIN;
2434 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002435 }
Jens Axboe31b51512019-01-18 22:56:34 -07002436out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002437 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002438 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002439 return ret;
2440}
2441
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002442static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2443{
2444 struct io_splice* sp = &req->splice;
2445 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2446 int ret;
2447
2448 if (req->flags & REQ_F_NEED_CLEANUP)
2449 return 0;
2450
2451 sp->file_in = NULL;
2452 sp->off_in = READ_ONCE(sqe->splice_off_in);
2453 sp->off_out = READ_ONCE(sqe->off);
2454 sp->len = READ_ONCE(sqe->len);
2455 sp->flags = READ_ONCE(sqe->splice_flags);
2456
2457 if (unlikely(sp->flags & ~valid_flags))
2458 return -EINVAL;
2459
2460 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2461 (sp->flags & SPLICE_F_FD_IN_FIXED));
2462 if (ret)
2463 return ret;
2464 req->flags |= REQ_F_NEED_CLEANUP;
2465
2466 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2467 req->work.flags |= IO_WQ_WORK_UNBOUND;
2468
2469 return 0;
2470}
2471
2472static bool io_splice_punt(struct file *file)
2473{
2474 if (get_pipe_info(file))
2475 return false;
2476 if (!io_file_supports_async(file))
2477 return true;
2478 return !(file->f_mode & O_NONBLOCK);
2479}
2480
2481static int io_splice(struct io_kiocb *req, struct io_kiocb **nxt,
2482 bool force_nonblock)
2483{
2484 struct io_splice *sp = &req->splice;
2485 struct file *in = sp->file_in;
2486 struct file *out = sp->file_out;
2487 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2488 loff_t *poff_in, *poff_out;
2489 long ret;
2490
2491 if (force_nonblock) {
2492 if (io_splice_punt(in) || io_splice_punt(out))
2493 return -EAGAIN;
2494 flags |= SPLICE_F_NONBLOCK;
2495 }
2496
2497 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2498 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2499 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2500 if (force_nonblock && ret == -EAGAIN)
2501 return -EAGAIN;
2502
2503 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2504 req->flags &= ~REQ_F_NEED_CLEANUP;
2505
2506 io_cqring_add_event(req, ret);
2507 if (ret != sp->len)
2508 req_set_fail_links(req);
2509 io_put_req_find_next(req, nxt);
2510 return 0;
2511}
2512
Jens Axboe2b188cc2019-01-07 10:46:33 -07002513/*
2514 * IORING_OP_NOP just posts a completion event, nothing else.
2515 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002516static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002517{
2518 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002519
Jens Axboedef596e2019-01-09 08:59:42 -07002520 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2521 return -EINVAL;
2522
Jens Axboe78e19bb2019-11-06 15:21:34 -07002523 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002524 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002525 return 0;
2526}
2527
Jens Axboe3529d8c2019-12-19 18:24:38 -07002528static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002529{
Jens Axboe6b063142019-01-10 22:13:58 -07002530 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002531
Jens Axboe09bb8392019-03-13 12:39:28 -06002532 if (!req->file)
2533 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002534
Jens Axboe6b063142019-01-10 22:13:58 -07002535 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002536 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002537 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002538 return -EINVAL;
2539
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002540 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2541 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2542 return -EINVAL;
2543
2544 req->sync.off = READ_ONCE(sqe->off);
2545 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002546 return 0;
2547}
2548
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002549static bool io_req_cancelled(struct io_kiocb *req)
2550{
2551 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2552 req_set_fail_links(req);
2553 io_cqring_add_event(req, -ECANCELED);
2554 io_put_req(req);
2555 return true;
2556 }
2557
2558 return false;
2559}
2560
Jens Axboe78912932020-01-14 22:09:06 -07002561static void io_link_work_cb(struct io_wq_work **workptr)
2562{
2563 struct io_wq_work *work = *workptr;
2564 struct io_kiocb *link = work->data;
2565
2566 io_queue_linked_timeout(link);
Pavel Begunkov5eae8612020-02-28 10:36:38 +03002567 io_wq_submit_work(workptr);
Jens Axboe78912932020-01-14 22:09:06 -07002568}
2569
2570static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2571{
2572 struct io_kiocb *link;
2573
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +03002574 io_prep_next_work(nxt, &link);
Jens Axboe78912932020-01-14 22:09:06 -07002575 *workptr = &nxt->work;
2576 if (link) {
Jens Axboe78912932020-01-14 22:09:06 -07002577 nxt->work.func = io_link_work_cb;
2578 nxt->work.data = link;
2579 }
2580}
2581
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002582static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002583{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002584 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002585 int ret;
2586
Jens Axboe9adbd452019-12-20 08:45:55 -07002587 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002588 end > 0 ? end : LLONG_MAX,
2589 req->sync.flags & IORING_FSYNC_DATASYNC);
2590 if (ret < 0)
2591 req_set_fail_links(req);
2592 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002593 io_put_req_find_next(req, nxt);
2594}
2595
2596static void io_fsync_finish(struct io_wq_work **workptr)
2597{
2598 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2599 struct io_kiocb *nxt = NULL;
2600
2601 if (io_req_cancelled(req))
2602 return;
2603 __io_fsync(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002604 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002605 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002606}
2607
Jens Axboefc4df992019-12-10 14:38:45 -07002608static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2609 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002610{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002611 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002612 if (force_nonblock) {
2613 io_put_req(req);
2614 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002615 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002616 }
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002617 __io_fsync(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002618 return 0;
2619}
2620
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002621static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboed63d1b52019-12-10 10:38:56 -07002622{
Jens Axboed63d1b52019-12-10 10:38:56 -07002623 int ret;
2624
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002625 if (io_req_cancelled(req))
2626 return;
2627
Jens Axboed63d1b52019-12-10 10:38:56 -07002628 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2629 req->sync.len);
2630 if (ret < 0)
2631 req_set_fail_links(req);
2632 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002633 io_put_req_find_next(req, nxt);
2634}
2635
2636static void io_fallocate_finish(struct io_wq_work **workptr)
2637{
2638 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2639 struct io_kiocb *nxt = NULL;
2640
2641 __io_fallocate(req, &nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002642 if (nxt)
2643 io_wq_assign_next(workptr, nxt);
2644}
2645
2646static int io_fallocate_prep(struct io_kiocb *req,
2647 const struct io_uring_sqe *sqe)
2648{
2649 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2650 return -EINVAL;
2651
2652 req->sync.off = READ_ONCE(sqe->off);
2653 req->sync.len = READ_ONCE(sqe->addr);
2654 req->sync.mode = READ_ONCE(sqe->len);
2655 return 0;
2656}
2657
2658static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2659 bool force_nonblock)
2660{
Jens Axboed63d1b52019-12-10 10:38:56 -07002661 /* fallocate always requiring blocking context */
2662 if (force_nonblock) {
2663 io_put_req(req);
2664 req->work.func = io_fallocate_finish;
2665 return -EAGAIN;
2666 }
2667
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002668 __io_fallocate(req, nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002669 return 0;
2670}
2671
Jens Axboe15b71ab2019-12-11 11:20:36 -07002672static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2673{
Jens Axboef8748882020-01-08 17:47:02 -07002674 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002675 int ret;
2676
2677 if (sqe->ioprio || sqe->buf_index)
2678 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002679 if (sqe->flags & IOSQE_FIXED_FILE)
2680 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002681 if (req->flags & REQ_F_NEED_CLEANUP)
2682 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002683
2684 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002685 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002686 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002687 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002688
Jens Axboef8748882020-01-08 17:47:02 -07002689 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002690 if (IS_ERR(req->open.filename)) {
2691 ret = PTR_ERR(req->open.filename);
2692 req->open.filename = NULL;
2693 return ret;
2694 }
2695
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002696 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002697 return 0;
2698}
2699
Jens Axboecebdb982020-01-08 17:59:24 -07002700static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2701{
2702 struct open_how __user *how;
2703 const char __user *fname;
2704 size_t len;
2705 int ret;
2706
2707 if (sqe->ioprio || sqe->buf_index)
2708 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002709 if (sqe->flags & IOSQE_FIXED_FILE)
2710 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002711 if (req->flags & REQ_F_NEED_CLEANUP)
2712 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002713
2714 req->open.dfd = READ_ONCE(sqe->fd);
2715 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2716 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2717 len = READ_ONCE(sqe->len);
2718
2719 if (len < OPEN_HOW_SIZE_VER0)
2720 return -EINVAL;
2721
2722 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2723 len);
2724 if (ret)
2725 return ret;
2726
2727 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2728 req->open.how.flags |= O_LARGEFILE;
2729
2730 req->open.filename = getname(fname);
2731 if (IS_ERR(req->open.filename)) {
2732 ret = PTR_ERR(req->open.filename);
2733 req->open.filename = NULL;
2734 return ret;
2735 }
2736
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002737 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002738 return 0;
2739}
2740
2741static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2742 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002743{
2744 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002745 struct file *file;
2746 int ret;
2747
Jens Axboef86cd202020-01-29 13:46:44 -07002748 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002749 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002750
Jens Axboecebdb982020-01-08 17:59:24 -07002751 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002752 if (ret)
2753 goto err;
2754
Jens Axboecebdb982020-01-08 17:59:24 -07002755 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002756 if (ret < 0)
2757 goto err;
2758
2759 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2760 if (IS_ERR(file)) {
2761 put_unused_fd(ret);
2762 ret = PTR_ERR(file);
2763 } else {
2764 fsnotify_open(file);
2765 fd_install(ret, file);
2766 }
2767err:
2768 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002769 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002770 if (ret < 0)
2771 req_set_fail_links(req);
2772 io_cqring_add_event(req, ret);
2773 io_put_req_find_next(req, nxt);
2774 return 0;
2775}
2776
Jens Axboecebdb982020-01-08 17:59:24 -07002777static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2778 bool force_nonblock)
2779{
2780 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2781 return io_openat2(req, nxt, force_nonblock);
2782}
2783
Jens Axboe3e4827b2020-01-08 15:18:09 -07002784static int io_epoll_ctl_prep(struct io_kiocb *req,
2785 const struct io_uring_sqe *sqe)
2786{
2787#if defined(CONFIG_EPOLL)
2788 if (sqe->ioprio || sqe->buf_index)
2789 return -EINVAL;
2790
2791 req->epoll.epfd = READ_ONCE(sqe->fd);
2792 req->epoll.op = READ_ONCE(sqe->len);
2793 req->epoll.fd = READ_ONCE(sqe->off);
2794
2795 if (ep_op_has_event(req->epoll.op)) {
2796 struct epoll_event __user *ev;
2797
2798 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2799 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2800 return -EFAULT;
2801 }
2802
2803 return 0;
2804#else
2805 return -EOPNOTSUPP;
2806#endif
2807}
2808
2809static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2810 bool force_nonblock)
2811{
2812#if defined(CONFIG_EPOLL)
2813 struct io_epoll *ie = &req->epoll;
2814 int ret;
2815
2816 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2817 if (force_nonblock && ret == -EAGAIN)
2818 return -EAGAIN;
2819
2820 if (ret < 0)
2821 req_set_fail_links(req);
2822 io_cqring_add_event(req, ret);
2823 io_put_req_find_next(req, nxt);
2824 return 0;
2825#else
2826 return -EOPNOTSUPP;
2827#endif
2828}
2829
Jens Axboec1ca7572019-12-25 22:18:28 -07002830static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2831{
2832#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2833 if (sqe->ioprio || sqe->buf_index || sqe->off)
2834 return -EINVAL;
2835
2836 req->madvise.addr = READ_ONCE(sqe->addr);
2837 req->madvise.len = READ_ONCE(sqe->len);
2838 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2839 return 0;
2840#else
2841 return -EOPNOTSUPP;
2842#endif
2843}
2844
2845static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2846 bool force_nonblock)
2847{
2848#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2849 struct io_madvise *ma = &req->madvise;
2850 int ret;
2851
2852 if (force_nonblock)
2853 return -EAGAIN;
2854
2855 ret = do_madvise(ma->addr, ma->len, ma->advice);
2856 if (ret < 0)
2857 req_set_fail_links(req);
2858 io_cqring_add_event(req, ret);
2859 io_put_req_find_next(req, nxt);
2860 return 0;
2861#else
2862 return -EOPNOTSUPP;
2863#endif
2864}
2865
Jens Axboe4840e412019-12-25 22:03:45 -07002866static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2867{
2868 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2869 return -EINVAL;
2870
2871 req->fadvise.offset = READ_ONCE(sqe->off);
2872 req->fadvise.len = READ_ONCE(sqe->len);
2873 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2874 return 0;
2875}
2876
2877static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2878 bool force_nonblock)
2879{
2880 struct io_fadvise *fa = &req->fadvise;
2881 int ret;
2882
Jens Axboe3e694262020-02-01 09:22:49 -07002883 if (force_nonblock) {
2884 switch (fa->advice) {
2885 case POSIX_FADV_NORMAL:
2886 case POSIX_FADV_RANDOM:
2887 case POSIX_FADV_SEQUENTIAL:
2888 break;
2889 default:
2890 return -EAGAIN;
2891 }
2892 }
Jens Axboe4840e412019-12-25 22:03:45 -07002893
2894 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2895 if (ret < 0)
2896 req_set_fail_links(req);
2897 io_cqring_add_event(req, ret);
2898 io_put_req_find_next(req, nxt);
2899 return 0;
2900}
2901
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002902static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2903{
Jens Axboef8748882020-01-08 17:47:02 -07002904 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002905 unsigned lookup_flags;
2906 int ret;
2907
2908 if (sqe->ioprio || sqe->buf_index)
2909 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002910 if (sqe->flags & IOSQE_FIXED_FILE)
2911 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002912 if (req->flags & REQ_F_NEED_CLEANUP)
2913 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002914
2915 req->open.dfd = READ_ONCE(sqe->fd);
2916 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002917 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002918 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002919 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002920
Jens Axboec12cedf2020-01-08 17:41:21 -07002921 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002922 return -EINVAL;
2923
Jens Axboef8748882020-01-08 17:47:02 -07002924 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002925 if (IS_ERR(req->open.filename)) {
2926 ret = PTR_ERR(req->open.filename);
2927 req->open.filename = NULL;
2928 return ret;
2929 }
2930
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002931 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002932 return 0;
2933}
2934
2935static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2936 bool force_nonblock)
2937{
2938 struct io_open *ctx = &req->open;
2939 unsigned lookup_flags;
2940 struct path path;
2941 struct kstat stat;
2942 int ret;
2943
2944 if (force_nonblock)
2945 return -EAGAIN;
2946
Jens Axboec12cedf2020-01-08 17:41:21 -07002947 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002948 return -EINVAL;
2949
2950retry:
2951 /* filename_lookup() drops it, keep a reference */
2952 ctx->filename->refcnt++;
2953
2954 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2955 NULL);
2956 if (ret)
2957 goto err;
2958
Jens Axboec12cedf2020-01-08 17:41:21 -07002959 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002960 path_put(&path);
2961 if (retry_estale(ret, lookup_flags)) {
2962 lookup_flags |= LOOKUP_REVAL;
2963 goto retry;
2964 }
2965 if (!ret)
2966 ret = cp_statx(&stat, ctx->buffer);
2967err:
2968 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002969 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002970 if (ret < 0)
2971 req_set_fail_links(req);
2972 io_cqring_add_event(req, ret);
2973 io_put_req_find_next(req, nxt);
2974 return 0;
2975}
2976
Jens Axboeb5dba592019-12-11 14:02:38 -07002977static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2978{
2979 /*
2980 * If we queue this for async, it must not be cancellable. That would
2981 * leave the 'file' in an undeterminate state.
2982 */
2983 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2984
2985 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2986 sqe->rw_flags || sqe->buf_index)
2987 return -EINVAL;
2988 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002989 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002990
2991 req->close.fd = READ_ONCE(sqe->fd);
2992 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002993 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002994 return -EBADF;
2995
2996 return 0;
2997}
2998
Pavel Begunkova93b3332020-02-08 14:04:34 +03002999/* only called when __close_fd_get_file() is done */
3000static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
3001{
3002 int ret;
3003
3004 ret = filp_close(req->close.put_file, req->work.files);
3005 if (ret < 0)
3006 req_set_fail_links(req);
3007 io_cqring_add_event(req, ret);
3008 fput(req->close.put_file);
3009 io_put_req_find_next(req, nxt);
3010}
3011
Jens Axboeb5dba592019-12-11 14:02:38 -07003012static void io_close_finish(struct io_wq_work **workptr)
3013{
3014 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3015 struct io_kiocb *nxt = NULL;
3016
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003017 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03003018 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07003019 if (nxt)
3020 io_wq_assign_next(workptr, nxt);
3021}
3022
3023static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
3024 bool force_nonblock)
3025{
3026 int ret;
3027
3028 req->close.put_file = NULL;
3029 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3030 if (ret < 0)
3031 return ret;
3032
3033 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07003034 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07003035 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07003036
3037 /*
3038 * No ->flush(), safely close from here and just punt the
3039 * fput() to async context.
3040 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03003041 __io_close_finish(req, nxt);
3042 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003043eagain:
3044 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07003045 /*
3046 * Do manual async queue here to avoid grabbing files - we don't
3047 * need the files, and it'll cause io_close_finish() to close
3048 * the file again and cause a double CQE entry for this request
3049 */
3050 io_queue_async_work(req);
3051 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003052}
3053
Jens Axboe3529d8c2019-12-19 18:24:38 -07003054static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003055{
3056 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003057
3058 if (!req->file)
3059 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003060
3061 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3062 return -EINVAL;
3063 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3064 return -EINVAL;
3065
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003066 req->sync.off = READ_ONCE(sqe->off);
3067 req->sync.len = READ_ONCE(sqe->len);
3068 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003069 return 0;
3070}
3071
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003072static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003073{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003074 int ret;
3075
Jens Axboe9adbd452019-12-20 08:45:55 -07003076 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003077 req->sync.flags);
3078 if (ret < 0)
3079 req_set_fail_links(req);
3080 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003081 io_put_req_find_next(req, nxt);
3082}
3083
3084
3085static void io_sync_file_range_finish(struct io_wq_work **workptr)
3086{
3087 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3088 struct io_kiocb *nxt = NULL;
3089
3090 if (io_req_cancelled(req))
3091 return;
3092 __io_sync_file_range(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003093 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003094 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003095}
3096
Jens Axboefc4df992019-12-10 14:38:45 -07003097static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003098 bool force_nonblock)
3099{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003100 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003101 if (force_nonblock) {
3102 io_put_req(req);
3103 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003104 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003105 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003106
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003107 __io_sync_file_range(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003108 return 0;
3109}
3110
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003111static int io_setup_async_msg(struct io_kiocb *req,
3112 struct io_async_msghdr *kmsg)
3113{
3114 if (req->io)
3115 return -EAGAIN;
3116 if (io_alloc_async_ctx(req)) {
3117 if (kmsg->iov != kmsg->fast_iov)
3118 kfree(kmsg->iov);
3119 return -ENOMEM;
3120 }
3121 req->flags |= REQ_F_NEED_CLEANUP;
3122 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3123 return -EAGAIN;
3124}
3125
Jens Axboe3529d8c2019-12-19 18:24:38 -07003126static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003127{
Jens Axboe03b12302019-12-02 18:50:25 -07003128#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003129 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003130 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003131 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003132
Jens Axboee47293f2019-12-20 08:58:21 -07003133 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3134 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003135 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003136
Jens Axboed8768362020-02-27 14:17:49 -07003137#ifdef CONFIG_COMPAT
3138 if (req->ctx->compat)
3139 sr->msg_flags |= MSG_CMSG_COMPAT;
3140#endif
3141
Jens Axboefddafac2020-01-04 20:19:44 -07003142 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003143 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003144 /* iovec is already imported */
3145 if (req->flags & REQ_F_NEED_CLEANUP)
3146 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003147
Jens Axboed9688562019-12-09 19:35:20 -07003148 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003149 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003150 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003151 if (!ret)
3152 req->flags |= REQ_F_NEED_CLEANUP;
3153 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003154#else
Jens Axboee47293f2019-12-20 08:58:21 -07003155 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003156#endif
3157}
3158
Jens Axboefc4df992019-12-10 14:38:45 -07003159static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3160 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003161{
3162#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003163 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003164 struct socket *sock;
3165 int ret;
3166
3167 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3168 return -EINVAL;
3169
3170 sock = sock_from_file(req->file, &ret);
3171 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003172 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003173 unsigned flags;
3174
Jens Axboe03b12302019-12-02 18:50:25 -07003175 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003176 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003177 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003178 /* if iov is set, it's allocated already */
3179 if (!kmsg->iov)
3180 kmsg->iov = kmsg->fast_iov;
3181 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003182 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003183 struct io_sr_msg *sr = &req->sr_msg;
3184
Jens Axboe0b416c32019-12-15 10:57:46 -07003185 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003186 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003187
3188 io.msg.iov = io.msg.fast_iov;
3189 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3190 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003191 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003192 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003193 }
3194
Jens Axboee47293f2019-12-20 08:58:21 -07003195 flags = req->sr_msg.msg_flags;
3196 if (flags & MSG_DONTWAIT)
3197 req->flags |= REQ_F_NOWAIT;
3198 else if (force_nonblock)
3199 flags |= MSG_DONTWAIT;
3200
Jens Axboe0b416c32019-12-15 10:57:46 -07003201 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003202 if (force_nonblock && ret == -EAGAIN)
3203 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003204 if (ret == -ERESTARTSYS)
3205 ret = -EINTR;
3206 }
3207
Pavel Begunkov1e950812020-02-06 19:51:16 +03003208 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003209 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003210 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003211 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003212 if (ret < 0)
3213 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003214 io_put_req_find_next(req, nxt);
3215 return 0;
3216#else
3217 return -EOPNOTSUPP;
3218#endif
3219}
3220
Jens Axboefddafac2020-01-04 20:19:44 -07003221static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3222 bool force_nonblock)
3223{
3224#if defined(CONFIG_NET)
3225 struct socket *sock;
3226 int ret;
3227
3228 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3229 return -EINVAL;
3230
3231 sock = sock_from_file(req->file, &ret);
3232 if (sock) {
3233 struct io_sr_msg *sr = &req->sr_msg;
3234 struct msghdr msg;
3235 struct iovec iov;
3236 unsigned flags;
3237
3238 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3239 &msg.msg_iter);
3240 if (ret)
3241 return ret;
3242
3243 msg.msg_name = NULL;
3244 msg.msg_control = NULL;
3245 msg.msg_controllen = 0;
3246 msg.msg_namelen = 0;
3247
3248 flags = req->sr_msg.msg_flags;
3249 if (flags & MSG_DONTWAIT)
3250 req->flags |= REQ_F_NOWAIT;
3251 else if (force_nonblock)
3252 flags |= MSG_DONTWAIT;
3253
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003254 msg.msg_flags = flags;
3255 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003256 if (force_nonblock && ret == -EAGAIN)
3257 return -EAGAIN;
3258 if (ret == -ERESTARTSYS)
3259 ret = -EINTR;
3260 }
3261
3262 io_cqring_add_event(req, ret);
3263 if (ret < 0)
3264 req_set_fail_links(req);
3265 io_put_req_find_next(req, nxt);
3266 return 0;
3267#else
3268 return -EOPNOTSUPP;
3269#endif
3270}
3271
Jens Axboe3529d8c2019-12-19 18:24:38 -07003272static int io_recvmsg_prep(struct io_kiocb *req,
3273 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003274{
3275#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003276 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003277 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003278 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003279
Jens Axboe3529d8c2019-12-19 18:24:38 -07003280 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3281 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003282 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003283
Jens Axboed8768362020-02-27 14:17:49 -07003284#ifdef CONFIG_COMPAT
3285 if (req->ctx->compat)
3286 sr->msg_flags |= MSG_CMSG_COMPAT;
3287#endif
3288
Jens Axboefddafac2020-01-04 20:19:44 -07003289 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003290 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003291 /* iovec is already imported */
3292 if (req->flags & REQ_F_NEED_CLEANUP)
3293 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003294
Jens Axboed9688562019-12-09 19:35:20 -07003295 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003296 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003297 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003298 if (!ret)
3299 req->flags |= REQ_F_NEED_CLEANUP;
3300 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003301#else
Jens Axboee47293f2019-12-20 08:58:21 -07003302 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003303#endif
3304}
3305
Jens Axboefc4df992019-12-10 14:38:45 -07003306static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3307 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003308{
3309#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003310 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003311 struct socket *sock;
3312 int ret;
3313
3314 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3315 return -EINVAL;
3316
3317 sock = sock_from_file(req->file, &ret);
3318 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003319 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003320 unsigned flags;
3321
Jens Axboe03b12302019-12-02 18:50:25 -07003322 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003323 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003324 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003325 /* if iov is set, it's allocated already */
3326 if (!kmsg->iov)
3327 kmsg->iov = kmsg->fast_iov;
3328 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003329 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003330 struct io_sr_msg *sr = &req->sr_msg;
3331
Jens Axboe0b416c32019-12-15 10:57:46 -07003332 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003333 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003334
3335 io.msg.iov = io.msg.fast_iov;
3336 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3337 sr->msg_flags, &io.msg.uaddr,
3338 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003339 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003340 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003341 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003342
Jens Axboee47293f2019-12-20 08:58:21 -07003343 flags = req->sr_msg.msg_flags;
3344 if (flags & MSG_DONTWAIT)
3345 req->flags |= REQ_F_NOWAIT;
3346 else if (force_nonblock)
3347 flags |= MSG_DONTWAIT;
3348
3349 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3350 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003351 if (force_nonblock && ret == -EAGAIN)
3352 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003353 if (ret == -ERESTARTSYS)
3354 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003355 }
3356
Pavel Begunkov1e950812020-02-06 19:51:16 +03003357 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003358 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003359 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003360 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003361 if (ret < 0)
3362 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003363 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003364 return 0;
3365#else
3366 return -EOPNOTSUPP;
3367#endif
3368}
3369
Jens Axboefddafac2020-01-04 20:19:44 -07003370static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3371 bool force_nonblock)
3372{
3373#if defined(CONFIG_NET)
3374 struct socket *sock;
3375 int ret;
3376
3377 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3378 return -EINVAL;
3379
3380 sock = sock_from_file(req->file, &ret);
3381 if (sock) {
3382 struct io_sr_msg *sr = &req->sr_msg;
3383 struct msghdr msg;
3384 struct iovec iov;
3385 unsigned flags;
3386
3387 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3388 &msg.msg_iter);
3389 if (ret)
3390 return ret;
3391
3392 msg.msg_name = NULL;
3393 msg.msg_control = NULL;
3394 msg.msg_controllen = 0;
3395 msg.msg_namelen = 0;
3396 msg.msg_iocb = NULL;
3397 msg.msg_flags = 0;
3398
3399 flags = req->sr_msg.msg_flags;
3400 if (flags & MSG_DONTWAIT)
3401 req->flags |= REQ_F_NOWAIT;
3402 else if (force_nonblock)
3403 flags |= MSG_DONTWAIT;
3404
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003405 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003406 if (force_nonblock && ret == -EAGAIN)
3407 return -EAGAIN;
3408 if (ret == -ERESTARTSYS)
3409 ret = -EINTR;
3410 }
3411
3412 io_cqring_add_event(req, ret);
3413 if (ret < 0)
3414 req_set_fail_links(req);
3415 io_put_req_find_next(req, nxt);
3416 return 0;
3417#else
3418 return -EOPNOTSUPP;
3419#endif
3420}
3421
3422
Jens Axboe3529d8c2019-12-19 18:24:38 -07003423static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003424{
3425#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003426 struct io_accept *accept = &req->accept;
3427
Jens Axboe17f2fe32019-10-17 14:42:58 -06003428 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3429 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003430 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003431 return -EINVAL;
3432
Jens Axboed55e5f52019-12-11 16:12:15 -07003433 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3434 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003435 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003436 return 0;
3437#else
3438 return -EOPNOTSUPP;
3439#endif
3440}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003441
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003442#if defined(CONFIG_NET)
3443static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3444 bool force_nonblock)
3445{
3446 struct io_accept *accept = &req->accept;
3447 unsigned file_flags;
3448 int ret;
3449
3450 file_flags = force_nonblock ? O_NONBLOCK : 0;
3451 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3452 accept->addr_len, accept->flags);
3453 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003454 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003455 if (ret == -ERESTARTSYS)
3456 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003457 if (ret < 0)
3458 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003459 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003460 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003461 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003462}
3463
3464static void io_accept_finish(struct io_wq_work **workptr)
3465{
3466 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3467 struct io_kiocb *nxt = NULL;
3468
Jens Axboee441d1c2020-02-20 09:59:02 -07003469 io_put_req(req);
3470
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003471 if (io_req_cancelled(req))
3472 return;
3473 __io_accept(req, &nxt, false);
3474 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003475 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003476}
3477#endif
3478
3479static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3480 bool force_nonblock)
3481{
3482#if defined(CONFIG_NET)
3483 int ret;
3484
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003485 ret = __io_accept(req, nxt, force_nonblock);
3486 if (ret == -EAGAIN && force_nonblock) {
3487 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003488 return -EAGAIN;
3489 }
3490 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003491#else
3492 return -EOPNOTSUPP;
3493#endif
3494}
3495
Jens Axboe3529d8c2019-12-19 18:24:38 -07003496static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003497{
3498#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003499 struct io_connect *conn = &req->connect;
3500 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003501
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003502 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3503 return -EINVAL;
3504 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3505 return -EINVAL;
3506
Jens Axboe3529d8c2019-12-19 18:24:38 -07003507 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3508 conn->addr_len = READ_ONCE(sqe->addr2);
3509
3510 if (!io)
3511 return 0;
3512
3513 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003514 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003515#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003516 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003517#endif
3518}
3519
Jens Axboefc4df992019-12-10 14:38:45 -07003520static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3521 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003522{
3523#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003524 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003525 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003526 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003527
Jens Axboef499a022019-12-02 16:28:46 -07003528 if (req->io) {
3529 io = req->io;
3530 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003531 ret = move_addr_to_kernel(req->connect.addr,
3532 req->connect.addr_len,
3533 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003534 if (ret)
3535 goto out;
3536 io = &__io;
3537 }
3538
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003539 file_flags = force_nonblock ? O_NONBLOCK : 0;
3540
3541 ret = __sys_connect_file(req->file, &io->connect.address,
3542 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003543 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003544 if (req->io)
3545 return -EAGAIN;
3546 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003547 ret = -ENOMEM;
3548 goto out;
3549 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003550 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003551 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003552 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003553 if (ret == -ERESTARTSYS)
3554 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003555out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003556 if (ret < 0)
3557 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003558 io_cqring_add_event(req, ret);
3559 io_put_req_find_next(req, nxt);
3560 return 0;
3561#else
3562 return -EOPNOTSUPP;
3563#endif
3564}
3565
Jens Axboeb41e9852020-02-17 09:52:41 -07003566static bool io_poll_remove_one(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003567{
3568 struct io_poll_iocb *poll = &req->poll;
Jens Axboeb41e9852020-02-17 09:52:41 -07003569 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003570
3571 spin_lock(&poll->head->lock);
3572 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003573 if (!list_empty(&poll->wait.entry)) {
3574 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07003575 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003576 }
3577 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003578 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07003579 if (do_complete) {
3580 io_cqring_fill_event(req, -ECANCELED);
3581 io_commit_cqring(req->ctx);
3582 req->flags |= REQ_F_COMP_LOCKED;
3583 io_put_req(req);
3584 }
3585
3586 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003587}
3588
3589static void io_poll_remove_all(struct io_ring_ctx *ctx)
3590{
Jens Axboe78076bb2019-12-04 19:56:40 -07003591 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003592 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003593 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003594
3595 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003596 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3597 struct hlist_head *list;
3598
3599 list = &ctx->cancel_hash[i];
3600 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3601 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003602 }
3603 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003604
3605 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003606}
3607
Jens Axboe47f46762019-11-09 17:43:02 -07003608static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3609{
Jens Axboe78076bb2019-12-04 19:56:40 -07003610 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003611 struct io_kiocb *req;
3612
Jens Axboe78076bb2019-12-04 19:56:40 -07003613 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3614 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07003615 if (sqe_addr != req->user_data)
3616 continue;
3617 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07003618 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07003619 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07003620 }
3621
3622 return -ENOENT;
3623}
3624
Jens Axboe3529d8c2019-12-19 18:24:38 -07003625static int io_poll_remove_prep(struct io_kiocb *req,
3626 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003627{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003628 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3629 return -EINVAL;
3630 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3631 sqe->poll_events)
3632 return -EINVAL;
3633
Jens Axboe0969e782019-12-17 18:40:57 -07003634 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003635 return 0;
3636}
3637
3638/*
3639 * Find a running poll command that matches one specified in sqe->addr,
3640 * and remove it if found.
3641 */
3642static int io_poll_remove(struct io_kiocb *req)
3643{
3644 struct io_ring_ctx *ctx = req->ctx;
3645 u64 addr;
3646 int ret;
3647
Jens Axboe0969e782019-12-17 18:40:57 -07003648 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003649 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003650 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003651 spin_unlock_irq(&ctx->completion_lock);
3652
Jens Axboe78e19bb2019-11-06 15:21:34 -07003653 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003654 if (ret < 0)
3655 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003656 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003657 return 0;
3658}
3659
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003660static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003661{
Jackie Liua197f662019-11-08 08:09:12 -07003662 struct io_ring_ctx *ctx = req->ctx;
3663
Jens Axboe8c838782019-03-12 15:48:16 -06003664 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03003665 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003666 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003667}
3668
Jens Axboeb41e9852020-02-17 09:52:41 -07003669static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003670{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003671 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003672
Jens Axboe221c5eb2019-01-17 09:41:58 -07003673 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003674 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07003675 io_poll_complete(req, req->result, 0);
3676 req->flags |= REQ_F_COMP_LOCKED;
3677 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003678 spin_unlock_irq(&ctx->completion_lock);
3679
Jens Axboe8c838782019-03-12 15:48:16 -06003680 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07003681}
Jens Axboe89723d02019-11-05 15:32:58 -07003682
Jens Axboeb41e9852020-02-17 09:52:41 -07003683static void io_poll_task_func(struct callback_head *cb)
3684{
3685 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3686 struct io_kiocb *nxt = NULL;
3687
3688 io_poll_task_handler(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003689 if (nxt)
Jens Axboeb41e9852020-02-17 09:52:41 -07003690 __io_queue_sqe(nxt, NULL);
Jens Axboef0b493e2020-02-01 21:30:11 -07003691}
3692
Jens Axboe221c5eb2019-01-17 09:41:58 -07003693static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3694 void *key)
3695{
Jens Axboec2f2eb72020-02-10 09:07:05 -07003696 struct io_kiocb *req = wait->private;
3697 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003698 __poll_t mask = key_to_poll(key);
Jens Axboeb41e9852020-02-17 09:52:41 -07003699 struct task_struct *tsk;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003700
3701 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003702 if (mask && !(mask & poll->events))
3703 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003704
Jens Axboe392edb42019-12-09 17:52:20 -07003705 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003706
Jens Axboeb41e9852020-02-17 09:52:41 -07003707 tsk = req->task;
3708 req->result = mask;
3709 init_task_work(&req->task_work, io_poll_task_func);
3710 task_work_add(tsk, &req->task_work, true);
3711 wake_up_process(tsk);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003712 return 1;
3713}
3714
3715struct io_poll_table {
3716 struct poll_table_struct pt;
3717 struct io_kiocb *req;
3718 int error;
3719};
3720
3721static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3722 struct poll_table_struct *p)
3723{
3724 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3725
3726 if (unlikely(pt->req->poll.head)) {
3727 pt->error = -EINVAL;
3728 return;
3729 }
3730
3731 pt->error = 0;
3732 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003733 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003734}
3735
Jens Axboeeac406c2019-11-14 12:09:58 -07003736static void io_poll_req_insert(struct io_kiocb *req)
3737{
3738 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003739 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003740
Jens Axboe78076bb2019-12-04 19:56:40 -07003741 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3742 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003743}
3744
Jens Axboe3529d8c2019-12-19 18:24:38 -07003745static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003746{
3747 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003748 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003749
3750 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3751 return -EINVAL;
3752 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3753 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003754 if (!poll->file)
3755 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003756
Jens Axboe221c5eb2019-01-17 09:41:58 -07003757 events = READ_ONCE(sqe->poll_events);
3758 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07003759
3760 /* task will wait for requests on exit, don't need a ref */
3761 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07003762 return 0;
3763}
3764
3765static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3766{
3767 struct io_poll_iocb *poll = &req->poll;
3768 struct io_ring_ctx *ctx = req->ctx;
3769 struct io_poll_table ipt;
3770 bool cancel = false;
3771 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003772
Jens Axboe78076bb2019-12-04 19:56:40 -07003773 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003774
Jens Axboe221c5eb2019-01-17 09:41:58 -07003775 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003776 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003777 poll->canceled = false;
3778
3779 ipt.pt._qproc = io_poll_queue_proc;
3780 ipt.pt._key = poll->events;
3781 ipt.req = req;
3782 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3783
3784 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003785 INIT_LIST_HEAD(&poll->wait.entry);
3786 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
Jens Axboec2f2eb72020-02-10 09:07:05 -07003787 poll->wait.private = req;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003788
Jens Axboe36703242019-07-25 10:20:18 -06003789 INIT_LIST_HEAD(&req->list);
3790
Jens Axboe221c5eb2019-01-17 09:41:58 -07003791 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003792
3793 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003794 if (likely(poll->head)) {
3795 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003796 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003797 if (ipt.error)
3798 cancel = true;
3799 ipt.error = 0;
3800 mask = 0;
3801 }
3802 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003803 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003804 else if (cancel)
3805 WRITE_ONCE(poll->canceled, true);
3806 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003807 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003808 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003809 }
Jens Axboe8c838782019-03-12 15:48:16 -06003810 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003811 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003812 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003813 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003814 spin_unlock_irq(&ctx->completion_lock);
3815
Jens Axboe8c838782019-03-12 15:48:16 -06003816 if (mask) {
3817 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003818 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003819 }
Jens Axboe8c838782019-03-12 15:48:16 -06003820 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003821}
3822
Jens Axboe5262f562019-09-17 12:26:57 -06003823static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3824{
Jens Axboead8a48a2019-11-15 08:49:11 -07003825 struct io_timeout_data *data = container_of(timer,
3826 struct io_timeout_data, timer);
3827 struct io_kiocb *req = data->req;
3828 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003829 unsigned long flags;
3830
Jens Axboe5262f562019-09-17 12:26:57 -06003831 atomic_inc(&ctx->cq_timeouts);
3832
3833 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003834 /*
Jens Axboe11365042019-10-16 09:08:32 -06003835 * We could be racing with timeout deletion. If the list is empty,
3836 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003837 */
Jens Axboe842f9612019-10-29 12:34:10 -06003838 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003839 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003840
Jens Axboe11365042019-10-16 09:08:32 -06003841 /*
3842 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003843 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003844 * pointer will be increased, otherwise other timeout reqs may
3845 * return in advance without waiting for enough wait_nr.
3846 */
3847 prev = req;
3848 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3849 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003850 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003851 }
Jens Axboe842f9612019-10-29 12:34:10 -06003852
Jens Axboe78e19bb2019-11-06 15:21:34 -07003853 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003854 io_commit_cqring(ctx);
3855 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3856
3857 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003858 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003859 io_put_req(req);
3860 return HRTIMER_NORESTART;
3861}
3862
Jens Axboe47f46762019-11-09 17:43:02 -07003863static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3864{
3865 struct io_kiocb *req;
3866 int ret = -ENOENT;
3867
3868 list_for_each_entry(req, &ctx->timeout_list, list) {
3869 if (user_data == req->user_data) {
3870 list_del_init(&req->list);
3871 ret = 0;
3872 break;
3873 }
3874 }
3875
3876 if (ret == -ENOENT)
3877 return ret;
3878
Jens Axboe2d283902019-12-04 11:08:05 -07003879 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003880 if (ret == -1)
3881 return -EALREADY;
3882
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003883 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003884 io_cqring_fill_event(req, -ECANCELED);
3885 io_put_req(req);
3886 return 0;
3887}
3888
Jens Axboe3529d8c2019-12-19 18:24:38 -07003889static int io_timeout_remove_prep(struct io_kiocb *req,
3890 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003891{
Jens Axboeb29472e2019-12-17 18:50:29 -07003892 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3893 return -EINVAL;
3894 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3895 return -EINVAL;
3896
3897 req->timeout.addr = READ_ONCE(sqe->addr);
3898 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3899 if (req->timeout.flags)
3900 return -EINVAL;
3901
Jens Axboeb29472e2019-12-17 18:50:29 -07003902 return 0;
3903}
3904
Jens Axboe11365042019-10-16 09:08:32 -06003905/*
3906 * Remove or update an existing timeout command
3907 */
Jens Axboefc4df992019-12-10 14:38:45 -07003908static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003909{
3910 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003911 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003912
Jens Axboe11365042019-10-16 09:08:32 -06003913 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003914 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003915
Jens Axboe47f46762019-11-09 17:43:02 -07003916 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003917 io_commit_cqring(ctx);
3918 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003919 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003920 if (ret < 0)
3921 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003922 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003923 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003924}
3925
Jens Axboe3529d8c2019-12-19 18:24:38 -07003926static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003927 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003928{
Jens Axboead8a48a2019-11-15 08:49:11 -07003929 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003930 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003931
Jens Axboead8a48a2019-11-15 08:49:11 -07003932 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003933 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003934 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003935 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003936 if (sqe->off && is_timeout_link)
3937 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003938 flags = READ_ONCE(sqe->timeout_flags);
3939 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003940 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003941
Jens Axboe26a61672019-12-20 09:02:01 -07003942 req->timeout.count = READ_ONCE(sqe->off);
3943
Jens Axboe3529d8c2019-12-19 18:24:38 -07003944 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003945 return -ENOMEM;
3946
3947 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003948 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003949 req->flags |= REQ_F_TIMEOUT;
3950
3951 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003952 return -EFAULT;
3953
Jens Axboe11365042019-10-16 09:08:32 -06003954 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003955 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003956 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003957 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003958
Jens Axboead8a48a2019-11-15 08:49:11 -07003959 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3960 return 0;
3961}
3962
Jens Axboefc4df992019-12-10 14:38:45 -07003963static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003964{
3965 unsigned count;
3966 struct io_ring_ctx *ctx = req->ctx;
3967 struct io_timeout_data *data;
3968 struct list_head *entry;
3969 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003970
Jens Axboe2d283902019-12-04 11:08:05 -07003971 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003972
Jens Axboe5262f562019-09-17 12:26:57 -06003973 /*
3974 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003975 * timeout event to be satisfied. If it isn't set, then this is
3976 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003977 */
Jens Axboe26a61672019-12-20 09:02:01 -07003978 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003979 if (!count) {
3980 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3981 spin_lock_irq(&ctx->completion_lock);
3982 entry = ctx->timeout_list.prev;
3983 goto add;
3984 }
Jens Axboe5262f562019-09-17 12:26:57 -06003985
3986 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003987 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003988
3989 /*
3990 * Insertion sort, ensuring the first entry in the list is always
3991 * the one we need first.
3992 */
Jens Axboe5262f562019-09-17 12:26:57 -06003993 spin_lock_irq(&ctx->completion_lock);
3994 list_for_each_prev(entry, &ctx->timeout_list) {
3995 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003996 unsigned nxt_sq_head;
3997 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003998 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003999
Jens Axboe93bd25b2019-11-11 23:34:31 -07004000 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4001 continue;
4002
yangerkun5da0fb12019-10-15 21:59:29 +08004003 /*
4004 * Since cached_sq_head + count - 1 can overflow, use type long
4005 * long to store it.
4006 */
4007 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004008 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4009 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004010
4011 /*
4012 * cached_sq_head may overflow, and it will never overflow twice
4013 * once there is some timeout req still be valid.
4014 */
4015 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004016 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004017
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004018 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004019 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004020
4021 /*
4022 * Sequence of reqs after the insert one and itself should
4023 * be adjusted because each timeout req consumes a slot.
4024 */
4025 span++;
4026 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004027 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004028 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004029add:
Jens Axboe5262f562019-09-17 12:26:57 -06004030 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004031 data->timer.function = io_timeout_fn;
4032 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004033 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004034 return 0;
4035}
4036
Jens Axboe62755e32019-10-28 21:49:21 -06004037static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004038{
Jens Axboe62755e32019-10-28 21:49:21 -06004039 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004040
Jens Axboe62755e32019-10-28 21:49:21 -06004041 return req->user_data == (unsigned long) data;
4042}
4043
Jens Axboee977d6d2019-11-05 12:39:45 -07004044static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004045{
Jens Axboe62755e32019-10-28 21:49:21 -06004046 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004047 int ret = 0;
4048
Jens Axboe62755e32019-10-28 21:49:21 -06004049 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4050 switch (cancel_ret) {
4051 case IO_WQ_CANCEL_OK:
4052 ret = 0;
4053 break;
4054 case IO_WQ_CANCEL_RUNNING:
4055 ret = -EALREADY;
4056 break;
4057 case IO_WQ_CANCEL_NOTFOUND:
4058 ret = -ENOENT;
4059 break;
4060 }
4061
Jens Axboee977d6d2019-11-05 12:39:45 -07004062 return ret;
4063}
4064
Jens Axboe47f46762019-11-09 17:43:02 -07004065static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4066 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004067 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004068{
4069 unsigned long flags;
4070 int ret;
4071
4072 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4073 if (ret != -ENOENT) {
4074 spin_lock_irqsave(&ctx->completion_lock, flags);
4075 goto done;
4076 }
4077
4078 spin_lock_irqsave(&ctx->completion_lock, flags);
4079 ret = io_timeout_cancel(ctx, sqe_addr);
4080 if (ret != -ENOENT)
4081 goto done;
4082 ret = io_poll_cancel(ctx, sqe_addr);
4083done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004084 if (!ret)
4085 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004086 io_cqring_fill_event(req, ret);
4087 io_commit_cqring(ctx);
4088 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4089 io_cqring_ev_posted(ctx);
4090
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004091 if (ret < 0)
4092 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004093 io_put_req_find_next(req, nxt);
4094}
4095
Jens Axboe3529d8c2019-12-19 18:24:38 -07004096static int io_async_cancel_prep(struct io_kiocb *req,
4097 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004098{
Jens Axboefbf23842019-12-17 18:45:56 -07004099 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004100 return -EINVAL;
4101 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4102 sqe->cancel_flags)
4103 return -EINVAL;
4104
Jens Axboefbf23842019-12-17 18:45:56 -07004105 req->cancel.addr = READ_ONCE(sqe->addr);
4106 return 0;
4107}
4108
4109static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4110{
4111 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004112
4113 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004114 return 0;
4115}
4116
Jens Axboe05f3fb32019-12-09 11:22:50 -07004117static int io_files_update_prep(struct io_kiocb *req,
4118 const struct io_uring_sqe *sqe)
4119{
4120 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4121 return -EINVAL;
4122
4123 req->files_update.offset = READ_ONCE(sqe->off);
4124 req->files_update.nr_args = READ_ONCE(sqe->len);
4125 if (!req->files_update.nr_args)
4126 return -EINVAL;
4127 req->files_update.arg = READ_ONCE(sqe->addr);
4128 return 0;
4129}
4130
4131static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4132{
4133 struct io_ring_ctx *ctx = req->ctx;
4134 struct io_uring_files_update up;
4135 int ret;
4136
Jens Axboef86cd202020-01-29 13:46:44 -07004137 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004138 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004139
4140 up.offset = req->files_update.offset;
4141 up.fds = req->files_update.arg;
4142
4143 mutex_lock(&ctx->uring_lock);
4144 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4145 mutex_unlock(&ctx->uring_lock);
4146
4147 if (ret < 0)
4148 req_set_fail_links(req);
4149 io_cqring_add_event(req, ret);
4150 io_put_req(req);
4151 return 0;
4152}
4153
Jens Axboe3529d8c2019-12-19 18:24:38 -07004154static int io_req_defer_prep(struct io_kiocb *req,
4155 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004156{
Jens Axboee7815732019-12-17 19:45:06 -07004157 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004158
Jens Axboef86cd202020-01-29 13:46:44 -07004159 if (io_op_defs[req->opcode].file_table) {
4160 ret = io_grab_files(req);
4161 if (unlikely(ret))
4162 return ret;
4163 }
4164
Jens Axboecccf0ee2020-01-27 16:34:48 -07004165 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4166
Jens Axboed625c6e2019-12-17 19:53:05 -07004167 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004168 case IORING_OP_NOP:
4169 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004170 case IORING_OP_READV:
4171 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004172 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004173 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004174 break;
4175 case IORING_OP_WRITEV:
4176 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004177 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004178 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004179 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004180 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004181 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004182 break;
4183 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004184 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004185 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004186 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004187 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004188 break;
4189 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004190 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004191 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004192 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004193 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004194 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004195 break;
4196 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004197 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004198 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004199 break;
Jens Axboef499a022019-12-02 16:28:46 -07004200 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004201 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004202 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004203 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004204 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004205 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004206 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004207 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004208 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004209 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004210 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004211 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004212 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004213 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004214 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004215 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004216 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004217 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004218 case IORING_OP_FALLOCATE:
4219 ret = io_fallocate_prep(req, sqe);
4220 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004221 case IORING_OP_OPENAT:
4222 ret = io_openat_prep(req, sqe);
4223 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004224 case IORING_OP_CLOSE:
4225 ret = io_close_prep(req, sqe);
4226 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004227 case IORING_OP_FILES_UPDATE:
4228 ret = io_files_update_prep(req, sqe);
4229 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004230 case IORING_OP_STATX:
4231 ret = io_statx_prep(req, sqe);
4232 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004233 case IORING_OP_FADVISE:
4234 ret = io_fadvise_prep(req, sqe);
4235 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004236 case IORING_OP_MADVISE:
4237 ret = io_madvise_prep(req, sqe);
4238 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004239 case IORING_OP_OPENAT2:
4240 ret = io_openat2_prep(req, sqe);
4241 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004242 case IORING_OP_EPOLL_CTL:
4243 ret = io_epoll_ctl_prep(req, sqe);
4244 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004245 case IORING_OP_SPLICE:
4246 ret = io_splice_prep(req, sqe);
4247 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004248 default:
Jens Axboee7815732019-12-17 19:45:06 -07004249 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4250 req->opcode);
4251 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004252 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004253 }
4254
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004255 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004256}
4257
Jens Axboe3529d8c2019-12-19 18:24:38 -07004258static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004259{
Jackie Liua197f662019-11-08 08:09:12 -07004260 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004261 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004262
Bob Liu9d858b22019-11-13 18:06:25 +08004263 /* Still need defer if there is pending req in defer list. */
4264 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004265 return 0;
4266
Jens Axboe3529d8c2019-12-19 18:24:38 -07004267 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004268 return -EAGAIN;
4269
Jens Axboe3529d8c2019-12-19 18:24:38 -07004270 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004271 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004272 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004273
Jens Axboede0617e2019-04-06 21:51:27 -06004274 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004275 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004276 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004277 return 0;
4278 }
4279
Jens Axboe915967f2019-11-21 09:01:20 -07004280 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004281 list_add_tail(&req->list, &ctx->defer_list);
4282 spin_unlock_irq(&ctx->completion_lock);
4283 return -EIOCBQUEUED;
4284}
4285
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004286static void io_cleanup_req(struct io_kiocb *req)
4287{
4288 struct io_async_ctx *io = req->io;
4289
4290 switch (req->opcode) {
4291 case IORING_OP_READV:
4292 case IORING_OP_READ_FIXED:
4293 case IORING_OP_READ:
4294 case IORING_OP_WRITEV:
4295 case IORING_OP_WRITE_FIXED:
4296 case IORING_OP_WRITE:
4297 if (io->rw.iov != io->rw.fast_iov)
4298 kfree(io->rw.iov);
4299 break;
4300 case IORING_OP_SENDMSG:
4301 case IORING_OP_RECVMSG:
4302 if (io->msg.iov != io->msg.fast_iov)
4303 kfree(io->msg.iov);
4304 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004305 case IORING_OP_OPENAT:
4306 case IORING_OP_OPENAT2:
4307 case IORING_OP_STATX:
4308 putname(req->open.filename);
4309 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004310 case IORING_OP_SPLICE:
4311 io_put_file(req, req->splice.file_in,
4312 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4313 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004314 }
4315
4316 req->flags &= ~REQ_F_NEED_CLEANUP;
4317}
4318
Jens Axboe3529d8c2019-12-19 18:24:38 -07004319static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4320 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004321{
Jackie Liua197f662019-11-08 08:09:12 -07004322 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004323 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004324
Jens Axboed625c6e2019-12-17 19:53:05 -07004325 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004326 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004327 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004328 break;
4329 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004330 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004331 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004332 if (sqe) {
4333 ret = io_read_prep(req, sqe, force_nonblock);
4334 if (ret < 0)
4335 break;
4336 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004337 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004338 break;
4339 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004340 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004341 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004342 if (sqe) {
4343 ret = io_write_prep(req, sqe, force_nonblock);
4344 if (ret < 0)
4345 break;
4346 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004347 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004348 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004349 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004350 if (sqe) {
4351 ret = io_prep_fsync(req, sqe);
4352 if (ret < 0)
4353 break;
4354 }
Jens Axboefc4df992019-12-10 14:38:45 -07004355 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004356 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004357 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004358 if (sqe) {
4359 ret = io_poll_add_prep(req, sqe);
4360 if (ret)
4361 break;
4362 }
Jens Axboefc4df992019-12-10 14:38:45 -07004363 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004364 break;
4365 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004366 if (sqe) {
4367 ret = io_poll_remove_prep(req, sqe);
4368 if (ret < 0)
4369 break;
4370 }
Jens Axboefc4df992019-12-10 14:38:45 -07004371 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004372 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004373 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004374 if (sqe) {
4375 ret = io_prep_sfr(req, sqe);
4376 if (ret < 0)
4377 break;
4378 }
Jens Axboefc4df992019-12-10 14:38:45 -07004379 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004380 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004381 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004382 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004383 if (sqe) {
4384 ret = io_sendmsg_prep(req, sqe);
4385 if (ret < 0)
4386 break;
4387 }
Jens Axboefddafac2020-01-04 20:19:44 -07004388 if (req->opcode == IORING_OP_SENDMSG)
4389 ret = io_sendmsg(req, nxt, force_nonblock);
4390 else
4391 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004392 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004393 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004394 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004395 if (sqe) {
4396 ret = io_recvmsg_prep(req, sqe);
4397 if (ret)
4398 break;
4399 }
Jens Axboefddafac2020-01-04 20:19:44 -07004400 if (req->opcode == IORING_OP_RECVMSG)
4401 ret = io_recvmsg(req, nxt, force_nonblock);
4402 else
4403 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004404 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004405 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004406 if (sqe) {
4407 ret = io_timeout_prep(req, sqe, false);
4408 if (ret)
4409 break;
4410 }
Jens Axboefc4df992019-12-10 14:38:45 -07004411 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004412 break;
Jens Axboe11365042019-10-16 09:08:32 -06004413 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004414 if (sqe) {
4415 ret = io_timeout_remove_prep(req, sqe);
4416 if (ret)
4417 break;
4418 }
Jens Axboefc4df992019-12-10 14:38:45 -07004419 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004420 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004421 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004422 if (sqe) {
4423 ret = io_accept_prep(req, sqe);
4424 if (ret)
4425 break;
4426 }
Jens Axboefc4df992019-12-10 14:38:45 -07004427 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004428 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004429 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004430 if (sqe) {
4431 ret = io_connect_prep(req, sqe);
4432 if (ret)
4433 break;
4434 }
Jens Axboefc4df992019-12-10 14:38:45 -07004435 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004436 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004437 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004438 if (sqe) {
4439 ret = io_async_cancel_prep(req, sqe);
4440 if (ret)
4441 break;
4442 }
Jens Axboefc4df992019-12-10 14:38:45 -07004443 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004444 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004445 case IORING_OP_FALLOCATE:
4446 if (sqe) {
4447 ret = io_fallocate_prep(req, sqe);
4448 if (ret)
4449 break;
4450 }
4451 ret = io_fallocate(req, nxt, force_nonblock);
4452 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004453 case IORING_OP_OPENAT:
4454 if (sqe) {
4455 ret = io_openat_prep(req, sqe);
4456 if (ret)
4457 break;
4458 }
4459 ret = io_openat(req, nxt, force_nonblock);
4460 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004461 case IORING_OP_CLOSE:
4462 if (sqe) {
4463 ret = io_close_prep(req, sqe);
4464 if (ret)
4465 break;
4466 }
4467 ret = io_close(req, nxt, force_nonblock);
4468 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004469 case IORING_OP_FILES_UPDATE:
4470 if (sqe) {
4471 ret = io_files_update_prep(req, sqe);
4472 if (ret)
4473 break;
4474 }
4475 ret = io_files_update(req, force_nonblock);
4476 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004477 case IORING_OP_STATX:
4478 if (sqe) {
4479 ret = io_statx_prep(req, sqe);
4480 if (ret)
4481 break;
4482 }
4483 ret = io_statx(req, nxt, force_nonblock);
4484 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004485 case IORING_OP_FADVISE:
4486 if (sqe) {
4487 ret = io_fadvise_prep(req, sqe);
4488 if (ret)
4489 break;
4490 }
4491 ret = io_fadvise(req, nxt, force_nonblock);
4492 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004493 case IORING_OP_MADVISE:
4494 if (sqe) {
4495 ret = io_madvise_prep(req, sqe);
4496 if (ret)
4497 break;
4498 }
4499 ret = io_madvise(req, nxt, force_nonblock);
4500 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004501 case IORING_OP_OPENAT2:
4502 if (sqe) {
4503 ret = io_openat2_prep(req, sqe);
4504 if (ret)
4505 break;
4506 }
4507 ret = io_openat2(req, nxt, force_nonblock);
4508 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004509 case IORING_OP_EPOLL_CTL:
4510 if (sqe) {
4511 ret = io_epoll_ctl_prep(req, sqe);
4512 if (ret)
4513 break;
4514 }
4515 ret = io_epoll_ctl(req, nxt, force_nonblock);
4516 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004517 case IORING_OP_SPLICE:
4518 if (sqe) {
4519 ret = io_splice_prep(req, sqe);
4520 if (ret < 0)
4521 break;
4522 }
4523 ret = io_splice(req, nxt, force_nonblock);
4524 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004525 default:
4526 ret = -EINVAL;
4527 break;
4528 }
4529
Jens Axboedef596e2019-01-09 08:59:42 -07004530 if (ret)
4531 return ret;
4532
4533 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004534 const bool in_async = io_wq_current_is_worker();
4535
Jens Axboe9e645e112019-05-10 16:07:28 -06004536 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004537 return -EAGAIN;
4538
Jens Axboe11ba8202020-01-15 21:51:17 -07004539 /* workqueue context doesn't hold uring_lock, grab it now */
4540 if (in_async)
4541 mutex_lock(&ctx->uring_lock);
4542
Jens Axboedef596e2019-01-09 08:59:42 -07004543 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004544
4545 if (in_async)
4546 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004547 }
4548
4549 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004550}
4551
Jens Axboe561fb042019-10-24 07:25:42 -06004552static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004553{
Jens Axboe561fb042019-10-24 07:25:42 -06004554 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004555 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004556 struct io_kiocb *nxt = NULL;
4557 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004558
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004559 /* if NO_CANCEL is set, we must still run the work */
4560 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4561 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004562 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004563 }
Jens Axboe31b51512019-01-18 22:56:34 -07004564
Jens Axboe561fb042019-10-24 07:25:42 -06004565 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004566 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004567 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004568 /*
4569 * We can get EAGAIN for polled IO even though we're
4570 * forcing a sync submission from here, since we can't
4571 * wait for request slots on the block side.
4572 */
4573 if (ret != -EAGAIN)
4574 break;
4575 cond_resched();
4576 } while (1);
4577 }
Jens Axboe31b51512019-01-18 22:56:34 -07004578
Jens Axboe561fb042019-10-24 07:25:42 -06004579 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004580 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004581
Jens Axboe561fb042019-10-24 07:25:42 -06004582 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004583 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004584 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004585 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004586 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004587
Jens Axboe561fb042019-10-24 07:25:42 -06004588 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004589 if (!ret && nxt)
4590 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004591}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004592
Jens Axboe15b71ab2019-12-11 11:20:36 -07004593static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004594{
Jens Axboed3656342019-12-18 09:50:26 -07004595 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004596 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004597 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004598 return 0;
4599 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004600}
4601
Jens Axboe65e19f52019-10-26 07:20:21 -06004602static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4603 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004604{
Jens Axboe65e19f52019-10-26 07:20:21 -06004605 struct fixed_file_table *table;
4606
Jens Axboe05f3fb32019-12-09 11:22:50 -07004607 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4608 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004609}
4610
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004611static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
4612 int fd, struct file **out_file, bool fixed)
4613{
4614 struct io_ring_ctx *ctx = req->ctx;
4615 struct file *file;
4616
4617 if (fixed) {
4618 if (unlikely(!ctx->file_data ||
4619 (unsigned) fd >= ctx->nr_user_files))
4620 return -EBADF;
4621 fd = array_index_nospec(fd, ctx->nr_user_files);
4622 file = io_file_from_index(ctx, fd);
4623 if (!file)
4624 return -EBADF;
4625 percpu_ref_get(&ctx->file_data->refs);
4626 } else {
4627 trace_io_uring_file_get(ctx, fd);
4628 file = __io_file_get(state, fd);
4629 if (unlikely(!file))
4630 return -EBADF;
4631 }
4632
4633 *out_file = file;
4634 return 0;
4635}
4636
Jens Axboe3529d8c2019-12-19 18:24:38 -07004637static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4638 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004639{
4640 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004641 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004642 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06004643
Jens Axboe3529d8c2019-12-19 18:24:38 -07004644 flags = READ_ONCE(sqe->flags);
4645 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004646
Jens Axboed3656342019-12-18 09:50:26 -07004647 if (!io_req_needs_file(req, fd))
4648 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004649
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004650 fixed = (flags & IOSQE_FIXED_FILE);
4651 if (unlikely(!fixed && req->needs_fixed_file))
4652 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004653
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004654 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06004655}
4656
Jackie Liua197f662019-11-08 08:09:12 -07004657static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004658{
Jens Axboefcb323c2019-10-24 12:39:47 -06004659 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004660 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004661
Jens Axboef86cd202020-01-29 13:46:44 -07004662 if (req->work.files)
4663 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004664 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004665 return -EBADF;
4666
Jens Axboefcb323c2019-10-24 12:39:47 -06004667 rcu_read_lock();
4668 spin_lock_irq(&ctx->inflight_lock);
4669 /*
4670 * We use the f_ops->flush() handler to ensure that we can flush
4671 * out work accessing these files if the fd is closed. Check if
4672 * the fd has changed since we started down this path, and disallow
4673 * this operation if it has.
4674 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004675 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004676 list_add(&req->inflight_entry, &ctx->inflight_list);
4677 req->flags |= REQ_F_INFLIGHT;
4678 req->work.files = current->files;
4679 ret = 0;
4680 }
4681 spin_unlock_irq(&ctx->inflight_lock);
4682 rcu_read_unlock();
4683
4684 return ret;
4685}
4686
Jens Axboe2665abf2019-11-05 12:40:47 -07004687static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4688{
Jens Axboead8a48a2019-11-15 08:49:11 -07004689 struct io_timeout_data *data = container_of(timer,
4690 struct io_timeout_data, timer);
4691 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004692 struct io_ring_ctx *ctx = req->ctx;
4693 struct io_kiocb *prev = NULL;
4694 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004695
4696 spin_lock_irqsave(&ctx->completion_lock, flags);
4697
4698 /*
4699 * We don't expect the list to be empty, that will only happen if we
4700 * race with the completion of the linked work.
4701 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004702 if (!list_empty(&req->link_list)) {
4703 prev = list_entry(req->link_list.prev, struct io_kiocb,
4704 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004705 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004706 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004707 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4708 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004709 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004710 }
4711
4712 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4713
4714 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004715 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004716 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4717 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004718 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004719 } else {
4720 io_cqring_add_event(req, -ETIME);
4721 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004722 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004723 return HRTIMER_NORESTART;
4724}
4725
Jens Axboead8a48a2019-11-15 08:49:11 -07004726static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004727{
Jens Axboe76a46e02019-11-10 23:34:16 -07004728 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004729
Jens Axboe76a46e02019-11-10 23:34:16 -07004730 /*
4731 * If the list is now empty, then our linked request finished before
4732 * we got a chance to setup the timer
4733 */
4734 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004735 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004736 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004737
Jens Axboead8a48a2019-11-15 08:49:11 -07004738 data->timer.function = io_link_timeout_fn;
4739 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4740 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004741 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004742 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004743
Jens Axboe2665abf2019-11-05 12:40:47 -07004744 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004745 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004746}
4747
Jens Axboead8a48a2019-11-15 08:49:11 -07004748static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004749{
4750 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004751
Jens Axboe2665abf2019-11-05 12:40:47 -07004752 if (!(req->flags & REQ_F_LINK))
4753 return NULL;
4754
Pavel Begunkov44932332019-12-05 16:16:35 +03004755 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4756 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004757 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004758 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004759
Jens Axboe76a46e02019-11-10 23:34:16 -07004760 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004761 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004762}
4763
Jens Axboe3529d8c2019-12-19 18:24:38 -07004764static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004765{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004766 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004767 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004768 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004769 int ret;
4770
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004771again:
4772 linked_timeout = io_prep_linked_timeout(req);
4773
Jens Axboe193155c2020-02-22 23:22:19 -07004774 if (req->work.creds && req->work.creds != current_cred()) {
4775 if (old_creds)
4776 revert_creds(old_creds);
4777 if (old_creds == req->work.creds)
4778 old_creds = NULL; /* restored original creds */
4779 else
4780 old_creds = override_creds(req->work.creds);
4781 }
4782
Jens Axboe3529d8c2019-12-19 18:24:38 -07004783 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004784
4785 /*
4786 * We async punt it if the file wasn't marked NOWAIT, or if the file
4787 * doesn't support non-blocking read/write attempts
4788 */
4789 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4790 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004791punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004792 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004793 ret = io_grab_files(req);
4794 if (ret)
4795 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004796 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004797
4798 /*
4799 * Queued up for async execution, worker will release
4800 * submit reference when the iocb is actually submitted.
4801 */
4802 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004803 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004804 }
Jens Axboee65ef562019-03-12 10:16:44 -06004805
Jens Axboefcb323c2019-10-24 12:39:47 -06004806err:
Jens Axboee65ef562019-03-12 10:16:44 -06004807 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004808 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004809
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004810 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004811 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004812 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004813 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004814 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004815 }
4816
Jens Axboee65ef562019-03-12 10:16:44 -06004817 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004818 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004819 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004820 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004821 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004822 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004823done_req:
4824 if (nxt) {
4825 req = nxt;
4826 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004827
4828 if (req->flags & REQ_F_FORCE_ASYNC)
4829 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004830 goto again;
4831 }
Jens Axboe193155c2020-02-22 23:22:19 -07004832 if (old_creds)
4833 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004834}
4835
Jens Axboe3529d8c2019-12-19 18:24:38 -07004836static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004837{
4838 int ret;
4839
Jens Axboe3529d8c2019-12-19 18:24:38 -07004840 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004841 if (ret) {
4842 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004843fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004844 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004845 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004846 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004847 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004848 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004849 ret = io_req_defer_prep(req, sqe);
4850 if (unlikely(ret < 0))
4851 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004852 /*
4853 * Never try inline submit of IOSQE_ASYNC is set, go straight
4854 * to async execution.
4855 */
4856 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4857 io_queue_async_work(req);
4858 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004859 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004860 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004861}
4862
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004863static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004864{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004865 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004866 io_cqring_add_event(req, -ECANCELED);
4867 io_double_put_req(req);
4868 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004869 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004870}
4871
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004872#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004873 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004874
Jens Axboe3529d8c2019-12-19 18:24:38 -07004875static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4876 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004877{
Jackie Liua197f662019-11-08 08:09:12 -07004878 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004879 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004880 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004881
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004882 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004883
4884 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004885 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004886 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004887 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004888 }
4889
Jens Axboe75c6a032020-01-28 10:15:23 -07004890 id = READ_ONCE(sqe->personality);
4891 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004892 req->work.creds = idr_find(&ctx->personality_idr, id);
4893 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004894 ret = -EINVAL;
4895 goto err_req;
4896 }
Jens Axboe193155c2020-02-22 23:22:19 -07004897 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004898 }
4899
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004900 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004901 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
4902 IOSQE_ASYNC | IOSQE_FIXED_FILE);
Jens Axboe9e645e112019-05-10 16:07:28 -06004903
Jens Axboe3529d8c2019-12-19 18:24:38 -07004904 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004905 if (unlikely(ret)) {
4906err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004907 io_cqring_add_event(req, ret);
4908 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004909 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004910 }
4911
Jens Axboe9e645e112019-05-10 16:07:28 -06004912 /*
4913 * If we already have a head request, queue this one for async
4914 * submittal once the head completes. If we don't have a head but
4915 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4916 * submitted sync once the chain is complete. If none of those
4917 * conditions are true (normal request), then just queue it.
4918 */
4919 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004920 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004921
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004922 /*
4923 * Taking sequential execution of a link, draining both sides
4924 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4925 * requests in the link. So, it drains the head and the
4926 * next after the link request. The last one is done via
4927 * drain_next flag to persist the effect across calls.
4928 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004929 if (sqe_flags & IOSQE_IO_DRAIN) {
4930 head->flags |= REQ_F_IO_DRAIN;
4931 ctx->drain_next = 1;
4932 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004933 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004934 ret = -EAGAIN;
4935 goto err_req;
4936 }
4937
Jens Axboe3529d8c2019-12-19 18:24:38 -07004938 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004939 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004940 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004941 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004942 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004943 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004944 trace_io_uring_link(ctx, req, head);
4945 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004946
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004947 /* last request of a link, enqueue the link */
4948 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4949 io_queue_link_head(head);
4950 *link = NULL;
4951 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004952 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004953 if (unlikely(ctx->drain_next)) {
4954 req->flags |= REQ_F_IO_DRAIN;
4955 req->ctx->drain_next = 0;
4956 }
4957 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4958 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004959 INIT_LIST_HEAD(&req->link_list);
4960 ret = io_req_defer_prep(req, sqe);
4961 if (ret)
4962 req->flags |= REQ_F_FAIL_LINK;
4963 *link = req;
4964 } else {
4965 io_queue_sqe(req, sqe);
4966 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004967 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004968
4969 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004970}
4971
Jens Axboe9a56a232019-01-09 09:06:50 -07004972/*
4973 * Batched submission is done, ensure local IO is flushed out.
4974 */
4975static void io_submit_state_end(struct io_submit_state *state)
4976{
4977 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004978 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004979 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004980 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004981}
4982
4983/*
4984 * Start submission side cache.
4985 */
4986static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004987 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004988{
4989 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004990 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004991 state->file = NULL;
4992 state->ios_left = max_ios;
4993}
4994
Jens Axboe2b188cc2019-01-07 10:46:33 -07004995static void io_commit_sqring(struct io_ring_ctx *ctx)
4996{
Hristo Venev75b28af2019-08-26 17:23:46 +00004997 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004998
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004999 /*
5000 * Ensure any loads from the SQEs are done at this point,
5001 * since once we write the new head, the application could
5002 * write new data to them.
5003 */
5004 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005005}
5006
5007/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005008 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005009 * that is mapped by userspace. This means that care needs to be taken to
5010 * ensure that reads are stable, as we cannot rely on userspace always
5011 * being a good citizen. If members of the sqe are validated and then later
5012 * used, it's important that those reads are done through READ_ONCE() to
5013 * prevent a re-load down the line.
5014 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005015static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5016 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005017{
Hristo Venev75b28af2019-08-26 17:23:46 +00005018 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005019 unsigned head;
5020
5021 /*
5022 * The cached sq head (or cq tail) serves two purposes:
5023 *
5024 * 1) allows us to batch the cost of updating the user visible
5025 * head updates.
5026 * 2) allows the kernel side to track the head on its own, even
5027 * though the application is the one updating it.
5028 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005029 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005030 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005031 /*
5032 * All io need record the previous position, if LINK vs DARIN,
5033 * it can be used to mark the position of the first IO in the
5034 * link list.
5035 */
5036 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005037 *sqe_ptr = &ctx->sq_sqes[head];
5038 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5039 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005040 ctx->cached_sq_head++;
5041 return true;
5042 }
5043
5044 /* drop invalid entries */
5045 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005046 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005047 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005048 return false;
5049}
5050
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005051static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005052 struct file *ring_file, int ring_fd,
5053 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005054{
5055 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005056 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005057 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005058 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005059
Jens Axboec4a2ed72019-11-21 21:01:26 -07005060 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005061 if (test_bit(0, &ctx->sq_check_overflow)) {
5062 if (!list_empty(&ctx->cq_overflow_list) &&
5063 !io_cqring_overflow_flush(ctx, false))
5064 return -EBUSY;
5065 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005066
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005067 /* make sure SQ entry isn't read before tail */
5068 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005069
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005070 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5071 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005072
5073 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005074 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005075 statep = &state;
5076 }
5077
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005078 ctx->ring_fd = ring_fd;
5079 ctx->ring_file = ring_file;
5080
Jens Axboe6c271ce2019-01-10 11:22:30 -07005081 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005082 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005083 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005084 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005085
Pavel Begunkov196be952019-11-07 01:41:06 +03005086 req = io_get_req(ctx, statep);
5087 if (unlikely(!req)) {
5088 if (!submitted)
5089 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005090 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005091 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005092 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005093 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005094 break;
5095 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005096
Jens Axboed3656342019-12-18 09:50:26 -07005097 /* will complete beyond this point, count as submitted */
5098 submitted++;
5099
5100 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005101 err = -EINVAL;
5102fail_req:
5103 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005104 io_double_put_req(req);
5105 break;
5106 }
5107
5108 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005109 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005110 if (unlikely(mm_fault)) {
5111 err = -EFAULT;
5112 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005113 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005114 use_mm(ctx->sqo_mm);
5115 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005116 }
5117
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005118 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005119 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5120 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005121 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005122 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005123 }
5124
Pavel Begunkov9466f432020-01-25 22:34:01 +03005125 if (unlikely(submitted != nr)) {
5126 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5127
5128 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5129 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005130 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005131 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005132 if (statep)
5133 io_submit_state_end(&state);
5134
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005135 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5136 io_commit_sqring(ctx);
5137
Jens Axboe6c271ce2019-01-10 11:22:30 -07005138 return submitted;
5139}
5140
5141static int io_sq_thread(void *data)
5142{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005143 struct io_ring_ctx *ctx = data;
5144 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005145 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005146 mm_segment_t old_fs;
5147 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005148 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005149 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005150
Jens Axboe206aefd2019-11-07 18:27:42 -07005151 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005152
Jens Axboe6c271ce2019-01-10 11:22:30 -07005153 old_fs = get_fs();
5154 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005155 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005156
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005157 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005158 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005159 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005160
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005161 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005162 unsigned nr_events = 0;
5163
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005164 mutex_lock(&ctx->uring_lock);
5165 if (!list_empty(&ctx->poll_list))
5166 io_iopoll_getevents(ctx, &nr_events, 0);
5167 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005168 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005169 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005170 }
5171
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005172 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005173
5174 /*
5175 * If submit got -EBUSY, flag us as needing the application
5176 * to enter the kernel to reap and flush events.
5177 */
5178 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005179 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005180 * Drop cur_mm before scheduling, we can't hold it for
5181 * long periods (or over schedule()). Do this before
5182 * adding ourselves to the waitqueue, as the unuse/drop
5183 * may sleep.
5184 */
5185 if (cur_mm) {
5186 unuse_mm(cur_mm);
5187 mmput(cur_mm);
5188 cur_mm = NULL;
5189 }
5190
5191 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005192 * We're polling. If we're within the defined idle
5193 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005194 * to sleep. The exception is if we got EBUSY doing
5195 * more IO, we should wait for the application to
5196 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005197 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005198 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005199 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5200 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005201 if (current->task_works)
5202 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005203 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005204 continue;
5205 }
5206
Jens Axboe6c271ce2019-01-10 11:22:30 -07005207 prepare_to_wait(&ctx->sqo_wait, &wait,
5208 TASK_INTERRUPTIBLE);
5209
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005210 /*
5211 * While doing polled IO, before going to sleep, we need
5212 * to check if there are new reqs added to poll_list, it
5213 * is because reqs may have been punted to io worker and
5214 * will be added to poll_list later, hence check the
5215 * poll_list again.
5216 */
5217 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5218 !list_empty_careful(&ctx->poll_list)) {
5219 finish_wait(&ctx->sqo_wait, &wait);
5220 continue;
5221 }
5222
Jens Axboe6c271ce2019-01-10 11:22:30 -07005223 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005224 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005225 /* make sure to read SQ tail after writing flags */
5226 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005227
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005228 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005229 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005230 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005231 finish_wait(&ctx->sqo_wait, &wait);
5232 break;
5233 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005234 if (current->task_works) {
5235 task_work_run();
5236 continue;
5237 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005238 if (signal_pending(current))
5239 flush_signals(current);
5240 schedule();
5241 finish_wait(&ctx->sqo_wait, &wait);
5242
Hristo Venev75b28af2019-08-26 17:23:46 +00005243 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005244 continue;
5245 }
5246 finish_wait(&ctx->sqo_wait, &wait);
5247
Hristo Venev75b28af2019-08-26 17:23:46 +00005248 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005249 }
5250
Jens Axboe8a4955f2019-12-09 14:52:35 -07005251 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005252 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005253 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005254 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005255 }
5256
Jens Axboeb41e9852020-02-17 09:52:41 -07005257 if (current->task_works)
5258 task_work_run();
5259
Jens Axboe6c271ce2019-01-10 11:22:30 -07005260 set_fs(old_fs);
5261 if (cur_mm) {
5262 unuse_mm(cur_mm);
5263 mmput(cur_mm);
5264 }
Jens Axboe181e4482019-11-25 08:52:30 -07005265 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005266
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005267 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005268
Jens Axboe6c271ce2019-01-10 11:22:30 -07005269 return 0;
5270}
5271
Jens Axboebda52162019-09-24 13:47:15 -06005272struct io_wait_queue {
5273 struct wait_queue_entry wq;
5274 struct io_ring_ctx *ctx;
5275 unsigned to_wait;
5276 unsigned nr_timeouts;
5277};
5278
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005279static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005280{
5281 struct io_ring_ctx *ctx = iowq->ctx;
5282
5283 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005284 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005285 * started waiting. For timeouts, we always want to return to userspace,
5286 * regardless of event count.
5287 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005288 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005289 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5290}
5291
5292static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5293 int wake_flags, void *key)
5294{
5295 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5296 wq);
5297
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005298 /* use noflush == true, as we can't safely rely on locking context */
5299 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005300 return -1;
5301
5302 return autoremove_wake_function(curr, mode, wake_flags, key);
5303}
5304
Jens Axboe2b188cc2019-01-07 10:46:33 -07005305/*
5306 * Wait until events become available, if we don't already have some. The
5307 * application must reap them itself, as they reside on the shared cq ring.
5308 */
5309static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5310 const sigset_t __user *sig, size_t sigsz)
5311{
Jens Axboebda52162019-09-24 13:47:15 -06005312 struct io_wait_queue iowq = {
5313 .wq = {
5314 .private = current,
5315 .func = io_wake_function,
5316 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5317 },
5318 .ctx = ctx,
5319 .to_wait = min_events,
5320 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005321 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005322 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005323
Jens Axboeb41e9852020-02-17 09:52:41 -07005324 do {
5325 if (io_cqring_events(ctx, false) >= min_events)
5326 return 0;
5327 if (!current->task_works)
5328 break;
5329 task_work_run();
5330 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005331
5332 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005333#ifdef CONFIG_COMPAT
5334 if (in_compat_syscall())
5335 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005336 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005337 else
5338#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005339 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005340
Jens Axboe2b188cc2019-01-07 10:46:33 -07005341 if (ret)
5342 return ret;
5343 }
5344
Jens Axboebda52162019-09-24 13:47:15 -06005345 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005346 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005347 do {
5348 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5349 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005350 if (current->task_works)
5351 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005352 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005353 break;
5354 schedule();
5355 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005356 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005357 break;
5358 }
5359 } while (1);
5360 finish_wait(&ctx->wait, &iowq.wq);
5361
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005362 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005363
Hristo Venev75b28af2019-08-26 17:23:46 +00005364 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005365}
5366
Jens Axboe6b063142019-01-10 22:13:58 -07005367static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5368{
5369#if defined(CONFIG_UNIX)
5370 if (ctx->ring_sock) {
5371 struct sock *sock = ctx->ring_sock->sk;
5372 struct sk_buff *skb;
5373
5374 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5375 kfree_skb(skb);
5376 }
5377#else
5378 int i;
5379
Jens Axboe65e19f52019-10-26 07:20:21 -06005380 for (i = 0; i < ctx->nr_user_files; i++) {
5381 struct file *file;
5382
5383 file = io_file_from_index(ctx, i);
5384 if (file)
5385 fput(file);
5386 }
Jens Axboe6b063142019-01-10 22:13:58 -07005387#endif
5388}
5389
Jens Axboe05f3fb32019-12-09 11:22:50 -07005390static void io_file_ref_kill(struct percpu_ref *ref)
5391{
5392 struct fixed_file_data *data;
5393
5394 data = container_of(ref, struct fixed_file_data, refs);
5395 complete(&data->done);
5396}
5397
Jens Axboe6b063142019-01-10 22:13:58 -07005398static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5399{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005400 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005401 unsigned nr_tables, i;
5402
Jens Axboe05f3fb32019-12-09 11:22:50 -07005403 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005404 return -ENXIO;
5405
Jens Axboe05f3fb32019-12-09 11:22:50 -07005406 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005407 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005408 wait_for_completion(&data->done);
5409 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005410 percpu_ref_exit(&data->refs);
5411
Jens Axboe6b063142019-01-10 22:13:58 -07005412 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005413 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5414 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005415 kfree(data->table[i].files);
5416 kfree(data->table);
5417 kfree(data);
5418 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005419 ctx->nr_user_files = 0;
5420 return 0;
5421}
5422
Jens Axboe6c271ce2019-01-10 11:22:30 -07005423static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5424{
5425 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005426 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005427 /*
5428 * The park is a bit of a work-around, without it we get
5429 * warning spews on shutdown with SQPOLL set and affinity
5430 * set to a single CPU.
5431 */
Jens Axboe06058632019-04-13 09:26:03 -06005432 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005433 kthread_stop(ctx->sqo_thread);
5434 ctx->sqo_thread = NULL;
5435 }
5436}
5437
Jens Axboe6b063142019-01-10 22:13:58 -07005438static void io_finish_async(struct io_ring_ctx *ctx)
5439{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005440 io_sq_thread_stop(ctx);
5441
Jens Axboe561fb042019-10-24 07:25:42 -06005442 if (ctx->io_wq) {
5443 io_wq_destroy(ctx->io_wq);
5444 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005445 }
5446}
5447
5448#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005449/*
5450 * Ensure the UNIX gc is aware of our file set, so we are certain that
5451 * the io_uring can be safely unregistered on process exit, even if we have
5452 * loops in the file referencing.
5453 */
5454static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5455{
5456 struct sock *sk = ctx->ring_sock->sk;
5457 struct scm_fp_list *fpl;
5458 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005459 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005460
5461 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5462 unsigned long inflight = ctx->user->unix_inflight + nr;
5463
5464 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5465 return -EMFILE;
5466 }
5467
5468 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5469 if (!fpl)
5470 return -ENOMEM;
5471
5472 skb = alloc_skb(0, GFP_KERNEL);
5473 if (!skb) {
5474 kfree(fpl);
5475 return -ENOMEM;
5476 }
5477
5478 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005479
Jens Axboe08a45172019-10-03 08:11:03 -06005480 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005481 fpl->user = get_uid(ctx->user);
5482 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005483 struct file *file = io_file_from_index(ctx, i + offset);
5484
5485 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005486 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005487 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005488 unix_inflight(fpl->user, fpl->fp[nr_files]);
5489 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005490 }
5491
Jens Axboe08a45172019-10-03 08:11:03 -06005492 if (nr_files) {
5493 fpl->max = SCM_MAX_FD;
5494 fpl->count = nr_files;
5495 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005496 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005497 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5498 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005499
Jens Axboe08a45172019-10-03 08:11:03 -06005500 for (i = 0; i < nr_files; i++)
5501 fput(fpl->fp[i]);
5502 } else {
5503 kfree_skb(skb);
5504 kfree(fpl);
5505 }
Jens Axboe6b063142019-01-10 22:13:58 -07005506
5507 return 0;
5508}
5509
5510/*
5511 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5512 * causes regular reference counting to break down. We rely on the UNIX
5513 * garbage collection to take care of this problem for us.
5514 */
5515static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5516{
5517 unsigned left, total;
5518 int ret = 0;
5519
5520 total = 0;
5521 left = ctx->nr_user_files;
5522 while (left) {
5523 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005524
5525 ret = __io_sqe_files_scm(ctx, this_files, total);
5526 if (ret)
5527 break;
5528 left -= this_files;
5529 total += this_files;
5530 }
5531
5532 if (!ret)
5533 return 0;
5534
5535 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005536 struct file *file = io_file_from_index(ctx, total);
5537
5538 if (file)
5539 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005540 total++;
5541 }
5542
5543 return ret;
5544}
5545#else
5546static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5547{
5548 return 0;
5549}
5550#endif
5551
Jens Axboe65e19f52019-10-26 07:20:21 -06005552static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5553 unsigned nr_files)
5554{
5555 int i;
5556
5557 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005558 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005559 unsigned this_files;
5560
5561 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5562 table->files = kcalloc(this_files, sizeof(struct file *),
5563 GFP_KERNEL);
5564 if (!table->files)
5565 break;
5566 nr_files -= this_files;
5567 }
5568
5569 if (i == nr_tables)
5570 return 0;
5571
5572 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005573 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005574 kfree(table->files);
5575 }
5576 return 1;
5577}
5578
Jens Axboe05f3fb32019-12-09 11:22:50 -07005579static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005580{
5581#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005582 struct sock *sock = ctx->ring_sock->sk;
5583 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5584 struct sk_buff *skb;
5585 int i;
5586
5587 __skb_queue_head_init(&list);
5588
5589 /*
5590 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5591 * remove this entry and rearrange the file array.
5592 */
5593 skb = skb_dequeue(head);
5594 while (skb) {
5595 struct scm_fp_list *fp;
5596
5597 fp = UNIXCB(skb).fp;
5598 for (i = 0; i < fp->count; i++) {
5599 int left;
5600
5601 if (fp->fp[i] != file)
5602 continue;
5603
5604 unix_notinflight(fp->user, fp->fp[i]);
5605 left = fp->count - 1 - i;
5606 if (left) {
5607 memmove(&fp->fp[i], &fp->fp[i + 1],
5608 left * sizeof(struct file *));
5609 }
5610 fp->count--;
5611 if (!fp->count) {
5612 kfree_skb(skb);
5613 skb = NULL;
5614 } else {
5615 __skb_queue_tail(&list, skb);
5616 }
5617 fput(file);
5618 file = NULL;
5619 break;
5620 }
5621
5622 if (!file)
5623 break;
5624
5625 __skb_queue_tail(&list, skb);
5626
5627 skb = skb_dequeue(head);
5628 }
5629
5630 if (skb_peek(&list)) {
5631 spin_lock_irq(&head->lock);
5632 while ((skb = __skb_dequeue(&list)) != NULL)
5633 __skb_queue_tail(head, skb);
5634 spin_unlock_irq(&head->lock);
5635 }
5636#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005637 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005638#endif
5639}
5640
Jens Axboe05f3fb32019-12-09 11:22:50 -07005641struct io_file_put {
5642 struct llist_node llist;
5643 struct file *file;
5644 struct completion *done;
5645};
5646
Jens Axboe2faf8522020-02-04 19:54:55 -07005647static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005648{
5649 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005650 struct llist_node *node;
5651
Jens Axboe05f3fb32019-12-09 11:22:50 -07005652 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5653 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5654 io_ring_file_put(data->ctx, pfile->file);
5655 if (pfile->done)
5656 complete(pfile->done);
5657 else
5658 kfree(pfile);
5659 }
5660 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005661}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005662
Jens Axboe2faf8522020-02-04 19:54:55 -07005663static void io_ring_file_ref_switch(struct work_struct *work)
5664{
5665 struct fixed_file_data *data;
5666
5667 data = container_of(work, struct fixed_file_data, ref_work);
5668 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005669 percpu_ref_switch_to_percpu(&data->refs);
5670}
5671
5672static void io_file_data_ref_zero(struct percpu_ref *ref)
5673{
5674 struct fixed_file_data *data;
5675
5676 data = container_of(ref, struct fixed_file_data, refs);
5677
Jens Axboe2faf8522020-02-04 19:54:55 -07005678 /*
5679 * We can't safely switch from inside this context, punt to wq. If
5680 * the table ref is going away, the table is being unregistered.
5681 * Don't queue up the async work for that case, the caller will
5682 * handle it.
5683 */
5684 if (!percpu_ref_is_dying(&data->refs))
5685 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005686}
5687
5688static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5689 unsigned nr_args)
5690{
5691 __s32 __user *fds = (__s32 __user *) arg;
5692 unsigned nr_tables;
5693 struct file *file;
5694 int fd, ret = 0;
5695 unsigned i;
5696
5697 if (ctx->file_data)
5698 return -EBUSY;
5699 if (!nr_args)
5700 return -EINVAL;
5701 if (nr_args > IORING_MAX_FIXED_FILES)
5702 return -EMFILE;
5703
5704 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5705 if (!ctx->file_data)
5706 return -ENOMEM;
5707 ctx->file_data->ctx = ctx;
5708 init_completion(&ctx->file_data->done);
5709
5710 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5711 ctx->file_data->table = kcalloc(nr_tables,
5712 sizeof(struct fixed_file_table),
5713 GFP_KERNEL);
5714 if (!ctx->file_data->table) {
5715 kfree(ctx->file_data);
5716 ctx->file_data = NULL;
5717 return -ENOMEM;
5718 }
5719
5720 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5721 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5722 kfree(ctx->file_data->table);
5723 kfree(ctx->file_data);
5724 ctx->file_data = NULL;
5725 return -ENOMEM;
5726 }
5727 ctx->file_data->put_llist.first = NULL;
5728 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5729
5730 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5731 percpu_ref_exit(&ctx->file_data->refs);
5732 kfree(ctx->file_data->table);
5733 kfree(ctx->file_data);
5734 ctx->file_data = NULL;
5735 return -ENOMEM;
5736 }
5737
5738 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5739 struct fixed_file_table *table;
5740 unsigned index;
5741
5742 ret = -EFAULT;
5743 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5744 break;
5745 /* allow sparse sets */
5746 if (fd == -1) {
5747 ret = 0;
5748 continue;
5749 }
5750
5751 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5752 index = i & IORING_FILE_TABLE_MASK;
5753 file = fget(fd);
5754
5755 ret = -EBADF;
5756 if (!file)
5757 break;
5758
5759 /*
5760 * Don't allow io_uring instances to be registered. If UNIX
5761 * isn't enabled, then this causes a reference cycle and this
5762 * instance can never get freed. If UNIX is enabled we'll
5763 * handle it just fine, but there's still no point in allowing
5764 * a ring fd as it doesn't support regular read/write anyway.
5765 */
5766 if (file->f_op == &io_uring_fops) {
5767 fput(file);
5768 break;
5769 }
5770 ret = 0;
5771 table->files[index] = file;
5772 }
5773
5774 if (ret) {
5775 for (i = 0; i < ctx->nr_user_files; i++) {
5776 file = io_file_from_index(ctx, i);
5777 if (file)
5778 fput(file);
5779 }
5780 for (i = 0; i < nr_tables; i++)
5781 kfree(ctx->file_data->table[i].files);
5782
5783 kfree(ctx->file_data->table);
5784 kfree(ctx->file_data);
5785 ctx->file_data = NULL;
5786 ctx->nr_user_files = 0;
5787 return ret;
5788 }
5789
5790 ret = io_sqe_files_scm(ctx);
5791 if (ret)
5792 io_sqe_files_unregister(ctx);
5793
5794 return ret;
5795}
5796
Jens Axboec3a31e62019-10-03 13:59:56 -06005797static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5798 int index)
5799{
5800#if defined(CONFIG_UNIX)
5801 struct sock *sock = ctx->ring_sock->sk;
5802 struct sk_buff_head *head = &sock->sk_receive_queue;
5803 struct sk_buff *skb;
5804
5805 /*
5806 * See if we can merge this file into an existing skb SCM_RIGHTS
5807 * file set. If there's no room, fall back to allocating a new skb
5808 * and filling it in.
5809 */
5810 spin_lock_irq(&head->lock);
5811 skb = skb_peek(head);
5812 if (skb) {
5813 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5814
5815 if (fpl->count < SCM_MAX_FD) {
5816 __skb_unlink(skb, head);
5817 spin_unlock_irq(&head->lock);
5818 fpl->fp[fpl->count] = get_file(file);
5819 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5820 fpl->count++;
5821 spin_lock_irq(&head->lock);
5822 __skb_queue_head(head, skb);
5823 } else {
5824 skb = NULL;
5825 }
5826 }
5827 spin_unlock_irq(&head->lock);
5828
5829 if (skb) {
5830 fput(file);
5831 return 0;
5832 }
5833
5834 return __io_sqe_files_scm(ctx, 1, index);
5835#else
5836 return 0;
5837#endif
5838}
5839
Jens Axboe05f3fb32019-12-09 11:22:50 -07005840static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005841{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005842 struct fixed_file_data *data;
5843
Jens Axboedd3db2a2020-02-26 10:23:43 -07005844 /*
5845 * Juggle reference to ensure we hit zero, if needed, so we can
5846 * switch back to percpu mode
5847 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005848 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005849 percpu_ref_put(&data->refs);
5850 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005851}
5852
5853static bool io_queue_file_removal(struct fixed_file_data *data,
5854 struct file *file)
5855{
5856 struct io_file_put *pfile, pfile_stack;
5857 DECLARE_COMPLETION_ONSTACK(done);
5858
5859 /*
5860 * If we fail allocating the struct we need for doing async reomval
5861 * of this file, just punt to sync and wait for it.
5862 */
5863 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5864 if (!pfile) {
5865 pfile = &pfile_stack;
5866 pfile->done = &done;
5867 }
5868
5869 pfile->file = file;
5870 llist_add(&pfile->llist, &data->put_llist);
5871
5872 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005873 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005874 wait_for_completion(&done);
5875 flush_work(&data->ref_work);
5876 return false;
5877 }
5878
5879 return true;
5880}
5881
5882static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5883 struct io_uring_files_update *up,
5884 unsigned nr_args)
5885{
5886 struct fixed_file_data *data = ctx->file_data;
5887 bool ref_switch = false;
5888 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005889 __s32 __user *fds;
5890 int fd, i, err;
5891 __u32 done;
5892
Jens Axboe05f3fb32019-12-09 11:22:50 -07005893 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005894 return -EOVERFLOW;
5895 if (done > ctx->nr_user_files)
5896 return -EINVAL;
5897
5898 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005899 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005900 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005901 struct fixed_file_table *table;
5902 unsigned index;
5903
Jens Axboec3a31e62019-10-03 13:59:56 -06005904 err = 0;
5905 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5906 err = -EFAULT;
5907 break;
5908 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005909 i = array_index_nospec(up->offset, ctx->nr_user_files);
5910 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005911 index = i & IORING_FILE_TABLE_MASK;
5912 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005913 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005914 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005915 if (io_queue_file_removal(data, file))
5916 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005917 }
5918 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005919 file = fget(fd);
5920 if (!file) {
5921 err = -EBADF;
5922 break;
5923 }
5924 /*
5925 * Don't allow io_uring instances to be registered. If
5926 * UNIX isn't enabled, then this causes a reference
5927 * cycle and this instance can never get freed. If UNIX
5928 * is enabled we'll handle it just fine, but there's
5929 * still no point in allowing a ring fd as it doesn't
5930 * support regular read/write anyway.
5931 */
5932 if (file->f_op == &io_uring_fops) {
5933 fput(file);
5934 err = -EBADF;
5935 break;
5936 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005937 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005938 err = io_sqe_file_register(ctx, file, i);
5939 if (err)
5940 break;
5941 }
5942 nr_args--;
5943 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005944 up->offset++;
5945 }
5946
Jens Axboedd3db2a2020-02-26 10:23:43 -07005947 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005948 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005949
5950 return done ? done : err;
5951}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005952static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5953 unsigned nr_args)
5954{
5955 struct io_uring_files_update up;
5956
5957 if (!ctx->file_data)
5958 return -ENXIO;
5959 if (!nr_args)
5960 return -EINVAL;
5961 if (copy_from_user(&up, arg, sizeof(up)))
5962 return -EFAULT;
5963 if (up.resv)
5964 return -EINVAL;
5965
5966 return __io_sqe_files_update(ctx, &up, nr_args);
5967}
Jens Axboec3a31e62019-10-03 13:59:56 -06005968
Jens Axboe7d723062019-11-12 22:31:31 -07005969static void io_put_work(struct io_wq_work *work)
5970{
5971 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5972
5973 io_put_req(req);
5974}
5975
5976static void io_get_work(struct io_wq_work *work)
5977{
5978 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5979
5980 refcount_inc(&req->refs);
5981}
5982
Pavel Begunkov24369c22020-01-28 03:15:48 +03005983static int io_init_wq_offload(struct io_ring_ctx *ctx,
5984 struct io_uring_params *p)
5985{
5986 struct io_wq_data data;
5987 struct fd f;
5988 struct io_ring_ctx *ctx_attach;
5989 unsigned int concurrency;
5990 int ret = 0;
5991
5992 data.user = ctx->user;
5993 data.get_work = io_get_work;
5994 data.put_work = io_put_work;
5995
5996 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5997 /* Do QD, or 4 * CPUS, whatever is smallest */
5998 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5999
6000 ctx->io_wq = io_wq_create(concurrency, &data);
6001 if (IS_ERR(ctx->io_wq)) {
6002 ret = PTR_ERR(ctx->io_wq);
6003 ctx->io_wq = NULL;
6004 }
6005 return ret;
6006 }
6007
6008 f = fdget(p->wq_fd);
6009 if (!f.file)
6010 return -EBADF;
6011
6012 if (f.file->f_op != &io_uring_fops) {
6013 ret = -EINVAL;
6014 goto out_fput;
6015 }
6016
6017 ctx_attach = f.file->private_data;
6018 /* @io_wq is protected by holding the fd */
6019 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6020 ret = -EINVAL;
6021 goto out_fput;
6022 }
6023
6024 ctx->io_wq = ctx_attach->io_wq;
6025out_fput:
6026 fdput(f);
6027 return ret;
6028}
6029
Jens Axboe6c271ce2019-01-10 11:22:30 -07006030static int io_sq_offload_start(struct io_ring_ctx *ctx,
6031 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006032{
6033 int ret;
6034
Jens Axboe6c271ce2019-01-10 11:22:30 -07006035 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006036 mmgrab(current->mm);
6037 ctx->sqo_mm = current->mm;
6038
Jens Axboe6c271ce2019-01-10 11:22:30 -07006039 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006040 ret = -EPERM;
6041 if (!capable(CAP_SYS_ADMIN))
6042 goto err;
6043
Jens Axboe917257d2019-04-13 09:28:55 -06006044 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6045 if (!ctx->sq_thread_idle)
6046 ctx->sq_thread_idle = HZ;
6047
Jens Axboe6c271ce2019-01-10 11:22:30 -07006048 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006049 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006050
Jens Axboe917257d2019-04-13 09:28:55 -06006051 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006052 if (cpu >= nr_cpu_ids)
6053 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006054 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006055 goto err;
6056
Jens Axboe6c271ce2019-01-10 11:22:30 -07006057 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6058 ctx, cpu,
6059 "io_uring-sq");
6060 } else {
6061 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6062 "io_uring-sq");
6063 }
6064 if (IS_ERR(ctx->sqo_thread)) {
6065 ret = PTR_ERR(ctx->sqo_thread);
6066 ctx->sqo_thread = NULL;
6067 goto err;
6068 }
6069 wake_up_process(ctx->sqo_thread);
6070 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6071 /* Can't have SQ_AFF without SQPOLL */
6072 ret = -EINVAL;
6073 goto err;
6074 }
6075
Pavel Begunkov24369c22020-01-28 03:15:48 +03006076 ret = io_init_wq_offload(ctx, p);
6077 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006078 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006079
6080 return 0;
6081err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006082 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006083 mmdrop(ctx->sqo_mm);
6084 ctx->sqo_mm = NULL;
6085 return ret;
6086}
6087
6088static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6089{
6090 atomic_long_sub(nr_pages, &user->locked_vm);
6091}
6092
6093static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6094{
6095 unsigned long page_limit, cur_pages, new_pages;
6096
6097 /* Don't allow more pages than we can safely lock */
6098 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6099
6100 do {
6101 cur_pages = atomic_long_read(&user->locked_vm);
6102 new_pages = cur_pages + nr_pages;
6103 if (new_pages > page_limit)
6104 return -ENOMEM;
6105 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6106 new_pages) != cur_pages);
6107
6108 return 0;
6109}
6110
6111static void io_mem_free(void *ptr)
6112{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006113 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006114
Mark Rutland52e04ef2019-04-30 17:30:21 +01006115 if (!ptr)
6116 return;
6117
6118 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006119 if (put_page_testzero(page))
6120 free_compound_page(page);
6121}
6122
6123static void *io_mem_alloc(size_t size)
6124{
6125 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6126 __GFP_NORETRY;
6127
6128 return (void *) __get_free_pages(gfp_flags, get_order(size));
6129}
6130
Hristo Venev75b28af2019-08-26 17:23:46 +00006131static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6132 size_t *sq_offset)
6133{
6134 struct io_rings *rings;
6135 size_t off, sq_array_size;
6136
6137 off = struct_size(rings, cqes, cq_entries);
6138 if (off == SIZE_MAX)
6139 return SIZE_MAX;
6140
6141#ifdef CONFIG_SMP
6142 off = ALIGN(off, SMP_CACHE_BYTES);
6143 if (off == 0)
6144 return SIZE_MAX;
6145#endif
6146
6147 sq_array_size = array_size(sizeof(u32), sq_entries);
6148 if (sq_array_size == SIZE_MAX)
6149 return SIZE_MAX;
6150
6151 if (check_add_overflow(off, sq_array_size, &off))
6152 return SIZE_MAX;
6153
6154 if (sq_offset)
6155 *sq_offset = off;
6156
6157 return off;
6158}
6159
Jens Axboe2b188cc2019-01-07 10:46:33 -07006160static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6161{
Hristo Venev75b28af2019-08-26 17:23:46 +00006162 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006163
Hristo Venev75b28af2019-08-26 17:23:46 +00006164 pages = (size_t)1 << get_order(
6165 rings_size(sq_entries, cq_entries, NULL));
6166 pages += (size_t)1 << get_order(
6167 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006168
Hristo Venev75b28af2019-08-26 17:23:46 +00006169 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006170}
6171
Jens Axboeedafcce2019-01-09 09:16:05 -07006172static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6173{
6174 int i, j;
6175
6176 if (!ctx->user_bufs)
6177 return -ENXIO;
6178
6179 for (i = 0; i < ctx->nr_user_bufs; i++) {
6180 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6181
6182 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006183 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006184
6185 if (ctx->account_mem)
6186 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006187 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006188 imu->nr_bvecs = 0;
6189 }
6190
6191 kfree(ctx->user_bufs);
6192 ctx->user_bufs = NULL;
6193 ctx->nr_user_bufs = 0;
6194 return 0;
6195}
6196
6197static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6198 void __user *arg, unsigned index)
6199{
6200 struct iovec __user *src;
6201
6202#ifdef CONFIG_COMPAT
6203 if (ctx->compat) {
6204 struct compat_iovec __user *ciovs;
6205 struct compat_iovec ciov;
6206
6207 ciovs = (struct compat_iovec __user *) arg;
6208 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6209 return -EFAULT;
6210
Jens Axboed55e5f52019-12-11 16:12:15 -07006211 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006212 dst->iov_len = ciov.iov_len;
6213 return 0;
6214 }
6215#endif
6216 src = (struct iovec __user *) arg;
6217 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6218 return -EFAULT;
6219 return 0;
6220}
6221
6222static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6223 unsigned nr_args)
6224{
6225 struct vm_area_struct **vmas = NULL;
6226 struct page **pages = NULL;
6227 int i, j, got_pages = 0;
6228 int ret = -EINVAL;
6229
6230 if (ctx->user_bufs)
6231 return -EBUSY;
6232 if (!nr_args || nr_args > UIO_MAXIOV)
6233 return -EINVAL;
6234
6235 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6236 GFP_KERNEL);
6237 if (!ctx->user_bufs)
6238 return -ENOMEM;
6239
6240 for (i = 0; i < nr_args; i++) {
6241 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6242 unsigned long off, start, end, ubuf;
6243 int pret, nr_pages;
6244 struct iovec iov;
6245 size_t size;
6246
6247 ret = io_copy_iov(ctx, &iov, arg, i);
6248 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006249 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006250
6251 /*
6252 * Don't impose further limits on the size and buffer
6253 * constraints here, we'll -EINVAL later when IO is
6254 * submitted if they are wrong.
6255 */
6256 ret = -EFAULT;
6257 if (!iov.iov_base || !iov.iov_len)
6258 goto err;
6259
6260 /* arbitrary limit, but we need something */
6261 if (iov.iov_len > SZ_1G)
6262 goto err;
6263
6264 ubuf = (unsigned long) iov.iov_base;
6265 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6266 start = ubuf >> PAGE_SHIFT;
6267 nr_pages = end - start;
6268
6269 if (ctx->account_mem) {
6270 ret = io_account_mem(ctx->user, nr_pages);
6271 if (ret)
6272 goto err;
6273 }
6274
6275 ret = 0;
6276 if (!pages || nr_pages > got_pages) {
6277 kfree(vmas);
6278 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006279 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006280 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006281 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006282 sizeof(struct vm_area_struct *),
6283 GFP_KERNEL);
6284 if (!pages || !vmas) {
6285 ret = -ENOMEM;
6286 if (ctx->account_mem)
6287 io_unaccount_mem(ctx->user, nr_pages);
6288 goto err;
6289 }
6290 got_pages = nr_pages;
6291 }
6292
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006293 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006294 GFP_KERNEL);
6295 ret = -ENOMEM;
6296 if (!imu->bvec) {
6297 if (ctx->account_mem)
6298 io_unaccount_mem(ctx->user, nr_pages);
6299 goto err;
6300 }
6301
6302 ret = 0;
6303 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006304 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006305 FOLL_WRITE | FOLL_LONGTERM,
6306 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006307 if (pret == nr_pages) {
6308 /* don't support file backed memory */
6309 for (j = 0; j < nr_pages; j++) {
6310 struct vm_area_struct *vma = vmas[j];
6311
6312 if (vma->vm_file &&
6313 !is_file_hugepages(vma->vm_file)) {
6314 ret = -EOPNOTSUPP;
6315 break;
6316 }
6317 }
6318 } else {
6319 ret = pret < 0 ? pret : -EFAULT;
6320 }
6321 up_read(&current->mm->mmap_sem);
6322 if (ret) {
6323 /*
6324 * if we did partial map, or found file backed vmas,
6325 * release any pages we did get
6326 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006327 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006328 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006329 if (ctx->account_mem)
6330 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006331 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006332 goto err;
6333 }
6334
6335 off = ubuf & ~PAGE_MASK;
6336 size = iov.iov_len;
6337 for (j = 0; j < nr_pages; j++) {
6338 size_t vec_len;
6339
6340 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6341 imu->bvec[j].bv_page = pages[j];
6342 imu->bvec[j].bv_len = vec_len;
6343 imu->bvec[j].bv_offset = off;
6344 off = 0;
6345 size -= vec_len;
6346 }
6347 /* store original address for later verification */
6348 imu->ubuf = ubuf;
6349 imu->len = iov.iov_len;
6350 imu->nr_bvecs = nr_pages;
6351
6352 ctx->nr_user_bufs++;
6353 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006354 kvfree(pages);
6355 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006356 return 0;
6357err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006358 kvfree(pages);
6359 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006360 io_sqe_buffer_unregister(ctx);
6361 return ret;
6362}
6363
Jens Axboe9b402842019-04-11 11:45:41 -06006364static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6365{
6366 __s32 __user *fds = arg;
6367 int fd;
6368
6369 if (ctx->cq_ev_fd)
6370 return -EBUSY;
6371
6372 if (copy_from_user(&fd, fds, sizeof(*fds)))
6373 return -EFAULT;
6374
6375 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6376 if (IS_ERR(ctx->cq_ev_fd)) {
6377 int ret = PTR_ERR(ctx->cq_ev_fd);
6378 ctx->cq_ev_fd = NULL;
6379 return ret;
6380 }
6381
6382 return 0;
6383}
6384
6385static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6386{
6387 if (ctx->cq_ev_fd) {
6388 eventfd_ctx_put(ctx->cq_ev_fd);
6389 ctx->cq_ev_fd = NULL;
6390 return 0;
6391 }
6392
6393 return -ENXIO;
6394}
6395
Jens Axboe2b188cc2019-01-07 10:46:33 -07006396static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6397{
Jens Axboe6b063142019-01-10 22:13:58 -07006398 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006399 if (ctx->sqo_mm)
6400 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006401
6402 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006403 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006404 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006405 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006406 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006407
Jens Axboe2b188cc2019-01-07 10:46:33 -07006408#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006409 if (ctx->ring_sock) {
6410 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006411 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006412 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006413#endif
6414
Hristo Venev75b28af2019-08-26 17:23:46 +00006415 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006416 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006417
6418 percpu_ref_exit(&ctx->refs);
6419 if (ctx->account_mem)
6420 io_unaccount_mem(ctx->user,
6421 ring_pages(ctx->sq_entries, ctx->cq_entries));
6422 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006423 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006424 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006425 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006426 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006427 kfree(ctx);
6428}
6429
6430static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6431{
6432 struct io_ring_ctx *ctx = file->private_data;
6433 __poll_t mask = 0;
6434
6435 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006436 /*
6437 * synchronizes with barrier from wq_has_sleeper call in
6438 * io_commit_cqring
6439 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006440 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006441 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6442 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006443 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006444 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006445 mask |= EPOLLIN | EPOLLRDNORM;
6446
6447 return mask;
6448}
6449
6450static int io_uring_fasync(int fd, struct file *file, int on)
6451{
6452 struct io_ring_ctx *ctx = file->private_data;
6453
6454 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6455}
6456
Jens Axboe071698e2020-01-28 10:04:42 -07006457static int io_remove_personalities(int id, void *p, void *data)
6458{
6459 struct io_ring_ctx *ctx = data;
6460 const struct cred *cred;
6461
6462 cred = idr_remove(&ctx->personality_idr, id);
6463 if (cred)
6464 put_cred(cred);
6465 return 0;
6466}
6467
Jens Axboe2b188cc2019-01-07 10:46:33 -07006468static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6469{
6470 mutex_lock(&ctx->uring_lock);
6471 percpu_ref_kill(&ctx->refs);
6472 mutex_unlock(&ctx->uring_lock);
6473
Jens Axboedf069d82020-02-04 16:48:34 -07006474 /*
6475 * Wait for sq thread to idle, if we have one. It won't spin on new
6476 * work after we've killed the ctx ref above. This is important to do
6477 * before we cancel existing commands, as the thread could otherwise
6478 * be queueing new work post that. If that's work we need to cancel,
6479 * it could cause shutdown to hang.
6480 */
6481 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6482 cpu_relax();
6483
Jens Axboe5262f562019-09-17 12:26:57 -06006484 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006485 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006486
6487 if (ctx->io_wq)
6488 io_wq_cancel_all(ctx->io_wq);
6489
Jens Axboedef596e2019-01-09 08:59:42 -07006490 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006491 /* if we failed setting up the ctx, we might not have any rings */
6492 if (ctx->rings)
6493 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006494 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006495 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006496 io_ring_ctx_free(ctx);
6497}
6498
6499static int io_uring_release(struct inode *inode, struct file *file)
6500{
6501 struct io_ring_ctx *ctx = file->private_data;
6502
6503 file->private_data = NULL;
6504 io_ring_ctx_wait_and_kill(ctx);
6505 return 0;
6506}
6507
Jens Axboefcb323c2019-10-24 12:39:47 -06006508static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6509 struct files_struct *files)
6510{
6511 struct io_kiocb *req;
6512 DEFINE_WAIT(wait);
6513
6514 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006515 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006516
6517 spin_lock_irq(&ctx->inflight_lock);
6518 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006519 if (req->work.files != files)
6520 continue;
6521 /* req is being completed, ignore */
6522 if (!refcount_inc_not_zero(&req->refs))
6523 continue;
6524 cancel_req = req;
6525 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006526 }
Jens Axboe768134d2019-11-10 20:30:53 -07006527 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006528 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006529 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006530 spin_unlock_irq(&ctx->inflight_lock);
6531
Jens Axboe768134d2019-11-10 20:30:53 -07006532 /* We need to keep going until we don't find a matching req */
6533 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006534 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006535
Jens Axboe2ca10252020-02-13 17:17:35 -07006536 if (cancel_req->flags & REQ_F_OVERFLOW) {
6537 spin_lock_irq(&ctx->completion_lock);
6538 list_del(&cancel_req->list);
6539 cancel_req->flags &= ~REQ_F_OVERFLOW;
6540 if (list_empty(&ctx->cq_overflow_list)) {
6541 clear_bit(0, &ctx->sq_check_overflow);
6542 clear_bit(0, &ctx->cq_check_overflow);
6543 }
6544 spin_unlock_irq(&ctx->completion_lock);
6545
6546 WRITE_ONCE(ctx->rings->cq_overflow,
6547 atomic_inc_return(&ctx->cached_cq_overflow));
6548
6549 /*
6550 * Put inflight ref and overflow ref. If that's
6551 * all we had, then we're done with this request.
6552 */
6553 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6554 io_put_req(cancel_req);
6555 continue;
6556 }
6557 }
6558
Bob Liu2f6d9b92019-11-13 18:06:24 +08006559 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6560 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006561 schedule();
6562 }
Jens Axboe768134d2019-11-10 20:30:53 -07006563 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006564}
6565
6566static int io_uring_flush(struct file *file, void *data)
6567{
6568 struct io_ring_ctx *ctx = file->private_data;
6569
6570 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006571
6572 /*
6573 * If the task is going away, cancel work it may have pending
6574 */
6575 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6576 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6577
Jens Axboefcb323c2019-10-24 12:39:47 -06006578 return 0;
6579}
6580
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006581static void *io_uring_validate_mmap_request(struct file *file,
6582 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006583{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006584 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006585 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006586 struct page *page;
6587 void *ptr;
6588
6589 switch (offset) {
6590 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006591 case IORING_OFF_CQ_RING:
6592 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006593 break;
6594 case IORING_OFF_SQES:
6595 ptr = ctx->sq_sqes;
6596 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006597 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006598 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006599 }
6600
6601 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006602 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006603 return ERR_PTR(-EINVAL);
6604
6605 return ptr;
6606}
6607
6608#ifdef CONFIG_MMU
6609
6610static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6611{
6612 size_t sz = vma->vm_end - vma->vm_start;
6613 unsigned long pfn;
6614 void *ptr;
6615
6616 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6617 if (IS_ERR(ptr))
6618 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006619
6620 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6621 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6622}
6623
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006624#else /* !CONFIG_MMU */
6625
6626static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6627{
6628 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6629}
6630
6631static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6632{
6633 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6634}
6635
6636static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6637 unsigned long addr, unsigned long len,
6638 unsigned long pgoff, unsigned long flags)
6639{
6640 void *ptr;
6641
6642 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6643 if (IS_ERR(ptr))
6644 return PTR_ERR(ptr);
6645
6646 return (unsigned long) ptr;
6647}
6648
6649#endif /* !CONFIG_MMU */
6650
Jens Axboe2b188cc2019-01-07 10:46:33 -07006651SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6652 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6653 size_t, sigsz)
6654{
6655 struct io_ring_ctx *ctx;
6656 long ret = -EBADF;
6657 int submitted = 0;
6658 struct fd f;
6659
Jens Axboeb41e9852020-02-17 09:52:41 -07006660 if (current->task_works)
6661 task_work_run();
6662
Jens Axboe6c271ce2019-01-10 11:22:30 -07006663 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006664 return -EINVAL;
6665
6666 f = fdget(fd);
6667 if (!f.file)
6668 return -EBADF;
6669
6670 ret = -EOPNOTSUPP;
6671 if (f.file->f_op != &io_uring_fops)
6672 goto out_fput;
6673
6674 ret = -ENXIO;
6675 ctx = f.file->private_data;
6676 if (!percpu_ref_tryget(&ctx->refs))
6677 goto out_fput;
6678
Jens Axboe6c271ce2019-01-10 11:22:30 -07006679 /*
6680 * For SQ polling, the thread will do all submissions and completions.
6681 * Just return the requested submit count, and wake the thread if
6682 * we were asked to.
6683 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006684 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006685 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006686 if (!list_empty_careful(&ctx->cq_overflow_list))
6687 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006688 if (flags & IORING_ENTER_SQ_WAKEUP)
6689 wake_up(&ctx->sqo_wait);
6690 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006691 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006692 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006693
6694 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006695 /* already have mm, so io_submit_sqes() won't try to grab it */
6696 cur_mm = ctx->sqo_mm;
6697 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6698 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006699 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006700
6701 if (submitted != to_submit)
6702 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006703 }
6704 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006705 unsigned nr_events = 0;
6706
Jens Axboe2b188cc2019-01-07 10:46:33 -07006707 min_complete = min(min_complete, ctx->cq_entries);
6708
Jens Axboedef596e2019-01-09 08:59:42 -07006709 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006710 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006711 } else {
6712 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6713 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006714 }
6715
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006716out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006717 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006718out_fput:
6719 fdput(f);
6720 return submitted ? submitted : ret;
6721}
6722
Tobias Klauserbebdb652020-02-26 18:38:32 +01006723#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006724static int io_uring_show_cred(int id, void *p, void *data)
6725{
6726 const struct cred *cred = p;
6727 struct seq_file *m = data;
6728 struct user_namespace *uns = seq_user_ns(m);
6729 struct group_info *gi;
6730 kernel_cap_t cap;
6731 unsigned __capi;
6732 int g;
6733
6734 seq_printf(m, "%5d\n", id);
6735 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6736 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6737 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6738 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6739 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6740 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6741 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6742 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6743 seq_puts(m, "\n\tGroups:\t");
6744 gi = cred->group_info;
6745 for (g = 0; g < gi->ngroups; g++) {
6746 seq_put_decimal_ull(m, g ? " " : "",
6747 from_kgid_munged(uns, gi->gid[g]));
6748 }
6749 seq_puts(m, "\n\tCapEff:\t");
6750 cap = cred->cap_effective;
6751 CAP_FOR_EACH_U32(__capi)
6752 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6753 seq_putc(m, '\n');
6754 return 0;
6755}
6756
6757static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6758{
6759 int i;
6760
6761 mutex_lock(&ctx->uring_lock);
6762 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6763 for (i = 0; i < ctx->nr_user_files; i++) {
6764 struct fixed_file_table *table;
6765 struct file *f;
6766
6767 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6768 f = table->files[i & IORING_FILE_TABLE_MASK];
6769 if (f)
6770 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6771 else
6772 seq_printf(m, "%5u: <none>\n", i);
6773 }
6774 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6775 for (i = 0; i < ctx->nr_user_bufs; i++) {
6776 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6777
6778 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6779 (unsigned int) buf->len);
6780 }
6781 if (!idr_is_empty(&ctx->personality_idr)) {
6782 seq_printf(m, "Personalities:\n");
6783 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6784 }
6785 mutex_unlock(&ctx->uring_lock);
6786}
6787
6788static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6789{
6790 struct io_ring_ctx *ctx = f->private_data;
6791
6792 if (percpu_ref_tryget(&ctx->refs)) {
6793 __io_uring_show_fdinfo(ctx, m);
6794 percpu_ref_put(&ctx->refs);
6795 }
6796}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006797#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006798
Jens Axboe2b188cc2019-01-07 10:46:33 -07006799static const struct file_operations io_uring_fops = {
6800 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006801 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006802 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006803#ifndef CONFIG_MMU
6804 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6805 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6806#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006807 .poll = io_uring_poll,
6808 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006809#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006810 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006811#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006812};
6813
6814static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6815 struct io_uring_params *p)
6816{
Hristo Venev75b28af2019-08-26 17:23:46 +00006817 struct io_rings *rings;
6818 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006819
Hristo Venev75b28af2019-08-26 17:23:46 +00006820 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6821 if (size == SIZE_MAX)
6822 return -EOVERFLOW;
6823
6824 rings = io_mem_alloc(size);
6825 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006826 return -ENOMEM;
6827
Hristo Venev75b28af2019-08-26 17:23:46 +00006828 ctx->rings = rings;
6829 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6830 rings->sq_ring_mask = p->sq_entries - 1;
6831 rings->cq_ring_mask = p->cq_entries - 1;
6832 rings->sq_ring_entries = p->sq_entries;
6833 rings->cq_ring_entries = p->cq_entries;
6834 ctx->sq_mask = rings->sq_ring_mask;
6835 ctx->cq_mask = rings->cq_ring_mask;
6836 ctx->sq_entries = rings->sq_ring_entries;
6837 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006838
6839 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006840 if (size == SIZE_MAX) {
6841 io_mem_free(ctx->rings);
6842 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006843 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006844 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006845
6846 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006847 if (!ctx->sq_sqes) {
6848 io_mem_free(ctx->rings);
6849 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006850 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006851 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006852
Jens Axboe2b188cc2019-01-07 10:46:33 -07006853 return 0;
6854}
6855
6856/*
6857 * Allocate an anonymous fd, this is what constitutes the application
6858 * visible backing of an io_uring instance. The application mmaps this
6859 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6860 * we have to tie this fd to a socket for file garbage collection purposes.
6861 */
6862static int io_uring_get_fd(struct io_ring_ctx *ctx)
6863{
6864 struct file *file;
6865 int ret;
6866
6867#if defined(CONFIG_UNIX)
6868 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6869 &ctx->ring_sock);
6870 if (ret)
6871 return ret;
6872#endif
6873
6874 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6875 if (ret < 0)
6876 goto err;
6877
6878 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6879 O_RDWR | O_CLOEXEC);
6880 if (IS_ERR(file)) {
6881 put_unused_fd(ret);
6882 ret = PTR_ERR(file);
6883 goto err;
6884 }
6885
6886#if defined(CONFIG_UNIX)
6887 ctx->ring_sock->file = file;
6888#endif
6889 fd_install(ret, file);
6890 return ret;
6891err:
6892#if defined(CONFIG_UNIX)
6893 sock_release(ctx->ring_sock);
6894 ctx->ring_sock = NULL;
6895#endif
6896 return ret;
6897}
6898
6899static int io_uring_create(unsigned entries, struct io_uring_params *p)
6900{
6901 struct user_struct *user = NULL;
6902 struct io_ring_ctx *ctx;
6903 bool account_mem;
6904 int ret;
6905
Jens Axboe8110c1a2019-12-28 15:39:54 -07006906 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006907 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006908 if (entries > IORING_MAX_ENTRIES) {
6909 if (!(p->flags & IORING_SETUP_CLAMP))
6910 return -EINVAL;
6911 entries = IORING_MAX_ENTRIES;
6912 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006913
6914 /*
6915 * Use twice as many entries for the CQ ring. It's possible for the
6916 * application to drive a higher depth than the size of the SQ ring,
6917 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006918 * some flexibility in overcommitting a bit. If the application has
6919 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6920 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006921 */
6922 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006923 if (p->flags & IORING_SETUP_CQSIZE) {
6924 /*
6925 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6926 * to a power-of-two, if it isn't already. We do NOT impose
6927 * any cq vs sq ring sizing.
6928 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006929 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006930 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006931 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6932 if (!(p->flags & IORING_SETUP_CLAMP))
6933 return -EINVAL;
6934 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6935 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006936 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6937 } else {
6938 p->cq_entries = 2 * p->sq_entries;
6939 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006940
6941 user = get_uid(current_user());
6942 account_mem = !capable(CAP_IPC_LOCK);
6943
6944 if (account_mem) {
6945 ret = io_account_mem(user,
6946 ring_pages(p->sq_entries, p->cq_entries));
6947 if (ret) {
6948 free_uid(user);
6949 return ret;
6950 }
6951 }
6952
6953 ctx = io_ring_ctx_alloc(p);
6954 if (!ctx) {
6955 if (account_mem)
6956 io_unaccount_mem(user, ring_pages(p->sq_entries,
6957 p->cq_entries));
6958 free_uid(user);
6959 return -ENOMEM;
6960 }
6961 ctx->compat = in_compat_syscall();
6962 ctx->account_mem = account_mem;
6963 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006964 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006965
6966 ret = io_allocate_scq_urings(ctx, p);
6967 if (ret)
6968 goto err;
6969
Jens Axboe6c271ce2019-01-10 11:22:30 -07006970 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006971 if (ret)
6972 goto err;
6973
Jens Axboe2b188cc2019-01-07 10:46:33 -07006974 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006975 p->sq_off.head = offsetof(struct io_rings, sq.head);
6976 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6977 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6978 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6979 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6980 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6981 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006982
6983 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006984 p->cq_off.head = offsetof(struct io_rings, cq.head);
6985 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6986 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6987 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6988 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6989 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006990
Jens Axboe044c1ab2019-10-28 09:15:33 -06006991 /*
6992 * Install ring fd as the very last thing, so we don't risk someone
6993 * having closed it before we finish setup
6994 */
6995 ret = io_uring_get_fd(ctx);
6996 if (ret < 0)
6997 goto err;
6998
Jens Axboeda8c9692019-12-02 18:51:26 -07006999 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007000 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7001 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007002 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007003 return ret;
7004err:
7005 io_ring_ctx_wait_and_kill(ctx);
7006 return ret;
7007}
7008
7009/*
7010 * Sets up an aio uring context, and returns the fd. Applications asks for a
7011 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7012 * params structure passed in.
7013 */
7014static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7015{
7016 struct io_uring_params p;
7017 long ret;
7018 int i;
7019
7020 if (copy_from_user(&p, params, sizeof(p)))
7021 return -EFAULT;
7022 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7023 if (p.resv[i])
7024 return -EINVAL;
7025 }
7026
Jens Axboe6c271ce2019-01-10 11:22:30 -07007027 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007028 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007029 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007030 return -EINVAL;
7031
7032 ret = io_uring_create(entries, &p);
7033 if (ret < 0)
7034 return ret;
7035
7036 if (copy_to_user(params, &p, sizeof(p)))
7037 return -EFAULT;
7038
7039 return ret;
7040}
7041
7042SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7043 struct io_uring_params __user *, params)
7044{
7045 return io_uring_setup(entries, params);
7046}
7047
Jens Axboe66f4af92020-01-16 15:36:52 -07007048static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7049{
7050 struct io_uring_probe *p;
7051 size_t size;
7052 int i, ret;
7053
7054 size = struct_size(p, ops, nr_args);
7055 if (size == SIZE_MAX)
7056 return -EOVERFLOW;
7057 p = kzalloc(size, GFP_KERNEL);
7058 if (!p)
7059 return -ENOMEM;
7060
7061 ret = -EFAULT;
7062 if (copy_from_user(p, arg, size))
7063 goto out;
7064 ret = -EINVAL;
7065 if (memchr_inv(p, 0, size))
7066 goto out;
7067
7068 p->last_op = IORING_OP_LAST - 1;
7069 if (nr_args > IORING_OP_LAST)
7070 nr_args = IORING_OP_LAST;
7071
7072 for (i = 0; i < nr_args; i++) {
7073 p->ops[i].op = i;
7074 if (!io_op_defs[i].not_supported)
7075 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7076 }
7077 p->ops_len = i;
7078
7079 ret = 0;
7080 if (copy_to_user(arg, p, size))
7081 ret = -EFAULT;
7082out:
7083 kfree(p);
7084 return ret;
7085}
7086
Jens Axboe071698e2020-01-28 10:04:42 -07007087static int io_register_personality(struct io_ring_ctx *ctx)
7088{
7089 const struct cred *creds = get_current_cred();
7090 int id;
7091
7092 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7093 USHRT_MAX, GFP_KERNEL);
7094 if (id < 0)
7095 put_cred(creds);
7096 return id;
7097}
7098
7099static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7100{
7101 const struct cred *old_creds;
7102
7103 old_creds = idr_remove(&ctx->personality_idr, id);
7104 if (old_creds) {
7105 put_cred(old_creds);
7106 return 0;
7107 }
7108
7109 return -EINVAL;
7110}
7111
7112static bool io_register_op_must_quiesce(int op)
7113{
7114 switch (op) {
7115 case IORING_UNREGISTER_FILES:
7116 case IORING_REGISTER_FILES_UPDATE:
7117 case IORING_REGISTER_PROBE:
7118 case IORING_REGISTER_PERSONALITY:
7119 case IORING_UNREGISTER_PERSONALITY:
7120 return false;
7121 default:
7122 return true;
7123 }
7124}
7125
Jens Axboeedafcce2019-01-09 09:16:05 -07007126static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7127 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007128 __releases(ctx->uring_lock)
7129 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007130{
7131 int ret;
7132
Jens Axboe35fa71a2019-04-22 10:23:23 -06007133 /*
7134 * We're inside the ring mutex, if the ref is already dying, then
7135 * someone else killed the ctx or is already going through
7136 * io_uring_register().
7137 */
7138 if (percpu_ref_is_dying(&ctx->refs))
7139 return -ENXIO;
7140
Jens Axboe071698e2020-01-28 10:04:42 -07007141 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007142 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007143
Jens Axboe05f3fb32019-12-09 11:22:50 -07007144 /*
7145 * Drop uring mutex before waiting for references to exit. If
7146 * another thread is currently inside io_uring_enter() it might
7147 * need to grab the uring_lock to make progress. If we hold it
7148 * here across the drain wait, then we can deadlock. It's safe
7149 * to drop the mutex here, since no new references will come in
7150 * after we've killed the percpu ref.
7151 */
7152 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007153 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007154 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007155 if (ret) {
7156 percpu_ref_resurrect(&ctx->refs);
7157 ret = -EINTR;
7158 goto out;
7159 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007160 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007161
7162 switch (opcode) {
7163 case IORING_REGISTER_BUFFERS:
7164 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7165 break;
7166 case IORING_UNREGISTER_BUFFERS:
7167 ret = -EINVAL;
7168 if (arg || nr_args)
7169 break;
7170 ret = io_sqe_buffer_unregister(ctx);
7171 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007172 case IORING_REGISTER_FILES:
7173 ret = io_sqe_files_register(ctx, arg, nr_args);
7174 break;
7175 case IORING_UNREGISTER_FILES:
7176 ret = -EINVAL;
7177 if (arg || nr_args)
7178 break;
7179 ret = io_sqe_files_unregister(ctx);
7180 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007181 case IORING_REGISTER_FILES_UPDATE:
7182 ret = io_sqe_files_update(ctx, arg, nr_args);
7183 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007184 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007185 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007186 ret = -EINVAL;
7187 if (nr_args != 1)
7188 break;
7189 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007190 if (ret)
7191 break;
7192 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7193 ctx->eventfd_async = 1;
7194 else
7195 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007196 break;
7197 case IORING_UNREGISTER_EVENTFD:
7198 ret = -EINVAL;
7199 if (arg || nr_args)
7200 break;
7201 ret = io_eventfd_unregister(ctx);
7202 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007203 case IORING_REGISTER_PROBE:
7204 ret = -EINVAL;
7205 if (!arg || nr_args > 256)
7206 break;
7207 ret = io_probe(ctx, arg, nr_args);
7208 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007209 case IORING_REGISTER_PERSONALITY:
7210 ret = -EINVAL;
7211 if (arg || nr_args)
7212 break;
7213 ret = io_register_personality(ctx);
7214 break;
7215 case IORING_UNREGISTER_PERSONALITY:
7216 ret = -EINVAL;
7217 if (arg)
7218 break;
7219 ret = io_unregister_personality(ctx, nr_args);
7220 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007221 default:
7222 ret = -EINVAL;
7223 break;
7224 }
7225
Jens Axboe071698e2020-01-28 10:04:42 -07007226 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007227 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007228 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007229out:
7230 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007231 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007232 return ret;
7233}
7234
7235SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7236 void __user *, arg, unsigned int, nr_args)
7237{
7238 struct io_ring_ctx *ctx;
7239 long ret = -EBADF;
7240 struct fd f;
7241
7242 f = fdget(fd);
7243 if (!f.file)
7244 return -EBADF;
7245
7246 ret = -EOPNOTSUPP;
7247 if (f.file->f_op != &io_uring_fops)
7248 goto out_fput;
7249
7250 ctx = f.file->private_data;
7251
7252 mutex_lock(&ctx->uring_lock);
7253 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7254 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007255 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7256 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007257out_fput:
7258 fdput(f);
7259 return ret;
7260}
7261
Jens Axboe2b188cc2019-01-07 10:46:33 -07007262static int __init io_uring_init(void)
7263{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007264#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7265 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7266 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7267} while (0)
7268
7269#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7270 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7271 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7272 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7273 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7274 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7275 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7276 BUILD_BUG_SQE_ELEM(8, __u64, off);
7277 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7278 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007279 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007280 BUILD_BUG_SQE_ELEM(24, __u32, len);
7281 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7282 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7283 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7284 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7285 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7286 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7287 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7288 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7289 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7290 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7291 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7292 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7293 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007294 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007295 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7296 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7297 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007298 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007299
Jens Axboed3656342019-12-18 09:50:26 -07007300 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007301 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7302 return 0;
7303};
7304__initcall(io_uring_init);