blob: c7e8e9a1b27bd96590e55ade9e748ae8ab00bffd [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>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#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 Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200145 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application.
149 *
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
152 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000153 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200155 * Runtime CQ flags
156 *
157 * Written by the application, shouldn't be modified by the
158 * kernel.
159 */
160 u32 cq_flags;
161 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 * Number of completion events lost because the queue was full;
163 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800164 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * the completion queue.
166 *
167 * Written by the kernel, shouldn't be modified by the
168 * application (i.e. get number of "new events" by comparing to
169 * cached value).
170 *
171 * As completion events come in out of order this counter is not
172 * ordered with any other data.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200175 /*
176 * Ring buffer of completion events.
177 *
178 * The kernel writes completion events fresh every time they are
179 * produced, so the application is allowed to modify pending
180 * entries.
181 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000182 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700183};
184
Jens Axboeedafcce2019-01-09 09:16:05 -0700185struct io_mapped_ubuf {
186 u64 ubuf;
187 size_t len;
188 struct bio_vec *bvec;
189 unsigned int nr_bvecs;
190};
191
Jens Axboe65e19f52019-10-26 07:20:21 -0600192struct fixed_file_table {
193 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700194};
195
Xiaoguang Wang05589552020-03-31 14:05:18 +0800196struct fixed_file_ref_node {
197 struct percpu_ref refs;
198 struct list_head node;
199 struct list_head file_list;
200 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600201 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202};
203
Jens Axboe05f3fb32019-12-09 11:22:50 -0700204struct fixed_file_data {
205 struct fixed_file_table *table;
206 struct io_ring_ctx *ctx;
207
Xiaoguang Wang05589552020-03-31 14:05:18 +0800208 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700210 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct list_head ref_list;
212 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213};
214
Jens Axboe5a2e7452020-02-23 16:23:11 -0700215struct io_buffer {
216 struct list_head list;
217 __u64 addr;
218 __s32 len;
219 __u16 bid;
220};
221
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222struct io_ring_ctx {
223 struct {
224 struct percpu_ref refs;
225 } ____cacheline_aligned_in_smp;
226
227 struct {
228 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800229 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700230 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800231 unsigned int cq_overflow_flushed: 1;
232 unsigned int drain_next: 1;
233 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Hristo Venev75b28af2019-08-26 17:23:46 +0000235 /*
236 * Ring buffer of indices into array of io_uring_sqe, which is
237 * mmapped by the application using the IORING_OFF_SQES offset.
238 *
239 * This indirection could e.g. be used to assign fixed
240 * io_uring_sqe entries to operations and only submit them to
241 * the queue when needed.
242 *
243 * The kernel modifies neither the indices array nor the entries
244 * array.
245 */
246 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700247 unsigned cached_sq_head;
248 unsigned sq_entries;
249 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700250 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600251 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700252 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700253 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600254
255 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600256 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700257 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258
Jens Axboefcb323c2019-10-24 12:39:47 -0600259 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700260 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 } ____cacheline_aligned_in_smp;
262
Hristo Venev75b28af2019-08-26 17:23:46 +0000263 struct io_rings *rings;
264
Jens Axboe2b188cc2019-01-07 10:46:33 -0700265 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600266 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700267 struct task_struct *sqo_thread; /* if using sq thread polling */
268 struct mm_struct *sqo_mm;
269 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270
Jens Axboe6b063142019-01-10 22:13:58 -0700271 /*
272 * If used, fixed file set. Writers must ensure that ->refs is dead,
273 * readers must ensure that ->refs is alive as long as the file* is
274 * used. Only updated through io_uring_register(2).
275 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700276 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700277 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300278 int ring_fd;
279 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700280
Jens Axboeedafcce2019-01-09 09:16:05 -0700281 /* if used, fixed mapped user buffers */
282 unsigned nr_user_bufs;
283 struct io_mapped_ubuf *user_bufs;
284
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285 struct user_struct *user;
286
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700287 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700288
Jens Axboe0f158b42020-05-14 17:18:39 -0600289 struct completion ref_comp;
290 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700291
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700292 /* if all else fails... */
293 struct io_kiocb *fallback_req;
294
Jens Axboe206aefd2019-11-07 18:27:42 -0700295#if defined(CONFIG_UNIX)
296 struct socket *ring_sock;
297#endif
298
Jens Axboe5a2e7452020-02-23 16:23:11 -0700299 struct idr io_buffer_idr;
300
Jens Axboe071698e2020-01-28 10:04:42 -0700301 struct idr personality_idr;
302
Jens Axboe206aefd2019-11-07 18:27:42 -0700303 struct {
304 unsigned cached_cq_tail;
305 unsigned cq_entries;
306 unsigned cq_mask;
307 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700308 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700309 struct wait_queue_head cq_wait;
310 struct fasync_struct *cq_fasync;
311 struct eventfd_ctx *cq_ev_fd;
312 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
314 struct {
315 struct mutex uring_lock;
316 wait_queue_head_t wait;
317 } ____cacheline_aligned_in_smp;
318
319 struct {
320 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700321
Jens Axboedef596e2019-01-09 08:59:42 -0700322 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300323 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700324 * io_uring instances that don't use IORING_SETUP_SQPOLL.
325 * For SQPOLL, only the single threaded io_sq_thread() will
326 * manipulate the list, hence no extra locking is needed there.
327 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300328 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700329 struct hlist_head *cancel_hash;
330 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700331 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600332
333 spinlock_t inflight_lock;
334 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600336
Jens Axboe4a38aed22020-05-14 17:21:15 -0600337 struct delayed_work file_put_work;
338 struct llist_head file_put_llist;
339
Jens Axboe85faa7b2020-04-09 18:14:00 -0600340 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341};
342
Jens Axboe09bb8392019-03-13 12:39:28 -0600343/*
344 * First field must be the file pointer in all the
345 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
346 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700347struct io_poll_iocb {
348 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700349 union {
350 struct wait_queue_head *head;
351 u64 addr;
352 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700353 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600354 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700355 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700356 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700357};
358
Jens Axboeb5dba592019-12-11 14:02:38 -0700359struct io_close {
360 struct file *file;
361 struct file *put_file;
362 int fd;
363};
364
Jens Axboead8a48a2019-11-15 08:49:11 -0700365struct io_timeout_data {
366 struct io_kiocb *req;
367 struct hrtimer timer;
368 struct timespec64 ts;
369 enum hrtimer_mode mode;
370};
371
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700372struct io_accept {
373 struct file *file;
374 struct sockaddr __user *addr;
375 int __user *addr_len;
376 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600377 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700378};
379
380struct io_sync {
381 struct file *file;
382 loff_t len;
383 loff_t off;
384 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700385 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700386};
387
Jens Axboefbf23842019-12-17 18:45:56 -0700388struct io_cancel {
389 struct file *file;
390 u64 addr;
391};
392
Jens Axboeb29472e2019-12-17 18:50:29 -0700393struct io_timeout {
394 struct file *file;
395 u64 addr;
396 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300397 u32 off;
398 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300399 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700400};
401
Jens Axboe9adbd452019-12-20 08:45:55 -0700402struct io_rw {
403 /* NOTE: kiocb has the file as the first member, so don't do it here */
404 struct kiocb kiocb;
405 u64 addr;
406 u64 len;
407};
408
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700409struct io_connect {
410 struct file *file;
411 struct sockaddr __user *addr;
412 int addr_len;
413};
414
Jens Axboee47293f2019-12-20 08:58:21 -0700415struct io_sr_msg {
416 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700417 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300418 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700419 void __user *buf;
420 };
Jens Axboee47293f2019-12-20 08:58:21 -0700421 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700422 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700423 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700424 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700425};
426
Jens Axboe15b71ab2019-12-11 11:20:36 -0700427struct io_open {
428 struct file *file;
429 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700430 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700431 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600432 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700433};
434
Jens Axboe05f3fb32019-12-09 11:22:50 -0700435struct io_files_update {
436 struct file *file;
437 u64 arg;
438 u32 nr_args;
439 u32 offset;
440};
441
Jens Axboe4840e412019-12-25 22:03:45 -0700442struct io_fadvise {
443 struct file *file;
444 u64 offset;
445 u32 len;
446 u32 advice;
447};
448
Jens Axboec1ca7572019-12-25 22:18:28 -0700449struct io_madvise {
450 struct file *file;
451 u64 addr;
452 u32 len;
453 u32 advice;
454};
455
Jens Axboe3e4827b2020-01-08 15:18:09 -0700456struct io_epoll {
457 struct file *file;
458 int epfd;
459 int op;
460 int fd;
461 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700462};
463
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300464struct io_splice {
465 struct file *file_out;
466 struct file *file_in;
467 loff_t off_out;
468 loff_t off_in;
469 u64 len;
470 unsigned int flags;
471};
472
Jens Axboeddf0322d2020-02-23 16:41:33 -0700473struct io_provide_buf {
474 struct file *file;
475 __u64 addr;
476 __s32 len;
477 __u32 bgid;
478 __u16 nbufs;
479 __u16 bid;
480};
481
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700482struct io_statx {
483 struct file *file;
484 int dfd;
485 unsigned int mask;
486 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700487 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700488 struct statx __user *buffer;
489};
490
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300491struct io_completion {
492 struct file *file;
493 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300494 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300495};
496
Jens Axboef499a022019-12-02 16:28:46 -0700497struct io_async_connect {
498 struct sockaddr_storage address;
499};
500
Jens Axboe03b12302019-12-02 18:50:25 -0700501struct io_async_msghdr {
502 struct iovec fast_iov[UIO_FASTIOV];
503 struct iovec *iov;
504 struct sockaddr __user *uaddr;
505 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700506 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700507};
508
Jens Axboef67676d2019-12-02 11:03:47 -0700509struct io_async_rw {
510 struct iovec fast_iov[UIO_FASTIOV];
511 struct iovec *iov;
512 ssize_t nr_segs;
513 ssize_t size;
Jens Axboebcf5a062020-05-22 09:24:42 -0600514 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700515};
516
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700517struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700518 union {
519 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700520 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700521 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700522 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700523 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700524};
525
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300526enum {
527 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
528 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
529 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
530 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
531 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700532 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300533
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300534 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300535 REQ_F_FAIL_LINK_BIT,
536 REQ_F_INFLIGHT_BIT,
537 REQ_F_CUR_POS_BIT,
538 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300539 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300540 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300541 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300542 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700543 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700544 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700545 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600546 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800547 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300548 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700549
550 /* not a real bit, just to check we're not overflowing the space */
551 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300552};
553
554enum {
555 /* ctx owns file */
556 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
557 /* drain existing IO first */
558 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
559 /* linked sqes */
560 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
561 /* doesn't sever on completion < 0 */
562 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
563 /* IOSQE_ASYNC */
564 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700565 /* IOSQE_BUFFER_SELECT */
566 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300567
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300568 /* head of a link */
569 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300570 /* fail rest of links */
571 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
572 /* on inflight list */
573 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
574 /* read/write uses file position */
575 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
576 /* must not punt to workers */
577 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300578 /* has linked timeout */
579 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300580 /* regular file */
581 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300582 /* completion under lock */
583 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300584 /* needs cleanup */
585 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700586 /* in overflow list */
587 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700588 /* already went through poll handler */
589 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700590 /* buffer already selected */
591 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600592 /* doesn't need file table for this request */
593 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800594 /* io_wq_work is initialized */
595 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300596 /* req->task is refcounted */
597 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700598};
599
600struct async_poll {
601 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600602 struct io_poll_iocb *double_poll;
Jens Axboed7718a92020-02-14 22:23:12 -0700603 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300604};
605
Jens Axboe09bb8392019-03-13 12:39:28 -0600606/*
607 * NOTE! Each of the iocb union members has the file pointer
608 * as the first entry in their struct definition. So you can
609 * access the file pointer through any of the sub-structs,
610 * or directly as just 'ki_filp' in this struct.
611 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700613 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600614 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700615 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700616 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700617 struct io_accept accept;
618 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700619 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700620 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700621 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700622 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700623 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700624 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700625 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700626 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700627 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700628 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300629 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700630 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700631 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300632 /* use only after cleaning per-op data, see io_clean_op() */
633 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700634 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700635
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700636 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700637 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800638 /* polled IO has completed */
639 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700641 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300642 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700643
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700646 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600647 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700648 u64 user_data;
649
Jens Axboed7718a92020-02-14 22:23:12 -0700650 struct list_head link_list;
651
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300652 /*
653 * 1. used with ctx->iopoll_list with reads/writes
654 * 2. to track reqs with ->files (see io_op_def::file_table)
655 */
Jens Axboefcb323c2019-10-24 12:39:47 -0600656 struct list_head inflight_entry;
657
Xiaoguang Wang05589552020-03-31 14:05:18 +0800658 struct percpu_ref *fixed_file_refs;
659
Jens Axboeb41e9852020-02-17 09:52:41 -0700660 union {
661 /*
662 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700663 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
664 * async armed poll handlers for regular commands. The latter
665 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700666 */
667 struct {
Jens Axboed7718a92020-02-14 22:23:12 -0700668 struct hlist_node hash_node;
669 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700670 };
671 struct io_wq_work work;
672 };
Pavel Begunkov8ef77762020-06-27 14:04:59 +0300673 struct callback_head task_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700674};
675
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300676struct io_defer_entry {
677 struct list_head list;
678 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300679 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300680};
681
Jens Axboedef596e2019-01-09 08:59:42 -0700682#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700683
Jens Axboe013538b2020-06-22 09:29:15 -0600684struct io_comp_state {
685 unsigned int nr;
686 struct list_head list;
687 struct io_ring_ctx *ctx;
688};
689
Jens Axboe9a56a232019-01-09 09:06:50 -0700690struct io_submit_state {
691 struct blk_plug plug;
692
693 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700694 * io_kiocb alloc cache
695 */
696 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300697 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700698
699 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600700 * Batch completion logic
701 */
702 struct io_comp_state comp;
703
704 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700705 * File reference cache
706 */
707 struct file *file;
708 unsigned int fd;
709 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700710 unsigned int ios_left;
711};
712
Jens Axboed3656342019-12-18 09:50:26 -0700713struct io_op_def {
714 /* needs req->io allocated for deferral/async */
715 unsigned async_ctx : 1;
716 /* needs current->mm setup, does mm access */
717 unsigned needs_mm : 1;
718 /* needs req->file assigned */
719 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600720 /* don't fail if file grab fails */
721 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700722 /* hash wq insertion if file is a regular file */
723 unsigned hash_reg_file : 1;
724 /* unbound wq insertion if file is a non-regular file */
725 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700726 /* opcode is not supported by this kernel */
727 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700728 /* needs file table */
729 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700730 /* needs ->fs */
731 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700732 /* set if opcode supports polled "wait" */
733 unsigned pollin : 1;
734 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700735 /* op supports buffer selection */
736 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300737 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700738};
739
740static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300741 [IORING_OP_NOP] = {},
742 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700743 .async_ctx = 1,
744 .needs_mm = 1,
745 .needs_file = 1,
746 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700747 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700748 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700749 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300750 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700751 .async_ctx = 1,
752 .needs_mm = 1,
753 .needs_file = 1,
754 .hash_reg_file = 1,
755 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700756 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300757 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .needs_file = 1,
761 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300762 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700763 .needs_file = 1,
764 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700765 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700768 .needs_file = 1,
769 .hash_reg_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700771 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300772 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_file = 1,
776 .unbound_nonreg_file = 1,
777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_POLL_REMOVE] = {},
779 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700780 .needs_file = 1,
781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700783 .async_ctx = 1,
784 .needs_mm = 1,
785 .needs_file = 1,
786 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700787 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700788 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700789 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300790 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700791 .async_ctx = 1,
792 .needs_mm = 1,
793 .needs_file = 1,
794 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700795 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700796 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700797 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700798 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300799 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700800 .async_ctx = 1,
801 .needs_mm = 1,
802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_TIMEOUT_REMOVE] = {},
804 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700805 .needs_mm = 1,
806 .needs_file = 1,
807 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700808 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700809 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_ASYNC_CANCEL] = {},
812 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700813 .async_ctx = 1,
814 .needs_mm = 1,
815 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700817 .async_ctx = 1,
818 .needs_mm = 1,
819 .needs_file = 1,
820 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700821 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300825 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700826 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300827 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700828 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700829 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700830 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300831 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600832 .needs_file = 1,
833 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700834 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700835 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300836 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700837 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700838 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700841 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700842 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600843 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700846 .needs_mm = 1,
847 .needs_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700849 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700850 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700853 .needs_mm = 1,
854 .needs_file = 1,
855 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700856 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300857 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700858 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300859 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700860 .needs_file = 1,
861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700863 .needs_mm = 1,
864 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300865 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700866 .needs_mm = 1,
867 .needs_file = 1,
868 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700869 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700872 .needs_mm = 1,
873 .needs_file = 1,
874 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700875 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700876 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700879 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700880 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700881 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700882 [IORING_OP_EPOLL_CTL] = {
883 .unbound_nonreg_file = 1,
884 .file_table = 1,
885 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300886 [IORING_OP_SPLICE] = {
887 .needs_file = 1,
888 .hash_reg_file = 1,
889 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700890 },
891 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700892 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300893 [IORING_OP_TEE] = {
894 .needs_file = 1,
895 .hash_reg_file = 1,
896 .unbound_nonreg_file = 1,
897 },
Jens Axboed3656342019-12-18 09:50:26 -0700898};
899
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700900enum io_mem_account {
901 ACCT_LOCKED,
902 ACCT_PINNED,
903};
904
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +0300905static bool io_rw_reissue(struct io_kiocb *req, long res);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700906static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800907static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600908static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700909static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700910static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
911static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700912static int __io_sqe_files_update(struct io_ring_ctx *ctx,
913 struct io_uring_files_update *ip,
914 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300915static int io_prep_work_files(struct io_kiocb *req);
Jens Axboe2237d762020-06-26 13:44:16 -0600916static void io_complete_rw_common(struct kiocb *kiocb, long res,
917 struct io_comp_state *cs);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300918static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700919static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
920 int fd, struct file **out_file, bool fixed);
921static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600922 const struct io_uring_sqe *sqe,
923 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600924static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600925
Jens Axboeb63534c2020-06-04 11:28:00 -0600926static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
927 struct iovec **iovec, struct iov_iter *iter,
928 bool needs_lock);
929static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
930 struct iovec *iovec, struct iovec *fast_iov,
931 struct iov_iter *iter);
932
Jens Axboe2b188cc2019-01-07 10:46:33 -0700933static struct kmem_cache *req_cachep;
934
935static const struct file_operations io_uring_fops;
936
937struct sock *io_uring_get_socket(struct file *file)
938{
939#if defined(CONFIG_UNIX)
940 if (file->f_op == &io_uring_fops) {
941 struct io_ring_ctx *ctx = file->private_data;
942
943 return ctx->ring_sock->sk;
944 }
945#endif
946 return NULL;
947}
948EXPORT_SYMBOL(io_uring_get_socket);
949
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300950static void io_get_req_task(struct io_kiocb *req)
951{
952 if (req->flags & REQ_F_TASK_PINNED)
953 return;
954 get_task_struct(req->task);
955 req->flags |= REQ_F_TASK_PINNED;
956}
957
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300958static inline void io_clean_op(struct io_kiocb *req)
959{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +0300960 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300961 __io_clean_op(req);
962}
963
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300964/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
965static void __io_put_req_task(struct io_kiocb *req)
966{
967 if (req->flags & REQ_F_TASK_PINNED)
968 put_task_struct(req->task);
969}
970
Jens Axboe4349f302020-07-09 15:07:01 -0600971static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600972{
973 struct mm_struct *mm = current->mm;
974
975 if (mm) {
976 kthread_unuse_mm(mm);
977 mmput(mm);
978 }
979}
980
981static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
982{
983 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300984 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
985 !mmget_not_zero(ctx->sqo_mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600986 return -EFAULT;
987 kthread_use_mm(ctx->sqo_mm);
988 }
989
990 return 0;
991}
992
993static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
994 struct io_kiocb *req)
995{
996 if (!io_op_defs[req->opcode].needs_mm)
997 return 0;
998 return __io_sq_thread_acquire_mm(ctx);
999}
1000
1001static inline void req_set_fail_links(struct io_kiocb *req)
1002{
1003 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1004 req->flags |= REQ_F_FAIL_LINK;
1005}
1006
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001007/*
1008 * Note: must call io_req_init_async() for the first time you
1009 * touch any members of io_wq_work.
1010 */
1011static inline void io_req_init_async(struct io_kiocb *req)
1012{
1013 if (req->flags & REQ_F_WORK_INITIALIZED)
1014 return;
1015
1016 memset(&req->work, 0, sizeof(req->work));
1017 req->flags |= REQ_F_WORK_INITIALIZED;
1018}
1019
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001020static inline bool io_async_submit(struct io_ring_ctx *ctx)
1021{
1022 return ctx->flags & IORING_SETUP_SQPOLL;
1023}
1024
Jens Axboe2b188cc2019-01-07 10:46:33 -07001025static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1026{
1027 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1028
Jens Axboe0f158b42020-05-14 17:18:39 -06001029 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030}
1031
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001032static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1033{
1034 return !req->timeout.off;
1035}
1036
Jens Axboe2b188cc2019-01-07 10:46:33 -07001037static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1038{
1039 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001040 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001041
1042 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1043 if (!ctx)
1044 return NULL;
1045
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001046 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1047 if (!ctx->fallback_req)
1048 goto err;
1049
Jens Axboe78076bb2019-12-04 19:56:40 -07001050 /*
1051 * Use 5 bits less than the max cq entries, that should give us around
1052 * 32 entries per hash list if totally full and uniformly spread.
1053 */
1054 hash_bits = ilog2(p->cq_entries);
1055 hash_bits -= 5;
1056 if (hash_bits <= 0)
1057 hash_bits = 1;
1058 ctx->cancel_hash_bits = hash_bits;
1059 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1060 GFP_KERNEL);
1061 if (!ctx->cancel_hash)
1062 goto err;
1063 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1064
Roman Gushchin21482892019-05-07 10:01:48 -07001065 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001066 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1067 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001068
1069 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001070 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001071 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001072 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001073 init_completion(&ctx->ref_comp);
1074 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001075 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001076 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077 mutex_init(&ctx->uring_lock);
1078 init_waitqueue_head(&ctx->wait);
1079 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001080 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001081 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001082 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001083 init_waitqueue_head(&ctx->inflight_wait);
1084 spin_lock_init(&ctx->inflight_lock);
1085 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001086 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1087 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001088 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001089err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001090 if (ctx->fallback_req)
1091 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001092 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001093 kfree(ctx);
1094 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095}
1096
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001097static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001098{
Jens Axboe2bc99302020-07-09 09:43:27 -06001099 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1100 struct io_ring_ctx *ctx = req->ctx;
1101
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001102 return seq != ctx->cached_cq_tail
1103 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001104 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001105
Bob Liu9d858b22019-11-13 18:06:25 +08001106 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001107}
1108
Jens Axboede0617e2019-04-06 21:51:27 -06001109static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001110{
Hristo Venev75b28af2019-08-26 17:23:46 +00001111 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001112
Pavel Begunkov07910152020-01-17 03:52:46 +03001113 /* order cqe stores with ring update */
1114 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115
Pavel Begunkov07910152020-01-17 03:52:46 +03001116 if (wq_has_sleeper(&ctx->cq_wait)) {
1117 wake_up_interruptible(&ctx->cq_wait);
1118 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119 }
1120}
1121
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001122static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001123{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001124 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1125 return;
1126
Jens Axboecccf0ee2020-01-27 16:34:48 -07001127 if (req->work.mm) {
1128 mmdrop(req->work.mm);
1129 req->work.mm = NULL;
1130 }
1131 if (req->work.creds) {
1132 put_cred(req->work.creds);
1133 req->work.creds = NULL;
1134 }
Jens Axboeff002b32020-02-07 16:05:21 -07001135 if (req->work.fs) {
1136 struct fs_struct *fs = req->work.fs;
1137
1138 spin_lock(&req->work.fs->lock);
1139 if (--fs->users)
1140 fs = NULL;
1141 spin_unlock(&req->work.fs->lock);
1142 if (fs)
1143 free_fs_struct(fs);
1144 }
Jens Axboe561fb042019-10-24 07:25:42 -06001145}
1146
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001147static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001148{
Jens Axboed3656342019-12-18 09:50:26 -07001149 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001150
Pavel Begunkov16d59802020-07-12 16:16:47 +03001151 io_req_init_async(req);
1152
Jens Axboed3656342019-12-18 09:50:26 -07001153 if (req->flags & REQ_F_ISREG) {
1154 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001155 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001156 } else {
1157 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001158 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001159 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001160 if (!req->work.mm && def->needs_mm) {
1161 mmgrab(current->mm);
1162 req->work.mm = current->mm;
1163 }
1164 if (!req->work.creds)
1165 req->work.creds = get_current_cred();
1166 if (!req->work.fs && def->needs_fs) {
1167 spin_lock(&current->fs->lock);
1168 if (!current->fs->in_exec) {
1169 req->work.fs = current->fs;
1170 req->work.fs->users++;
1171 } else {
1172 req->work.flags |= IO_WQ_WORK_CANCEL;
1173 }
1174 spin_unlock(&current->fs->lock);
1175 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001176 if (def->needs_fsize)
1177 req->work.fsize = rlimit(RLIMIT_FSIZE);
1178 else
1179 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001180}
1181
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001182static void io_prep_async_link(struct io_kiocb *req)
1183{
1184 struct io_kiocb *cur;
1185
1186 io_prep_async_work(req);
1187 if (req->flags & REQ_F_LINK_HEAD)
1188 list_for_each_entry(cur, &req->link_list, link_list)
1189 io_prep_async_work(cur);
1190}
1191
1192static void __io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001193{
Jackie Liua197f662019-11-08 08:09:12 -07001194 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001195 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001196
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001197 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1198 &req->work, req->flags);
1199 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001200
1201 if (link)
1202 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001203}
1204
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001205static void io_queue_async_work(struct io_kiocb *req)
1206{
1207 /* init ->work of the whole link before punting */
1208 io_prep_async_link(req);
1209 __io_queue_async_work(req);
1210}
1211
Jens Axboe5262f562019-09-17 12:26:57 -06001212static void io_kill_timeout(struct io_kiocb *req)
1213{
1214 int ret;
1215
Jens Axboe2d283902019-12-04 11:08:05 -07001216 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001217 if (ret != -1) {
1218 atomic_inc(&req->ctx->cq_timeouts);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001219 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001220 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001221 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001222 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001223 }
1224}
1225
1226static void io_kill_timeouts(struct io_ring_ctx *ctx)
1227{
1228 struct io_kiocb *req, *tmp;
1229
1230 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001231 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list)
Jens Axboe5262f562019-09-17 12:26:57 -06001232 io_kill_timeout(req);
1233 spin_unlock_irq(&ctx->completion_lock);
1234}
1235
Pavel Begunkov04518942020-05-26 20:34:05 +03001236static void __io_queue_deferred(struct io_ring_ctx *ctx)
1237{
1238 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001239 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1240 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001241
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001242 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001243 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001244 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001245 /* punt-init is done before queueing for defer */
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001246 __io_queue_async_work(de->req);
1247 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001248 } while (!list_empty(&ctx->defer_list));
1249}
1250
Pavel Begunkov360428f2020-05-30 14:54:17 +03001251static void io_flush_timeouts(struct io_ring_ctx *ctx)
1252{
1253 while (!list_empty(&ctx->timeout_list)) {
1254 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001255 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001256
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001257 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001258 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001259 if (req->timeout.target_seq != ctx->cached_cq_tail
1260 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001261 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001262
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001263 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001264 io_kill_timeout(req);
1265 }
1266}
1267
Jens Axboede0617e2019-04-06 21:51:27 -06001268static void io_commit_cqring(struct io_ring_ctx *ctx)
1269{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001270 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001271 __io_commit_cqring(ctx);
1272
Pavel Begunkov04518942020-05-26 20:34:05 +03001273 if (unlikely(!list_empty(&ctx->defer_list)))
1274 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001275}
1276
Jens Axboe2b188cc2019-01-07 10:46:33 -07001277static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1278{
Hristo Venev75b28af2019-08-26 17:23:46 +00001279 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280 unsigned tail;
1281
1282 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001283 /*
1284 * writes to the cq entry need to come after reading head; the
1285 * control dependency is enough as we're using WRITE_ONCE to
1286 * fill the cq entry
1287 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001288 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001289 return NULL;
1290
1291 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001292 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293}
1294
Jens Axboef2842ab2020-01-08 11:04:00 -07001295static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1296{
Jens Axboef0b493e2020-02-01 21:30:11 -07001297 if (!ctx->cq_ev_fd)
1298 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001299 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1300 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001301 if (!ctx->eventfd_async)
1302 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001303 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001304}
1305
Jens Axboeb41e9852020-02-17 09:52:41 -07001306static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001307{
1308 if (waitqueue_active(&ctx->wait))
1309 wake_up(&ctx->wait);
1310 if (waitqueue_active(&ctx->sqo_wait))
1311 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001312 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001313 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001314}
1315
Jens Axboec4a2ed72019-11-21 21:01:26 -07001316/* Returns true if there are no backlogged entries after the flush */
1317static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001319 struct io_rings *rings = ctx->rings;
1320 struct io_uring_cqe *cqe;
1321 struct io_kiocb *req;
1322 unsigned long flags;
1323 LIST_HEAD(list);
1324
1325 if (!force) {
1326 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001327 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001328 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1329 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001330 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001331 }
1332
1333 spin_lock_irqsave(&ctx->completion_lock, flags);
1334
1335 /* if force is set, the ring is going away. always drop after that */
1336 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001337 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001338
Jens Axboec4a2ed72019-11-21 21:01:26 -07001339 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001340 while (!list_empty(&ctx->cq_overflow_list)) {
1341 cqe = io_get_cqring(ctx);
1342 if (!cqe && !force)
1343 break;
1344
1345 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001346 compl.list);
1347 list_move(&req->compl.list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001348 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001349 if (cqe) {
1350 WRITE_ONCE(cqe->user_data, req->user_data);
1351 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001352 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001353 } else {
1354 WRITE_ONCE(ctx->rings->cq_overflow,
1355 atomic_inc_return(&ctx->cached_cq_overflow));
1356 }
1357 }
1358
1359 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001360 if (cqe) {
1361 clear_bit(0, &ctx->sq_check_overflow);
1362 clear_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001363 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001364 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001365 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1366 io_cqring_ev_posted(ctx);
1367
1368 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001369 req = list_first_entry(&list, struct io_kiocb, compl.list);
1370 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001371 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001372 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001373
1374 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001375}
1376
Jens Axboebcda7ba2020-02-23 16:42:51 -07001377static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001378{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001379 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380 struct io_uring_cqe *cqe;
1381
Jens Axboe78e19bb2019-11-06 15:21:34 -07001382 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001383
Jens Axboe2b188cc2019-01-07 10:46:33 -07001384 /*
1385 * If we can't get a cq entry, userspace overflowed the
1386 * submission (by quite a lot). Increment the overflow count in
1387 * the ring.
1388 */
1389 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001390 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001391 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001392 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001393 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001394 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001395 WRITE_ONCE(ctx->rings->cq_overflow,
1396 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001397 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001398 if (list_empty(&ctx->cq_overflow_list)) {
1399 set_bit(0, &ctx->sq_check_overflow);
1400 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001401 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001402 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001403 io_clean_op(req);
Jens Axboe2ca10252020-02-13 17:17:35 -07001404 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001405 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001406 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001407 refcount_inc(&req->refs);
1408 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409 }
1410}
1411
Jens Axboebcda7ba2020-02-23 16:42:51 -07001412static void io_cqring_fill_event(struct io_kiocb *req, long res)
1413{
1414 __io_cqring_fill_event(req, res, 0);
1415}
1416
Jens Axboee1e16092020-06-22 09:17:17 -06001417static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001418{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001419 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 unsigned long flags;
1421
1422 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001423 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424 io_commit_cqring(ctx);
1425 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1426
Jens Axboe8c838782019-03-12 15:48:16 -06001427 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001428}
1429
Jens Axboe229a7b62020-06-22 10:13:11 -06001430static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001431{
Jens Axboe229a7b62020-06-22 10:13:11 -06001432 struct io_ring_ctx *ctx = cs->ctx;
1433
1434 spin_lock_irq(&ctx->completion_lock);
1435 while (!list_empty(&cs->list)) {
1436 struct io_kiocb *req;
1437
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001438 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1439 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001440 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001441 if (!(req->flags & REQ_F_LINK_HEAD)) {
1442 req->flags |= REQ_F_COMP_LOCKED;
1443 io_put_req(req);
1444 } else {
1445 spin_unlock_irq(&ctx->completion_lock);
1446 io_put_req(req);
1447 spin_lock_irq(&ctx->completion_lock);
1448 }
1449 }
1450 io_commit_cqring(ctx);
1451 spin_unlock_irq(&ctx->completion_lock);
1452
1453 io_cqring_ev_posted(ctx);
1454 cs->nr = 0;
1455}
1456
1457static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1458 struct io_comp_state *cs)
1459{
1460 if (!cs) {
1461 io_cqring_add_event(req, res, cflags);
1462 io_put_req(req);
1463 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001464 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001465 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001466 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001467 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001468 if (++cs->nr >= 32)
1469 io_submit_flush_completions(cs);
1470 }
Jens Axboee1e16092020-06-22 09:17:17 -06001471}
1472
1473static void io_req_complete(struct io_kiocb *req, long res)
1474{
Jens Axboe229a7b62020-06-22 10:13:11 -06001475 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001476}
1477
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001478static inline bool io_is_fallback_req(struct io_kiocb *req)
1479{
1480 return req == (struct io_kiocb *)
1481 ((unsigned long) req->ctx->fallback_req & ~1UL);
1482}
1483
1484static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1485{
1486 struct io_kiocb *req;
1487
1488 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001489 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001490 return req;
1491
1492 return NULL;
1493}
1494
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001495static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1496 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001497{
Jens Axboefd6fab22019-03-14 16:30:06 -06001498 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499 struct io_kiocb *req;
1500
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001501 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001502 size_t sz;
1503 int ret;
1504
1505 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001506 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1507
1508 /*
1509 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1510 * retry single alloc to be on the safe side.
1511 */
1512 if (unlikely(ret <= 0)) {
1513 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1514 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001515 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001516 ret = 1;
1517 }
Jens Axboe2579f912019-01-09 09:10:43 -07001518 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001519 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001520 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001521 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001522 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001523 }
1524
Jens Axboe2579f912019-01-09 09:10:43 -07001525 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001526fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001527 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528}
1529
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001530static inline void io_put_file(struct io_kiocb *req, struct file *file,
1531 bool fixed)
1532{
1533 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001534 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001535 else
1536 fput(file);
1537}
1538
Pavel Begunkove6543a82020-06-28 12:52:30 +03001539static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001540{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001541 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001542
Jens Axboe5acbbc82020-07-08 15:15:26 -06001543 if (req->io)
1544 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001545 if (req->file)
1546 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001547 io_req_clean_work(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001548
Jens Axboefcb323c2019-10-24 12:39:47 -06001549 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001550 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001551 unsigned long flags;
1552
1553 spin_lock_irqsave(&ctx->inflight_lock, flags);
1554 list_del(&req->inflight_entry);
1555 if (waitqueue_active(&ctx->inflight_wait))
1556 wake_up(&ctx->inflight_wait);
1557 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1558 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001559}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001560
Pavel Begunkove6543a82020-06-28 12:52:30 +03001561static void __io_free_req(struct io_kiocb *req)
1562{
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001563 struct io_ring_ctx *ctx;
1564
Pavel Begunkove6543a82020-06-28 12:52:30 +03001565 io_dismantle_req(req);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001566 __io_put_req_task(req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001567 ctx = req->ctx;
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001568 if (likely(!io_is_fallback_req(req)))
1569 kmem_cache_free(req_cachep, req);
1570 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001571 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1572 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001573}
1574
Jackie Liua197f662019-11-08 08:09:12 -07001575static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001576{
Jackie Liua197f662019-11-08 08:09:12 -07001577 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001578 int ret;
1579
Jens Axboe2d283902019-12-04 11:08:05 -07001580 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001581 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001582 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001583 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001584 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001585 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001586 return true;
1587 }
1588
1589 return false;
1590}
1591
Jens Axboeab0b6452020-06-30 08:43:15 -06001592static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001593{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001594 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001595 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001596
1597 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001598 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001599 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1600 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001601 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001602
1603 list_del_init(&link->link_list);
1604 wake_ev = io_link_cancel_timeout(link);
1605 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001606 return wake_ev;
1607}
1608
1609static void io_kill_linked_timeout(struct io_kiocb *req)
1610{
1611 struct io_ring_ctx *ctx = req->ctx;
1612 bool wake_ev;
1613
1614 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1615 unsigned long flags;
1616
1617 spin_lock_irqsave(&ctx->completion_lock, flags);
1618 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001619 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001620 } else {
1621 wake_ev = __io_kill_linked_timeout(req);
1622 }
1623
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001624 if (wake_ev)
1625 io_cqring_ev_posted(ctx);
1626}
1627
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001628static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001629{
1630 struct io_kiocb *nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001631
1632 /*
1633 * The list should never be empty when we are called here. But could
1634 * potentially happen if the chain is messed up, check to be on the
1635 * safe side.
1636 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001637 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001638 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001639
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001640 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1641 list_del_init(&req->link_list);
1642 if (!list_empty(&nxt->link_list))
1643 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001644 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001645}
1646
1647/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001648 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001649 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001650static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001651{
Jens Axboe2665abf2019-11-05 12:40:47 -07001652 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001653
1654 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001655 struct io_kiocb *link = list_first_entry(&req->link_list,
1656 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001657
Pavel Begunkov44932332019-12-05 16:16:35 +03001658 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001659 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001660
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001661 io_cqring_fill_event(link, -ECANCELED);
1662 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001663 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001664 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001665
1666 io_commit_cqring(ctx);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001667 io_cqring_ev_posted(ctx);
1668}
1669
1670static void io_fail_links(struct io_kiocb *req)
1671{
1672 struct io_ring_ctx *ctx = req->ctx;
1673
1674 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1675 unsigned long flags;
1676
1677 spin_lock_irqsave(&ctx->completion_lock, flags);
1678 __io_fail_links(req);
1679 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1680 } else {
1681 __io_fail_links(req);
1682 }
1683
Jens Axboe2665abf2019-11-05 12:40:47 -07001684 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001685}
1686
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001687static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001688{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001689 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001690 if (req->flags & REQ_F_LINK_TIMEOUT)
1691 io_kill_linked_timeout(req);
1692
Jens Axboe9e645e112019-05-10 16:07:28 -06001693 /*
1694 * If LINK is set, we have dependent requests in this chain. If we
1695 * didn't fail this request, queue the first one up, moving any other
1696 * dependencies to the next request. In case of failure, fail the rest
1697 * of the chain.
1698 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001699 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1700 return io_req_link_next(req);
1701 io_fail_links(req);
1702 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001703}
Jens Axboe9e645e112019-05-10 16:07:28 -06001704
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001705static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1706{
1707 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1708 return NULL;
1709 return __io_req_find_next(req);
1710}
1711
Jens Axboec2c4c832020-07-01 15:37:11 -06001712static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb)
1713{
1714 struct task_struct *tsk = req->task;
1715 struct io_ring_ctx *ctx = req->ctx;
1716 int ret, notify = TWA_RESUME;
1717
1718 /*
1719 * SQPOLL kernel thread doesn't need notification, just a wakeup.
1720 * If we're not using an eventfd, then TWA_RESUME is always fine,
1721 * as we won't have dependencies between request completions for
1722 * other kernel wait conditions.
1723 */
1724 if (ctx->flags & IORING_SETUP_SQPOLL)
1725 notify = 0;
1726 else if (ctx->cq_ev_fd)
1727 notify = TWA_SIGNAL;
1728
1729 ret = task_work_add(tsk, cb, notify);
1730 if (!ret)
1731 wake_up_process(tsk);
1732 return ret;
1733}
1734
Jens Axboec40f6372020-06-25 15:39:59 -06001735static void __io_req_task_cancel(struct io_kiocb *req, int error)
1736{
1737 struct io_ring_ctx *ctx = req->ctx;
1738
1739 spin_lock_irq(&ctx->completion_lock);
1740 io_cqring_fill_event(req, error);
1741 io_commit_cqring(ctx);
1742 spin_unlock_irq(&ctx->completion_lock);
1743
1744 io_cqring_ev_posted(ctx);
1745 req_set_fail_links(req);
1746 io_double_put_req(req);
1747}
1748
1749static void io_req_task_cancel(struct callback_head *cb)
1750{
1751 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1752
1753 __io_req_task_cancel(req, -ECANCELED);
1754}
1755
1756static void __io_req_task_submit(struct io_kiocb *req)
1757{
1758 struct io_ring_ctx *ctx = req->ctx;
1759
Jens Axboec40f6372020-06-25 15:39:59 -06001760 if (!__io_sq_thread_acquire_mm(ctx)) {
1761 mutex_lock(&ctx->uring_lock);
1762 __io_queue_sqe(req, NULL, NULL);
1763 mutex_unlock(&ctx->uring_lock);
1764 } else {
1765 __io_req_task_cancel(req, -EFAULT);
1766 }
1767}
1768
1769static void io_req_task_submit(struct callback_head *cb)
1770{
1771 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1772
1773 __io_req_task_submit(req);
1774}
1775
1776static void io_req_task_queue(struct io_kiocb *req)
1777{
Jens Axboec40f6372020-06-25 15:39:59 -06001778 int ret;
1779
1780 init_task_work(&req->task_work, io_req_task_submit);
1781
Jens Axboec2c4c832020-07-01 15:37:11 -06001782 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboec40f6372020-06-25 15:39:59 -06001783 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001784 struct task_struct *tsk;
1785
Jens Axboec40f6372020-06-25 15:39:59 -06001786 init_task_work(&req->task_work, io_req_task_cancel);
1787 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001788 task_work_add(tsk, &req->task_work, 0);
1789 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001790 }
Jens Axboec40f6372020-06-25 15:39:59 -06001791}
1792
Pavel Begunkovc3524382020-06-28 12:52:32 +03001793static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001794{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001795 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001796
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001797 if (nxt)
1798 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001799}
1800
Pavel Begunkovc3524382020-06-28 12:52:32 +03001801static void io_free_req(struct io_kiocb *req)
1802{
1803 io_queue_next(req);
1804 __io_free_req(req);
1805}
1806
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001807struct req_batch {
1808 void *reqs[IO_IOPOLL_BATCH];
1809 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001810
1811 struct task_struct *task;
1812 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001813};
1814
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001815static inline void io_init_req_batch(struct req_batch *rb)
1816{
1817 rb->to_free = 0;
1818 rb->task_refs = 0;
1819 rb->task = NULL;
1820}
1821
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001822static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1823 struct req_batch *rb)
1824{
1825 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1826 percpu_ref_put_many(&ctx->refs, rb->to_free);
1827 rb->to_free = 0;
1828}
1829
1830static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1831 struct req_batch *rb)
1832{
1833 if (rb->to_free)
1834 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001835 if (rb->task) {
1836 put_task_struct_many(rb->task, rb->task_refs);
1837 rb->task = NULL;
1838 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001839}
1840
1841static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1842{
1843 if (unlikely(io_is_fallback_req(req))) {
1844 io_free_req(req);
1845 return;
1846 }
1847 if (req->flags & REQ_F_LINK_HEAD)
1848 io_queue_next(req);
1849
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001850 if (req->flags & REQ_F_TASK_PINNED) {
1851 if (req->task != rb->task) {
1852 if (rb->task)
1853 put_task_struct_many(rb->task, rb->task_refs);
1854 rb->task = req->task;
1855 rb->task_refs = 0;
1856 }
1857 rb->task_refs++;
1858 req->flags &= ~REQ_F_TASK_PINNED;
1859 }
1860
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001861 io_dismantle_req(req);
1862 rb->reqs[rb->to_free++] = req;
1863 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1864 __io_req_free_batch_flush(req->ctx, rb);
1865}
1866
Jens Axboeba816ad2019-09-28 11:36:45 -06001867/*
1868 * Drop reference to request, return next in chain (if there is one) if this
1869 * was the last reference to this request.
1870 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001871static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001872{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001873 struct io_kiocb *nxt = NULL;
1874
Jens Axboe2a44f462020-02-25 13:25:41 -07001875 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001876 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001877 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001878 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001879 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001880}
1881
Jens Axboe2b188cc2019-01-07 10:46:33 -07001882static void io_put_req(struct io_kiocb *req)
1883{
Jens Axboedef596e2019-01-09 08:59:42 -07001884 if (refcount_dec_and_test(&req->refs))
1885 io_free_req(req);
1886}
1887
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001888static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001889{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001890 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001891
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001892 /*
1893 * A ref is owned by io-wq in which context we're. So, if that's the
1894 * last one, it's safe to steal next work. False negatives are Ok,
1895 * it just will be re-punted async in io_put_work()
1896 */
1897 if (refcount_read(&req->refs) != 1)
1898 return NULL;
1899
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001900 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001901 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001902}
1903
Jens Axboe978db572019-11-14 22:39:04 -07001904/*
1905 * Must only be used if we don't need to care about links, usually from
1906 * within the completion handling itself.
1907 */
1908static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001909{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001910 /* drop both submit and complete references */
1911 if (refcount_sub_and_test(2, &req->refs))
1912 __io_free_req(req);
1913}
1914
Jens Axboe978db572019-11-14 22:39:04 -07001915static void io_double_put_req(struct io_kiocb *req)
1916{
1917 /* drop both submit and complete references */
1918 if (refcount_sub_and_test(2, &req->refs))
1919 io_free_req(req);
1920}
1921
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001922static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001923{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001924 struct io_rings *rings = ctx->rings;
1925
Jens Axboead3eb2c2019-12-18 17:12:20 -07001926 if (test_bit(0, &ctx->cq_check_overflow)) {
1927 /*
1928 * noflush == true is from the waitqueue handler, just ensure
1929 * we wake up the task, and the next invocation will flush the
1930 * entries. We cannot safely to it from here.
1931 */
1932 if (noflush && !list_empty(&ctx->cq_overflow_list))
1933 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001934
Jens Axboead3eb2c2019-12-18 17:12:20 -07001935 io_cqring_overflow_flush(ctx, false);
1936 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001937
Jens Axboea3a0e432019-08-20 11:03:11 -06001938 /* See comment at the top of this file */
1939 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001940 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001941}
1942
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001943static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1944{
1945 struct io_rings *rings = ctx->rings;
1946
1947 /* make sure SQ entry isn't read before tail */
1948 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1949}
1950
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03001951static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001952{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03001953 unsigned int cflags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001954
1955 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1956 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03001957 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001958 kfree(kbuf);
1959 return cflags;
1960}
1961
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03001962static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
1963{
1964 struct io_buffer *kbuf;
1965
1966 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
1967 return io_put_kbuf(req, kbuf);
1968}
1969
Jens Axboe4c6e2772020-07-01 11:29:10 -06001970static inline bool io_run_task_work(void)
1971{
1972 if (current->task_works) {
1973 __set_current_state(TASK_RUNNING);
1974 task_work_run();
1975 return true;
1976 }
1977
1978 return false;
1979}
1980
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001981static void io_iopoll_queue(struct list_head *again)
1982{
1983 struct io_kiocb *req;
1984
1985 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03001986 req = list_first_entry(again, struct io_kiocb, inflight_entry);
1987 list_del(&req->inflight_entry);
Pavel Begunkovcf2f5422020-06-30 15:20:40 +03001988 if (!io_rw_reissue(req, -EAGAIN))
Jens Axboe2237d762020-06-26 13:44:16 -06001989 io_complete_rw_common(&req->rw.kiocb, -EAGAIN, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001990 } while (!list_empty(again));
1991}
1992
Jens Axboedef596e2019-01-09 08:59:42 -07001993/*
1994 * Find and free completed poll iocbs
1995 */
1996static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1997 struct list_head *done)
1998{
Jens Axboe8237e042019-12-28 10:48:22 -07001999 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002000 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002001 LIST_HEAD(again);
2002
2003 /* order with ->result store in io_complete_rw_iopoll() */
2004 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002005
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002006 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002007 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002008 int cflags = 0;
2009
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002010 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002011 if (READ_ONCE(req->result) == -EAGAIN) {
2012 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002013 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002014 continue;
2015 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002016 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002017
Jens Axboebcda7ba2020-02-23 16:42:51 -07002018 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002019 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002020
2021 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002022 (*nr_events)++;
2023
Pavel Begunkovc3524382020-06-28 12:52:32 +03002024 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002025 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002026 }
Jens Axboedef596e2019-01-09 08:59:42 -07002027
Jens Axboe09bb8392019-03-13 12:39:28 -06002028 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002029 if (ctx->flags & IORING_SETUP_SQPOLL)
2030 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002031 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002032
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002033 if (!list_empty(&again))
2034 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002035}
2036
Jens Axboedef596e2019-01-09 08:59:42 -07002037static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2038 long min)
2039{
2040 struct io_kiocb *req, *tmp;
2041 LIST_HEAD(done);
2042 bool spin;
2043 int ret;
2044
2045 /*
2046 * Only spin for completions if we don't have multiple devices hanging
2047 * off our complete list, and we're under the requested amount.
2048 */
2049 spin = !ctx->poll_multi_file && *nr_events < min;
2050
2051 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002052 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002053 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002054
2055 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002056 * Move completed and retryable entries to our local lists.
2057 * If we find a request that requires polling, break out
2058 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002059 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002060 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002061 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002062 continue;
2063 }
2064 if (!list_empty(&done))
2065 break;
2066
2067 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2068 if (ret < 0)
2069 break;
2070
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002071 /* iopoll may have completed current req */
2072 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002073 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002074
Jens Axboedef596e2019-01-09 08:59:42 -07002075 if (ret && spin)
2076 spin = false;
2077 ret = 0;
2078 }
2079
2080 if (!list_empty(&done))
2081 io_iopoll_complete(ctx, nr_events, &done);
2082
2083 return ret;
2084}
2085
2086/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002087 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002088 * non-spinning poll check - we'll still enter the driver poll loop, but only
2089 * as a non-spinning completion check.
2090 */
2091static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2092 long min)
2093{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002094 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002095 int ret;
2096
2097 ret = io_do_iopoll(ctx, nr_events, min);
2098 if (ret < 0)
2099 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002100 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002101 return 0;
2102 }
2103
2104 return 1;
2105}
2106
2107/*
2108 * We can't just wait for polled events to come to us, we have to actively
2109 * find and complete them.
2110 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002111static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002112{
2113 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2114 return;
2115
2116 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002117 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002118 unsigned int nr_events = 0;
2119
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002120 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002121
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002122 /* let it sleep and repeat later if can't complete a request */
2123 if (nr_events == 0)
2124 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002125 /*
2126 * Ensure we allow local-to-the-cpu processing to take place,
2127 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002128 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002129 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002130 if (need_resched()) {
2131 mutex_unlock(&ctx->uring_lock);
2132 cond_resched();
2133 mutex_lock(&ctx->uring_lock);
2134 }
Jens Axboedef596e2019-01-09 08:59:42 -07002135 }
2136 mutex_unlock(&ctx->uring_lock);
2137}
2138
Pavel Begunkov7668b922020-07-07 16:36:21 +03002139static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002140{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002141 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002142 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002143
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002144 /*
2145 * We disallow the app entering submit/complete with polling, but we
2146 * still need to lock the ring to prevent racing with polled issue
2147 * that got punted to a workqueue.
2148 */
2149 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002150 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002151 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002152 * Don't enter poll loop if we already have events pending.
2153 * If we do, we can potentially be spinning for commands that
2154 * already triggered a CQE (eg in error).
2155 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002156 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002157 break;
2158
2159 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002160 * If a submit got punted to a workqueue, we can have the
2161 * application entering polling for a command before it gets
2162 * issued. That app will hold the uring_lock for the duration
2163 * of the poll right here, so we need to take a breather every
2164 * now and then to ensure that the issue has a chance to add
2165 * the poll to the issued list. Otherwise we can spin here
2166 * forever, while the workqueue is stuck trying to acquire the
2167 * very same mutex.
2168 */
2169 if (!(++iters & 7)) {
2170 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002171 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002172 mutex_lock(&ctx->uring_lock);
2173 }
2174
Pavel Begunkov7668b922020-07-07 16:36:21 +03002175 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002176 if (ret <= 0)
2177 break;
2178 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002179 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002180
Jens Axboe500f9fb2019-08-19 12:15:59 -06002181 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002182 return ret;
2183}
2184
Jens Axboe491381ce2019-10-17 09:20:46 -06002185static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002186{
Jens Axboe491381ce2019-10-17 09:20:46 -06002187 /*
2188 * Tell lockdep we inherited freeze protection from submission
2189 * thread.
2190 */
2191 if (req->flags & REQ_F_ISREG) {
2192 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002193
Jens Axboe491381ce2019-10-17 09:20:46 -06002194 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002195 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002196 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002197}
2198
Jens Axboea1d7c392020-06-22 11:09:46 -06002199static void io_complete_rw_common(struct kiocb *kiocb, long res,
2200 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002201{
Jens Axboe9adbd452019-12-20 08:45:55 -07002202 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002203 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002204
Jens Axboe491381ce2019-10-17 09:20:46 -06002205 if (kiocb->ki_flags & IOCB_WRITE)
2206 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002207
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002208 if (res != req->result)
2209 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002210 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002211 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002212 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002213}
2214
Jens Axboeb63534c2020-06-04 11:28:00 -06002215#ifdef CONFIG_BLOCK
2216static bool io_resubmit_prep(struct io_kiocb *req, int error)
2217{
2218 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2219 ssize_t ret = -ECANCELED;
2220 struct iov_iter iter;
2221 int rw;
2222
2223 if (error) {
2224 ret = error;
2225 goto end_req;
2226 }
2227
2228 switch (req->opcode) {
2229 case IORING_OP_READV:
2230 case IORING_OP_READ_FIXED:
2231 case IORING_OP_READ:
2232 rw = READ;
2233 break;
2234 case IORING_OP_WRITEV:
2235 case IORING_OP_WRITE_FIXED:
2236 case IORING_OP_WRITE:
2237 rw = WRITE;
2238 break;
2239 default:
2240 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2241 req->opcode);
2242 goto end_req;
2243 }
2244
2245 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2246 if (ret < 0)
2247 goto end_req;
2248 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2249 if (!ret)
2250 return true;
2251 kfree(iovec);
2252end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002253 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002254 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002255 return false;
2256}
2257
2258static void io_rw_resubmit(struct callback_head *cb)
2259{
2260 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2261 struct io_ring_ctx *ctx = req->ctx;
2262 int err;
2263
Jens Axboeb63534c2020-06-04 11:28:00 -06002264 err = io_sq_thread_acquire_mm(ctx, req);
2265
2266 if (io_resubmit_prep(req, err)) {
2267 refcount_inc(&req->refs);
2268 io_queue_async_work(req);
2269 }
2270}
2271#endif
2272
2273static bool io_rw_reissue(struct io_kiocb *req, long res)
2274{
2275#ifdef CONFIG_BLOCK
Jens Axboeb63534c2020-06-04 11:28:00 -06002276 int ret;
2277
2278 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2279 return false;
2280
Jens Axboeb63534c2020-06-04 11:28:00 -06002281 init_task_work(&req->task_work, io_rw_resubmit);
Jens Axboec2c4c832020-07-01 15:37:11 -06002282 ret = io_req_task_work_add(req, &req->task_work);
2283 if (!ret)
Jens Axboeb63534c2020-06-04 11:28:00 -06002284 return true;
2285#endif
2286 return false;
2287}
2288
Jens Axboea1d7c392020-06-22 11:09:46 -06002289static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2290 struct io_comp_state *cs)
2291{
2292 if (!io_rw_reissue(req, res))
2293 io_complete_rw_common(&req->rw.kiocb, res, cs);
2294}
2295
Jens Axboeba816ad2019-09-28 11:36:45 -06002296static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2297{
Jens Axboe9adbd452019-12-20 08:45:55 -07002298 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002299
Jens Axboea1d7c392020-06-22 11:09:46 -06002300 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002301}
2302
Jens Axboedef596e2019-01-09 08:59:42 -07002303static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2304{
Jens Axboe9adbd452019-12-20 08:45:55 -07002305 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002306
Jens Axboe491381ce2019-10-17 09:20:46 -06002307 if (kiocb->ki_flags & IOCB_WRITE)
2308 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002309
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002310 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002311 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002312
2313 WRITE_ONCE(req->result, res);
2314 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002315 smp_wmb();
2316 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002317}
2318
2319/*
2320 * After the iocb has been issued, it's safe to be found on the poll list.
2321 * Adding the kiocb to the list AFTER submission ensures that we don't
2322 * find it from a io_iopoll_getevents() thread before the issuer is done
2323 * accessing the kiocb cookie.
2324 */
2325static void io_iopoll_req_issued(struct io_kiocb *req)
2326{
2327 struct io_ring_ctx *ctx = req->ctx;
2328
2329 /*
2330 * Track whether we have multiple files in our lists. This will impact
2331 * how we do polling eventually, not spinning if we're on potentially
2332 * different devices.
2333 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002334 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002335 ctx->poll_multi_file = false;
2336 } else if (!ctx->poll_multi_file) {
2337 struct io_kiocb *list_req;
2338
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002339 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002340 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002341 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002342 ctx->poll_multi_file = true;
2343 }
2344
2345 /*
2346 * For fast devices, IO may have already completed. If it has, add
2347 * it to the front so we find it first.
2348 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002349 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002350 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002351 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002352 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002353
2354 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2355 wq_has_sleeper(&ctx->sqo_wait))
2356 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002357}
2358
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002359static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002360{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002361 if (state->has_refs)
2362 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002363 state->file = NULL;
2364}
2365
2366static inline void io_state_file_put(struct io_submit_state *state)
2367{
2368 if (state->file)
2369 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002370}
2371
2372/*
2373 * Get as many references to a file as we have IOs left in this submission,
2374 * assuming most submissions are for one file, or at least that each file
2375 * has more than one submission.
2376 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002377static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002378{
2379 if (!state)
2380 return fget(fd);
2381
2382 if (state->file) {
2383 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002384 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002385 state->ios_left--;
2386 return state->file;
2387 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002388 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002389 }
2390 state->file = fget_many(fd, state->ios_left);
2391 if (!state->file)
2392 return NULL;
2393
2394 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002395 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002396 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002397 return state->file;
2398}
2399
Jens Axboe4503b762020-06-01 10:00:27 -06002400static bool io_bdev_nowait(struct block_device *bdev)
2401{
2402#ifdef CONFIG_BLOCK
2403 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2404#else
2405 return true;
2406#endif
2407}
2408
Jens Axboe2b188cc2019-01-07 10:46:33 -07002409/*
2410 * If we tracked the file through the SCM inflight mechanism, we could support
2411 * any file. For now, just ensure that anything potentially problematic is done
2412 * inline.
2413 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002414static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002415{
2416 umode_t mode = file_inode(file)->i_mode;
2417
Jens Axboe4503b762020-06-01 10:00:27 -06002418 if (S_ISBLK(mode)) {
2419 if (io_bdev_nowait(file->f_inode->i_bdev))
2420 return true;
2421 return false;
2422 }
2423 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002424 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002425 if (S_ISREG(mode)) {
2426 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2427 file->f_op != &io_uring_fops)
2428 return true;
2429 return false;
2430 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002431
Jens Axboec5b85622020-06-09 19:23:05 -06002432 /* any ->read/write should understand O_NONBLOCK */
2433 if (file->f_flags & O_NONBLOCK)
2434 return true;
2435
Jens Axboeaf197f52020-04-28 13:15:06 -06002436 if (!(file->f_mode & FMODE_NOWAIT))
2437 return false;
2438
2439 if (rw == READ)
2440 return file->f_op->read_iter != NULL;
2441
2442 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002443}
2444
Jens Axboe3529d8c2019-12-19 18:24:38 -07002445static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2446 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002447{
Jens Axboedef596e2019-01-09 08:59:42 -07002448 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002449 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002450 unsigned ioprio;
2451 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002452
Jens Axboe491381ce2019-10-17 09:20:46 -06002453 if (S_ISREG(file_inode(req->file)->i_mode))
2454 req->flags |= REQ_F_ISREG;
2455
Jens Axboe2b188cc2019-01-07 10:46:33 -07002456 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002457 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2458 req->flags |= REQ_F_CUR_POS;
2459 kiocb->ki_pos = req->file->f_pos;
2460 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002461 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002462 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2463 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2464 if (unlikely(ret))
2465 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002466
2467 ioprio = READ_ONCE(sqe->ioprio);
2468 if (ioprio) {
2469 ret = ioprio_check_cap(ioprio);
2470 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002471 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002472
2473 kiocb->ki_ioprio = ioprio;
2474 } else
2475 kiocb->ki_ioprio = get_current_ioprio();
2476
Stefan Bühler8449eed2019-04-27 20:34:19 +02002477 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002478 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002479 req->flags |= REQ_F_NOWAIT;
2480
Jens Axboeb63534c2020-06-04 11:28:00 -06002481 if (kiocb->ki_flags & IOCB_DIRECT)
2482 io_get_req_task(req);
2483
Stefan Bühler8449eed2019-04-27 20:34:19 +02002484 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002485 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002486
Jens Axboedef596e2019-01-09 08:59:42 -07002487 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002488 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2489 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002490 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002491
Jens Axboedef596e2019-01-09 08:59:42 -07002492 kiocb->ki_flags |= IOCB_HIPRI;
2493 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002494 req->iopoll_completed = 0;
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +03002495 io_get_req_task(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002496 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002497 if (kiocb->ki_flags & IOCB_HIPRI)
2498 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002499 kiocb->ki_complete = io_complete_rw;
2500 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002501
Jens Axboe3529d8c2019-12-19 18:24:38 -07002502 req->rw.addr = READ_ONCE(sqe->addr);
2503 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002504 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002505 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002506}
2507
2508static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2509{
2510 switch (ret) {
2511 case -EIOCBQUEUED:
2512 break;
2513 case -ERESTARTSYS:
2514 case -ERESTARTNOINTR:
2515 case -ERESTARTNOHAND:
2516 case -ERESTART_RESTARTBLOCK:
2517 /*
2518 * We can't just restart the syscall, since previously
2519 * submitted sqes may already be in progress. Just fail this
2520 * IO with EINTR.
2521 */
2522 ret = -EINTR;
2523 /* fall through */
2524 default:
2525 kiocb->ki_complete(kiocb, ret, 0);
2526 }
2527}
2528
Jens Axboea1d7c392020-06-22 11:09:46 -06002529static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2530 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002531{
Jens Axboeba042912019-12-25 16:33:42 -07002532 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2533
2534 if (req->flags & REQ_F_CUR_POS)
2535 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002536 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002537 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002538 else
2539 io_rw_done(kiocb, ret);
2540}
2541
Jens Axboe9adbd452019-12-20 08:45:55 -07002542static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002543 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002544{
Jens Axboe9adbd452019-12-20 08:45:55 -07002545 struct io_ring_ctx *ctx = req->ctx;
2546 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002547 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002548 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002549 size_t offset;
2550 u64 buf_addr;
2551
2552 /* attempt to use fixed buffers without having provided iovecs */
2553 if (unlikely(!ctx->user_bufs))
2554 return -EFAULT;
2555
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002556 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002557 if (unlikely(buf_index >= ctx->nr_user_bufs))
2558 return -EFAULT;
2559
2560 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2561 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002562 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002563
2564 /* overflow */
2565 if (buf_addr + len < buf_addr)
2566 return -EFAULT;
2567 /* not inside the mapped region */
2568 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2569 return -EFAULT;
2570
2571 /*
2572 * May not be a start of buffer, set size appropriately
2573 * and advance us to the beginning.
2574 */
2575 offset = buf_addr - imu->ubuf;
2576 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002577
2578 if (offset) {
2579 /*
2580 * Don't use iov_iter_advance() here, as it's really slow for
2581 * using the latter parts of a big fixed buffer - it iterates
2582 * over each segment manually. We can cheat a bit here, because
2583 * we know that:
2584 *
2585 * 1) it's a BVEC iter, we set it up
2586 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2587 * first and last bvec
2588 *
2589 * So just find our index, and adjust the iterator afterwards.
2590 * If the offset is within the first bvec (or the whole first
2591 * bvec, just use iov_iter_advance(). This makes it easier
2592 * since we can just skip the first segment, which may not
2593 * be PAGE_SIZE aligned.
2594 */
2595 const struct bio_vec *bvec = imu->bvec;
2596
2597 if (offset <= bvec->bv_len) {
2598 iov_iter_advance(iter, offset);
2599 } else {
2600 unsigned long seg_skip;
2601
2602 /* skip first vec */
2603 offset -= bvec->bv_len;
2604 seg_skip = 1 + (offset >> PAGE_SHIFT);
2605
2606 iter->bvec = bvec + seg_skip;
2607 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002608 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002609 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002610 }
2611 }
2612
Jens Axboe5e559562019-11-13 16:12:46 -07002613 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002614}
2615
Jens Axboebcda7ba2020-02-23 16:42:51 -07002616static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2617{
2618 if (needs_lock)
2619 mutex_unlock(&ctx->uring_lock);
2620}
2621
2622static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2623{
2624 /*
2625 * "Normal" inline submissions always hold the uring_lock, since we
2626 * grab it from the system call. Same is true for the SQPOLL offload.
2627 * The only exception is when we've detached the request and issue it
2628 * from an async worker thread, grab the lock for that case.
2629 */
2630 if (needs_lock)
2631 mutex_lock(&ctx->uring_lock);
2632}
2633
2634static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2635 int bgid, struct io_buffer *kbuf,
2636 bool needs_lock)
2637{
2638 struct io_buffer *head;
2639
2640 if (req->flags & REQ_F_BUFFER_SELECTED)
2641 return kbuf;
2642
2643 io_ring_submit_lock(req->ctx, needs_lock);
2644
2645 lockdep_assert_held(&req->ctx->uring_lock);
2646
2647 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2648 if (head) {
2649 if (!list_empty(&head->list)) {
2650 kbuf = list_last_entry(&head->list, struct io_buffer,
2651 list);
2652 list_del(&kbuf->list);
2653 } else {
2654 kbuf = head;
2655 idr_remove(&req->ctx->io_buffer_idr, bgid);
2656 }
2657 if (*len > kbuf->len)
2658 *len = kbuf->len;
2659 } else {
2660 kbuf = ERR_PTR(-ENOBUFS);
2661 }
2662
2663 io_ring_submit_unlock(req->ctx, needs_lock);
2664
2665 return kbuf;
2666}
2667
Jens Axboe4d954c22020-02-27 07:31:19 -07002668static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2669 bool needs_lock)
2670{
2671 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002672 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002673
2674 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002675 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002676 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2677 if (IS_ERR(kbuf))
2678 return kbuf;
2679 req->rw.addr = (u64) (unsigned long) kbuf;
2680 req->flags |= REQ_F_BUFFER_SELECTED;
2681 return u64_to_user_ptr(kbuf->addr);
2682}
2683
2684#ifdef CONFIG_COMPAT
2685static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2686 bool needs_lock)
2687{
2688 struct compat_iovec __user *uiov;
2689 compat_ssize_t clen;
2690 void __user *buf;
2691 ssize_t len;
2692
2693 uiov = u64_to_user_ptr(req->rw.addr);
2694 if (!access_ok(uiov, sizeof(*uiov)))
2695 return -EFAULT;
2696 if (__get_user(clen, &uiov->iov_len))
2697 return -EFAULT;
2698 if (clen < 0)
2699 return -EINVAL;
2700
2701 len = clen;
2702 buf = io_rw_buffer_select(req, &len, needs_lock);
2703 if (IS_ERR(buf))
2704 return PTR_ERR(buf);
2705 iov[0].iov_base = buf;
2706 iov[0].iov_len = (compat_size_t) len;
2707 return 0;
2708}
2709#endif
2710
2711static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2712 bool needs_lock)
2713{
2714 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2715 void __user *buf;
2716 ssize_t len;
2717
2718 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2719 return -EFAULT;
2720
2721 len = iov[0].iov_len;
2722 if (len < 0)
2723 return -EINVAL;
2724 buf = io_rw_buffer_select(req, &len, needs_lock);
2725 if (IS_ERR(buf))
2726 return PTR_ERR(buf);
2727 iov[0].iov_base = buf;
2728 iov[0].iov_len = len;
2729 return 0;
2730}
2731
2732static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2733 bool needs_lock)
2734{
Jens Axboedddb3e22020-06-04 11:27:01 -06002735 if (req->flags & REQ_F_BUFFER_SELECTED) {
2736 struct io_buffer *kbuf;
2737
2738 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2739 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2740 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002741 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002742 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002743 if (!req->rw.len)
2744 return 0;
2745 else if (req->rw.len > 1)
2746 return -EINVAL;
2747
2748#ifdef CONFIG_COMPAT
2749 if (req->ctx->compat)
2750 return io_compat_import(req, iov, needs_lock);
2751#endif
2752
2753 return __io_iov_buffer_select(req, iov, needs_lock);
2754}
2755
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002756static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002757 struct iovec **iovec, struct iov_iter *iter,
2758 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002759{
Jens Axboe9adbd452019-12-20 08:45:55 -07002760 void __user *buf = u64_to_user_ptr(req->rw.addr);
2761 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002762 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002763 u8 opcode;
2764
Jens Axboed625c6e2019-12-17 19:53:05 -07002765 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002766 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002767 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002768 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002769 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002770
Jens Axboebcda7ba2020-02-23 16:42:51 -07002771 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002772 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002773 return -EINVAL;
2774
Jens Axboe3a6820f2019-12-22 15:19:35 -07002775 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002776 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002777 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2778 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002779 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002780 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002781 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002782 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002783 }
2784
Jens Axboe3a6820f2019-12-22 15:19:35 -07002785 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2786 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002787 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002788 }
2789
Jens Axboef67676d2019-12-02 11:03:47 -07002790 if (req->io) {
2791 struct io_async_rw *iorw = &req->io->rw;
2792
Pavel Begunkov252917c2020-07-13 22:59:20 +03002793 iov_iter_init(iter, rw, iorw->iov, iorw->nr_segs, iorw->size);
2794 *iovec = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07002795 return iorw->size;
2796 }
2797
Jens Axboe4d954c22020-02-27 07:31:19 -07002798 if (req->flags & REQ_F_BUFFER_SELECT) {
2799 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002800 if (!ret) {
2801 ret = (*iovec)->iov_len;
2802 iov_iter_init(iter, rw, *iovec, 1, ret);
2803 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002804 *iovec = NULL;
2805 return ret;
2806 }
2807
Jens Axboe2b188cc2019-01-07 10:46:33 -07002808#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002809 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002810 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2811 iovec, iter);
2812#endif
2813
2814 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2815}
2816
Jens Axboe32960612019-09-23 11:05:34 -06002817/*
2818 * For files that don't have ->read_iter() and ->write_iter(), handle them
2819 * by looping over ->read() or ->write() manually.
2820 */
2821static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2822 struct iov_iter *iter)
2823{
2824 ssize_t ret = 0;
2825
2826 /*
2827 * Don't support polled IO through this interface, and we can't
2828 * support non-blocking either. For the latter, this just causes
2829 * the kiocb to be handled from an async context.
2830 */
2831 if (kiocb->ki_flags & IOCB_HIPRI)
2832 return -EOPNOTSUPP;
2833 if (kiocb->ki_flags & IOCB_NOWAIT)
2834 return -EAGAIN;
2835
2836 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002837 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002838 ssize_t nr;
2839
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002840 if (!iov_iter_is_bvec(iter)) {
2841 iovec = iov_iter_iovec(iter);
2842 } else {
2843 /* fixed buffers import bvec */
2844 iovec.iov_base = kmap(iter->bvec->bv_page)
2845 + iter->iov_offset;
2846 iovec.iov_len = min(iter->count,
2847 iter->bvec->bv_len - iter->iov_offset);
2848 }
2849
Jens Axboe32960612019-09-23 11:05:34 -06002850 if (rw == READ) {
2851 nr = file->f_op->read(file, iovec.iov_base,
2852 iovec.iov_len, &kiocb->ki_pos);
2853 } else {
2854 nr = file->f_op->write(file, iovec.iov_base,
2855 iovec.iov_len, &kiocb->ki_pos);
2856 }
2857
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002858 if (iov_iter_is_bvec(iter))
2859 kunmap(iter->bvec->bv_page);
2860
Jens Axboe32960612019-09-23 11:05:34 -06002861 if (nr < 0) {
2862 if (!ret)
2863 ret = nr;
2864 break;
2865 }
2866 ret += nr;
2867 if (nr != iovec.iov_len)
2868 break;
2869 iov_iter_advance(iter, nr);
2870 }
2871
2872 return ret;
2873}
2874
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002875static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002876 struct iovec *iovec, struct iovec *fast_iov,
2877 struct iov_iter *iter)
2878{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002879 struct io_async_rw *rw = &req->io->rw;
2880
2881 rw->nr_segs = iter->nr_segs;
2882 rw->size = io_size;
2883 if (!iovec) {
2884 rw->iov = rw->fast_iov;
2885 if (rw->iov != fast_iov)
2886 memcpy(rw->iov, fast_iov,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002887 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002888 } else {
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002889 rw->iov = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002890 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002891 }
2892}
2893
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002894static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2895{
2896 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2897 return req->io == NULL;
2898}
2899
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002900static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002901{
Jens Axboed3656342019-12-18 09:50:26 -07002902 if (!io_op_defs[req->opcode].async_ctx)
2903 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002904
2905 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002906}
2907
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002908static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2909 struct iovec *iovec, struct iovec *fast_iov,
2910 struct iov_iter *iter)
2911{
Jens Axboe980ad262020-01-24 23:08:54 -07002912 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002913 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002914 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002915 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002916 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002917
Jens Axboe5d204bc2020-01-31 12:06:52 -07002918 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2919 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002920 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002921}
2922
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03002923static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
2924 bool force_nonblock)
2925{
2926 struct io_async_ctx *io = req->io;
2927 struct iov_iter iter;
2928 ssize_t ret;
2929
2930 io->rw.iov = io->rw.fast_iov;
2931 req->io = NULL;
2932 ret = io_import_iovec(rw, req, &io->rw.iov, &iter, !force_nonblock);
2933 req->io = io;
2934 if (unlikely(ret < 0))
2935 return ret;
2936
2937 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2938 return 0;
2939}
2940
Jens Axboe3529d8c2019-12-19 18:24:38 -07002941static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2942 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002943{
2944 ssize_t ret;
2945
Jens Axboe3529d8c2019-12-19 18:24:38 -07002946 ret = io_prep_rw(req, sqe, force_nonblock);
2947 if (ret)
2948 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002949
Jens Axboe3529d8c2019-12-19 18:24:38 -07002950 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2951 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002952
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002953 /* either don't need iovec imported or already have it */
2954 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002955 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03002956 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07002957}
2958
Jens Axboebcf5a062020-05-22 09:24:42 -06002959static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2960 int sync, void *arg)
2961{
2962 struct wait_page_queue *wpq;
2963 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06002964 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06002965 int ret;
2966
2967 wpq = container_of(wait, struct wait_page_queue, wait);
2968
2969 ret = wake_page_match(wpq, key);
2970 if (ret != 1)
2971 return ret;
2972
2973 list_del_init(&wait->entry);
2974
Pavel Begunkove7375122020-07-12 20:42:04 +03002975 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboebcf5a062020-05-22 09:24:42 -06002976 /* submit ref gets dropped, acquire a new one */
2977 refcount_inc(&req->refs);
Pavel Begunkove7375122020-07-12 20:42:04 +03002978 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboebcf5a062020-05-22 09:24:42 -06002979 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002980 struct task_struct *tsk;
2981
Jens Axboebcf5a062020-05-22 09:24:42 -06002982 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03002983 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06002984 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03002985 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06002986 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06002987 }
Jens Axboebcf5a062020-05-22 09:24:42 -06002988 return 1;
2989}
2990
2991static bool io_rw_should_retry(struct io_kiocb *req)
2992{
2993 struct kiocb *kiocb = &req->rw.kiocb;
2994 int ret;
2995
2996 /* never retry for NOWAIT, we just complete with -EAGAIN */
2997 if (req->flags & REQ_F_NOWAIT)
2998 return false;
2999
3000 /* already tried, or we're doing O_DIRECT */
3001 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
3002 return false;
3003 /*
3004 * just use poll if we can, and don't attempt if the fs doesn't
3005 * support callback based unlocks
3006 */
3007 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3008 return false;
3009
3010 /*
3011 * If request type doesn't require req->io to defer in general,
3012 * we need to allocate it here
3013 */
3014 if (!req->io && __io_alloc_async_ctx(req))
3015 return false;
3016
3017 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
3018 io_async_buf_func, req);
3019 if (!ret) {
3020 io_get_req_task(req);
3021 return true;
3022 }
3023
3024 return false;
3025}
3026
3027static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3028{
3029 if (req->file->f_op->read_iter)
3030 return call_read_iter(req->file, &req->rw.kiocb, iter);
3031 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3032}
3033
Jens Axboea1d7c392020-06-22 11:09:46 -06003034static int io_read(struct io_kiocb *req, bool force_nonblock,
3035 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003036{
3037 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003038 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003039 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003040 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003041 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003042
Jens Axboebcda7ba2020-02-23 16:42:51 -07003043 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003044 if (ret < 0)
3045 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003046
Jens Axboefd6c2e42019-12-18 12:19:41 -07003047 /* Ensure we clear previously set non-block flag */
3048 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003049 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003050
Jens Axboef67676d2019-12-02 11:03:47 -07003051 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03003052 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07003053
Pavel Begunkov24c74672020-06-21 13:09:51 +03003054 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003055 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07003056 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003057
Jens Axboe31b51512019-01-18 22:56:34 -07003058 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003059 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003060 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003061 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06003062 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003063
Jens Axboebcf5a062020-05-22 09:24:42 -06003064 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003065
Jens Axboe9d93a3f2019-05-15 13:53:07 -06003066 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06003067 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003068 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003069 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003070 iter.count = iov_count;
3071 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003072copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003073 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003074 inline_vecs, &iter);
3075 if (ret)
3076 goto out_free;
Pavel Begunkov252917c2020-07-13 22:59:20 +03003077 /* it's copied and will be cleaned with ->io */
3078 iovec = NULL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003079 /* if we can retry, do so with the callbacks armed */
3080 if (io_rw_should_retry(req)) {
3081 ret2 = io_iter_do_read(req, &iter);
3082 if (ret2 == -EIOCBQUEUED) {
3083 goto out_free;
3084 } else if (ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003085 kiocb_done(kiocb, ret2, cs);
Jens Axboebcf5a062020-05-22 09:24:42 -06003086 goto out_free;
3087 }
3088 }
3089 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07003090 return -EAGAIN;
3091 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003092 }
Jens Axboef67676d2019-12-02 11:03:47 -07003093out_free:
Pavel Begunkov252917c2020-07-13 22:59:20 +03003094 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003095 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003096 return ret;
3097}
3098
Jens Axboe3529d8c2019-12-19 18:24:38 -07003099static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3100 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003101{
3102 ssize_t ret;
3103
Jens Axboe3529d8c2019-12-19 18:24:38 -07003104 ret = io_prep_rw(req, sqe, force_nonblock);
3105 if (ret)
3106 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003107
Jens Axboe3529d8c2019-12-19 18:24:38 -07003108 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3109 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003110
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003111 /* either don't need iovec imported or already have it */
3112 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003113 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003114 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003115}
3116
Jens Axboea1d7c392020-06-22 11:09:46 -06003117static int io_write(struct io_kiocb *req, bool force_nonblock,
3118 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003119{
3120 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003121 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003122 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003123 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003124 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003125
Jens Axboebcda7ba2020-02-23 16:42:51 -07003126 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003127 if (ret < 0)
3128 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003129
Jens Axboefd6c2e42019-12-18 12:19:41 -07003130 /* Ensure we clear previously set non-block flag */
3131 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003132 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003133
Jens Axboef67676d2019-12-02 11:03:47 -07003134 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03003135 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07003136
Pavel Begunkov24c74672020-06-21 13:09:51 +03003137 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003138 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003139 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003140
Jens Axboe10d59342019-12-09 20:16:22 -07003141 /* file path doesn't support NOWAIT for non-direct_IO */
3142 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3143 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003144 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003145
Jens Axboe31b51512019-01-18 22:56:34 -07003146 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003147 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003149 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003150 ssize_t ret2;
3151
Jens Axboe2b188cc2019-01-07 10:46:33 -07003152 /*
3153 * Open-code file_start_write here to grab freeze protection,
3154 * which will be released by another thread in
3155 * io_complete_rw(). Fool lockdep by telling it the lock got
3156 * released so that it doesn't complain about the held lock when
3157 * we return to userspace.
3158 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003159 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003160 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003161 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003162 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003163 SB_FREEZE_WRITE);
3164 }
3165 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003166
Jens Axboe9adbd452019-12-20 08:45:55 -07003167 if (req->file->f_op->write_iter)
3168 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003169 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003170 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003171
Jens Axboefaac9962020-02-07 15:45:22 -07003172 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003173 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003174 * retry them without IOCB_NOWAIT.
3175 */
3176 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3177 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003178 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003179 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003180 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003181 iter.count = iov_count;
3182 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003183copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003184 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003185 inline_vecs, &iter);
3186 if (ret)
3187 goto out_free;
Pavel Begunkov252917c2020-07-13 22:59:20 +03003188 /* it's copied and will be cleaned with ->io */
3189 iovec = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003190 return -EAGAIN;
3191 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003192 }
Jens Axboe31b51512019-01-18 22:56:34 -07003193out_free:
Pavel Begunkov252917c2020-07-13 22:59:20 +03003194 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003195 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003196 return ret;
3197}
3198
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003199static int __io_splice_prep(struct io_kiocb *req,
3200 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003201{
3202 struct io_splice* sp = &req->splice;
3203 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3204 int ret;
3205
3206 if (req->flags & REQ_F_NEED_CLEANUP)
3207 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003208 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3209 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003210
3211 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003212 sp->len = READ_ONCE(sqe->len);
3213 sp->flags = READ_ONCE(sqe->splice_flags);
3214
3215 if (unlikely(sp->flags & ~valid_flags))
3216 return -EINVAL;
3217
3218 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3219 (sp->flags & SPLICE_F_FD_IN_FIXED));
3220 if (ret)
3221 return ret;
3222 req->flags |= REQ_F_NEED_CLEANUP;
3223
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003224 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3225 /*
3226 * Splice operation will be punted aync, and here need to
3227 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3228 */
3229 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003230 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003231 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003232
3233 return 0;
3234}
3235
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003236static int io_tee_prep(struct io_kiocb *req,
3237 const struct io_uring_sqe *sqe)
3238{
3239 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3240 return -EINVAL;
3241 return __io_splice_prep(req, sqe);
3242}
3243
3244static int io_tee(struct io_kiocb *req, bool force_nonblock)
3245{
3246 struct io_splice *sp = &req->splice;
3247 struct file *in = sp->file_in;
3248 struct file *out = sp->file_out;
3249 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3250 long ret = 0;
3251
3252 if (force_nonblock)
3253 return -EAGAIN;
3254 if (sp->len)
3255 ret = do_tee(in, out, sp->len, flags);
3256
3257 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3258 req->flags &= ~REQ_F_NEED_CLEANUP;
3259
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003260 if (ret != sp->len)
3261 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003262 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003263 return 0;
3264}
3265
3266static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3267{
3268 struct io_splice* sp = &req->splice;
3269
3270 sp->off_in = READ_ONCE(sqe->splice_off_in);
3271 sp->off_out = READ_ONCE(sqe->off);
3272 return __io_splice_prep(req, sqe);
3273}
3274
Pavel Begunkov014db002020-03-03 21:33:12 +03003275static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003276{
3277 struct io_splice *sp = &req->splice;
3278 struct file *in = sp->file_in;
3279 struct file *out = sp->file_out;
3280 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3281 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003282 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003283
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003284 if (force_nonblock)
3285 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003286
3287 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3288 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003289
Jens Axboe948a7742020-05-17 14:21:38 -06003290 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003291 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003292
3293 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3294 req->flags &= ~REQ_F_NEED_CLEANUP;
3295
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003296 if (ret != sp->len)
3297 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003298 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003299 return 0;
3300}
3301
Jens Axboe2b188cc2019-01-07 10:46:33 -07003302/*
3303 * IORING_OP_NOP just posts a completion event, nothing else.
3304 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003305static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003306{
3307 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003308
Jens Axboedef596e2019-01-09 08:59:42 -07003309 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3310 return -EINVAL;
3311
Jens Axboe229a7b62020-06-22 10:13:11 -06003312 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003313 return 0;
3314}
3315
Jens Axboe3529d8c2019-12-19 18:24:38 -07003316static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003317{
Jens Axboe6b063142019-01-10 22:13:58 -07003318 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003319
Jens Axboe09bb8392019-03-13 12:39:28 -06003320 if (!req->file)
3321 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003322
Jens Axboe6b063142019-01-10 22:13:58 -07003323 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003324 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003325 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003326 return -EINVAL;
3327
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003328 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3329 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3330 return -EINVAL;
3331
3332 req->sync.off = READ_ONCE(sqe->off);
3333 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003334 return 0;
3335}
3336
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003337static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003338{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003339 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003340 int ret;
3341
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003342 /* fsync always requires a blocking context */
3343 if (force_nonblock)
3344 return -EAGAIN;
3345
Jens Axboe9adbd452019-12-20 08:45:55 -07003346 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003347 end > 0 ? end : LLONG_MAX,
3348 req->sync.flags & IORING_FSYNC_DATASYNC);
3349 if (ret < 0)
3350 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003351 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003352 return 0;
3353}
3354
Jens Axboed63d1b52019-12-10 10:38:56 -07003355static int io_fallocate_prep(struct io_kiocb *req,
3356 const struct io_uring_sqe *sqe)
3357{
3358 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3359 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003360 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3361 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003362
3363 req->sync.off = READ_ONCE(sqe->off);
3364 req->sync.len = READ_ONCE(sqe->addr);
3365 req->sync.mode = READ_ONCE(sqe->len);
3366 return 0;
3367}
3368
Pavel Begunkov014db002020-03-03 21:33:12 +03003369static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003370{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003371 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003372
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003373 /* fallocate always requiring blocking context */
3374 if (force_nonblock)
3375 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003376 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3377 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003378 if (ret < 0)
3379 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003380 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003381 return 0;
3382}
3383
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003384static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003385{
Jens Axboef8748882020-01-08 17:47:02 -07003386 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003387 int ret;
3388
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003389 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003390 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003391 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003392 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003393 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003394 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003395
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003396 /* open.how should be already initialised */
3397 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003398 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003399
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003400 req->open.dfd = READ_ONCE(sqe->fd);
3401 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003402 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003403 if (IS_ERR(req->open.filename)) {
3404 ret = PTR_ERR(req->open.filename);
3405 req->open.filename = NULL;
3406 return ret;
3407 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003408 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003409 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003410 return 0;
3411}
3412
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003413static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3414{
3415 u64 flags, mode;
3416
3417 if (req->flags & REQ_F_NEED_CLEANUP)
3418 return 0;
3419 mode = READ_ONCE(sqe->len);
3420 flags = READ_ONCE(sqe->open_flags);
3421 req->open.how = build_open_how(flags, mode);
3422 return __io_openat_prep(req, sqe);
3423}
3424
Jens Axboecebdb982020-01-08 17:59:24 -07003425static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3426{
3427 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003428 size_t len;
3429 int ret;
3430
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003431 if (req->flags & REQ_F_NEED_CLEANUP)
3432 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003433 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3434 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003435 if (len < OPEN_HOW_SIZE_VER0)
3436 return -EINVAL;
3437
3438 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3439 len);
3440 if (ret)
3441 return ret;
3442
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003443 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003444}
3445
Pavel Begunkov014db002020-03-03 21:33:12 +03003446static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003447{
3448 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003449 struct file *file;
3450 int ret;
3451
Jens Axboef86cd202020-01-29 13:46:44 -07003452 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003453 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003454
Jens Axboecebdb982020-01-08 17:59:24 -07003455 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003456 if (ret)
3457 goto err;
3458
Jens Axboe4022e7a2020-03-19 19:23:18 -06003459 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003460 if (ret < 0)
3461 goto err;
3462
3463 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3464 if (IS_ERR(file)) {
3465 put_unused_fd(ret);
3466 ret = PTR_ERR(file);
3467 } else {
3468 fsnotify_open(file);
3469 fd_install(ret, file);
3470 }
3471err:
3472 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003473 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003474 if (ret < 0)
3475 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003476 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003477 return 0;
3478}
3479
Pavel Begunkov014db002020-03-03 21:33:12 +03003480static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003481{
Pavel Begunkov014db002020-03-03 21:33:12 +03003482 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003483}
3484
Jens Axboe067524e2020-03-02 16:32:28 -07003485static int io_remove_buffers_prep(struct io_kiocb *req,
3486 const struct io_uring_sqe *sqe)
3487{
3488 struct io_provide_buf *p = &req->pbuf;
3489 u64 tmp;
3490
3491 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3492 return -EINVAL;
3493
3494 tmp = READ_ONCE(sqe->fd);
3495 if (!tmp || tmp > USHRT_MAX)
3496 return -EINVAL;
3497
3498 memset(p, 0, sizeof(*p));
3499 p->nbufs = tmp;
3500 p->bgid = READ_ONCE(sqe->buf_group);
3501 return 0;
3502}
3503
3504static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3505 int bgid, unsigned nbufs)
3506{
3507 unsigned i = 0;
3508
3509 /* shouldn't happen */
3510 if (!nbufs)
3511 return 0;
3512
3513 /* the head kbuf is the list itself */
3514 while (!list_empty(&buf->list)) {
3515 struct io_buffer *nxt;
3516
3517 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3518 list_del(&nxt->list);
3519 kfree(nxt);
3520 if (++i == nbufs)
3521 return i;
3522 }
3523 i++;
3524 kfree(buf);
3525 idr_remove(&ctx->io_buffer_idr, bgid);
3526
3527 return i;
3528}
3529
Jens Axboe229a7b62020-06-22 10:13:11 -06003530static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3531 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003532{
3533 struct io_provide_buf *p = &req->pbuf;
3534 struct io_ring_ctx *ctx = req->ctx;
3535 struct io_buffer *head;
3536 int ret = 0;
3537
3538 io_ring_submit_lock(ctx, !force_nonblock);
3539
3540 lockdep_assert_held(&ctx->uring_lock);
3541
3542 ret = -ENOENT;
3543 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3544 if (head)
3545 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3546
3547 io_ring_submit_lock(ctx, !force_nonblock);
3548 if (ret < 0)
3549 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003550 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003551 return 0;
3552}
3553
Jens Axboeddf0322d2020-02-23 16:41:33 -07003554static int io_provide_buffers_prep(struct io_kiocb *req,
3555 const struct io_uring_sqe *sqe)
3556{
3557 struct io_provide_buf *p = &req->pbuf;
3558 u64 tmp;
3559
3560 if (sqe->ioprio || sqe->rw_flags)
3561 return -EINVAL;
3562
3563 tmp = READ_ONCE(sqe->fd);
3564 if (!tmp || tmp > USHRT_MAX)
3565 return -E2BIG;
3566 p->nbufs = tmp;
3567 p->addr = READ_ONCE(sqe->addr);
3568 p->len = READ_ONCE(sqe->len);
3569
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003570 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003571 return -EFAULT;
3572
3573 p->bgid = READ_ONCE(sqe->buf_group);
3574 tmp = READ_ONCE(sqe->off);
3575 if (tmp > USHRT_MAX)
3576 return -E2BIG;
3577 p->bid = tmp;
3578 return 0;
3579}
3580
3581static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3582{
3583 struct io_buffer *buf;
3584 u64 addr = pbuf->addr;
3585 int i, bid = pbuf->bid;
3586
3587 for (i = 0; i < pbuf->nbufs; i++) {
3588 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3589 if (!buf)
3590 break;
3591
3592 buf->addr = addr;
3593 buf->len = pbuf->len;
3594 buf->bid = bid;
3595 addr += pbuf->len;
3596 bid++;
3597 if (!*head) {
3598 INIT_LIST_HEAD(&buf->list);
3599 *head = buf;
3600 } else {
3601 list_add_tail(&buf->list, &(*head)->list);
3602 }
3603 }
3604
3605 return i ? i : -ENOMEM;
3606}
3607
Jens Axboe229a7b62020-06-22 10:13:11 -06003608static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3609 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003610{
3611 struct io_provide_buf *p = &req->pbuf;
3612 struct io_ring_ctx *ctx = req->ctx;
3613 struct io_buffer *head, *list;
3614 int ret = 0;
3615
3616 io_ring_submit_lock(ctx, !force_nonblock);
3617
3618 lockdep_assert_held(&ctx->uring_lock);
3619
3620 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3621
3622 ret = io_add_buffers(p, &head);
3623 if (ret < 0)
3624 goto out;
3625
3626 if (!list) {
3627 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3628 GFP_KERNEL);
3629 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003630 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003631 goto out;
3632 }
3633 }
3634out:
3635 io_ring_submit_unlock(ctx, !force_nonblock);
3636 if (ret < 0)
3637 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003638 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003639 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003640}
3641
Jens Axboe3e4827b2020-01-08 15:18:09 -07003642static int io_epoll_ctl_prep(struct io_kiocb *req,
3643 const struct io_uring_sqe *sqe)
3644{
3645#if defined(CONFIG_EPOLL)
3646 if (sqe->ioprio || sqe->buf_index)
3647 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003648 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3649 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003650
3651 req->epoll.epfd = READ_ONCE(sqe->fd);
3652 req->epoll.op = READ_ONCE(sqe->len);
3653 req->epoll.fd = READ_ONCE(sqe->off);
3654
3655 if (ep_op_has_event(req->epoll.op)) {
3656 struct epoll_event __user *ev;
3657
3658 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3659 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3660 return -EFAULT;
3661 }
3662
3663 return 0;
3664#else
3665 return -EOPNOTSUPP;
3666#endif
3667}
3668
Jens Axboe229a7b62020-06-22 10:13:11 -06003669static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3670 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003671{
3672#if defined(CONFIG_EPOLL)
3673 struct io_epoll *ie = &req->epoll;
3674 int ret;
3675
3676 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3677 if (force_nonblock && ret == -EAGAIN)
3678 return -EAGAIN;
3679
3680 if (ret < 0)
3681 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003682 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003683 return 0;
3684#else
3685 return -EOPNOTSUPP;
3686#endif
3687}
3688
Jens Axboec1ca7572019-12-25 22:18:28 -07003689static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3690{
3691#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3692 if (sqe->ioprio || sqe->buf_index || sqe->off)
3693 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003694 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3695 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003696
3697 req->madvise.addr = READ_ONCE(sqe->addr);
3698 req->madvise.len = READ_ONCE(sqe->len);
3699 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3700 return 0;
3701#else
3702 return -EOPNOTSUPP;
3703#endif
3704}
3705
Pavel Begunkov014db002020-03-03 21:33:12 +03003706static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003707{
3708#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3709 struct io_madvise *ma = &req->madvise;
3710 int ret;
3711
3712 if (force_nonblock)
3713 return -EAGAIN;
3714
3715 ret = do_madvise(ma->addr, ma->len, ma->advice);
3716 if (ret < 0)
3717 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003718 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003719 return 0;
3720#else
3721 return -EOPNOTSUPP;
3722#endif
3723}
3724
Jens Axboe4840e412019-12-25 22:03:45 -07003725static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3726{
3727 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3728 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003729 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3730 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003731
3732 req->fadvise.offset = READ_ONCE(sqe->off);
3733 req->fadvise.len = READ_ONCE(sqe->len);
3734 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3735 return 0;
3736}
3737
Pavel Begunkov014db002020-03-03 21:33:12 +03003738static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003739{
3740 struct io_fadvise *fa = &req->fadvise;
3741 int ret;
3742
Jens Axboe3e694262020-02-01 09:22:49 -07003743 if (force_nonblock) {
3744 switch (fa->advice) {
3745 case POSIX_FADV_NORMAL:
3746 case POSIX_FADV_RANDOM:
3747 case POSIX_FADV_SEQUENTIAL:
3748 break;
3749 default:
3750 return -EAGAIN;
3751 }
3752 }
Jens Axboe4840e412019-12-25 22:03:45 -07003753
3754 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3755 if (ret < 0)
3756 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003757 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003758 return 0;
3759}
3760
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003761static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3762{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003763 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3764 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003765 if (sqe->ioprio || sqe->buf_index)
3766 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003767 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003768 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003769
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003770 req->statx.dfd = READ_ONCE(sqe->fd);
3771 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003772 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003773 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3774 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003775
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003776 return 0;
3777}
3778
Pavel Begunkov014db002020-03-03 21:33:12 +03003779static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003780{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003781 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003782 int ret;
3783
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003784 if (force_nonblock) {
3785 /* only need file table for an actual valid fd */
3786 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3787 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003788 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003789 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003790
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003791 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3792 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003793
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003794 if (ret < 0)
3795 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003796 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003797 return 0;
3798}
3799
Jens Axboeb5dba592019-12-11 14:02:38 -07003800static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3801{
3802 /*
3803 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003804 * leave the 'file' in an undeterminate state, and here need to modify
3805 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003806 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003807 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003808 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3809
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003810 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3811 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003812 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3813 sqe->rw_flags || sqe->buf_index)
3814 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003815 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003816 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003817
3818 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003819 if ((req->file && req->file->f_op == &io_uring_fops) ||
3820 req->close.fd == req->ctx->ring_fd)
3821 return -EBADF;
3822
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003823 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003824 return 0;
3825}
3826
Jens Axboe229a7b62020-06-22 10:13:11 -06003827static int io_close(struct io_kiocb *req, bool force_nonblock,
3828 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003829{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003830 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003831 int ret;
3832
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003833 /* might be already done during nonblock submission */
3834 if (!close->put_file) {
3835 ret = __close_fd_get_file(close->fd, &close->put_file);
3836 if (ret < 0)
3837 return (ret == -ENOENT) ? -EBADF : ret;
3838 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003839
3840 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003841 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003842 /* was never set, but play safe */
3843 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003844 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003845 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003846 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003847 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003848
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003849 /* No ->flush() or already async, safely close from here */
3850 ret = filp_close(close->put_file, req->work.files);
3851 if (ret < 0)
3852 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003853 fput(close->put_file);
3854 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003855 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003856 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003857}
3858
Jens Axboe3529d8c2019-12-19 18:24:38 -07003859static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003860{
3861 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003862
3863 if (!req->file)
3864 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003865
3866 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3867 return -EINVAL;
3868 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3869 return -EINVAL;
3870
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003871 req->sync.off = READ_ONCE(sqe->off);
3872 req->sync.len = READ_ONCE(sqe->len);
3873 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003874 return 0;
3875}
3876
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003877static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003878{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003879 int ret;
3880
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003881 /* sync_file_range always requires a blocking context */
3882 if (force_nonblock)
3883 return -EAGAIN;
3884
Jens Axboe9adbd452019-12-20 08:45:55 -07003885 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003886 req->sync.flags);
3887 if (ret < 0)
3888 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003889 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003890 return 0;
3891}
3892
YueHaibing469956e2020-03-04 15:53:52 +08003893#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003894static int io_setup_async_msg(struct io_kiocb *req,
3895 struct io_async_msghdr *kmsg)
3896{
3897 if (req->io)
3898 return -EAGAIN;
3899 if (io_alloc_async_ctx(req)) {
3900 if (kmsg->iov != kmsg->fast_iov)
3901 kfree(kmsg->iov);
3902 return -ENOMEM;
3903 }
3904 req->flags |= REQ_F_NEED_CLEANUP;
3905 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3906 return -EAGAIN;
3907}
3908
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03003909static int io_sendmsg_copy_hdr(struct io_kiocb *req,
3910 struct io_async_msghdr *iomsg)
3911{
3912 iomsg->iov = iomsg->fast_iov;
3913 iomsg->msg.msg_name = &iomsg->addr;
3914 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
3915 req->sr_msg.msg_flags, &iomsg->iov);
3916}
3917
Jens Axboe3529d8c2019-12-19 18:24:38 -07003918static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003919{
Jens Axboee47293f2019-12-20 08:58:21 -07003920 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003921 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003922 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003923
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003924 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3925 return -EINVAL;
3926
Jens Axboee47293f2019-12-20 08:58:21 -07003927 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03003928 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003929 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003930
Jens Axboed8768362020-02-27 14:17:49 -07003931#ifdef CONFIG_COMPAT
3932 if (req->ctx->compat)
3933 sr->msg_flags |= MSG_CMSG_COMPAT;
3934#endif
3935
Jens Axboefddafac2020-01-04 20:19:44 -07003936 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003937 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003938 /* iovec is already imported */
3939 if (req->flags & REQ_F_NEED_CLEANUP)
3940 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003941
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03003942 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003943 if (!ret)
3944 req->flags |= REQ_F_NEED_CLEANUP;
3945 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003946}
3947
Jens Axboe229a7b62020-06-22 10:13:11 -06003948static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3949 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07003950{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03003951 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07003952 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003953 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07003954 int ret;
3955
Jens Axboe03b12302019-12-02 18:50:25 -07003956 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003957 if (unlikely(!sock))
3958 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003959
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003960 if (req->io) {
3961 kmsg = &req->io->msg;
3962 kmsg->msg.msg_name = &req->io->msg.addr;
3963 /* if iov is set, it's allocated already */
3964 if (!kmsg->iov)
3965 kmsg->iov = kmsg->fast_iov;
3966 kmsg->msg.msg_iter.iov = kmsg->iov;
3967 } else {
3968 ret = io_sendmsg_copy_hdr(req, &iomsg);
3969 if (ret)
3970 return ret;
3971 kmsg = &iomsg;
Jens Axboe03b12302019-12-02 18:50:25 -07003972 }
3973
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003974 flags = req->sr_msg.msg_flags;
3975 if (flags & MSG_DONTWAIT)
3976 req->flags |= REQ_F_NOWAIT;
3977 else if (force_nonblock)
3978 flags |= MSG_DONTWAIT;
3979
3980 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
3981 if (force_nonblock && ret == -EAGAIN)
3982 return io_setup_async_msg(req, kmsg);
3983 if (ret == -ERESTARTSYS)
3984 ret = -EINTR;
3985
Pavel Begunkov6b754c82020-07-16 23:28:00 +03003986 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003987 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003988 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003989 if (ret < 0)
3990 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003991 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07003992 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003993}
3994
Jens Axboe229a7b62020-06-22 10:13:11 -06003995static int io_send(struct io_kiocb *req, bool force_nonblock,
3996 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07003997{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003998 struct io_sr_msg *sr = &req->sr_msg;
3999 struct msghdr msg;
4000 struct iovec iov;
Jens Axboefddafac2020-01-04 20:19:44 -07004001 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004002 unsigned flags;
Jens Axboefddafac2020-01-04 20:19:44 -07004003 int ret;
4004
Jens Axboefddafac2020-01-04 20:19:44 -07004005 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004006 if (unlikely(!sock))
4007 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004008
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004009 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4010 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004011 return ret;;
Jens Axboefddafac2020-01-04 20:19:44 -07004012
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004013 msg.msg_name = NULL;
4014 msg.msg_control = NULL;
4015 msg.msg_controllen = 0;
4016 msg.msg_namelen = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004017
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004018 flags = req->sr_msg.msg_flags;
4019 if (flags & MSG_DONTWAIT)
4020 req->flags |= REQ_F_NOWAIT;
4021 else if (force_nonblock)
4022 flags |= MSG_DONTWAIT;
Jens Axboefddafac2020-01-04 20:19:44 -07004023
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004024 msg.msg_flags = flags;
4025 ret = sock_sendmsg(sock, &msg);
4026 if (force_nonblock && ret == -EAGAIN)
4027 return -EAGAIN;
4028 if (ret == -ERESTARTSYS)
4029 ret = -EINTR;
Jens Axboefddafac2020-01-04 20:19:44 -07004030
Jens Axboefddafac2020-01-04 20:19:44 -07004031 if (ret < 0)
4032 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004033 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004034 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004035}
4036
Pavel Begunkov1400e692020-07-12 20:41:05 +03004037static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4038 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004039{
4040 struct io_sr_msg *sr = &req->sr_msg;
4041 struct iovec __user *uiov;
4042 size_t iov_len;
4043 int ret;
4044
Pavel Begunkov1400e692020-07-12 20:41:05 +03004045 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4046 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004047 if (ret)
4048 return ret;
4049
4050 if (req->flags & REQ_F_BUFFER_SELECT) {
4051 if (iov_len > 1)
4052 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004053 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004054 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004055 sr->len = iomsg->iov[0].iov_len;
4056 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004057 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004058 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004059 } else {
4060 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004061 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004062 if (ret > 0)
4063 ret = 0;
4064 }
4065
4066 return ret;
4067}
4068
4069#ifdef CONFIG_COMPAT
4070static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004071 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004072{
4073 struct compat_msghdr __user *msg_compat;
4074 struct io_sr_msg *sr = &req->sr_msg;
4075 struct compat_iovec __user *uiov;
4076 compat_uptr_t ptr;
4077 compat_size_t len;
4078 int ret;
4079
Pavel Begunkov270a5942020-07-12 20:41:04 +03004080 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004081 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004082 &ptr, &len);
4083 if (ret)
4084 return ret;
4085
4086 uiov = compat_ptr(ptr);
4087 if (req->flags & REQ_F_BUFFER_SELECT) {
4088 compat_ssize_t clen;
4089
4090 if (len > 1)
4091 return -EINVAL;
4092 if (!access_ok(uiov, sizeof(*uiov)))
4093 return -EFAULT;
4094 if (__get_user(clen, &uiov->iov_len))
4095 return -EFAULT;
4096 if (clen < 0)
4097 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004098 sr->len = iomsg->iov[0].iov_len;
4099 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004100 } else {
4101 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004102 &iomsg->iov,
4103 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004104 if (ret < 0)
4105 return ret;
4106 }
4107
4108 return 0;
4109}
Jens Axboe03b12302019-12-02 18:50:25 -07004110#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004111
Pavel Begunkov1400e692020-07-12 20:41:05 +03004112static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4113 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004114{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004115 iomsg->msg.msg_name = &iomsg->addr;
4116 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004117
4118#ifdef CONFIG_COMPAT
4119 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004120 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004121#endif
4122
Pavel Begunkov1400e692020-07-12 20:41:05 +03004123 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004124}
4125
Jens Axboebcda7ba2020-02-23 16:42:51 -07004126static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004127 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004128{
4129 struct io_sr_msg *sr = &req->sr_msg;
4130 struct io_buffer *kbuf;
4131
Jens Axboebcda7ba2020-02-23 16:42:51 -07004132 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4133 if (IS_ERR(kbuf))
4134 return kbuf;
4135
4136 sr->kbuf = kbuf;
4137 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004138 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004139}
4140
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004141static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4142{
4143 return io_put_kbuf(req, req->sr_msg.kbuf);
4144}
4145
Jens Axboe3529d8c2019-12-19 18:24:38 -07004146static int io_recvmsg_prep(struct io_kiocb *req,
4147 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004148{
Jens Axboee47293f2019-12-20 08:58:21 -07004149 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004150 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004151 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004152
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004153 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4154 return -EINVAL;
4155
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004157 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004158 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004159 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004160
Jens Axboed8768362020-02-27 14:17:49 -07004161#ifdef CONFIG_COMPAT
4162 if (req->ctx->compat)
4163 sr->msg_flags |= MSG_CMSG_COMPAT;
4164#endif
4165
Jens Axboefddafac2020-01-04 20:19:44 -07004166 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004167 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004168 /* iovec is already imported */
4169 if (req->flags & REQ_F_NEED_CLEANUP)
4170 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004171
Pavel Begunkov1400e692020-07-12 20:41:05 +03004172 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004173 if (!ret)
4174 req->flags |= REQ_F_NEED_CLEANUP;
4175 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004176}
4177
Jens Axboe229a7b62020-06-22 10:13:11 -06004178static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4179 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004180{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004181 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004182 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004183 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004184 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004185 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004186
Jens Axboe0fa03c62019-04-19 13:34:07 -06004187 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004188 if (unlikely(!sock))
4189 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004190
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004191 if (req->io) {
4192 kmsg = &req->io->msg;
4193 kmsg->msg.msg_name = &req->io->msg.addr;
4194 /* if iov is set, it's allocated already */
4195 if (!kmsg->iov)
4196 kmsg->iov = kmsg->fast_iov;
4197 kmsg->msg.msg_iter.iov = kmsg->iov;
4198 } else {
4199 ret = io_recvmsg_copy_hdr(req, &iomsg);
4200 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004201 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004202 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004203 }
4204
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004205 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004206 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004207 if (IS_ERR(kbuf))
4208 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004209 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4210 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4211 1, req->sr_msg.len);
4212 }
4213
4214 flags = req->sr_msg.msg_flags;
4215 if (flags & MSG_DONTWAIT)
4216 req->flags |= REQ_F_NOWAIT;
4217 else if (force_nonblock)
4218 flags |= MSG_DONTWAIT;
4219
4220 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4221 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004222 if (force_nonblock && ret == -EAGAIN)
4223 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004224 if (ret == -ERESTARTSYS)
4225 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004226
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004227 if (req->flags & REQ_F_BUFFER_SELECTED)
4228 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004229 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004230 kfree(kmsg->iov);
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004231 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004232 if (ret < 0)
4233 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004234 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004235 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004236}
4237
Jens Axboe229a7b62020-06-22 10:13:11 -06004238static int io_recv(struct io_kiocb *req, bool force_nonblock,
4239 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004240{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004241 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004242 struct io_sr_msg *sr = &req->sr_msg;
4243 struct msghdr msg;
4244 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004245 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004246 struct iovec iov;
4247 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004248 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004249
Jens Axboefddafac2020-01-04 20:19:44 -07004250 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004251 if (unlikely(!sock))
4252 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004253
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004254 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004255 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004256 if (IS_ERR(kbuf))
4257 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004258 buf = u64_to_user_ptr(kbuf->addr);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004259 }
Jens Axboefddafac2020-01-04 20:19:44 -07004260
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004261 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004262 if (unlikely(ret))
4263 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004264
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004265 msg.msg_name = NULL;
4266 msg.msg_control = NULL;
4267 msg.msg_controllen = 0;
4268 msg.msg_namelen = 0;
4269 msg.msg_iocb = NULL;
4270 msg.msg_flags = 0;
4271
4272 flags = req->sr_msg.msg_flags;
4273 if (flags & MSG_DONTWAIT)
4274 req->flags |= REQ_F_NOWAIT;
4275 else if (force_nonblock)
4276 flags |= MSG_DONTWAIT;
4277
4278 ret = sock_recvmsg(sock, &msg, flags);
4279 if (force_nonblock && ret == -EAGAIN)
4280 return -EAGAIN;
4281 if (ret == -ERESTARTSYS)
4282 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004283out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004284 if (req->flags & REQ_F_BUFFER_SELECTED)
4285 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004286 if (ret < 0)
4287 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004288 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004289 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004290}
4291
Jens Axboe3529d8c2019-12-19 18:24:38 -07004292static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004293{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004294 struct io_accept *accept = &req->accept;
4295
Jens Axboe17f2fe32019-10-17 14:42:58 -06004296 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4297 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004298 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004299 return -EINVAL;
4300
Jens Axboed55e5f52019-12-11 16:12:15 -07004301 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4302 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004303 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004304 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004305 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004306}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004307
Jens Axboe229a7b62020-06-22 10:13:11 -06004308static int io_accept(struct io_kiocb *req, bool force_nonblock,
4309 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004310{
4311 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004312 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004313 int ret;
4314
Jiufei Xuee697dee2020-06-10 13:41:59 +08004315 if (req->file->f_flags & O_NONBLOCK)
4316 req->flags |= REQ_F_NOWAIT;
4317
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004318 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004319 accept->addr_len, accept->flags,
4320 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004321 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004322 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004323 if (ret < 0) {
4324 if (ret == -ERESTARTSYS)
4325 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004326 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004327 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004328 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004329 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004330}
4331
Jens Axboe3529d8c2019-12-19 18:24:38 -07004332static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004333{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004334 struct io_connect *conn = &req->connect;
4335 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004336
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004337 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4338 return -EINVAL;
4339 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4340 return -EINVAL;
4341
Jens Axboe3529d8c2019-12-19 18:24:38 -07004342 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4343 conn->addr_len = READ_ONCE(sqe->addr2);
4344
4345 if (!io)
4346 return 0;
4347
4348 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004349 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004350}
4351
Jens Axboe229a7b62020-06-22 10:13:11 -06004352static int io_connect(struct io_kiocb *req, bool force_nonblock,
4353 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004354{
Jens Axboef499a022019-12-02 16:28:46 -07004355 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004356 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004357 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004358
Jens Axboef499a022019-12-02 16:28:46 -07004359 if (req->io) {
4360 io = req->io;
4361 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004362 ret = move_addr_to_kernel(req->connect.addr,
4363 req->connect.addr_len,
4364 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004365 if (ret)
4366 goto out;
4367 io = &__io;
4368 }
4369
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004370 file_flags = force_nonblock ? O_NONBLOCK : 0;
4371
4372 ret = __sys_connect_file(req->file, &io->connect.address,
4373 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004374 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004375 if (req->io)
4376 return -EAGAIN;
4377 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004378 ret = -ENOMEM;
4379 goto out;
4380 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004381 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004382 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004383 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004384 if (ret == -ERESTARTSYS)
4385 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004386out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004387 if (ret < 0)
4388 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004389 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004390 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004391}
YueHaibing469956e2020-03-04 15:53:52 +08004392#else /* !CONFIG_NET */
4393static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4394{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004395 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004396}
4397
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004398static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4399 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004400{
YueHaibing469956e2020-03-04 15:53:52 +08004401 return -EOPNOTSUPP;
4402}
4403
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004404static int io_send(struct io_kiocb *req, bool force_nonblock,
4405 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004406{
4407 return -EOPNOTSUPP;
4408}
4409
4410static int io_recvmsg_prep(struct io_kiocb *req,
4411 const struct io_uring_sqe *sqe)
4412{
4413 return -EOPNOTSUPP;
4414}
4415
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004416static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4417 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004418{
4419 return -EOPNOTSUPP;
4420}
4421
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004422static int io_recv(struct io_kiocb *req, bool force_nonblock,
4423 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004424{
4425 return -EOPNOTSUPP;
4426}
4427
4428static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4429{
4430 return -EOPNOTSUPP;
4431}
4432
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004433static int io_accept(struct io_kiocb *req, bool force_nonblock,
4434 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004435{
4436 return -EOPNOTSUPP;
4437}
4438
4439static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4440{
4441 return -EOPNOTSUPP;
4442}
4443
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004444static int io_connect(struct io_kiocb *req, bool force_nonblock,
4445 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004446{
4447 return -EOPNOTSUPP;
4448}
4449#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004450
Jens Axboed7718a92020-02-14 22:23:12 -07004451struct io_poll_table {
4452 struct poll_table_struct pt;
4453 struct io_kiocb *req;
4454 int error;
4455};
4456
Jens Axboed7718a92020-02-14 22:23:12 -07004457static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4458 __poll_t mask, task_work_func_t func)
4459{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004460 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004461
4462 /* for instances that support it check for an event match first: */
4463 if (mask && !(mask & poll->events))
4464 return 0;
4465
4466 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4467
4468 list_del_init(&poll->wait.entry);
4469
Jens Axboed7718a92020-02-14 22:23:12 -07004470 req->result = mask;
4471 init_task_work(&req->task_work, func);
4472 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004473 * If this fails, then the task is exiting. When a task exits, the
4474 * work gets canceled, so just cancel this request as well instead
4475 * of executing it. We can't safely execute it anyway, as we may not
4476 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004477 */
Jens Axboeb7db41c2020-07-04 08:55:50 -06004478 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004479 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004480 struct task_struct *tsk;
4481
Jens Axboee3aabf92020-05-18 11:04:17 -06004482 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004483 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004484 task_work_add(tsk, &req->task_work, 0);
4485 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004486 }
Jens Axboed7718a92020-02-14 22:23:12 -07004487 return 1;
4488}
4489
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004490static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4491 __acquires(&req->ctx->completion_lock)
4492{
4493 struct io_ring_ctx *ctx = req->ctx;
4494
4495 if (!req->result && !READ_ONCE(poll->canceled)) {
4496 struct poll_table_struct pt = { ._key = poll->events };
4497
4498 req->result = vfs_poll(req->file, &pt) & poll->events;
4499 }
4500
4501 spin_lock_irq(&ctx->completion_lock);
4502 if (!req->result && !READ_ONCE(poll->canceled)) {
4503 add_wait_queue(poll->head, &poll->wait);
4504 return true;
4505 }
4506
4507 return false;
4508}
4509
Jens Axboe807abcb2020-07-17 17:09:27 -06004510static void io_poll_remove_double(struct io_kiocb *req, void *data)
Jens Axboe18bceab2020-05-15 11:56:54 -06004511{
Jens Axboe807abcb2020-07-17 17:09:27 -06004512 struct io_poll_iocb *poll = data;
Jens Axboe18bceab2020-05-15 11:56:54 -06004513
4514 lockdep_assert_held(&req->ctx->completion_lock);
4515
4516 if (poll && poll->head) {
4517 struct wait_queue_head *head = poll->head;
4518
4519 spin_lock(&head->lock);
4520 list_del_init(&poll->wait.entry);
4521 if (poll->wait.private)
4522 refcount_dec(&req->refs);
4523 poll->head = NULL;
4524 spin_unlock(&head->lock);
4525 }
4526}
4527
4528static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4529{
4530 struct io_ring_ctx *ctx = req->ctx;
4531
Jens Axboe807abcb2020-07-17 17:09:27 -06004532 io_poll_remove_double(req, req->io);
Jens Axboe18bceab2020-05-15 11:56:54 -06004533 req->poll.done = true;
4534 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4535 io_commit_cqring(ctx);
4536}
4537
4538static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4539{
4540 struct io_ring_ctx *ctx = req->ctx;
4541
4542 if (io_poll_rewait(req, &req->poll)) {
4543 spin_unlock_irq(&ctx->completion_lock);
4544 return;
4545 }
4546
4547 hash_del(&req->hash_node);
4548 io_poll_complete(req, req->result, 0);
4549 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004550 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004551 spin_unlock_irq(&ctx->completion_lock);
4552
4553 io_cqring_ev_posted(ctx);
4554}
4555
4556static void io_poll_task_func(struct callback_head *cb)
4557{
4558 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4559 struct io_kiocb *nxt = NULL;
4560
4561 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004562 if (nxt)
4563 __io_req_task_submit(nxt);
Jens Axboe18bceab2020-05-15 11:56:54 -06004564}
4565
4566static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4567 int sync, void *key)
4568{
4569 struct io_kiocb *req = wait->private;
Jens Axboe807abcb2020-07-17 17:09:27 -06004570 struct io_poll_iocb *poll = req->apoll->double_poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004571 __poll_t mask = key_to_poll(key);
4572
4573 /* for instances that support it check for an event match first: */
4574 if (mask && !(mask & poll->events))
4575 return 0;
4576
Jens Axboe807abcb2020-07-17 17:09:27 -06004577 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004578 bool done;
4579
Jens Axboe807abcb2020-07-17 17:09:27 -06004580 spin_lock(&poll->head->lock);
4581 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004582 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004583 list_del_init(&poll->wait.entry);
4584 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004585 if (!done)
4586 __io_async_wake(req, poll, mask, io_poll_task_func);
4587 }
4588 refcount_dec(&req->refs);
4589 return 1;
4590}
4591
4592static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4593 wait_queue_func_t wake_func)
4594{
4595 poll->head = NULL;
4596 poll->done = false;
4597 poll->canceled = false;
4598 poll->events = events;
4599 INIT_LIST_HEAD(&poll->wait.entry);
4600 init_waitqueue_func_entry(&poll->wait, wake_func);
4601}
4602
4603static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004604 struct wait_queue_head *head,
4605 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004606{
4607 struct io_kiocb *req = pt->req;
4608
4609 /*
4610 * If poll->head is already set, it's because the file being polled
4611 * uses multiple waitqueues for poll handling (eg one for read, one
4612 * for write). Setup a separate io_poll_iocb if this happens.
4613 */
4614 if (unlikely(poll->head)) {
4615 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004616 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004617 pt->error = -EINVAL;
4618 return;
4619 }
4620 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4621 if (!poll) {
4622 pt->error = -ENOMEM;
4623 return;
4624 }
4625 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4626 refcount_inc(&req->refs);
4627 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004628 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004629 }
4630
4631 pt->error = 0;
4632 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004633
4634 if (poll->events & EPOLLEXCLUSIVE)
4635 add_wait_queue_exclusive(head, &poll->wait);
4636 else
4637 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004638}
4639
4640static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4641 struct poll_table_struct *p)
4642{
4643 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004644 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004645
Jens Axboe807abcb2020-07-17 17:09:27 -06004646 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004647}
4648
Jens Axboed7718a92020-02-14 22:23:12 -07004649static void io_async_task_func(struct callback_head *cb)
4650{
4651 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4652 struct async_poll *apoll = req->apoll;
4653 struct io_ring_ctx *ctx = req->ctx;
4654
4655 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4656
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004657 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004658 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004659 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004660 }
4661
Jens Axboe31067252020-05-17 17:43:31 -06004662 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004663 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004664 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004665
Jens Axboe807abcb2020-07-17 17:09:27 -06004666 io_poll_remove_double(req, apoll->double_poll);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004667 spin_unlock_irq(&ctx->completion_lock);
4668
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004669 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004670 if (req->flags & REQ_F_WORK_INITIALIZED)
4671 memcpy(&req->work, &apoll->work, sizeof(req->work));
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004672
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004673 if (!READ_ONCE(apoll->poll.canceled))
4674 __io_req_task_submit(req);
4675 else
4676 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004677
Jens Axboe807abcb2020-07-17 17:09:27 -06004678 kfree(apoll->double_poll);
Dan Carpenteraa340842020-07-08 21:47:11 +03004679 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004680}
4681
4682static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4683 void *key)
4684{
4685 struct io_kiocb *req = wait->private;
4686 struct io_poll_iocb *poll = &req->apoll->poll;
4687
4688 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4689 key_to_poll(key));
4690
4691 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4692}
4693
4694static void io_poll_req_insert(struct io_kiocb *req)
4695{
4696 struct io_ring_ctx *ctx = req->ctx;
4697 struct hlist_head *list;
4698
4699 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4700 hlist_add_head(&req->hash_node, list);
4701}
4702
4703static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4704 struct io_poll_iocb *poll,
4705 struct io_poll_table *ipt, __poll_t mask,
4706 wait_queue_func_t wake_func)
4707 __acquires(&ctx->completion_lock)
4708{
4709 struct io_ring_ctx *ctx = req->ctx;
4710 bool cancel = false;
4711
Jens Axboe18bceab2020-05-15 11:56:54 -06004712 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004713 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004714 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004715
4716 ipt->pt._key = mask;
4717 ipt->req = req;
4718 ipt->error = -EINVAL;
4719
Jens Axboed7718a92020-02-14 22:23:12 -07004720 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4721
4722 spin_lock_irq(&ctx->completion_lock);
4723 if (likely(poll->head)) {
4724 spin_lock(&poll->head->lock);
4725 if (unlikely(list_empty(&poll->wait.entry))) {
4726 if (ipt->error)
4727 cancel = true;
4728 ipt->error = 0;
4729 mask = 0;
4730 }
4731 if (mask || ipt->error)
4732 list_del_init(&poll->wait.entry);
4733 else if (cancel)
4734 WRITE_ONCE(poll->canceled, true);
4735 else if (!poll->done) /* actually waiting for an event */
4736 io_poll_req_insert(req);
4737 spin_unlock(&poll->head->lock);
4738 }
4739
4740 return mask;
4741}
4742
4743static bool io_arm_poll_handler(struct io_kiocb *req)
4744{
4745 const struct io_op_def *def = &io_op_defs[req->opcode];
4746 struct io_ring_ctx *ctx = req->ctx;
4747 struct async_poll *apoll;
4748 struct io_poll_table ipt;
4749 __poll_t mask, ret;
4750
4751 if (!req->file || !file_can_poll(req->file))
4752 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004753 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004754 return false;
4755 if (!def->pollin && !def->pollout)
4756 return false;
4757
4758 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4759 if (unlikely(!apoll))
4760 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004761 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004762
4763 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004764 if (req->flags & REQ_F_WORK_INITIALIZED)
4765 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004766
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004767 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004768 req->apoll = apoll;
4769 INIT_HLIST_NODE(&req->hash_node);
4770
Nathan Chancellor8755d972020-03-02 16:01:19 -07004771 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004772 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004773 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004774 if (def->pollout)
4775 mask |= POLLOUT | POLLWRNORM;
4776 mask |= POLLERR | POLLPRI;
4777
4778 ipt.pt._qproc = io_async_queue_proc;
4779
4780 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4781 io_async_wake);
4782 if (ret) {
Jens Axboe807abcb2020-07-17 17:09:27 -06004783 io_poll_remove_double(req, apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004784 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004785 if (req->flags & REQ_F_WORK_INITIALIZED)
4786 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe807abcb2020-07-17 17:09:27 -06004787 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004788 kfree(apoll);
4789 return false;
4790 }
4791 spin_unlock_irq(&ctx->completion_lock);
4792 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4793 apoll->poll.events);
4794 return true;
4795}
4796
4797static bool __io_poll_remove_one(struct io_kiocb *req,
4798 struct io_poll_iocb *poll)
4799{
Jens Axboeb41e9852020-02-17 09:52:41 -07004800 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004801
4802 spin_lock(&poll->head->lock);
4803 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004804 if (!list_empty(&poll->wait.entry)) {
4805 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004806 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004807 }
4808 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004809 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004810 return do_complete;
4811}
4812
4813static bool io_poll_remove_one(struct io_kiocb *req)
4814{
4815 bool do_complete;
4816
4817 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe807abcb2020-07-17 17:09:27 -06004818 io_poll_remove_double(req, req->io);
Jens Axboed7718a92020-02-14 22:23:12 -07004819 do_complete = __io_poll_remove_one(req, &req->poll);
4820 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004821 struct async_poll *apoll = req->apoll;
4822
Jens Axboe807abcb2020-07-17 17:09:27 -06004823 io_poll_remove_double(req, apoll->double_poll);
4824
Jens Axboed7718a92020-02-14 22:23:12 -07004825 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004826 do_complete = __io_poll_remove_one(req, &apoll->poll);
4827 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004828 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004829 /*
4830 * restore ->work because we will call
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03004831 * io_req_clean_work below when dropping the
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004832 * final reference.
4833 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004834 if (req->flags & REQ_F_WORK_INITIALIZED)
4835 memcpy(&req->work, &apoll->work,
4836 sizeof(req->work));
Jens Axboe807abcb2020-07-17 17:09:27 -06004837 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004838 kfree(apoll);
4839 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004840 }
4841
Jens Axboeb41e9852020-02-17 09:52:41 -07004842 if (do_complete) {
4843 io_cqring_fill_event(req, -ECANCELED);
4844 io_commit_cqring(req->ctx);
4845 req->flags |= REQ_F_COMP_LOCKED;
4846 io_put_req(req);
4847 }
4848
4849 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004850}
4851
4852static void io_poll_remove_all(struct io_ring_ctx *ctx)
4853{
Jens Axboe78076bb2019-12-04 19:56:40 -07004854 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004855 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004856 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004857
4858 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004859 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4860 struct hlist_head *list;
4861
4862 list = &ctx->cancel_hash[i];
4863 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004864 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004865 }
4866 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004867
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004868 if (posted)
4869 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004870}
4871
Jens Axboe47f46762019-11-09 17:43:02 -07004872static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4873{
Jens Axboe78076bb2019-12-04 19:56:40 -07004874 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004875 struct io_kiocb *req;
4876
Jens Axboe78076bb2019-12-04 19:56:40 -07004877 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4878 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004879 if (sqe_addr != req->user_data)
4880 continue;
4881 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004882 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004883 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004884 }
4885
4886 return -ENOENT;
4887}
4888
Jens Axboe3529d8c2019-12-19 18:24:38 -07004889static int io_poll_remove_prep(struct io_kiocb *req,
4890 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004891{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004892 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4893 return -EINVAL;
4894 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4895 sqe->poll_events)
4896 return -EINVAL;
4897
Jens Axboe0969e782019-12-17 18:40:57 -07004898 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004899 return 0;
4900}
4901
4902/*
4903 * Find a running poll command that matches one specified in sqe->addr,
4904 * and remove it if found.
4905 */
4906static int io_poll_remove(struct io_kiocb *req)
4907{
4908 struct io_ring_ctx *ctx = req->ctx;
4909 u64 addr;
4910 int ret;
4911
Jens Axboe0969e782019-12-17 18:40:57 -07004912 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004913 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004914 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004915 spin_unlock_irq(&ctx->completion_lock);
4916
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004917 if (ret < 0)
4918 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004919 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004920 return 0;
4921}
4922
Jens Axboe221c5eb2019-01-17 09:41:58 -07004923static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4924 void *key)
4925{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004926 struct io_kiocb *req = wait->private;
4927 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004928
Jens Axboed7718a92020-02-14 22:23:12 -07004929 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004930}
4931
Jens Axboe221c5eb2019-01-17 09:41:58 -07004932static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4933 struct poll_table_struct *p)
4934{
4935 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4936
Jens Axboe807abcb2020-07-17 17:09:27 -06004937 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07004938}
4939
Jens Axboe3529d8c2019-12-19 18:24:38 -07004940static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004941{
4942 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004943 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004944
4945 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4946 return -EINVAL;
4947 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4948 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004949 if (!poll->file)
4950 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004951
Jiufei Xue5769a352020-06-17 17:53:55 +08004952 events = READ_ONCE(sqe->poll32_events);
4953#ifdef __BIG_ENDIAN
4954 events = swahw32(events);
4955#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004956 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4957 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004958
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004959 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004960 return 0;
4961}
4962
Pavel Begunkov014db002020-03-03 21:33:12 +03004963static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004964{
4965 struct io_poll_iocb *poll = &req->poll;
4966 struct io_ring_ctx *ctx = req->ctx;
4967 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004968 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004969
Pavel Begunkovd5e16d82020-07-24 20:07:20 +03004970 /* ->work is in union with hash_node and others */
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03004971 io_req_clean_work(req);
Pavel Begunkovd5e16d82020-07-24 20:07:20 +03004972 req->flags &= ~REQ_F_WORK_INITIALIZED;
4973
Jens Axboe78076bb2019-12-04 19:56:40 -07004974 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004975 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004976
Jens Axboed7718a92020-02-14 22:23:12 -07004977 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4978 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004979
Jens Axboe8c838782019-03-12 15:48:16 -06004980 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004981 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004982 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004983 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004984 spin_unlock_irq(&ctx->completion_lock);
4985
Jens Axboe8c838782019-03-12 15:48:16 -06004986 if (mask) {
4987 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004988 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004989 }
Jens Axboe8c838782019-03-12 15:48:16 -06004990 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004991}
4992
Jens Axboe5262f562019-09-17 12:26:57 -06004993static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4994{
Jens Axboead8a48a2019-11-15 08:49:11 -07004995 struct io_timeout_data *data = container_of(timer,
4996 struct io_timeout_data, timer);
4997 struct io_kiocb *req = data->req;
4998 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004999 unsigned long flags;
5000
Jens Axboe5262f562019-09-17 12:26:57 -06005001 atomic_inc(&ctx->cq_timeouts);
5002
5003 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08005004 /*
Jens Axboe11365042019-10-16 09:08:32 -06005005 * We could be racing with timeout deletion. If the list is empty,
5006 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005007 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005008 if (!list_empty(&req->timeout.list))
5009 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005010
Jens Axboe78e19bb2019-11-06 15:21:34 -07005011 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005012 io_commit_cqring(ctx);
5013 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5014
5015 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005016 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005017 io_put_req(req);
5018 return HRTIMER_NORESTART;
5019}
5020
Jens Axboe47f46762019-11-09 17:43:02 -07005021static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5022{
5023 struct io_kiocb *req;
5024 int ret = -ENOENT;
5025
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005026 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Jens Axboe47f46762019-11-09 17:43:02 -07005027 if (user_data == req->user_data) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005028 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005029 ret = 0;
5030 break;
5031 }
5032 }
5033
5034 if (ret == -ENOENT)
5035 return ret;
5036
Jens Axboe2d283902019-12-04 11:08:05 -07005037 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005038 if (ret == -1)
5039 return -EALREADY;
5040
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005041 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005042 io_cqring_fill_event(req, -ECANCELED);
5043 io_put_req(req);
5044 return 0;
5045}
5046
Jens Axboe3529d8c2019-12-19 18:24:38 -07005047static int io_timeout_remove_prep(struct io_kiocb *req,
5048 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005049{
Jens Axboeb29472e2019-12-17 18:50:29 -07005050 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5051 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005052 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5053 return -EINVAL;
5054 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005055 return -EINVAL;
5056
5057 req->timeout.addr = READ_ONCE(sqe->addr);
5058 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5059 if (req->timeout.flags)
5060 return -EINVAL;
5061
Jens Axboeb29472e2019-12-17 18:50:29 -07005062 return 0;
5063}
5064
Jens Axboe11365042019-10-16 09:08:32 -06005065/*
5066 * Remove or update an existing timeout command
5067 */
Jens Axboefc4df992019-12-10 14:38:45 -07005068static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005069{
5070 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005071 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005072
Jens Axboe11365042019-10-16 09:08:32 -06005073 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005074 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005075
Jens Axboe47f46762019-11-09 17:43:02 -07005076 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005077 io_commit_cqring(ctx);
5078 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005079 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005080 if (ret < 0)
5081 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005082 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005083 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005084}
5085
Jens Axboe3529d8c2019-12-19 18:24:38 -07005086static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005087 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005088{
Jens Axboead8a48a2019-11-15 08:49:11 -07005089 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005090 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005091 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005092
Jens Axboead8a48a2019-11-15 08:49:11 -07005093 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005094 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005095 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005096 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005097 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005098 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005099 flags = READ_ONCE(sqe->timeout_flags);
5100 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005101 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005102
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005103 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005104
Jens Axboe3529d8c2019-12-19 18:24:38 -07005105 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005106 return -ENOMEM;
5107
5108 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005109 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005110
5111 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005112 return -EFAULT;
5113
Jens Axboe11365042019-10-16 09:08:32 -06005114 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005115 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005116 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005117 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005118
Jens Axboead8a48a2019-11-15 08:49:11 -07005119 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5120 return 0;
5121}
5122
Jens Axboefc4df992019-12-10 14:38:45 -07005123static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005124{
Jens Axboead8a48a2019-11-15 08:49:11 -07005125 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005126 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005127 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005128 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005129
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005130 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005131
Jens Axboe5262f562019-09-17 12:26:57 -06005132 /*
5133 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005134 * timeout event to be satisfied. If it isn't set, then this is
5135 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005136 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005137 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005138 entry = ctx->timeout_list.prev;
5139 goto add;
5140 }
Jens Axboe5262f562019-09-17 12:26:57 -06005141
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005142 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5143 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005144
5145 /*
5146 * Insertion sort, ensuring the first entry in the list is always
5147 * the one we need first.
5148 */
Jens Axboe5262f562019-09-17 12:26:57 -06005149 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005150 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5151 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005152
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005153 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005154 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005155 /* nxt.seq is behind @tail, otherwise would've been completed */
5156 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005157 break;
5158 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005159add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005160 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005161 data->timer.function = io_timeout_fn;
5162 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005163 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005164 return 0;
5165}
5166
Jens Axboe62755e32019-10-28 21:49:21 -06005167static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005168{
Jens Axboe62755e32019-10-28 21:49:21 -06005169 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005170
Jens Axboe62755e32019-10-28 21:49:21 -06005171 return req->user_data == (unsigned long) data;
5172}
5173
Jens Axboee977d6d2019-11-05 12:39:45 -07005174static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005175{
Jens Axboe62755e32019-10-28 21:49:21 -06005176 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005177 int ret = 0;
5178
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005179 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005180 switch (cancel_ret) {
5181 case IO_WQ_CANCEL_OK:
5182 ret = 0;
5183 break;
5184 case IO_WQ_CANCEL_RUNNING:
5185 ret = -EALREADY;
5186 break;
5187 case IO_WQ_CANCEL_NOTFOUND:
5188 ret = -ENOENT;
5189 break;
5190 }
5191
Jens Axboee977d6d2019-11-05 12:39:45 -07005192 return ret;
5193}
5194
Jens Axboe47f46762019-11-09 17:43:02 -07005195static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5196 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005197 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005198{
5199 unsigned long flags;
5200 int ret;
5201
5202 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5203 if (ret != -ENOENT) {
5204 spin_lock_irqsave(&ctx->completion_lock, flags);
5205 goto done;
5206 }
5207
5208 spin_lock_irqsave(&ctx->completion_lock, flags);
5209 ret = io_timeout_cancel(ctx, sqe_addr);
5210 if (ret != -ENOENT)
5211 goto done;
5212 ret = io_poll_cancel(ctx, sqe_addr);
5213done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005214 if (!ret)
5215 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005216 io_cqring_fill_event(req, ret);
5217 io_commit_cqring(ctx);
5218 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5219 io_cqring_ev_posted(ctx);
5220
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005221 if (ret < 0)
5222 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005223 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005224}
5225
Jens Axboe3529d8c2019-12-19 18:24:38 -07005226static int io_async_cancel_prep(struct io_kiocb *req,
5227 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005228{
Jens Axboefbf23842019-12-17 18:45:56 -07005229 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005230 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005231 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5232 return -EINVAL;
5233 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005234 return -EINVAL;
5235
Jens Axboefbf23842019-12-17 18:45:56 -07005236 req->cancel.addr = READ_ONCE(sqe->addr);
5237 return 0;
5238}
5239
Pavel Begunkov014db002020-03-03 21:33:12 +03005240static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005241{
5242 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005243
Pavel Begunkov014db002020-03-03 21:33:12 +03005244 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005245 return 0;
5246}
5247
Jens Axboe05f3fb32019-12-09 11:22:50 -07005248static int io_files_update_prep(struct io_kiocb *req,
5249 const struct io_uring_sqe *sqe)
5250{
Daniele Albano61710e42020-07-18 14:15:16 -06005251 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5252 return -EINVAL;
5253 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005254 return -EINVAL;
5255
5256 req->files_update.offset = READ_ONCE(sqe->off);
5257 req->files_update.nr_args = READ_ONCE(sqe->len);
5258 if (!req->files_update.nr_args)
5259 return -EINVAL;
5260 req->files_update.arg = READ_ONCE(sqe->addr);
5261 return 0;
5262}
5263
Jens Axboe229a7b62020-06-22 10:13:11 -06005264static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5265 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005266{
5267 struct io_ring_ctx *ctx = req->ctx;
5268 struct io_uring_files_update up;
5269 int ret;
5270
Jens Axboef86cd202020-01-29 13:46:44 -07005271 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005272 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005273
5274 up.offset = req->files_update.offset;
5275 up.fds = req->files_update.arg;
5276
5277 mutex_lock(&ctx->uring_lock);
5278 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5279 mutex_unlock(&ctx->uring_lock);
5280
5281 if (ret < 0)
5282 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005283 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005284 return 0;
5285}
5286
Jens Axboe3529d8c2019-12-19 18:24:38 -07005287static int io_req_defer_prep(struct io_kiocb *req,
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005288 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005289{
Jens Axboee7815732019-12-17 19:45:06 -07005290 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005291
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005292 if (!sqe)
5293 return 0;
5294
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005295 if (io_alloc_async_ctx(req))
5296 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005297 ret = io_prep_work_files(req);
5298 if (unlikely(ret))
5299 return ret;
Pavel Begunkov710c2bf2020-06-27 14:04:58 +03005300
Jens Axboed625c6e2019-12-17 19:53:05 -07005301 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005302 case IORING_OP_NOP:
5303 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005304 case IORING_OP_READV:
5305 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005306 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005307 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005308 break;
5309 case IORING_OP_WRITEV:
5310 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005311 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005312 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005313 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005314 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005315 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005316 break;
5317 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005318 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005319 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005320 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005321 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005322 break;
5323 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005324 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005325 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005326 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005327 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005328 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005329 break;
5330 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005331 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005332 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005333 break;
Jens Axboef499a022019-12-02 16:28:46 -07005334 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005335 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005336 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005337 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005338 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005339 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005340 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005341 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005342 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005343 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005344 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005345 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005346 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005347 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005348 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005349 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005350 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005351 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005352 case IORING_OP_FALLOCATE:
5353 ret = io_fallocate_prep(req, sqe);
5354 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005355 case IORING_OP_OPENAT:
5356 ret = io_openat_prep(req, sqe);
5357 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005358 case IORING_OP_CLOSE:
5359 ret = io_close_prep(req, sqe);
5360 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005361 case IORING_OP_FILES_UPDATE:
5362 ret = io_files_update_prep(req, sqe);
5363 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005364 case IORING_OP_STATX:
5365 ret = io_statx_prep(req, sqe);
5366 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005367 case IORING_OP_FADVISE:
5368 ret = io_fadvise_prep(req, sqe);
5369 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005370 case IORING_OP_MADVISE:
5371 ret = io_madvise_prep(req, sqe);
5372 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005373 case IORING_OP_OPENAT2:
5374 ret = io_openat2_prep(req, sqe);
5375 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005376 case IORING_OP_EPOLL_CTL:
5377 ret = io_epoll_ctl_prep(req, sqe);
5378 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005379 case IORING_OP_SPLICE:
5380 ret = io_splice_prep(req, sqe);
5381 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005382 case IORING_OP_PROVIDE_BUFFERS:
5383 ret = io_provide_buffers_prep(req, sqe);
5384 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005385 case IORING_OP_REMOVE_BUFFERS:
5386 ret = io_remove_buffers_prep(req, sqe);
5387 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005388 case IORING_OP_TEE:
5389 ret = io_tee_prep(req, sqe);
5390 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005391 default:
Jens Axboee7815732019-12-17 19:45:06 -07005392 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5393 req->opcode);
5394 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005395 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005396 }
5397
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005398 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005399}
5400
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005401static u32 io_get_sequence(struct io_kiocb *req)
5402{
5403 struct io_kiocb *pos;
5404 struct io_ring_ctx *ctx = req->ctx;
5405 u32 total_submitted, nr_reqs = 1;
5406
5407 if (req->flags & REQ_F_LINK_HEAD)
5408 list_for_each_entry(pos, &req->link_list, link_list)
5409 nr_reqs++;
5410
5411 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5412 return total_submitted - nr_reqs;
5413}
5414
Jens Axboe3529d8c2019-12-19 18:24:38 -07005415static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005416{
Jackie Liua197f662019-11-08 08:09:12 -07005417 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005418 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005419 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005420 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005421
Bob Liu9d858b22019-11-13 18:06:25 +08005422 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005423 if (likely(list_empty_careful(&ctx->defer_list) &&
5424 !(req->flags & REQ_F_IO_DRAIN)))
5425 return 0;
5426
5427 seq = io_get_sequence(req);
5428 /* Still a chance to pass the sequence check */
5429 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005430 return 0;
5431
Pavel Begunkov650b5482020-05-17 14:02:11 +03005432 if (!req->io) {
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005433 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005434 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005435 return ret;
5436 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005437 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005438 de = kmalloc(sizeof(*de), GFP_KERNEL);
5439 if (!de)
5440 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005441
Jens Axboede0617e2019-04-06 21:51:27 -06005442 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005443 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005444 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005445 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005446 io_queue_async_work(req);
5447 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005448 }
5449
Jens Axboe915967f2019-11-21 09:01:20 -07005450 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005451 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005452 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005453 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005454 spin_unlock_irq(&ctx->completion_lock);
5455 return -EIOCBQUEUED;
5456}
5457
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005458static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005459{
5460 struct io_async_ctx *io = req->io;
5461
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005462 if (req->flags & REQ_F_BUFFER_SELECTED) {
5463 switch (req->opcode) {
5464 case IORING_OP_READV:
5465 case IORING_OP_READ_FIXED:
5466 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005467 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005468 break;
5469 case IORING_OP_RECVMSG:
5470 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005471 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005472 break;
5473 }
5474 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005475 }
5476
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005477 if (req->flags & REQ_F_NEED_CLEANUP) {
5478 switch (req->opcode) {
5479 case IORING_OP_READV:
5480 case IORING_OP_READ_FIXED:
5481 case IORING_OP_READ:
5482 case IORING_OP_WRITEV:
5483 case IORING_OP_WRITE_FIXED:
5484 case IORING_OP_WRITE:
5485 if (io->rw.iov != io->rw.fast_iov)
5486 kfree(io->rw.iov);
5487 break;
5488 case IORING_OP_RECVMSG:
5489 case IORING_OP_SENDMSG:
5490 if (io->msg.iov != io->msg.fast_iov)
5491 kfree(io->msg.iov);
5492 break;
5493 case IORING_OP_SPLICE:
5494 case IORING_OP_TEE:
5495 io_put_file(req, req->splice.file_in,
5496 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5497 break;
5498 }
5499 req->flags &= ~REQ_F_NEED_CLEANUP;
5500 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005501}
5502
Jens Axboe3529d8c2019-12-19 18:24:38 -07005503static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005504 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005505{
Jackie Liua197f662019-11-08 08:09:12 -07005506 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005507 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005508
Jens Axboed625c6e2019-12-17 19:53:05 -07005509 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005510 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005511 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005512 break;
5513 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005514 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005515 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005516 if (sqe) {
5517 ret = io_read_prep(req, sqe, force_nonblock);
5518 if (ret < 0)
5519 break;
5520 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005521 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005522 break;
5523 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005524 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005525 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005526 if (sqe) {
5527 ret = io_write_prep(req, sqe, force_nonblock);
5528 if (ret < 0)
5529 break;
5530 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005531 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005532 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005533 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005534 if (sqe) {
5535 ret = io_prep_fsync(req, sqe);
5536 if (ret < 0)
5537 break;
5538 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005539 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005540 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005541 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005542 if (sqe) {
5543 ret = io_poll_add_prep(req, sqe);
5544 if (ret)
5545 break;
5546 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005547 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005548 break;
5549 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005550 if (sqe) {
5551 ret = io_poll_remove_prep(req, sqe);
5552 if (ret < 0)
5553 break;
5554 }
Jens Axboefc4df992019-12-10 14:38:45 -07005555 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005556 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005557 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005558 if (sqe) {
5559 ret = io_prep_sfr(req, sqe);
5560 if (ret < 0)
5561 break;
5562 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005563 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005564 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005565 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005566 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005567 if (sqe) {
5568 ret = io_sendmsg_prep(req, sqe);
5569 if (ret < 0)
5570 break;
5571 }
Jens Axboefddafac2020-01-04 20:19:44 -07005572 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005573 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005574 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005575 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005576 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005577 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005578 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005579 if (sqe) {
5580 ret = io_recvmsg_prep(req, sqe);
5581 if (ret)
5582 break;
5583 }
Jens Axboefddafac2020-01-04 20:19:44 -07005584 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005585 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005586 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005587 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005588 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005589 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005590 if (sqe) {
5591 ret = io_timeout_prep(req, sqe, false);
5592 if (ret)
5593 break;
5594 }
Jens Axboefc4df992019-12-10 14:38:45 -07005595 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005596 break;
Jens Axboe11365042019-10-16 09:08:32 -06005597 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005598 if (sqe) {
5599 ret = io_timeout_remove_prep(req, sqe);
5600 if (ret)
5601 break;
5602 }
Jens Axboefc4df992019-12-10 14:38:45 -07005603 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005604 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005605 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005606 if (sqe) {
5607 ret = io_accept_prep(req, sqe);
5608 if (ret)
5609 break;
5610 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005611 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005612 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005613 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005614 if (sqe) {
5615 ret = io_connect_prep(req, sqe);
5616 if (ret)
5617 break;
5618 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005619 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005620 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005621 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005622 if (sqe) {
5623 ret = io_async_cancel_prep(req, sqe);
5624 if (ret)
5625 break;
5626 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005627 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005628 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005629 case IORING_OP_FALLOCATE:
5630 if (sqe) {
5631 ret = io_fallocate_prep(req, sqe);
5632 if (ret)
5633 break;
5634 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005635 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005636 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005637 case IORING_OP_OPENAT:
5638 if (sqe) {
5639 ret = io_openat_prep(req, sqe);
5640 if (ret)
5641 break;
5642 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005643 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005644 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005645 case IORING_OP_CLOSE:
5646 if (sqe) {
5647 ret = io_close_prep(req, sqe);
5648 if (ret)
5649 break;
5650 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005651 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005652 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005653 case IORING_OP_FILES_UPDATE:
5654 if (sqe) {
5655 ret = io_files_update_prep(req, sqe);
5656 if (ret)
5657 break;
5658 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005659 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005660 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005661 case IORING_OP_STATX:
5662 if (sqe) {
5663 ret = io_statx_prep(req, sqe);
5664 if (ret)
5665 break;
5666 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005667 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005668 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005669 case IORING_OP_FADVISE:
5670 if (sqe) {
5671 ret = io_fadvise_prep(req, sqe);
5672 if (ret)
5673 break;
5674 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005675 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005676 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005677 case IORING_OP_MADVISE:
5678 if (sqe) {
5679 ret = io_madvise_prep(req, sqe);
5680 if (ret)
5681 break;
5682 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005683 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005684 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005685 case IORING_OP_OPENAT2:
5686 if (sqe) {
5687 ret = io_openat2_prep(req, sqe);
5688 if (ret)
5689 break;
5690 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005691 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005692 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005693 case IORING_OP_EPOLL_CTL:
5694 if (sqe) {
5695 ret = io_epoll_ctl_prep(req, sqe);
5696 if (ret)
5697 break;
5698 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005699 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005700 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005701 case IORING_OP_SPLICE:
5702 if (sqe) {
5703 ret = io_splice_prep(req, sqe);
5704 if (ret < 0)
5705 break;
5706 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005707 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005708 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005709 case IORING_OP_PROVIDE_BUFFERS:
5710 if (sqe) {
5711 ret = io_provide_buffers_prep(req, sqe);
5712 if (ret)
5713 break;
5714 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005715 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005716 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005717 case IORING_OP_REMOVE_BUFFERS:
5718 if (sqe) {
5719 ret = io_remove_buffers_prep(req, sqe);
5720 if (ret)
5721 break;
5722 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005723 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005724 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005725 case IORING_OP_TEE:
5726 if (sqe) {
5727 ret = io_tee_prep(req, sqe);
5728 if (ret < 0)
5729 break;
5730 }
5731 ret = io_tee(req, force_nonblock);
5732 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005733 default:
5734 ret = -EINVAL;
5735 break;
5736 }
5737
5738 if (ret)
5739 return ret;
5740
Jens Axboeb5325762020-05-19 21:20:27 -06005741 /* If the op doesn't have a file, we're not polling for it */
5742 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005743 const bool in_async = io_wq_current_is_worker();
5744
Jens Axboe11ba8202020-01-15 21:51:17 -07005745 /* workqueue context doesn't hold uring_lock, grab it now */
5746 if (in_async)
5747 mutex_lock(&ctx->uring_lock);
5748
Jens Axboe2b188cc2019-01-07 10:46:33 -07005749 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005750
5751 if (in_async)
5752 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005753 }
5754
5755 return 0;
5756}
5757
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005758static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Jens Axboedef596e2019-01-09 08:59:42 -07005759{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005760 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005761 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005762 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005764 timeout = io_prep_linked_timeout(req);
5765 if (timeout)
5766 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005767
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005768 /* if NO_CANCEL is set, we must still run the work */
5769 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5770 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005771 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005772 }
Jens Axboe31b51512019-01-18 22:56:34 -07005773
Jens Axboe561fb042019-10-24 07:25:42 -06005774 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005775 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005776 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005777 /*
5778 * We can get EAGAIN for polled IO even though we're
5779 * forcing a sync submission from here, since we can't
5780 * wait for request slots on the block side.
5781 */
5782 if (ret != -EAGAIN)
5783 break;
5784 cond_resched();
5785 } while (1);
5786 }
Jens Axboe31b51512019-01-18 22:56:34 -07005787
Jens Axboe561fb042019-10-24 07:25:42 -06005788 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005789 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005790 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005791 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005792
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005793 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005794}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005795
Jens Axboe65e19f52019-10-26 07:20:21 -06005796static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5797 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005798{
Jens Axboe65e19f52019-10-26 07:20:21 -06005799 struct fixed_file_table *table;
5800
Jens Axboe05f3fb32019-12-09 11:22:50 -07005801 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005802 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005803}
5804
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005805static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5806 int fd, struct file **out_file, bool fixed)
5807{
5808 struct io_ring_ctx *ctx = req->ctx;
5809 struct file *file;
5810
5811 if (fixed) {
5812 if (unlikely(!ctx->file_data ||
5813 (unsigned) fd >= ctx->nr_user_files))
5814 return -EBADF;
5815 fd = array_index_nospec(fd, ctx->nr_user_files);
5816 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005817 if (file) {
5818 req->fixed_file_refs = ctx->file_data->cur_refs;
5819 percpu_ref_get(req->fixed_file_refs);
5820 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005821 } else {
5822 trace_io_uring_file_get(ctx, fd);
5823 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005824 }
5825
Jens Axboefd2206e2020-06-02 16:40:47 -06005826 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5827 *out_file = file;
5828 return 0;
5829 }
5830 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005831}
5832
Jens Axboe3529d8c2019-12-19 18:24:38 -07005833static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005834 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005835{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005836 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005837
Jens Axboe63ff8222020-05-07 14:56:15 -06005838 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005839 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005840 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005841
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005842 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005843}
5844
Jackie Liua197f662019-11-08 08:09:12 -07005845static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005846{
Jens Axboefcb323c2019-10-24 12:39:47 -06005847 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005848 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005849
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005850 io_req_init_async(req);
5851
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005852 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005853 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005854 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005855 return -EBADF;
5856
Jens Axboefcb323c2019-10-24 12:39:47 -06005857 rcu_read_lock();
5858 spin_lock_irq(&ctx->inflight_lock);
5859 /*
5860 * We use the f_ops->flush() handler to ensure that we can flush
5861 * out work accessing these files if the fd is closed. Check if
5862 * the fd has changed since we started down this path, and disallow
5863 * this operation if it has.
5864 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005865 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005866 list_add(&req->inflight_entry, &ctx->inflight_list);
5867 req->flags |= REQ_F_INFLIGHT;
5868 req->work.files = current->files;
5869 ret = 0;
5870 }
5871 spin_unlock_irq(&ctx->inflight_lock);
5872 rcu_read_unlock();
5873
5874 return ret;
5875}
5876
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005877static inline int io_prep_work_files(struct io_kiocb *req)
5878{
5879 if (!io_op_defs[req->opcode].file_table)
5880 return 0;
5881 return io_grab_files(req);
5882}
5883
Jens Axboe2665abf2019-11-05 12:40:47 -07005884static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5885{
Jens Axboead8a48a2019-11-15 08:49:11 -07005886 struct io_timeout_data *data = container_of(timer,
5887 struct io_timeout_data, timer);
5888 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005889 struct io_ring_ctx *ctx = req->ctx;
5890 struct io_kiocb *prev = NULL;
5891 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005892
5893 spin_lock_irqsave(&ctx->completion_lock, flags);
5894
5895 /*
5896 * We don't expect the list to be empty, that will only happen if we
5897 * race with the completion of the linked work.
5898 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005899 if (!list_empty(&req->link_list)) {
5900 prev = list_entry(req->link_list.prev, struct io_kiocb,
5901 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005902 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005903 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005904 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5905 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005906 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005907 }
5908
5909 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5910
5911 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005912 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005913 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005914 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005915 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005916 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005917 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005918 return HRTIMER_NORESTART;
5919}
5920
Jens Axboead8a48a2019-11-15 08:49:11 -07005921static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005922{
Jens Axboe76a46e02019-11-10 23:34:16 -07005923 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005924
Jens Axboe76a46e02019-11-10 23:34:16 -07005925 /*
5926 * If the list is now empty, then our linked request finished before
5927 * we got a chance to setup the timer
5928 */
5929 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005930 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005931 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005932
Jens Axboead8a48a2019-11-15 08:49:11 -07005933 data->timer.function = io_link_timeout_fn;
5934 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5935 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005936 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005937 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005938
Jens Axboe2665abf2019-11-05 12:40:47 -07005939 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005940 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005941}
5942
Jens Axboead8a48a2019-11-15 08:49:11 -07005943static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005944{
5945 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005946
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005947 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005948 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005949 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07005950 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005951
Pavel Begunkov44932332019-12-05 16:16:35 +03005952 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5953 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005954 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005955 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005956
Jens Axboe76a46e02019-11-10 23:34:16 -07005957 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005958 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005959}
5960
Jens Axboef13fad72020-06-22 09:34:30 -06005961static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5962 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005963{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005964 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005965 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005966 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005967 int ret;
5968
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005969again:
5970 linked_timeout = io_prep_linked_timeout(req);
5971
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005972 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5973 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005974 if (old_creds)
5975 revert_creds(old_creds);
5976 if (old_creds == req->work.creds)
5977 old_creds = NULL; /* restored original creds */
5978 else
5979 old_creds = override_creds(req->work.creds);
5980 }
5981
Jens Axboef13fad72020-06-22 09:34:30 -06005982 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06005983
5984 /*
5985 * We async punt it if the file wasn't marked NOWAIT, or if the file
5986 * doesn't support non-blocking read/write attempts
5987 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005988 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005989 if (io_arm_poll_handler(req)) {
5990 if (linked_timeout)
5991 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005992 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005993 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005994punt:
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005995 ret = io_prep_work_files(req);
5996 if (unlikely(ret))
5997 goto err;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005998 /*
5999 * Queued up for async execution, worker will release
6000 * submit reference when the iocb is actually submitted.
6001 */
6002 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006003 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006004 }
Jens Axboee65ef562019-03-12 10:16:44 -06006005
Pavel Begunkov652532a2020-07-03 22:15:07 +03006006 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006007err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006008 /* un-prep timeout, so it'll be killed as any other linked */
6009 req->flags &= ~REQ_F_LINK_TIMEOUT;
6010 req_set_fail_links(req);
6011 io_put_req(req);
6012 io_req_complete(req, ret);
6013 goto exit;
6014 }
6015
Jens Axboee65ef562019-03-12 10:16:44 -06006016 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006017 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006018 if (linked_timeout)
6019 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006020
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006021 if (nxt) {
6022 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006023
6024 if (req->flags & REQ_F_FORCE_ASYNC)
6025 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006026 goto again;
6027 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006028exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006029 if (old_creds)
6030 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031}
6032
Jens Axboef13fad72020-06-22 09:34:30 -06006033static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6034 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006035{
6036 int ret;
6037
Jens Axboe3529d8c2019-12-19 18:24:38 -07006038 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006039 if (ret) {
6040 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006041fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006042 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006043 io_put_req(req);
6044 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006045 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006046 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006047 if (!req->io) {
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006048 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006049 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006050 goto fail_req;
6051 }
6052
Jens Axboece35a472019-12-17 08:04:44 -07006053 /*
6054 * Never try inline submit of IOSQE_ASYNC is set, go straight
6055 * to async execution.
6056 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006057 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006058 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6059 io_queue_async_work(req);
6060 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006061 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006062 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006063}
6064
Jens Axboef13fad72020-06-22 09:34:30 -06006065static inline void io_queue_link_head(struct io_kiocb *req,
6066 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006067{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006068 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006069 io_put_req(req);
6070 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006071 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006072 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006073}
6074
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006075static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006076 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006077{
Jackie Liua197f662019-11-08 08:09:12 -07006078 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006079 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006080
Jens Axboe9e645e112019-05-10 16:07:28 -06006081 /*
6082 * If we already have a head request, queue this one for async
6083 * submittal once the head completes. If we don't have a head but
6084 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6085 * submitted sync once the chain is complete. If none of those
6086 * conditions are true (normal request), then just queue it.
6087 */
6088 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006089 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006090
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006091 /*
6092 * Taking sequential execution of a link, draining both sides
6093 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6094 * requests in the link. So, it drains the head and the
6095 * next after the link request. The last one is done via
6096 * drain_next flag to persist the effect across calls.
6097 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006098 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006099 head->flags |= REQ_F_IO_DRAIN;
6100 ctx->drain_next = 1;
6101 }
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006102 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006103 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006104 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006105 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006106 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006107 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006108 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006109 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006110 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006111
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006112 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006113 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006114 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006115 *link = NULL;
6116 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006117 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006118 if (unlikely(ctx->drain_next)) {
6119 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006120 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006121 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006122 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006123 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006124 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006125
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006126 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006127 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006128 req->flags |= REQ_F_FAIL_LINK;
6129 *link = req;
6130 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006131 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006132 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006133 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006134
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006135 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006136}
6137
Jens Axboe9a56a232019-01-09 09:06:50 -07006138/*
6139 * Batched submission is done, ensure local IO is flushed out.
6140 */
6141static void io_submit_state_end(struct io_submit_state *state)
6142{
Jens Axboef13fad72020-06-22 09:34:30 -06006143 if (!list_empty(&state->comp.list))
6144 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006145 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006146 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006147 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006148 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006149}
6150
6151/*
6152 * Start submission side cache.
6153 */
6154static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006155 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006156{
6157 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006158#ifdef CONFIG_BLOCK
6159 state->plug.nowait = true;
6160#endif
Jens Axboe013538b2020-06-22 09:29:15 -06006161 state->comp.nr = 0;
6162 INIT_LIST_HEAD(&state->comp.list);
6163 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006164 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006165 state->file = NULL;
6166 state->ios_left = max_ios;
6167}
6168
Jens Axboe2b188cc2019-01-07 10:46:33 -07006169static void io_commit_sqring(struct io_ring_ctx *ctx)
6170{
Hristo Venev75b28af2019-08-26 17:23:46 +00006171 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006172
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006173 /*
6174 * Ensure any loads from the SQEs are done at this point,
6175 * since once we write the new head, the application could
6176 * write new data to them.
6177 */
6178 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006179}
6180
6181/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006182 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006183 * that is mapped by userspace. This means that care needs to be taken to
6184 * ensure that reads are stable, as we cannot rely on userspace always
6185 * being a good citizen. If members of the sqe are validated and then later
6186 * used, it's important that those reads are done through READ_ONCE() to
6187 * prevent a re-load down the line.
6188 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006189static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006190{
Hristo Venev75b28af2019-08-26 17:23:46 +00006191 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006192 unsigned head;
6193
6194 /*
6195 * The cached sq head (or cq tail) serves two purposes:
6196 *
6197 * 1) allows us to batch the cost of updating the user visible
6198 * head updates.
6199 * 2) allows the kernel side to track the head on its own, even
6200 * though the application is the one updating it.
6201 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006202 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006203 if (likely(head < ctx->sq_entries))
6204 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006205
6206 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006207 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006208 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006209 return NULL;
6210}
6211
6212static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6213{
6214 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006215}
6216
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006217#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6218 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6219 IOSQE_BUFFER_SELECT)
6220
6221static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6222 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006223 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006224{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006225 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006226 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006227
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006228 req->opcode = READ_ONCE(sqe->opcode);
6229 req->user_data = READ_ONCE(sqe->user_data);
6230 req->io = NULL;
6231 req->file = NULL;
6232 req->ctx = ctx;
6233 req->flags = 0;
6234 /* one is dropped after submission, the other at completion */
6235 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006236 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006237 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006238
6239 if (unlikely(req->opcode >= IORING_OP_LAST))
6240 return -EINVAL;
6241
Jens Axboe9d8426a2020-06-16 18:42:49 -06006242 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6243 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006244
6245 sqe_flags = READ_ONCE(sqe->flags);
6246 /* enforce forwards compatibility on users */
6247 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6248 return -EINVAL;
6249
6250 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6251 !io_op_defs[req->opcode].buffer_select)
6252 return -EOPNOTSUPP;
6253
6254 id = READ_ONCE(sqe->personality);
6255 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006256 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006257 req->work.creds = idr_find(&ctx->personality_idr, id);
6258 if (unlikely(!req->work.creds))
6259 return -EINVAL;
6260 get_cred(req->work.creds);
6261 }
6262
6263 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006264 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006265
Jens Axboe63ff8222020-05-07 14:56:15 -06006266 if (!io_op_defs[req->opcode].needs_file)
6267 return 0;
6268
6269 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006270}
6271
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006272static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006273 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006274{
Jens Axboeac8691c2020-06-01 08:30:41 -06006275 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006276 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006277 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006278
Jens Axboec4a2ed72019-11-21 21:01:26 -07006279 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006280 if (test_bit(0, &ctx->sq_check_overflow)) {
6281 if (!list_empty(&ctx->cq_overflow_list) &&
6282 !io_cqring_overflow_flush(ctx, false))
6283 return -EBUSY;
6284 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006285
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006286 /* make sure SQ entry isn't read before tail */
6287 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006288
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006289 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6290 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006291
Jens Axboe013538b2020-06-22 09:29:15 -06006292 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006293
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006294 ctx->ring_fd = ring_fd;
6295 ctx->ring_file = ring_file;
6296
Jens Axboe6c271ce2019-01-10 11:22:30 -07006297 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006298 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006299 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006300 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006301
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006302 sqe = io_get_sqe(ctx);
6303 if (unlikely(!sqe)) {
6304 io_consume_sqe(ctx);
6305 break;
6306 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006307 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006308 if (unlikely(!req)) {
6309 if (!submitted)
6310 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006311 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006312 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006313
Jens Axboeac8691c2020-06-01 08:30:41 -06006314 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006315 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006316 /* will complete beyond this point, count as submitted */
6317 submitted++;
6318
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006319 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006320fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006321 io_put_req(req);
6322 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006323 break;
6324 }
6325
Jens Axboe354420f2020-01-08 18:55:15 -07006326 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006327 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006328 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006329 if (err)
6330 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006331 }
6332
Pavel Begunkov9466f432020-01-25 22:34:01 +03006333 if (unlikely(submitted != nr)) {
6334 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6335
6336 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6337 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006338 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006339 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006340 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006341
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006342 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6343 io_commit_sqring(ctx);
6344
Jens Axboe6c271ce2019-01-10 11:22:30 -07006345 return submitted;
6346}
6347
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006348static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6349{
6350 /* Tell userspace we may need a wakeup call */
6351 spin_lock_irq(&ctx->completion_lock);
6352 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6353 spin_unlock_irq(&ctx->completion_lock);
6354}
6355
6356static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6357{
6358 spin_lock_irq(&ctx->completion_lock);
6359 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6360 spin_unlock_irq(&ctx->completion_lock);
6361}
6362
Jens Axboe6c271ce2019-01-10 11:22:30 -07006363static int io_sq_thread(void *data)
6364{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006365 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006366 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006367 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006368 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006369 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006370
Jens Axboe0f158b42020-05-14 17:18:39 -06006371 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006372
Jens Axboe181e4482019-11-25 08:52:30 -07006373 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006374
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006375 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006376 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006377 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006378
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006379 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006380 unsigned nr_events = 0;
6381
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006382 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006383 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006384 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006385 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006386 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006387 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006388 }
6389
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006390 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006391
6392 /*
6393 * If submit got -EBUSY, flag us as needing the application
6394 * to enter the kernel to reap and flush events.
6395 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006396 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006397 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006398 * Drop cur_mm before scheduling, we can't hold it for
6399 * long periods (or over schedule()). Do this before
6400 * adding ourselves to the waitqueue, as the unuse/drop
6401 * may sleep.
6402 */
Jens Axboe4349f302020-07-09 15:07:01 -06006403 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006404
6405 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006406 * We're polling. If we're within the defined idle
6407 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006408 * to sleep. The exception is if we got EBUSY doing
6409 * more IO, we should wait for the application to
6410 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006411 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006412 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006413 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6414 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006415 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006416 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006417 continue;
6418 }
6419
Jens Axboe6c271ce2019-01-10 11:22:30 -07006420 prepare_to_wait(&ctx->sqo_wait, &wait,
6421 TASK_INTERRUPTIBLE);
6422
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006423 /*
6424 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006425 * to check if there are new reqs added to iopoll_list,
6426 * it is because reqs may have been punted to io worker
6427 * and will be added to iopoll_list later, hence check
6428 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006429 */
6430 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006431 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006432 finish_wait(&ctx->sqo_wait, &wait);
6433 continue;
6434 }
6435
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006436 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006437
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006438 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006439 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006440 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006441 finish_wait(&ctx->sqo_wait, &wait);
6442 break;
6443 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006444 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006445 finish_wait(&ctx->sqo_wait, &wait);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006446 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006447 continue;
6448 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006449 if (signal_pending(current))
6450 flush_signals(current);
6451 schedule();
6452 finish_wait(&ctx->sqo_wait, &wait);
6453
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006454 io_ring_clear_wakeup_flag(ctx);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006455 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006456 continue;
6457 }
6458 finish_wait(&ctx->sqo_wait, &wait);
6459
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006460 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006461 }
6462
Jens Axboe8a4955f2019-12-09 14:52:35 -07006463 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006464 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6465 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006466 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006467 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006468 }
6469
Jens Axboe4c6e2772020-07-01 11:29:10 -06006470 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006471
Jens Axboe4349f302020-07-09 15:07:01 -06006472 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006473 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006474
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006475 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006476
Jens Axboe6c271ce2019-01-10 11:22:30 -07006477 return 0;
6478}
6479
Jens Axboebda52162019-09-24 13:47:15 -06006480struct io_wait_queue {
6481 struct wait_queue_entry wq;
6482 struct io_ring_ctx *ctx;
6483 unsigned to_wait;
6484 unsigned nr_timeouts;
6485};
6486
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006487static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006488{
6489 struct io_ring_ctx *ctx = iowq->ctx;
6490
6491 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006492 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006493 * started waiting. For timeouts, we always want to return to userspace,
6494 * regardless of event count.
6495 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006496 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006497 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6498}
6499
6500static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6501 int wake_flags, void *key)
6502{
6503 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6504 wq);
6505
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006506 /* use noflush == true, as we can't safely rely on locking context */
6507 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006508 return -1;
6509
6510 return autoremove_wake_function(curr, mode, wake_flags, key);
6511}
6512
Jens Axboe2b188cc2019-01-07 10:46:33 -07006513/*
6514 * Wait until events become available, if we don't already have some. The
6515 * application must reap them itself, as they reside on the shared cq ring.
6516 */
6517static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6518 const sigset_t __user *sig, size_t sigsz)
6519{
Jens Axboebda52162019-09-24 13:47:15 -06006520 struct io_wait_queue iowq = {
6521 .wq = {
6522 .private = current,
6523 .func = io_wake_function,
6524 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6525 },
6526 .ctx = ctx,
6527 .to_wait = min_events,
6528 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006529 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006530 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006531
Jens Axboeb41e9852020-02-17 09:52:41 -07006532 do {
6533 if (io_cqring_events(ctx, false) >= min_events)
6534 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006535 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006536 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006537 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006538
6539 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006540#ifdef CONFIG_COMPAT
6541 if (in_compat_syscall())
6542 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006543 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006544 else
6545#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006546 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006547
Jens Axboe2b188cc2019-01-07 10:46:33 -07006548 if (ret)
6549 return ret;
6550 }
6551
Jens Axboebda52162019-09-24 13:47:15 -06006552 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006553 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006554 do {
6555 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6556 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006557 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006558 if (io_run_task_work())
6559 continue;
Jens Axboebda52162019-09-24 13:47:15 -06006560 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006561 if (current->jobctl & JOBCTL_TASK_WORK) {
6562 spin_lock_irq(&current->sighand->siglock);
6563 current->jobctl &= ~JOBCTL_TASK_WORK;
6564 recalc_sigpending();
6565 spin_unlock_irq(&current->sighand->siglock);
6566 continue;
6567 }
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006568 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006569 break;
6570 }
Jens Axboebda52162019-09-24 13:47:15 -06006571 if (io_should_wake(&iowq, false))
6572 break;
6573 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006574 } while (1);
6575 finish_wait(&ctx->wait, &iowq.wq);
6576
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006577 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006578
Hristo Venev75b28af2019-08-26 17:23:46 +00006579 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006580}
6581
Jens Axboe6b063142019-01-10 22:13:58 -07006582static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6583{
6584#if defined(CONFIG_UNIX)
6585 if (ctx->ring_sock) {
6586 struct sock *sock = ctx->ring_sock->sk;
6587 struct sk_buff *skb;
6588
6589 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6590 kfree_skb(skb);
6591 }
6592#else
6593 int i;
6594
Jens Axboe65e19f52019-10-26 07:20:21 -06006595 for (i = 0; i < ctx->nr_user_files; i++) {
6596 struct file *file;
6597
6598 file = io_file_from_index(ctx, i);
6599 if (file)
6600 fput(file);
6601 }
Jens Axboe6b063142019-01-10 22:13:58 -07006602#endif
6603}
6604
Jens Axboe05f3fb32019-12-09 11:22:50 -07006605static void io_file_ref_kill(struct percpu_ref *ref)
6606{
6607 struct fixed_file_data *data;
6608
6609 data = container_of(ref, struct fixed_file_data, refs);
6610 complete(&data->done);
6611}
6612
Jens Axboe6b063142019-01-10 22:13:58 -07006613static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6614{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006615 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006616 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006617 unsigned nr_tables, i;
6618
Jens Axboe05f3fb32019-12-09 11:22:50 -07006619 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006620 return -ENXIO;
6621
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006622 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006623 if (!list_empty(&data->ref_list))
6624 ref_node = list_first_entry(&data->ref_list,
6625 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006626 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006627 if (ref_node)
6628 percpu_ref_kill(&ref_node->refs);
6629
6630 percpu_ref_kill(&data->refs);
6631
6632 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006633 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006634 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006635
Jens Axboe6b063142019-01-10 22:13:58 -07006636 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006637 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6638 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006639 kfree(data->table[i].files);
6640 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006641 percpu_ref_exit(&data->refs);
6642 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006643 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006644 ctx->nr_user_files = 0;
6645 return 0;
6646}
6647
Jens Axboe6c271ce2019-01-10 11:22:30 -07006648static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6649{
6650 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006651 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006652 /*
6653 * The park is a bit of a work-around, without it we get
6654 * warning spews on shutdown with SQPOLL set and affinity
6655 * set to a single CPU.
6656 */
Jens Axboe06058632019-04-13 09:26:03 -06006657 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006658 kthread_stop(ctx->sqo_thread);
6659 ctx->sqo_thread = NULL;
6660 }
6661}
6662
Jens Axboe6b063142019-01-10 22:13:58 -07006663static void io_finish_async(struct io_ring_ctx *ctx)
6664{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006665 io_sq_thread_stop(ctx);
6666
Jens Axboe561fb042019-10-24 07:25:42 -06006667 if (ctx->io_wq) {
6668 io_wq_destroy(ctx->io_wq);
6669 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006670 }
6671}
6672
6673#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006674/*
6675 * Ensure the UNIX gc is aware of our file set, so we are certain that
6676 * the io_uring can be safely unregistered on process exit, even if we have
6677 * loops in the file referencing.
6678 */
6679static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6680{
6681 struct sock *sk = ctx->ring_sock->sk;
6682 struct scm_fp_list *fpl;
6683 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006684 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006685
Jens Axboe6b063142019-01-10 22:13:58 -07006686 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6687 if (!fpl)
6688 return -ENOMEM;
6689
6690 skb = alloc_skb(0, GFP_KERNEL);
6691 if (!skb) {
6692 kfree(fpl);
6693 return -ENOMEM;
6694 }
6695
6696 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006697
Jens Axboe08a45172019-10-03 08:11:03 -06006698 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006699 fpl->user = get_uid(ctx->user);
6700 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006701 struct file *file = io_file_from_index(ctx, i + offset);
6702
6703 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006704 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006705 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006706 unix_inflight(fpl->user, fpl->fp[nr_files]);
6707 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006708 }
6709
Jens Axboe08a45172019-10-03 08:11:03 -06006710 if (nr_files) {
6711 fpl->max = SCM_MAX_FD;
6712 fpl->count = nr_files;
6713 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006714 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006715 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6716 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006717
Jens Axboe08a45172019-10-03 08:11:03 -06006718 for (i = 0; i < nr_files; i++)
6719 fput(fpl->fp[i]);
6720 } else {
6721 kfree_skb(skb);
6722 kfree(fpl);
6723 }
Jens Axboe6b063142019-01-10 22:13:58 -07006724
6725 return 0;
6726}
6727
6728/*
6729 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6730 * causes regular reference counting to break down. We rely on the UNIX
6731 * garbage collection to take care of this problem for us.
6732 */
6733static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6734{
6735 unsigned left, total;
6736 int ret = 0;
6737
6738 total = 0;
6739 left = ctx->nr_user_files;
6740 while (left) {
6741 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006742
6743 ret = __io_sqe_files_scm(ctx, this_files, total);
6744 if (ret)
6745 break;
6746 left -= this_files;
6747 total += this_files;
6748 }
6749
6750 if (!ret)
6751 return 0;
6752
6753 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006754 struct file *file = io_file_from_index(ctx, total);
6755
6756 if (file)
6757 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006758 total++;
6759 }
6760
6761 return ret;
6762}
6763#else
6764static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6765{
6766 return 0;
6767}
6768#endif
6769
Jens Axboe65e19f52019-10-26 07:20:21 -06006770static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6771 unsigned nr_files)
6772{
6773 int i;
6774
6775 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006776 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006777 unsigned this_files;
6778
6779 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6780 table->files = kcalloc(this_files, sizeof(struct file *),
6781 GFP_KERNEL);
6782 if (!table->files)
6783 break;
6784 nr_files -= this_files;
6785 }
6786
6787 if (i == nr_tables)
6788 return 0;
6789
6790 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006791 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006792 kfree(table->files);
6793 }
6794 return 1;
6795}
6796
Jens Axboe05f3fb32019-12-09 11:22:50 -07006797static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006798{
6799#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006800 struct sock *sock = ctx->ring_sock->sk;
6801 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6802 struct sk_buff *skb;
6803 int i;
6804
6805 __skb_queue_head_init(&list);
6806
6807 /*
6808 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6809 * remove this entry and rearrange the file array.
6810 */
6811 skb = skb_dequeue(head);
6812 while (skb) {
6813 struct scm_fp_list *fp;
6814
6815 fp = UNIXCB(skb).fp;
6816 for (i = 0; i < fp->count; i++) {
6817 int left;
6818
6819 if (fp->fp[i] != file)
6820 continue;
6821
6822 unix_notinflight(fp->user, fp->fp[i]);
6823 left = fp->count - 1 - i;
6824 if (left) {
6825 memmove(&fp->fp[i], &fp->fp[i + 1],
6826 left * sizeof(struct file *));
6827 }
6828 fp->count--;
6829 if (!fp->count) {
6830 kfree_skb(skb);
6831 skb = NULL;
6832 } else {
6833 __skb_queue_tail(&list, skb);
6834 }
6835 fput(file);
6836 file = NULL;
6837 break;
6838 }
6839
6840 if (!file)
6841 break;
6842
6843 __skb_queue_tail(&list, skb);
6844
6845 skb = skb_dequeue(head);
6846 }
6847
6848 if (skb_peek(&list)) {
6849 spin_lock_irq(&head->lock);
6850 while ((skb = __skb_dequeue(&list)) != NULL)
6851 __skb_queue_tail(head, skb);
6852 spin_unlock_irq(&head->lock);
6853 }
6854#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006855 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006856#endif
6857}
6858
Jens Axboe05f3fb32019-12-09 11:22:50 -07006859struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006860 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006861 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006862};
6863
Jens Axboe4a38aed22020-05-14 17:21:15 -06006864static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006865{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006866 struct fixed_file_data *file_data = ref_node->file_data;
6867 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006868 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006869
6870 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006871 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006872 io_ring_file_put(ctx, pfile->file);
6873 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006874 }
6875
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006876 spin_lock(&file_data->lock);
6877 list_del(&ref_node->node);
6878 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006879
Xiaoguang Wang05589552020-03-31 14:05:18 +08006880 percpu_ref_exit(&ref_node->refs);
6881 kfree(ref_node);
6882 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006883}
6884
Jens Axboe4a38aed22020-05-14 17:21:15 -06006885static void io_file_put_work(struct work_struct *work)
6886{
6887 struct io_ring_ctx *ctx;
6888 struct llist_node *node;
6889
6890 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6891 node = llist_del_all(&ctx->file_put_llist);
6892
6893 while (node) {
6894 struct fixed_file_ref_node *ref_node;
6895 struct llist_node *next = node->next;
6896
6897 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6898 __io_file_put_work(ref_node);
6899 node = next;
6900 }
6901}
6902
Jens Axboe05f3fb32019-12-09 11:22:50 -07006903static void io_file_data_ref_zero(struct percpu_ref *ref)
6904{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006905 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006906 struct io_ring_ctx *ctx;
6907 bool first_add;
6908 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006909
Xiaoguang Wang05589552020-03-31 14:05:18 +08006910 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006911 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006912
Jens Axboe4a38aed22020-05-14 17:21:15 -06006913 if (percpu_ref_is_dying(&ctx->file_data->refs))
6914 delay = 0;
6915
6916 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6917 if (!delay)
6918 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6919 else if (first_add)
6920 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006921}
6922
6923static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6924 struct io_ring_ctx *ctx)
6925{
6926 struct fixed_file_ref_node *ref_node;
6927
6928 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6929 if (!ref_node)
6930 return ERR_PTR(-ENOMEM);
6931
6932 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6933 0, GFP_KERNEL)) {
6934 kfree(ref_node);
6935 return ERR_PTR(-ENOMEM);
6936 }
6937 INIT_LIST_HEAD(&ref_node->node);
6938 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006939 ref_node->file_data = ctx->file_data;
6940 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006941}
6942
6943static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6944{
6945 percpu_ref_exit(&ref_node->refs);
6946 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006947}
6948
6949static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6950 unsigned nr_args)
6951{
6952 __s32 __user *fds = (__s32 __user *) arg;
6953 unsigned nr_tables;
6954 struct file *file;
6955 int fd, ret = 0;
6956 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006957 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006958
6959 if (ctx->file_data)
6960 return -EBUSY;
6961 if (!nr_args)
6962 return -EINVAL;
6963 if (nr_args > IORING_MAX_FIXED_FILES)
6964 return -EMFILE;
6965
6966 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6967 if (!ctx->file_data)
6968 return -ENOMEM;
6969 ctx->file_data->ctx = ctx;
6970 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006971 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006972 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006973
6974 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6975 ctx->file_data->table = kcalloc(nr_tables,
6976 sizeof(struct fixed_file_table),
6977 GFP_KERNEL);
6978 if (!ctx->file_data->table) {
6979 kfree(ctx->file_data);
6980 ctx->file_data = NULL;
6981 return -ENOMEM;
6982 }
6983
Xiaoguang Wang05589552020-03-31 14:05:18 +08006984 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006985 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6986 kfree(ctx->file_data->table);
6987 kfree(ctx->file_data);
6988 ctx->file_data = NULL;
6989 return -ENOMEM;
6990 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006991
6992 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6993 percpu_ref_exit(&ctx->file_data->refs);
6994 kfree(ctx->file_data->table);
6995 kfree(ctx->file_data);
6996 ctx->file_data = NULL;
6997 return -ENOMEM;
6998 }
6999
7000 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7001 struct fixed_file_table *table;
7002 unsigned index;
7003
7004 ret = -EFAULT;
7005 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7006 break;
7007 /* allow sparse sets */
7008 if (fd == -1) {
7009 ret = 0;
7010 continue;
7011 }
7012
7013 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7014 index = i & IORING_FILE_TABLE_MASK;
7015 file = fget(fd);
7016
7017 ret = -EBADF;
7018 if (!file)
7019 break;
7020
7021 /*
7022 * Don't allow io_uring instances to be registered. If UNIX
7023 * isn't enabled, then this causes a reference cycle and this
7024 * instance can never get freed. If UNIX is enabled we'll
7025 * handle it just fine, but there's still no point in allowing
7026 * a ring fd as it doesn't support regular read/write anyway.
7027 */
7028 if (file->f_op == &io_uring_fops) {
7029 fput(file);
7030 break;
7031 }
7032 ret = 0;
7033 table->files[index] = file;
7034 }
7035
7036 if (ret) {
7037 for (i = 0; i < ctx->nr_user_files; i++) {
7038 file = io_file_from_index(ctx, i);
7039 if (file)
7040 fput(file);
7041 }
7042 for (i = 0; i < nr_tables; i++)
7043 kfree(ctx->file_data->table[i].files);
7044
Yang Yingliang667e57d2020-07-10 14:14:20 +00007045 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007046 kfree(ctx->file_data->table);
7047 kfree(ctx->file_data);
7048 ctx->file_data = NULL;
7049 ctx->nr_user_files = 0;
7050 return ret;
7051 }
7052
7053 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007054 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007055 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007056 return ret;
7057 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007058
Xiaoguang Wang05589552020-03-31 14:05:18 +08007059 ref_node = alloc_fixed_file_ref_node(ctx);
7060 if (IS_ERR(ref_node)) {
7061 io_sqe_files_unregister(ctx);
7062 return PTR_ERR(ref_node);
7063 }
7064
7065 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007066 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007067 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007068 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007069 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007070 return ret;
7071}
7072
Jens Axboec3a31e62019-10-03 13:59:56 -06007073static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7074 int index)
7075{
7076#if defined(CONFIG_UNIX)
7077 struct sock *sock = ctx->ring_sock->sk;
7078 struct sk_buff_head *head = &sock->sk_receive_queue;
7079 struct sk_buff *skb;
7080
7081 /*
7082 * See if we can merge this file into an existing skb SCM_RIGHTS
7083 * file set. If there's no room, fall back to allocating a new skb
7084 * and filling it in.
7085 */
7086 spin_lock_irq(&head->lock);
7087 skb = skb_peek(head);
7088 if (skb) {
7089 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7090
7091 if (fpl->count < SCM_MAX_FD) {
7092 __skb_unlink(skb, head);
7093 spin_unlock_irq(&head->lock);
7094 fpl->fp[fpl->count] = get_file(file);
7095 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7096 fpl->count++;
7097 spin_lock_irq(&head->lock);
7098 __skb_queue_head(head, skb);
7099 } else {
7100 skb = NULL;
7101 }
7102 }
7103 spin_unlock_irq(&head->lock);
7104
7105 if (skb) {
7106 fput(file);
7107 return 0;
7108 }
7109
7110 return __io_sqe_files_scm(ctx, 1, index);
7111#else
7112 return 0;
7113#endif
7114}
7115
Hillf Dantona5318d32020-03-23 17:47:15 +08007116static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007117 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007118{
Hillf Dantona5318d32020-03-23 17:47:15 +08007119 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007120 struct percpu_ref *refs = data->cur_refs;
7121 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007122
Jens Axboe05f3fb32019-12-09 11:22:50 -07007123 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007124 if (!pfile)
7125 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007126
Xiaoguang Wang05589552020-03-31 14:05:18 +08007127 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007128 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007129 list_add(&pfile->list, &ref_node->file_list);
7130
Hillf Dantona5318d32020-03-23 17:47:15 +08007131 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007132}
7133
7134static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7135 struct io_uring_files_update *up,
7136 unsigned nr_args)
7137{
7138 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007139 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007140 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007141 __s32 __user *fds;
7142 int fd, i, err;
7143 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007144 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007145
Jens Axboe05f3fb32019-12-09 11:22:50 -07007146 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007147 return -EOVERFLOW;
7148 if (done > ctx->nr_user_files)
7149 return -EINVAL;
7150
Xiaoguang Wang05589552020-03-31 14:05:18 +08007151 ref_node = alloc_fixed_file_ref_node(ctx);
7152 if (IS_ERR(ref_node))
7153 return PTR_ERR(ref_node);
7154
Jens Axboec3a31e62019-10-03 13:59:56 -06007155 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007156 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007157 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007158 struct fixed_file_table *table;
7159 unsigned index;
7160
Jens Axboec3a31e62019-10-03 13:59:56 -06007161 err = 0;
7162 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7163 err = -EFAULT;
7164 break;
7165 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007166 i = array_index_nospec(up->offset, ctx->nr_user_files);
7167 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007168 index = i & IORING_FILE_TABLE_MASK;
7169 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007170 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007171 err = io_queue_file_removal(data, file);
7172 if (err)
7173 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007174 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007175 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007176 }
7177 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007178 file = fget(fd);
7179 if (!file) {
7180 err = -EBADF;
7181 break;
7182 }
7183 /*
7184 * Don't allow io_uring instances to be registered. If
7185 * UNIX isn't enabled, then this causes a reference
7186 * cycle and this instance can never get freed. If UNIX
7187 * is enabled we'll handle it just fine, but there's
7188 * still no point in allowing a ring fd as it doesn't
7189 * support regular read/write anyway.
7190 */
7191 if (file->f_op == &io_uring_fops) {
7192 fput(file);
7193 err = -EBADF;
7194 break;
7195 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007196 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007197 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007198 if (err) {
7199 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007200 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007201 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007202 }
7203 nr_args--;
7204 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007205 up->offset++;
7206 }
7207
Xiaoguang Wang05589552020-03-31 14:05:18 +08007208 if (needs_switch) {
7209 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007210 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007211 list_add(&ref_node->node, &data->ref_list);
7212 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007213 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007214 percpu_ref_get(&ctx->file_data->refs);
7215 } else
7216 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007217
7218 return done ? done : err;
7219}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007220
Jens Axboe05f3fb32019-12-09 11:22:50 -07007221static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7222 unsigned nr_args)
7223{
7224 struct io_uring_files_update up;
7225
7226 if (!ctx->file_data)
7227 return -ENXIO;
7228 if (!nr_args)
7229 return -EINVAL;
7230 if (copy_from_user(&up, arg, sizeof(up)))
7231 return -EFAULT;
7232 if (up.resv)
7233 return -EINVAL;
7234
7235 return __io_sqe_files_update(ctx, &up, nr_args);
7236}
Jens Axboec3a31e62019-10-03 13:59:56 -06007237
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007238static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007239{
7240 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7241
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007242 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007243 io_put_req(req);
7244}
7245
Pavel Begunkov24369c22020-01-28 03:15:48 +03007246static int io_init_wq_offload(struct io_ring_ctx *ctx,
7247 struct io_uring_params *p)
7248{
7249 struct io_wq_data data;
7250 struct fd f;
7251 struct io_ring_ctx *ctx_attach;
7252 unsigned int concurrency;
7253 int ret = 0;
7254
7255 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007256 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007257 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007258
7259 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7260 /* Do QD, or 4 * CPUS, whatever is smallest */
7261 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7262
7263 ctx->io_wq = io_wq_create(concurrency, &data);
7264 if (IS_ERR(ctx->io_wq)) {
7265 ret = PTR_ERR(ctx->io_wq);
7266 ctx->io_wq = NULL;
7267 }
7268 return ret;
7269 }
7270
7271 f = fdget(p->wq_fd);
7272 if (!f.file)
7273 return -EBADF;
7274
7275 if (f.file->f_op != &io_uring_fops) {
7276 ret = -EINVAL;
7277 goto out_fput;
7278 }
7279
7280 ctx_attach = f.file->private_data;
7281 /* @io_wq is protected by holding the fd */
7282 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7283 ret = -EINVAL;
7284 goto out_fput;
7285 }
7286
7287 ctx->io_wq = ctx_attach->io_wq;
7288out_fput:
7289 fdput(f);
7290 return ret;
7291}
7292
Jens Axboe6c271ce2019-01-10 11:22:30 -07007293static int io_sq_offload_start(struct io_ring_ctx *ctx,
7294 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007295{
7296 int ret;
7297
Pavel Begunkovcbcf7212020-07-18 11:31:21 +03007298 mmgrab(current->mm);
7299 ctx->sqo_mm = current->mm;
Pavel Begunkov8eb06d72020-06-30 15:20:39 +03007300
Pavel Begunkovcbcf7212020-07-18 11:31:21 +03007301 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007302 ret = -EPERM;
7303 if (!capable(CAP_SYS_ADMIN))
7304 goto err;
7305
Jens Axboe917257d2019-04-13 09:28:55 -06007306 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7307 if (!ctx->sq_thread_idle)
7308 ctx->sq_thread_idle = HZ;
7309
Jens Axboe6c271ce2019-01-10 11:22:30 -07007310 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007311 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007312
Jens Axboe917257d2019-04-13 09:28:55 -06007313 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007314 if (cpu >= nr_cpu_ids)
7315 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007316 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007317 goto err;
7318
Jens Axboe6c271ce2019-01-10 11:22:30 -07007319 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7320 ctx, cpu,
7321 "io_uring-sq");
7322 } else {
7323 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7324 "io_uring-sq");
7325 }
7326 if (IS_ERR(ctx->sqo_thread)) {
7327 ret = PTR_ERR(ctx->sqo_thread);
7328 ctx->sqo_thread = NULL;
7329 goto err;
7330 }
7331 wake_up_process(ctx->sqo_thread);
7332 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7333 /* Can't have SQ_AFF without SQPOLL */
7334 ret = -EINVAL;
7335 goto err;
7336 }
7337
Pavel Begunkov24369c22020-01-28 03:15:48 +03007338 ret = io_init_wq_offload(ctx, p);
7339 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007340 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007341
7342 return 0;
7343err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007344 io_finish_async(ctx);
Pavel Begunkov8eb06d72020-06-30 15:20:39 +03007345 if (ctx->sqo_mm) {
7346 mmdrop(ctx->sqo_mm);
7347 ctx->sqo_mm = NULL;
7348 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007349 return ret;
7350}
7351
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007352static inline void __io_unaccount_mem(struct user_struct *user,
7353 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007354{
7355 atomic_long_sub(nr_pages, &user->locked_vm);
7356}
7357
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007358static inline int __io_account_mem(struct user_struct *user,
7359 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007360{
7361 unsigned long page_limit, cur_pages, new_pages;
7362
7363 /* Don't allow more pages than we can safely lock */
7364 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7365
7366 do {
7367 cur_pages = atomic_long_read(&user->locked_vm);
7368 new_pages = cur_pages + nr_pages;
7369 if (new_pages > page_limit)
7370 return -ENOMEM;
7371 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7372 new_pages) != cur_pages);
7373
7374 return 0;
7375}
7376
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007377static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7378 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007379{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007380 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007381 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007382
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007383 if (ctx->sqo_mm) {
7384 if (acct == ACCT_LOCKED)
7385 ctx->sqo_mm->locked_vm -= nr_pages;
7386 else if (acct == ACCT_PINNED)
7387 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7388 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007389}
7390
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007391static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7392 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007393{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007394 int ret;
7395
7396 if (ctx->limit_mem) {
7397 ret = __io_account_mem(ctx->user, nr_pages);
7398 if (ret)
7399 return ret;
7400 }
7401
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007402 if (ctx->sqo_mm) {
7403 if (acct == ACCT_LOCKED)
7404 ctx->sqo_mm->locked_vm += nr_pages;
7405 else if (acct == ACCT_PINNED)
7406 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7407 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007408
7409 return 0;
7410}
7411
Jens Axboe2b188cc2019-01-07 10:46:33 -07007412static void io_mem_free(void *ptr)
7413{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007414 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007415
Mark Rutland52e04ef2019-04-30 17:30:21 +01007416 if (!ptr)
7417 return;
7418
7419 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007420 if (put_page_testzero(page))
7421 free_compound_page(page);
7422}
7423
7424static void *io_mem_alloc(size_t size)
7425{
7426 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7427 __GFP_NORETRY;
7428
7429 return (void *) __get_free_pages(gfp_flags, get_order(size));
7430}
7431
Hristo Venev75b28af2019-08-26 17:23:46 +00007432static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7433 size_t *sq_offset)
7434{
7435 struct io_rings *rings;
7436 size_t off, sq_array_size;
7437
7438 off = struct_size(rings, cqes, cq_entries);
7439 if (off == SIZE_MAX)
7440 return SIZE_MAX;
7441
7442#ifdef CONFIG_SMP
7443 off = ALIGN(off, SMP_CACHE_BYTES);
7444 if (off == 0)
7445 return SIZE_MAX;
7446#endif
7447
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007448 if (sq_offset)
7449 *sq_offset = off;
7450
Hristo Venev75b28af2019-08-26 17:23:46 +00007451 sq_array_size = array_size(sizeof(u32), sq_entries);
7452 if (sq_array_size == SIZE_MAX)
7453 return SIZE_MAX;
7454
7455 if (check_add_overflow(off, sq_array_size, &off))
7456 return SIZE_MAX;
7457
Hristo Venev75b28af2019-08-26 17:23:46 +00007458 return off;
7459}
7460
Jens Axboe2b188cc2019-01-07 10:46:33 -07007461static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7462{
Hristo Venev75b28af2019-08-26 17:23:46 +00007463 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007464
Hristo Venev75b28af2019-08-26 17:23:46 +00007465 pages = (size_t)1 << get_order(
7466 rings_size(sq_entries, cq_entries, NULL));
7467 pages += (size_t)1 << get_order(
7468 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007469
Hristo Venev75b28af2019-08-26 17:23:46 +00007470 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007471}
7472
Jens Axboeedafcce2019-01-09 09:16:05 -07007473static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7474{
7475 int i, j;
7476
7477 if (!ctx->user_bufs)
7478 return -ENXIO;
7479
7480 for (i = 0; i < ctx->nr_user_bufs; i++) {
7481 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7482
7483 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007484 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007485
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007486 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007487 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007488 imu->nr_bvecs = 0;
7489 }
7490
7491 kfree(ctx->user_bufs);
7492 ctx->user_bufs = NULL;
7493 ctx->nr_user_bufs = 0;
7494 return 0;
7495}
7496
7497static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7498 void __user *arg, unsigned index)
7499{
7500 struct iovec __user *src;
7501
7502#ifdef CONFIG_COMPAT
7503 if (ctx->compat) {
7504 struct compat_iovec __user *ciovs;
7505 struct compat_iovec ciov;
7506
7507 ciovs = (struct compat_iovec __user *) arg;
7508 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7509 return -EFAULT;
7510
Jens Axboed55e5f52019-12-11 16:12:15 -07007511 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007512 dst->iov_len = ciov.iov_len;
7513 return 0;
7514 }
7515#endif
7516 src = (struct iovec __user *) arg;
7517 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7518 return -EFAULT;
7519 return 0;
7520}
7521
7522static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7523 unsigned nr_args)
7524{
7525 struct vm_area_struct **vmas = NULL;
7526 struct page **pages = NULL;
7527 int i, j, got_pages = 0;
7528 int ret = -EINVAL;
7529
7530 if (ctx->user_bufs)
7531 return -EBUSY;
7532 if (!nr_args || nr_args > UIO_MAXIOV)
7533 return -EINVAL;
7534
7535 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7536 GFP_KERNEL);
7537 if (!ctx->user_bufs)
7538 return -ENOMEM;
7539
7540 for (i = 0; i < nr_args; i++) {
7541 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7542 unsigned long off, start, end, ubuf;
7543 int pret, nr_pages;
7544 struct iovec iov;
7545 size_t size;
7546
7547 ret = io_copy_iov(ctx, &iov, arg, i);
7548 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007549 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007550
7551 /*
7552 * Don't impose further limits on the size and buffer
7553 * constraints here, we'll -EINVAL later when IO is
7554 * submitted if they are wrong.
7555 */
7556 ret = -EFAULT;
7557 if (!iov.iov_base || !iov.iov_len)
7558 goto err;
7559
7560 /* arbitrary limit, but we need something */
7561 if (iov.iov_len > SZ_1G)
7562 goto err;
7563
7564 ubuf = (unsigned long) iov.iov_base;
7565 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7566 start = ubuf >> PAGE_SHIFT;
7567 nr_pages = end - start;
7568
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007569 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007570 if (ret)
7571 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007572
7573 ret = 0;
7574 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007575 kvfree(vmas);
7576 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007577 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007578 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007579 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007580 sizeof(struct vm_area_struct *),
7581 GFP_KERNEL);
7582 if (!pages || !vmas) {
7583 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007584 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007585 goto err;
7586 }
7587 got_pages = nr_pages;
7588 }
7589
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007590 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007591 GFP_KERNEL);
7592 ret = -ENOMEM;
7593 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007594 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007595 goto err;
7596 }
7597
7598 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007599 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007600 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007601 FOLL_WRITE | FOLL_LONGTERM,
7602 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007603 if (pret == nr_pages) {
7604 /* don't support file backed memory */
7605 for (j = 0; j < nr_pages; j++) {
7606 struct vm_area_struct *vma = vmas[j];
7607
7608 if (vma->vm_file &&
7609 !is_file_hugepages(vma->vm_file)) {
7610 ret = -EOPNOTSUPP;
7611 break;
7612 }
7613 }
7614 } else {
7615 ret = pret < 0 ? pret : -EFAULT;
7616 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007617 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007618 if (ret) {
7619 /*
7620 * if we did partial map, or found file backed vmas,
7621 * release any pages we did get
7622 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007623 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007624 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007625 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007626 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007627 goto err;
7628 }
7629
7630 off = ubuf & ~PAGE_MASK;
7631 size = iov.iov_len;
7632 for (j = 0; j < nr_pages; j++) {
7633 size_t vec_len;
7634
7635 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7636 imu->bvec[j].bv_page = pages[j];
7637 imu->bvec[j].bv_len = vec_len;
7638 imu->bvec[j].bv_offset = off;
7639 off = 0;
7640 size -= vec_len;
7641 }
7642 /* store original address for later verification */
7643 imu->ubuf = ubuf;
7644 imu->len = iov.iov_len;
7645 imu->nr_bvecs = nr_pages;
7646
7647 ctx->nr_user_bufs++;
7648 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007649 kvfree(pages);
7650 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007651 return 0;
7652err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007653 kvfree(pages);
7654 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007655 io_sqe_buffer_unregister(ctx);
7656 return ret;
7657}
7658
Jens Axboe9b402842019-04-11 11:45:41 -06007659static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7660{
7661 __s32 __user *fds = arg;
7662 int fd;
7663
7664 if (ctx->cq_ev_fd)
7665 return -EBUSY;
7666
7667 if (copy_from_user(&fd, fds, sizeof(*fds)))
7668 return -EFAULT;
7669
7670 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7671 if (IS_ERR(ctx->cq_ev_fd)) {
7672 int ret = PTR_ERR(ctx->cq_ev_fd);
7673 ctx->cq_ev_fd = NULL;
7674 return ret;
7675 }
7676
7677 return 0;
7678}
7679
7680static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7681{
7682 if (ctx->cq_ev_fd) {
7683 eventfd_ctx_put(ctx->cq_ev_fd);
7684 ctx->cq_ev_fd = NULL;
7685 return 0;
7686 }
7687
7688 return -ENXIO;
7689}
7690
Jens Axboe5a2e7452020-02-23 16:23:11 -07007691static int __io_destroy_buffers(int id, void *p, void *data)
7692{
7693 struct io_ring_ctx *ctx = data;
7694 struct io_buffer *buf = p;
7695
Jens Axboe067524e2020-03-02 16:32:28 -07007696 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007697 return 0;
7698}
7699
7700static void io_destroy_buffers(struct io_ring_ctx *ctx)
7701{
7702 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7703 idr_destroy(&ctx->io_buffer_idr);
7704}
7705
Jens Axboe2b188cc2019-01-07 10:46:33 -07007706static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7707{
Jens Axboe6b063142019-01-10 22:13:58 -07007708 io_finish_async(ctx);
Pavel Begunkov5dbcad52020-07-18 11:31:20 +03007709 io_sqe_buffer_unregister(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007710 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007711 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007712 ctx->sqo_mm = NULL;
7713 }
Jens Axboedef596e2019-01-09 08:59:42 -07007714
Jens Axboe6b063142019-01-10 22:13:58 -07007715 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007716 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007717 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007718 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007719
Jens Axboe2b188cc2019-01-07 10:46:33 -07007720#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007721 if (ctx->ring_sock) {
7722 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007723 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007724 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007725#endif
7726
Hristo Venev75b28af2019-08-26 17:23:46 +00007727 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007728 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007729
7730 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007731 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007732 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007733 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007734 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007735 kfree(ctx);
7736}
7737
7738static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7739{
7740 struct io_ring_ctx *ctx = file->private_data;
7741 __poll_t mask = 0;
7742
7743 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007744 /*
7745 * synchronizes with barrier from wq_has_sleeper call in
7746 * io_commit_cqring
7747 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007748 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007749 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7750 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007751 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007752 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007753 mask |= EPOLLIN | EPOLLRDNORM;
7754
7755 return mask;
7756}
7757
7758static int io_uring_fasync(int fd, struct file *file, int on)
7759{
7760 struct io_ring_ctx *ctx = file->private_data;
7761
7762 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7763}
7764
Jens Axboe071698e2020-01-28 10:04:42 -07007765static int io_remove_personalities(int id, void *p, void *data)
7766{
7767 struct io_ring_ctx *ctx = data;
7768 const struct cred *cred;
7769
7770 cred = idr_remove(&ctx->personality_idr, id);
7771 if (cred)
7772 put_cred(cred);
7773 return 0;
7774}
7775
Jens Axboe85faa7b2020-04-09 18:14:00 -06007776static void io_ring_exit_work(struct work_struct *work)
7777{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007778 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
7779 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007780
Jens Axboe56952e92020-06-17 15:00:04 -06007781 /*
7782 * If we're doing polled IO and end up having requests being
7783 * submitted async (out-of-line), then completions can come in while
7784 * we're waiting for refs to drop. We need to reap these manually,
7785 * as nobody else will be looking for them.
7786 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007787 do {
Jens Axboe56952e92020-06-17 15:00:04 -06007788 if (ctx->rings)
7789 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007790 io_iopoll_try_reap_events(ctx);
7791 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06007792 io_ring_ctx_free(ctx);
7793}
7794
Jens Axboe2b188cc2019-01-07 10:46:33 -07007795static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7796{
7797 mutex_lock(&ctx->uring_lock);
7798 percpu_ref_kill(&ctx->refs);
7799 mutex_unlock(&ctx->uring_lock);
7800
Jens Axboe5262f562019-09-17 12:26:57 -06007801 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007802 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007803
7804 if (ctx->io_wq)
7805 io_wq_cancel_all(ctx->io_wq);
7806
Jens Axboe15dff282019-11-13 09:09:23 -07007807 /* if we failed setting up the ctx, we might not have any rings */
7808 if (ctx->rings)
7809 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007810 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07007811 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06007812
7813 /*
7814 * Do this upfront, so we won't have a grace period where the ring
7815 * is closed but resources aren't reaped yet. This can cause
7816 * spurious failure in setting up a new ring.
7817 */
Jens Axboe760618f2020-07-24 12:53:31 -06007818 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7819 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06007820
Jens Axboe85faa7b2020-04-09 18:14:00 -06007821 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7822 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007823}
7824
7825static int io_uring_release(struct inode *inode, struct file *file)
7826{
7827 struct io_ring_ctx *ctx = file->private_data;
7828
7829 file->private_data = NULL;
7830 io_ring_ctx_wait_and_kill(ctx);
7831 return 0;
7832}
7833
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007834static bool io_wq_files_match(struct io_wq_work *work, void *data)
7835{
7836 struct files_struct *files = data;
7837
7838 return work->files == files;
7839}
7840
Jens Axboefcb323c2019-10-24 12:39:47 -06007841static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7842 struct files_struct *files)
7843{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007844 if (list_empty_careful(&ctx->inflight_list))
7845 return;
7846
7847 /* cancel all at once, should be faster than doing it one by one*/
7848 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7849
Jens Axboefcb323c2019-10-24 12:39:47 -06007850 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007851 struct io_kiocb *cancel_req = NULL, *req;
7852 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007853
7854 spin_lock_irq(&ctx->inflight_lock);
7855 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007856 if (req->work.files != files)
7857 continue;
7858 /* req is being completed, ignore */
7859 if (!refcount_inc_not_zero(&req->refs))
7860 continue;
7861 cancel_req = req;
7862 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007863 }
Jens Axboe768134d2019-11-10 20:30:53 -07007864 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007865 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007866 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007867 spin_unlock_irq(&ctx->inflight_lock);
7868
Jens Axboe768134d2019-11-10 20:30:53 -07007869 /* We need to keep going until we don't find a matching req */
7870 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007871 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007872
Jens Axboe2ca10252020-02-13 17:17:35 -07007873 if (cancel_req->flags & REQ_F_OVERFLOW) {
7874 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03007875 list_del(&cancel_req->compl.list);
Jens Axboe2ca10252020-02-13 17:17:35 -07007876 cancel_req->flags &= ~REQ_F_OVERFLOW;
7877 if (list_empty(&ctx->cq_overflow_list)) {
7878 clear_bit(0, &ctx->sq_check_overflow);
7879 clear_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08007880 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
Jens Axboe2ca10252020-02-13 17:17:35 -07007881 }
7882 spin_unlock_irq(&ctx->completion_lock);
7883
7884 WRITE_ONCE(ctx->rings->cq_overflow,
7885 atomic_inc_return(&ctx->cached_cq_overflow));
7886
7887 /*
7888 * Put inflight ref and overflow ref. If that's
7889 * all we had, then we're done with this request.
7890 */
7891 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007892 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007893 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007894 continue;
7895 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007896 } else {
7897 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7898 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007899 }
7900
Jens Axboefcb323c2019-10-24 12:39:47 -06007901 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007902 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007903 }
7904}
7905
Pavel Begunkov801dd572020-06-15 10:33:14 +03007906static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007907{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007908 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7909 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007910
Pavel Begunkov801dd572020-06-15 10:33:14 +03007911 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007912}
7913
Jens Axboefcb323c2019-10-24 12:39:47 -06007914static int io_uring_flush(struct file *file, void *data)
7915{
7916 struct io_ring_ctx *ctx = file->private_data;
7917
7918 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007919
7920 /*
7921 * If the task is going away, cancel work it may have pending
7922 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007923 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7924 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007925
Jens Axboefcb323c2019-10-24 12:39:47 -06007926 return 0;
7927}
7928
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007929static void *io_uring_validate_mmap_request(struct file *file,
7930 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007931{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007932 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007933 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007934 struct page *page;
7935 void *ptr;
7936
7937 switch (offset) {
7938 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007939 case IORING_OFF_CQ_RING:
7940 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007941 break;
7942 case IORING_OFF_SQES:
7943 ptr = ctx->sq_sqes;
7944 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007945 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007946 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007947 }
7948
7949 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007950 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007951 return ERR_PTR(-EINVAL);
7952
7953 return ptr;
7954}
7955
7956#ifdef CONFIG_MMU
7957
7958static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7959{
7960 size_t sz = vma->vm_end - vma->vm_start;
7961 unsigned long pfn;
7962 void *ptr;
7963
7964 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7965 if (IS_ERR(ptr))
7966 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007967
7968 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7969 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7970}
7971
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007972#else /* !CONFIG_MMU */
7973
7974static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7975{
7976 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7977}
7978
7979static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7980{
7981 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7982}
7983
7984static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7985 unsigned long addr, unsigned long len,
7986 unsigned long pgoff, unsigned long flags)
7987{
7988 void *ptr;
7989
7990 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7991 if (IS_ERR(ptr))
7992 return PTR_ERR(ptr);
7993
7994 return (unsigned long) ptr;
7995}
7996
7997#endif /* !CONFIG_MMU */
7998
Jens Axboe2b188cc2019-01-07 10:46:33 -07007999SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8000 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8001 size_t, sigsz)
8002{
8003 struct io_ring_ctx *ctx;
8004 long ret = -EBADF;
8005 int submitted = 0;
8006 struct fd f;
8007
Jens Axboe4c6e2772020-07-01 11:29:10 -06008008 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008009
Jens Axboe6c271ce2019-01-10 11:22:30 -07008010 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008011 return -EINVAL;
8012
8013 f = fdget(fd);
8014 if (!f.file)
8015 return -EBADF;
8016
8017 ret = -EOPNOTSUPP;
8018 if (f.file->f_op != &io_uring_fops)
8019 goto out_fput;
8020
8021 ret = -ENXIO;
8022 ctx = f.file->private_data;
8023 if (!percpu_ref_tryget(&ctx->refs))
8024 goto out_fput;
8025
Jens Axboe6c271ce2019-01-10 11:22:30 -07008026 /*
8027 * For SQ polling, the thread will do all submissions and completions.
8028 * Just return the requested submit count, and wake the thread if
8029 * we were asked to.
8030 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008031 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008032 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008033 if (!list_empty_careful(&ctx->cq_overflow_list))
8034 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008035 if (flags & IORING_ENTER_SQ_WAKEUP)
8036 wake_up(&ctx->sqo_wait);
8037 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008038 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07008039 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03008040 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008041 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008042
8043 if (submitted != to_submit)
8044 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008045 }
8046 if (flags & IORING_ENTER_GETEVENTS) {
8047 min_complete = min(min_complete, ctx->cq_entries);
8048
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008049 /*
8050 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8051 * space applications don't need to do io completion events
8052 * polling again, they can rely on io_sq_thread to do polling
8053 * work, which can reduce cpu usage and uring_lock contention.
8054 */
8055 if (ctx->flags & IORING_SETUP_IOPOLL &&
8056 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008057 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008058 } else {
8059 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8060 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008061 }
8062
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008063out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008064 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008065out_fput:
8066 fdput(f);
8067 return submitted ? submitted : ret;
8068}
8069
Tobias Klauserbebdb652020-02-26 18:38:32 +01008070#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008071static int io_uring_show_cred(int id, void *p, void *data)
8072{
8073 const struct cred *cred = p;
8074 struct seq_file *m = data;
8075 struct user_namespace *uns = seq_user_ns(m);
8076 struct group_info *gi;
8077 kernel_cap_t cap;
8078 unsigned __capi;
8079 int g;
8080
8081 seq_printf(m, "%5d\n", id);
8082 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8083 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8084 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8085 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8086 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8087 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8088 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8089 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8090 seq_puts(m, "\n\tGroups:\t");
8091 gi = cred->group_info;
8092 for (g = 0; g < gi->ngroups; g++) {
8093 seq_put_decimal_ull(m, g ? " " : "",
8094 from_kgid_munged(uns, gi->gid[g]));
8095 }
8096 seq_puts(m, "\n\tCapEff:\t");
8097 cap = cred->cap_effective;
8098 CAP_FOR_EACH_U32(__capi)
8099 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8100 seq_putc(m, '\n');
8101 return 0;
8102}
8103
8104static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8105{
8106 int i;
8107
8108 mutex_lock(&ctx->uring_lock);
8109 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
8110 for (i = 0; i < ctx->nr_user_files; i++) {
8111 struct fixed_file_table *table;
8112 struct file *f;
8113
8114 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8115 f = table->files[i & IORING_FILE_TABLE_MASK];
8116 if (f)
8117 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8118 else
8119 seq_printf(m, "%5u: <none>\n", i);
8120 }
8121 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
8122 for (i = 0; i < ctx->nr_user_bufs; i++) {
8123 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8124
8125 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8126 (unsigned int) buf->len);
8127 }
8128 if (!idr_is_empty(&ctx->personality_idr)) {
8129 seq_printf(m, "Personalities:\n");
8130 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8131 }
Jens Axboed7718a92020-02-14 22:23:12 -07008132 seq_printf(m, "PollList:\n");
8133 spin_lock_irq(&ctx->completion_lock);
8134 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8135 struct hlist_head *list = &ctx->cancel_hash[i];
8136 struct io_kiocb *req;
8137
8138 hlist_for_each_entry(req, list, hash_node)
8139 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8140 req->task->task_works != NULL);
8141 }
8142 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008143 mutex_unlock(&ctx->uring_lock);
8144}
8145
8146static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8147{
8148 struct io_ring_ctx *ctx = f->private_data;
8149
8150 if (percpu_ref_tryget(&ctx->refs)) {
8151 __io_uring_show_fdinfo(ctx, m);
8152 percpu_ref_put(&ctx->refs);
8153 }
8154}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008155#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008156
Jens Axboe2b188cc2019-01-07 10:46:33 -07008157static const struct file_operations io_uring_fops = {
8158 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008159 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008160 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008161#ifndef CONFIG_MMU
8162 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8163 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8164#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008165 .poll = io_uring_poll,
8166 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008167#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008168 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008169#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008170};
8171
8172static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8173 struct io_uring_params *p)
8174{
Hristo Venev75b28af2019-08-26 17:23:46 +00008175 struct io_rings *rings;
8176 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008177
Hristo Venev75b28af2019-08-26 17:23:46 +00008178 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8179 if (size == SIZE_MAX)
8180 return -EOVERFLOW;
8181
8182 rings = io_mem_alloc(size);
8183 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008184 return -ENOMEM;
8185
Hristo Venev75b28af2019-08-26 17:23:46 +00008186 ctx->rings = rings;
8187 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8188 rings->sq_ring_mask = p->sq_entries - 1;
8189 rings->cq_ring_mask = p->cq_entries - 1;
8190 rings->sq_ring_entries = p->sq_entries;
8191 rings->cq_ring_entries = p->cq_entries;
8192 ctx->sq_mask = rings->sq_ring_mask;
8193 ctx->cq_mask = rings->cq_ring_mask;
8194 ctx->sq_entries = rings->sq_ring_entries;
8195 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008196
8197 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008198 if (size == SIZE_MAX) {
8199 io_mem_free(ctx->rings);
8200 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008201 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008202 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008203
8204 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008205 if (!ctx->sq_sqes) {
8206 io_mem_free(ctx->rings);
8207 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008208 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008209 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008210
Jens Axboe2b188cc2019-01-07 10:46:33 -07008211 return 0;
8212}
8213
8214/*
8215 * Allocate an anonymous fd, this is what constitutes the application
8216 * visible backing of an io_uring instance. The application mmaps this
8217 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8218 * we have to tie this fd to a socket for file garbage collection purposes.
8219 */
8220static int io_uring_get_fd(struct io_ring_ctx *ctx)
8221{
8222 struct file *file;
8223 int ret;
8224
8225#if defined(CONFIG_UNIX)
8226 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8227 &ctx->ring_sock);
8228 if (ret)
8229 return ret;
8230#endif
8231
8232 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8233 if (ret < 0)
8234 goto err;
8235
8236 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8237 O_RDWR | O_CLOEXEC);
8238 if (IS_ERR(file)) {
8239 put_unused_fd(ret);
8240 ret = PTR_ERR(file);
8241 goto err;
8242 }
8243
8244#if defined(CONFIG_UNIX)
8245 ctx->ring_sock->file = file;
8246#endif
8247 fd_install(ret, file);
8248 return ret;
8249err:
8250#if defined(CONFIG_UNIX)
8251 sock_release(ctx->ring_sock);
8252 ctx->ring_sock = NULL;
8253#endif
8254 return ret;
8255}
8256
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008257static int io_uring_create(unsigned entries, struct io_uring_params *p,
8258 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008259{
8260 struct user_struct *user = NULL;
8261 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008262 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008263 int ret;
8264
Jens Axboe8110c1a2019-12-28 15:39:54 -07008265 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008266 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008267 if (entries > IORING_MAX_ENTRIES) {
8268 if (!(p->flags & IORING_SETUP_CLAMP))
8269 return -EINVAL;
8270 entries = IORING_MAX_ENTRIES;
8271 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008272
8273 /*
8274 * Use twice as many entries for the CQ ring. It's possible for the
8275 * application to drive a higher depth than the size of the SQ ring,
8276 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008277 * some flexibility in overcommitting a bit. If the application has
8278 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8279 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008280 */
8281 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008282 if (p->flags & IORING_SETUP_CQSIZE) {
8283 /*
8284 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8285 * to a power-of-two, if it isn't already. We do NOT impose
8286 * any cq vs sq ring sizing.
8287 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008288 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008289 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008290 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8291 if (!(p->flags & IORING_SETUP_CLAMP))
8292 return -EINVAL;
8293 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8294 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008295 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8296 } else {
8297 p->cq_entries = 2 * p->sq_entries;
8298 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008299
8300 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008301 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008302
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008303 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008304 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008305 ring_pages(p->sq_entries, p->cq_entries));
8306 if (ret) {
8307 free_uid(user);
8308 return ret;
8309 }
8310 }
8311
8312 ctx = io_ring_ctx_alloc(p);
8313 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008314 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008315 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008316 p->cq_entries));
8317 free_uid(user);
8318 return -ENOMEM;
8319 }
8320 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008321 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008322 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008323
8324 ret = io_allocate_scq_urings(ctx, p);
8325 if (ret)
8326 goto err;
8327
Jens Axboe6c271ce2019-01-10 11:22:30 -07008328 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008329 if (ret)
8330 goto err;
8331
Jens Axboe2b188cc2019-01-07 10:46:33 -07008332 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008333 p->sq_off.head = offsetof(struct io_rings, sq.head);
8334 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8335 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8336 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8337 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8338 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8339 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008340
8341 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008342 p->cq_off.head = offsetof(struct io_rings, cq.head);
8343 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8344 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8345 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8346 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8347 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008348 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008349
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008350 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8351 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008352 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8353 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008354
8355 if (copy_to_user(params, p, sizeof(*p))) {
8356 ret = -EFAULT;
8357 goto err;
8358 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008359 /*
8360 * Install ring fd as the very last thing, so we don't risk someone
8361 * having closed it before we finish setup
8362 */
8363 ret = io_uring_get_fd(ctx);
8364 if (ret < 0)
8365 goto err;
8366
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008367 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008368 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8369 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008370 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008371 return ret;
8372err:
8373 io_ring_ctx_wait_and_kill(ctx);
8374 return ret;
8375}
8376
8377/*
8378 * Sets up an aio uring context, and returns the fd. Applications asks for a
8379 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8380 * params structure passed in.
8381 */
8382static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8383{
8384 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008385 int i;
8386
8387 if (copy_from_user(&p, params, sizeof(p)))
8388 return -EFAULT;
8389 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8390 if (p.resv[i])
8391 return -EINVAL;
8392 }
8393
Jens Axboe6c271ce2019-01-10 11:22:30 -07008394 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008395 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008396 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008397 return -EINVAL;
8398
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008399 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008400}
8401
8402SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8403 struct io_uring_params __user *, params)
8404{
8405 return io_uring_setup(entries, params);
8406}
8407
Jens Axboe66f4af92020-01-16 15:36:52 -07008408static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8409{
8410 struct io_uring_probe *p;
8411 size_t size;
8412 int i, ret;
8413
8414 size = struct_size(p, ops, nr_args);
8415 if (size == SIZE_MAX)
8416 return -EOVERFLOW;
8417 p = kzalloc(size, GFP_KERNEL);
8418 if (!p)
8419 return -ENOMEM;
8420
8421 ret = -EFAULT;
8422 if (copy_from_user(p, arg, size))
8423 goto out;
8424 ret = -EINVAL;
8425 if (memchr_inv(p, 0, size))
8426 goto out;
8427
8428 p->last_op = IORING_OP_LAST - 1;
8429 if (nr_args > IORING_OP_LAST)
8430 nr_args = IORING_OP_LAST;
8431
8432 for (i = 0; i < nr_args; i++) {
8433 p->ops[i].op = i;
8434 if (!io_op_defs[i].not_supported)
8435 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8436 }
8437 p->ops_len = i;
8438
8439 ret = 0;
8440 if (copy_to_user(arg, p, size))
8441 ret = -EFAULT;
8442out:
8443 kfree(p);
8444 return ret;
8445}
8446
Jens Axboe071698e2020-01-28 10:04:42 -07008447static int io_register_personality(struct io_ring_ctx *ctx)
8448{
8449 const struct cred *creds = get_current_cred();
8450 int id;
8451
8452 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8453 USHRT_MAX, GFP_KERNEL);
8454 if (id < 0)
8455 put_cred(creds);
8456 return id;
8457}
8458
8459static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8460{
8461 const struct cred *old_creds;
8462
8463 old_creds = idr_remove(&ctx->personality_idr, id);
8464 if (old_creds) {
8465 put_cred(old_creds);
8466 return 0;
8467 }
8468
8469 return -EINVAL;
8470}
8471
8472static bool io_register_op_must_quiesce(int op)
8473{
8474 switch (op) {
8475 case IORING_UNREGISTER_FILES:
8476 case IORING_REGISTER_FILES_UPDATE:
8477 case IORING_REGISTER_PROBE:
8478 case IORING_REGISTER_PERSONALITY:
8479 case IORING_UNREGISTER_PERSONALITY:
8480 return false;
8481 default:
8482 return true;
8483 }
8484}
8485
Jens Axboeedafcce2019-01-09 09:16:05 -07008486static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8487 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008488 __releases(ctx->uring_lock)
8489 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008490{
8491 int ret;
8492
Jens Axboe35fa71a2019-04-22 10:23:23 -06008493 /*
8494 * We're inside the ring mutex, if the ref is already dying, then
8495 * someone else killed the ctx or is already going through
8496 * io_uring_register().
8497 */
8498 if (percpu_ref_is_dying(&ctx->refs))
8499 return -ENXIO;
8500
Jens Axboe071698e2020-01-28 10:04:42 -07008501 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008502 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008503
Jens Axboe05f3fb32019-12-09 11:22:50 -07008504 /*
8505 * Drop uring mutex before waiting for references to exit. If
8506 * another thread is currently inside io_uring_enter() it might
8507 * need to grab the uring_lock to make progress. If we hold it
8508 * here across the drain wait, then we can deadlock. It's safe
8509 * to drop the mutex here, since no new references will come in
8510 * after we've killed the percpu ref.
8511 */
8512 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008513 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008514 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008515 if (ret) {
8516 percpu_ref_resurrect(&ctx->refs);
8517 ret = -EINTR;
8518 goto out;
8519 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008520 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008521
8522 switch (opcode) {
8523 case IORING_REGISTER_BUFFERS:
8524 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8525 break;
8526 case IORING_UNREGISTER_BUFFERS:
8527 ret = -EINVAL;
8528 if (arg || nr_args)
8529 break;
8530 ret = io_sqe_buffer_unregister(ctx);
8531 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008532 case IORING_REGISTER_FILES:
8533 ret = io_sqe_files_register(ctx, arg, nr_args);
8534 break;
8535 case IORING_UNREGISTER_FILES:
8536 ret = -EINVAL;
8537 if (arg || nr_args)
8538 break;
8539 ret = io_sqe_files_unregister(ctx);
8540 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008541 case IORING_REGISTER_FILES_UPDATE:
8542 ret = io_sqe_files_update(ctx, arg, nr_args);
8543 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008544 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008545 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008546 ret = -EINVAL;
8547 if (nr_args != 1)
8548 break;
8549 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008550 if (ret)
8551 break;
8552 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8553 ctx->eventfd_async = 1;
8554 else
8555 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008556 break;
8557 case IORING_UNREGISTER_EVENTFD:
8558 ret = -EINVAL;
8559 if (arg || nr_args)
8560 break;
8561 ret = io_eventfd_unregister(ctx);
8562 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008563 case IORING_REGISTER_PROBE:
8564 ret = -EINVAL;
8565 if (!arg || nr_args > 256)
8566 break;
8567 ret = io_probe(ctx, arg, nr_args);
8568 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008569 case IORING_REGISTER_PERSONALITY:
8570 ret = -EINVAL;
8571 if (arg || nr_args)
8572 break;
8573 ret = io_register_personality(ctx);
8574 break;
8575 case IORING_UNREGISTER_PERSONALITY:
8576 ret = -EINVAL;
8577 if (arg)
8578 break;
8579 ret = io_unregister_personality(ctx, nr_args);
8580 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008581 default:
8582 ret = -EINVAL;
8583 break;
8584 }
8585
Jens Axboe071698e2020-01-28 10:04:42 -07008586 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008587 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008588 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008589out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008590 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008591 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008592 return ret;
8593}
8594
8595SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8596 void __user *, arg, unsigned int, nr_args)
8597{
8598 struct io_ring_ctx *ctx;
8599 long ret = -EBADF;
8600 struct fd f;
8601
8602 f = fdget(fd);
8603 if (!f.file)
8604 return -EBADF;
8605
8606 ret = -EOPNOTSUPP;
8607 if (f.file->f_op != &io_uring_fops)
8608 goto out_fput;
8609
8610 ctx = f.file->private_data;
8611
8612 mutex_lock(&ctx->uring_lock);
8613 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8614 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008615 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8616 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008617out_fput:
8618 fdput(f);
8619 return ret;
8620}
8621
Jens Axboe2b188cc2019-01-07 10:46:33 -07008622static int __init io_uring_init(void)
8623{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008624#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8625 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8626 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8627} while (0)
8628
8629#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8630 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8631 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8632 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8633 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8634 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8635 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8636 BUILD_BUG_SQE_ELEM(8, __u64, off);
8637 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8638 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008639 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008640 BUILD_BUG_SQE_ELEM(24, __u32, len);
8641 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8642 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8643 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8644 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008645 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8646 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008647 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8648 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8649 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8650 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8651 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8652 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8653 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8654 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008655 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008656 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8657 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8658 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008659 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008660
Jens Axboed3656342019-12-18 09:50:26 -07008661 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008662 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008663 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8664 return 0;
8665};
8666__initcall(io_uring_init);