blob: 046d06266a116ee0e654c4512ceadbb86c4d02d8 [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 */
Jens Axboe2aede0e2020-09-14 10:45:53 -0600268
269 /*
270 * For SQPOLL usage - we hold a reference to the parent task, so we
271 * have access to the ->files
272 */
273 struct task_struct *sqo_task;
274
275 /* Only used for accounting purposes */
276 struct mm_struct *mm_account;
277
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279
Jens Axboe6b063142019-01-10 22:13:58 -0700280 /*
281 * If used, fixed file set. Writers must ensure that ->refs is dead,
282 * readers must ensure that ->refs is alive as long as the file* is
283 * used. Only updated through io_uring_register(2).
284 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700285 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700286 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300287 int ring_fd;
288 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700289
Jens Axboeedafcce2019-01-09 09:16:05 -0700290 /* if used, fixed mapped user buffers */
291 unsigned nr_user_bufs;
292 struct io_mapped_ubuf *user_bufs;
293
Jens Axboe2b188cc2019-01-07 10:46:33 -0700294 struct user_struct *user;
295
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700296 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700297
Jens Axboe0f158b42020-05-14 17:18:39 -0600298 struct completion ref_comp;
299 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700300
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700301 /* if all else fails... */
302 struct io_kiocb *fallback_req;
303
Jens Axboe206aefd2019-11-07 18:27:42 -0700304#if defined(CONFIG_UNIX)
305 struct socket *ring_sock;
306#endif
307
Jens Axboe5a2e7452020-02-23 16:23:11 -0700308 struct idr io_buffer_idr;
309
Jens Axboe071698e2020-01-28 10:04:42 -0700310 struct idr personality_idr;
311
Jens Axboe206aefd2019-11-07 18:27:42 -0700312 struct {
313 unsigned cached_cq_tail;
314 unsigned cq_entries;
315 unsigned cq_mask;
316 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700317 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700318 struct wait_queue_head cq_wait;
319 struct fasync_struct *cq_fasync;
320 struct eventfd_ctx *cq_ev_fd;
321 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700322
323 struct {
324 struct mutex uring_lock;
325 wait_queue_head_t wait;
326 } ____cacheline_aligned_in_smp;
327
328 struct {
329 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700330
Jens Axboedef596e2019-01-09 08:59:42 -0700331 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300332 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700333 * io_uring instances that don't use IORING_SETUP_SQPOLL.
334 * For SQPOLL, only the single threaded io_sq_thread() will
335 * manipulate the list, hence no extra locking is needed there.
336 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300337 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700338 struct hlist_head *cancel_hash;
339 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700340 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600341
342 spinlock_t inflight_lock;
343 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600345
Jens Axboe4a38aed22020-05-14 17:21:15 -0600346 struct delayed_work file_put_work;
347 struct llist_head file_put_llist;
348
Jens Axboe85faa7b2020-04-09 18:14:00 -0600349 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700350};
351
Jens Axboe09bb8392019-03-13 12:39:28 -0600352/*
353 * First field must be the file pointer in all the
354 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
355 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700356struct io_poll_iocb {
357 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700358 union {
359 struct wait_queue_head *head;
360 u64 addr;
361 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700362 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600363 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700364 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700365 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700366};
367
Jens Axboeb5dba592019-12-11 14:02:38 -0700368struct io_close {
369 struct file *file;
370 struct file *put_file;
371 int fd;
372};
373
Jens Axboead8a48a2019-11-15 08:49:11 -0700374struct io_timeout_data {
375 struct io_kiocb *req;
376 struct hrtimer timer;
377 struct timespec64 ts;
378 enum hrtimer_mode mode;
379};
380
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700381struct io_accept {
382 struct file *file;
383 struct sockaddr __user *addr;
384 int __user *addr_len;
385 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600386 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700387};
388
389struct io_sync {
390 struct file *file;
391 loff_t len;
392 loff_t off;
393 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700394 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700395};
396
Jens Axboefbf23842019-12-17 18:45:56 -0700397struct io_cancel {
398 struct file *file;
399 u64 addr;
400};
401
Jens Axboeb29472e2019-12-17 18:50:29 -0700402struct io_timeout {
403 struct file *file;
404 u64 addr;
405 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300406 u32 off;
407 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300408 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700409};
410
Jens Axboe9adbd452019-12-20 08:45:55 -0700411struct io_rw {
412 /* NOTE: kiocb has the file as the first member, so don't do it here */
413 struct kiocb kiocb;
414 u64 addr;
415 u64 len;
416};
417
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700418struct io_connect {
419 struct file *file;
420 struct sockaddr __user *addr;
421 int addr_len;
422};
423
Jens Axboee47293f2019-12-20 08:58:21 -0700424struct io_sr_msg {
425 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700426 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300427 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700428 void __user *buf;
429 };
Jens Axboee47293f2019-12-20 08:58:21 -0700430 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700431 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700432 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700433 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700434};
435
Jens Axboe15b71ab2019-12-11 11:20:36 -0700436struct io_open {
437 struct file *file;
438 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700439 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700440 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600441 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700442};
443
Jens Axboe05f3fb32019-12-09 11:22:50 -0700444struct io_files_update {
445 struct file *file;
446 u64 arg;
447 u32 nr_args;
448 u32 offset;
449};
450
Jens Axboe4840e412019-12-25 22:03:45 -0700451struct io_fadvise {
452 struct file *file;
453 u64 offset;
454 u32 len;
455 u32 advice;
456};
457
Jens Axboec1ca7572019-12-25 22:18:28 -0700458struct io_madvise {
459 struct file *file;
460 u64 addr;
461 u32 len;
462 u32 advice;
463};
464
Jens Axboe3e4827b2020-01-08 15:18:09 -0700465struct io_epoll {
466 struct file *file;
467 int epfd;
468 int op;
469 int fd;
470 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700471};
472
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300473struct io_splice {
474 struct file *file_out;
475 struct file *file_in;
476 loff_t off_out;
477 loff_t off_in;
478 u64 len;
479 unsigned int flags;
480};
481
Jens Axboeddf0322d2020-02-23 16:41:33 -0700482struct io_provide_buf {
483 struct file *file;
484 __u64 addr;
485 __s32 len;
486 __u32 bgid;
487 __u16 nbufs;
488 __u16 bid;
489};
490
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700491struct io_statx {
492 struct file *file;
493 int dfd;
494 unsigned int mask;
495 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700496 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700497 struct statx __user *buffer;
498};
499
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300500struct io_completion {
501 struct file *file;
502 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300503 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300504};
505
Jens Axboef499a022019-12-02 16:28:46 -0700506struct io_async_connect {
507 struct sockaddr_storage address;
508};
509
Jens Axboe03b12302019-12-02 18:50:25 -0700510struct io_async_msghdr {
511 struct iovec fast_iov[UIO_FASTIOV];
512 struct iovec *iov;
513 struct sockaddr __user *uaddr;
514 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700515 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700516};
517
Jens Axboef67676d2019-12-02 11:03:47 -0700518struct io_async_rw {
519 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600520 const struct iovec *free_iovec;
521 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600522 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600523 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700524};
525
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700526struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700527 union {
528 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700529 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700530 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700531 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700532 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700533};
534
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300535enum {
536 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
537 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
538 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
539 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
540 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700541 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300542
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300543 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300544 REQ_F_FAIL_LINK_BIT,
545 REQ_F_INFLIGHT_BIT,
546 REQ_F_CUR_POS_BIT,
547 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300548 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300549 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300550 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300551 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700552 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700553 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600554 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800555 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700556
557 /* not a real bit, just to check we're not overflowing the space */
558 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300559};
560
561enum {
562 /* ctx owns file */
563 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
564 /* drain existing IO first */
565 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
566 /* linked sqes */
567 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
568 /* doesn't sever on completion < 0 */
569 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
570 /* IOSQE_ASYNC */
571 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700572 /* IOSQE_BUFFER_SELECT */
573 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300574
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300575 /* head of a link */
576 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577 /* fail rest of links */
578 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
579 /* on inflight list */
580 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
581 /* read/write uses file position */
582 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
583 /* must not punt to workers */
584 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300585 /* has linked timeout */
586 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300587 /* regular file */
588 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300589 /* completion under lock */
590 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300591 /* needs cleanup */
592 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700593 /* already went through poll handler */
594 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700595 /* buffer already selected */
596 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600597 /* doesn't need file table for this request */
598 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800599 /* io_wq_work is initialized */
600 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700601};
602
603struct async_poll {
604 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600605 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300606};
607
Jens Axboe09bb8392019-03-13 12:39:28 -0600608/*
609 * NOTE! Each of the iocb union members has the file pointer
610 * as the first entry in their struct definition. So you can
611 * access the file pointer through any of the sub-structs,
612 * or directly as just 'ki_filp' in this struct.
613 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700614struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700615 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600616 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700617 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700618 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700619 struct io_accept accept;
620 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700621 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700622 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700623 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700624 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700625 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700626 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700627 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700628 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700629 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700630 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300631 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700632 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700633 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300634 /* use only after cleaning per-op data, see io_clean_op() */
635 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700636 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700637
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700638 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700639 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800640 /* polled IO has completed */
641 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700642
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700643 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300644 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700645
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300646 struct io_ring_ctx *ctx;
647 unsigned int flags;
648 refcount_t refs;
649 struct task_struct *task;
650 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700651
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300652 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700653
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300654 /*
655 * 1. used with ctx->iopoll_list with reads/writes
656 * 2. to track reqs with ->files (see io_op_def::file_table)
657 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300658 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600659
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300660 struct percpu_ref *fixed_file_refs;
661 struct callback_head task_work;
662 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
663 struct hlist_node hash_node;
664 struct async_poll *apoll;
665 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700666};
667
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300668struct io_defer_entry {
669 struct list_head list;
670 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300671 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300672};
673
Jens Axboedef596e2019-01-09 08:59:42 -0700674#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700675
Jens Axboe013538b2020-06-22 09:29:15 -0600676struct io_comp_state {
677 unsigned int nr;
678 struct list_head list;
679 struct io_ring_ctx *ctx;
680};
681
Jens Axboe9a56a232019-01-09 09:06:50 -0700682struct io_submit_state {
683 struct blk_plug plug;
684
685 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700686 * io_kiocb alloc cache
687 */
688 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300689 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700690
691 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600692 * Batch completion logic
693 */
694 struct io_comp_state comp;
695
696 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700697 * File reference cache
698 */
699 struct file *file;
700 unsigned int fd;
701 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700702 unsigned int ios_left;
703};
704
Jens Axboed3656342019-12-18 09:50:26 -0700705struct io_op_def {
706 /* needs req->io allocated for deferral/async */
707 unsigned async_ctx : 1;
708 /* needs current->mm setup, does mm access */
709 unsigned needs_mm : 1;
710 /* needs req->file assigned */
711 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600712 /* don't fail if file grab fails */
713 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700714 /* hash wq insertion if file is a regular file */
715 unsigned hash_reg_file : 1;
716 /* unbound wq insertion if file is a non-regular file */
717 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700718 /* opcode is not supported by this kernel */
719 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700720 /* needs file table */
721 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700722 /* needs ->fs */
723 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700724 /* set if opcode supports polled "wait" */
725 unsigned pollin : 1;
726 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700727 /* op supports buffer selection */
728 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300729 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700730};
731
732static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300733 [IORING_OP_NOP] = {},
734 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700735 .async_ctx = 1,
736 .needs_mm = 1,
737 .needs_file = 1,
738 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700739 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700740 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700741 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300742 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700743 .async_ctx = 1,
744 .needs_mm = 1,
745 .needs_file = 1,
746 .hash_reg_file = 1,
747 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700748 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300749 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700750 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300751 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700752 .needs_file = 1,
753 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300754 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700755 .needs_file = 1,
756 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700757 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .needs_file = 1,
761 .hash_reg_file = 1,
762 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700763 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300764 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700765 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300766 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700767 .needs_file = 1,
768 .unbound_nonreg_file = 1,
769 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300770 [IORING_OP_POLL_REMOVE] = {},
771 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700772 .needs_file = 1,
773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .async_ctx = 1,
776 .needs_mm = 1,
777 .needs_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700779 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700780 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_RECVMSG] = {
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 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700789 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700790 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300791 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700792 .async_ctx = 1,
793 .needs_mm = 1,
794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_TIMEOUT_REMOVE] = {},
796 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_mm = 1,
798 .needs_file = 1,
799 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700800 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700801 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_ASYNC_CANCEL] = {},
804 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700805 .async_ctx = 1,
806 .needs_mm = 1,
807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700809 .async_ctx = 1,
810 .needs_mm = 1,
811 .needs_file = 1,
812 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700813 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700814 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300815 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700816 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300817 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700818 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300819 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700820 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700821 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600824 .needs_file = 1,
825 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700826 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700829 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700830 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700831 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300832 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700833 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700834 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600835 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700836 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300837 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700838 .needs_mm = 1,
839 .needs_file = 1,
840 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700841 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700842 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700845 .needs_mm = 1,
846 .needs_file = 1,
847 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700848 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300849 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700850 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300851 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700852 .needs_file = 1,
853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700855 .needs_mm = 1,
856 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300857 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700858 .needs_mm = 1,
859 .needs_file = 1,
860 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700861 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700862 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300863 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700864 .needs_mm = 1,
865 .needs_file = 1,
866 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700867 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700868 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700871 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700872 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700873 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700874 [IORING_OP_EPOLL_CTL] = {
875 .unbound_nonreg_file = 1,
876 .file_table = 1,
877 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300878 [IORING_OP_SPLICE] = {
879 .needs_file = 1,
880 .hash_reg_file = 1,
881 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700882 },
883 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700884 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300885 [IORING_OP_TEE] = {
886 .needs_file = 1,
887 .hash_reg_file = 1,
888 .unbound_nonreg_file = 1,
889 },
Jens Axboed3656342019-12-18 09:50:26 -0700890};
891
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700892enum io_mem_account {
893 ACCT_LOCKED,
894 ACCT_PINNED,
895};
896
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300897static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
898 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700899static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800900static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600901static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700902static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700903static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600904static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700905static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700906static int __io_sqe_files_update(struct io_ring_ctx *ctx,
907 struct io_uring_files_update *ip,
908 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300909static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300910static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700911static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
912 int fd, struct file **out_file, bool fixed);
913static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600914 const struct io_uring_sqe *sqe,
915 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600916static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600917
Jens Axboeb63534c2020-06-04 11:28:00 -0600918static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
919 struct iovec **iovec, struct iov_iter *iter,
920 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600921static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
922 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600923 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700924
925static struct kmem_cache *req_cachep;
926
927static const struct file_operations io_uring_fops;
928
929struct sock *io_uring_get_socket(struct file *file)
930{
931#if defined(CONFIG_UNIX)
932 if (file->f_op == &io_uring_fops) {
933 struct io_ring_ctx *ctx = file->private_data;
934
935 return ctx->ring_sock->sk;
936 }
937#endif
938 return NULL;
939}
940EXPORT_SYMBOL(io_uring_get_socket);
941
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300942static inline void io_clean_op(struct io_kiocb *req)
943{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300944 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
945 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300946 __io_clean_op(req);
947}
948
Jens Axboe4349f302020-07-09 15:07:01 -0600949static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600950{
951 struct mm_struct *mm = current->mm;
952
953 if (mm) {
954 kthread_unuse_mm(mm);
955 mmput(mm);
956 }
957}
958
959static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
960{
961 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300962 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600963 !ctx->sqo_task->mm ||
964 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600965 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600966 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600967 }
968
969 return 0;
970}
971
972static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
973 struct io_kiocb *req)
974{
975 if (!io_op_defs[req->opcode].needs_mm)
976 return 0;
977 return __io_sq_thread_acquire_mm(ctx);
978}
979
980static inline void req_set_fail_links(struct io_kiocb *req)
981{
982 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
983 req->flags |= REQ_F_FAIL_LINK;
984}
Jens Axboe4a38aed22020-05-14 17:21:15 -0600985
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800986/*
987 * Note: must call io_req_init_async() for the first time you
988 * touch any members of io_wq_work.
989 */
990static inline void io_req_init_async(struct io_kiocb *req)
991{
992 if (req->flags & REQ_F_WORK_INITIALIZED)
993 return;
994
995 memset(&req->work, 0, sizeof(req->work));
996 req->flags |= REQ_F_WORK_INITIALIZED;
997}
998
Pavel Begunkov0cdaf762020-05-17 14:13:40 +0300999static inline bool io_async_submit(struct io_ring_ctx *ctx)
1000{
1001 return ctx->flags & IORING_SETUP_SQPOLL;
1002}
1003
Jens Axboe2b188cc2019-01-07 10:46:33 -07001004static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1005{
1006 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1007
Jens Axboe0f158b42020-05-14 17:18:39 -06001008 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009}
1010
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001011static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1012{
1013 return !req->timeout.off;
1014}
1015
Jens Axboe2b188cc2019-01-07 10:46:33 -07001016static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1017{
1018 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001019 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001020
1021 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1022 if (!ctx)
1023 return NULL;
1024
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001025 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1026 if (!ctx->fallback_req)
1027 goto err;
1028
Jens Axboe78076bb2019-12-04 19:56:40 -07001029 /*
1030 * Use 5 bits less than the max cq entries, that should give us around
1031 * 32 entries per hash list if totally full and uniformly spread.
1032 */
1033 hash_bits = ilog2(p->cq_entries);
1034 hash_bits -= 5;
1035 if (hash_bits <= 0)
1036 hash_bits = 1;
1037 ctx->cancel_hash_bits = hash_bits;
1038 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1039 GFP_KERNEL);
1040 if (!ctx->cancel_hash)
1041 goto err;
1042 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1043
Roman Gushchin21482892019-05-07 10:01:48 -07001044 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001045 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1046 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047
1048 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001049 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001050 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001051 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001052 init_completion(&ctx->ref_comp);
1053 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001054 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001055 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056 mutex_init(&ctx->uring_lock);
1057 init_waitqueue_head(&ctx->wait);
1058 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001059 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001060 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001061 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001062 init_waitqueue_head(&ctx->inflight_wait);
1063 spin_lock_init(&ctx->inflight_lock);
1064 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001065 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1066 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001067 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001068err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001069 if (ctx->fallback_req)
1070 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001071 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001072 kfree(ctx);
1073 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074}
1075
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001076static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001077{
Jens Axboe2bc99302020-07-09 09:43:27 -06001078 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1079 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001080
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001081 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001082 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001083 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001084
Bob Liu9d858b22019-11-13 18:06:25 +08001085 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001086}
1087
Jens Axboede0617e2019-04-06 21:51:27 -06001088static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001089{
Hristo Venev75b28af2019-08-26 17:23:46 +00001090 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091
Pavel Begunkov07910152020-01-17 03:52:46 +03001092 /* order cqe stores with ring update */
1093 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094
Pavel Begunkov07910152020-01-17 03:52:46 +03001095 if (wq_has_sleeper(&ctx->cq_wait)) {
1096 wake_up_interruptible(&ctx->cq_wait);
1097 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098 }
1099}
1100
Jens Axboe51a4cc12020-08-10 10:55:56 -06001101/*
1102 * Returns true if we need to defer file table putting. This can only happen
1103 * from the error path with REQ_F_COMP_LOCKED set.
1104 */
1105static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001106{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001107 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001108 return false;
1109
1110 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001111
Jens Axboecccf0ee2020-01-27 16:34:48 -07001112 if (req->work.mm) {
1113 mmdrop(req->work.mm);
1114 req->work.mm = NULL;
1115 }
1116 if (req->work.creds) {
1117 put_cred(req->work.creds);
1118 req->work.creds = NULL;
1119 }
Jens Axboeff002b32020-02-07 16:05:21 -07001120 if (req->work.fs) {
1121 struct fs_struct *fs = req->work.fs;
1122
Jens Axboe51a4cc12020-08-10 10:55:56 -06001123 if (req->flags & REQ_F_COMP_LOCKED)
1124 return true;
1125
Jens Axboeff002b32020-02-07 16:05:21 -07001126 spin_lock(&req->work.fs->lock);
1127 if (--fs->users)
1128 fs = NULL;
1129 spin_unlock(&req->work.fs->lock);
1130 if (fs)
1131 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001132 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001133 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001134
1135 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001136}
1137
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001138static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001139{
Jens Axboed3656342019-12-18 09:50:26 -07001140 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001141
Pavel Begunkov16d59802020-07-12 16:16:47 +03001142 io_req_init_async(req);
1143
Jens Axboed3656342019-12-18 09:50:26 -07001144 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001145 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001146 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001147 } else {
1148 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001149 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001150 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001151 if (!req->work.mm && def->needs_mm) {
1152 mmgrab(current->mm);
1153 req->work.mm = current->mm;
1154 }
1155 if (!req->work.creds)
1156 req->work.creds = get_current_cred();
1157 if (!req->work.fs && def->needs_fs) {
1158 spin_lock(&current->fs->lock);
1159 if (!current->fs->in_exec) {
1160 req->work.fs = current->fs;
1161 req->work.fs->users++;
1162 } else {
1163 req->work.flags |= IO_WQ_WORK_CANCEL;
1164 }
1165 spin_unlock(&current->fs->lock);
1166 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001167 if (def->needs_fsize)
1168 req->work.fsize = rlimit(RLIMIT_FSIZE);
1169 else
1170 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001171}
1172
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001173static void io_prep_async_link(struct io_kiocb *req)
1174{
1175 struct io_kiocb *cur;
1176
1177 io_prep_async_work(req);
1178 if (req->flags & REQ_F_LINK_HEAD)
1179 list_for_each_entry(cur, &req->link_list, link_list)
1180 io_prep_async_work(cur);
1181}
1182
Jens Axboe7271ef32020-08-10 09:55:22 -06001183static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001184{
Jackie Liua197f662019-11-08 08:09:12 -07001185 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001186 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001187
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001188 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1189 &req->work, req->flags);
1190 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001191 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001192}
1193
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001194static void io_queue_async_work(struct io_kiocb *req)
1195{
Jens Axboe7271ef32020-08-10 09:55:22 -06001196 struct io_kiocb *link;
1197
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001198 /* init ->work of the whole link before punting */
1199 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001200 link = __io_queue_async_work(req);
1201
1202 if (link)
1203 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001204}
1205
Jens Axboe5262f562019-09-17 12:26:57 -06001206static void io_kill_timeout(struct io_kiocb *req)
1207{
1208 int ret;
1209
Jens Axboe2d283902019-12-04 11:08:05 -07001210 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001211 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001212 atomic_set(&req->ctx->cq_timeouts,
1213 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001214 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001215 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001216 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001217 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001218 }
1219}
1220
Jens Axboef3606e32020-09-22 08:18:24 -06001221static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1222{
1223 struct io_ring_ctx *ctx = req->ctx;
1224
1225 if (!tsk || req->task == tsk)
1226 return true;
1227 if ((ctx->flags & IORING_SETUP_SQPOLL) && req->task == ctx->sqo_thread)
1228 return true;
1229 return false;
1230}
1231
Jens Axboe76e1b642020-09-26 15:05:03 -06001232/*
1233 * Returns true if we found and killed one or more timeouts
1234 */
1235static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001236{
1237 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001238 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001239
1240 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001241 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001242 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001243 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001244 canceled++;
1245 }
Jens Axboef3606e32020-09-22 08:18:24 -06001246 }
Jens Axboe5262f562019-09-17 12:26:57 -06001247 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001248 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001249}
1250
Pavel Begunkov04518942020-05-26 20:34:05 +03001251static void __io_queue_deferred(struct io_ring_ctx *ctx)
1252{
1253 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001254 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1255 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001256 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001257
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001258 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001259 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001260 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001261 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001262 link = __io_queue_async_work(de->req);
1263 if (link) {
1264 __io_queue_linked_timeout(link);
1265 /* drop submission reference */
1266 link->flags |= REQ_F_COMP_LOCKED;
1267 io_put_req(link);
1268 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001269 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001270 } while (!list_empty(&ctx->defer_list));
1271}
1272
Pavel Begunkov360428f2020-05-30 14:54:17 +03001273static void io_flush_timeouts(struct io_ring_ctx *ctx)
1274{
1275 while (!list_empty(&ctx->timeout_list)) {
1276 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001277 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001278
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001279 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001280 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001281 if (req->timeout.target_seq != ctx->cached_cq_tail
1282 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001283 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001284
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001285 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001286 io_kill_timeout(req);
1287 }
1288}
1289
Jens Axboede0617e2019-04-06 21:51:27 -06001290static void io_commit_cqring(struct io_ring_ctx *ctx)
1291{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001292 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001293 __io_commit_cqring(ctx);
1294
Pavel Begunkov04518942020-05-26 20:34:05 +03001295 if (unlikely(!list_empty(&ctx->defer_list)))
1296 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001297}
1298
Jens Axboe2b188cc2019-01-07 10:46:33 -07001299static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1300{
Hristo Venev75b28af2019-08-26 17:23:46 +00001301 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001302 unsigned tail;
1303
1304 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001305 /*
1306 * writes to the cq entry need to come after reading head; the
1307 * control dependency is enough as we're using WRITE_ONCE to
1308 * fill the cq entry
1309 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001310 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001311 return NULL;
1312
1313 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001314 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315}
1316
Jens Axboef2842ab2020-01-08 11:04:00 -07001317static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1318{
Jens Axboef0b493e2020-02-01 21:30:11 -07001319 if (!ctx->cq_ev_fd)
1320 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001321 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1322 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001323 if (!ctx->eventfd_async)
1324 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001325 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001326}
1327
Jens Axboeb41e9852020-02-17 09:52:41 -07001328static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001329{
1330 if (waitqueue_active(&ctx->wait))
1331 wake_up(&ctx->wait);
1332 if (waitqueue_active(&ctx->sqo_wait))
1333 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001334 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001335 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001336}
1337
Pavel Begunkov46930142020-07-30 18:43:49 +03001338static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1339{
1340 if (list_empty(&ctx->cq_overflow_list)) {
1341 clear_bit(0, &ctx->sq_check_overflow);
1342 clear_bit(0, &ctx->cq_check_overflow);
1343 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1344 }
1345}
1346
Jens Axboee6c8aa92020-09-28 13:10:13 -06001347static inline bool io_match_files(struct io_kiocb *req,
1348 struct files_struct *files)
1349{
1350 if (!files)
1351 return true;
1352 if (req->flags & REQ_F_WORK_INITIALIZED)
1353 return req->work.files == files;
1354 return false;
1355}
1356
Jens Axboec4a2ed72019-11-21 21:01:26 -07001357/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001358static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1359 struct task_struct *tsk,
1360 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001362 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001363 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001364 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001365 unsigned long flags;
1366 LIST_HEAD(list);
1367
1368 if (!force) {
1369 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001370 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001371 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1372 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001373 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001374 }
1375
1376 spin_lock_irqsave(&ctx->completion_lock, flags);
1377
1378 /* if force is set, the ring is going away. always drop after that */
1379 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001380 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001381
Jens Axboec4a2ed72019-11-21 21:01:26 -07001382 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001383 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1384 if (tsk && req->task != tsk)
1385 continue;
1386 if (!io_match_files(req, files))
1387 continue;
1388
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001389 cqe = io_get_cqring(ctx);
1390 if (!cqe && !force)
1391 break;
1392
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001393 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001394 if (cqe) {
1395 WRITE_ONCE(cqe->user_data, req->user_data);
1396 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001397 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001398 } else {
1399 WRITE_ONCE(ctx->rings->cq_overflow,
1400 atomic_inc_return(&ctx->cached_cq_overflow));
1401 }
1402 }
1403
1404 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001405 io_cqring_mark_overflow(ctx);
1406
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001407 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1408 io_cqring_ev_posted(ctx);
1409
1410 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001411 req = list_first_entry(&list, struct io_kiocb, compl.list);
1412 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001413 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001414 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001415
1416 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001417}
1418
Jens Axboebcda7ba2020-02-23 16:42:51 -07001419static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001421 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 struct io_uring_cqe *cqe;
1423
Jens Axboe78e19bb2019-11-06 15:21:34 -07001424 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001425
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426 /*
1427 * If we can't get a cq entry, userspace overflowed the
1428 * submission (by quite a lot). Increment the overflow count in
1429 * the ring.
1430 */
1431 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001432 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001433 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001434 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001435 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001436 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001437 WRITE_ONCE(ctx->rings->cq_overflow,
1438 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001439 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001440 if (list_empty(&ctx->cq_overflow_list)) {
1441 set_bit(0, &ctx->sq_check_overflow);
1442 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001443 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001444 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001445 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001446 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001447 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001448 refcount_inc(&req->refs);
1449 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001450 }
1451}
1452
Jens Axboebcda7ba2020-02-23 16:42:51 -07001453static void io_cqring_fill_event(struct io_kiocb *req, long res)
1454{
1455 __io_cqring_fill_event(req, res, 0);
1456}
1457
Jens Axboee1e16092020-06-22 09:17:17 -06001458static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001459{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001460 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 unsigned long flags;
1462
1463 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001464 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465 io_commit_cqring(ctx);
1466 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1467
Jens Axboe8c838782019-03-12 15:48:16 -06001468 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001469}
1470
Jens Axboe229a7b62020-06-22 10:13:11 -06001471static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001472{
Jens Axboe229a7b62020-06-22 10:13:11 -06001473 struct io_ring_ctx *ctx = cs->ctx;
1474
1475 spin_lock_irq(&ctx->completion_lock);
1476 while (!list_empty(&cs->list)) {
1477 struct io_kiocb *req;
1478
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001479 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1480 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001481 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001482 if (!(req->flags & REQ_F_LINK_HEAD)) {
1483 req->flags |= REQ_F_COMP_LOCKED;
1484 io_put_req(req);
1485 } else {
1486 spin_unlock_irq(&ctx->completion_lock);
1487 io_put_req(req);
1488 spin_lock_irq(&ctx->completion_lock);
1489 }
1490 }
1491 io_commit_cqring(ctx);
1492 spin_unlock_irq(&ctx->completion_lock);
1493
1494 io_cqring_ev_posted(ctx);
1495 cs->nr = 0;
1496}
1497
1498static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1499 struct io_comp_state *cs)
1500{
1501 if (!cs) {
1502 io_cqring_add_event(req, res, cflags);
1503 io_put_req(req);
1504 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001505 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001506 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001507 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001508 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001509 if (++cs->nr >= 32)
1510 io_submit_flush_completions(cs);
1511 }
Jens Axboee1e16092020-06-22 09:17:17 -06001512}
1513
1514static void io_req_complete(struct io_kiocb *req, long res)
1515{
Jens Axboe229a7b62020-06-22 10:13:11 -06001516 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001517}
1518
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001519static inline bool io_is_fallback_req(struct io_kiocb *req)
1520{
1521 return req == (struct io_kiocb *)
1522 ((unsigned long) req->ctx->fallback_req & ~1UL);
1523}
1524
1525static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1526{
1527 struct io_kiocb *req;
1528
1529 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001530 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001531 return req;
1532
1533 return NULL;
1534}
1535
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001536static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1537 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538{
Jens Axboefd6fab22019-03-14 16:30:06 -06001539 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001540 struct io_kiocb *req;
1541
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001542 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001543 size_t sz;
1544 int ret;
1545
1546 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001547 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1548
1549 /*
1550 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1551 * retry single alloc to be on the safe side.
1552 */
1553 if (unlikely(ret <= 0)) {
1554 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1555 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001556 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001557 ret = 1;
1558 }
Jens Axboe2579f912019-01-09 09:10:43 -07001559 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001560 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001561 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001562 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001563 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001564 }
1565
Jens Axboe2579f912019-01-09 09:10:43 -07001566 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001567fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001568 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001569}
1570
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001571static inline void io_put_file(struct io_kiocb *req, struct file *file,
1572 bool fixed)
1573{
1574 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001575 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001576 else
1577 fput(file);
1578}
1579
Jens Axboe51a4cc12020-08-10 10:55:56 -06001580static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001581{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001582 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001583
Jens Axboe5acbbc82020-07-08 15:15:26 -06001584 if (req->io)
1585 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001586 if (req->file)
1587 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001588
Jens Axboe51a4cc12020-08-10 10:55:56 -06001589 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001590}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001591
Jens Axboe51a4cc12020-08-10 10:55:56 -06001592static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001593{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001594 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001595
Jens Axboee3bc8e92020-09-24 08:45:57 -06001596 put_task_struct(req->task);
1597
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001598 if (likely(!io_is_fallback_req(req)))
1599 kmem_cache_free(req_cachep, req);
1600 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001601 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1602 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001603}
1604
Jens Axboe51a4cc12020-08-10 10:55:56 -06001605static void io_req_task_file_table_put(struct callback_head *cb)
1606{
1607 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1608 struct fs_struct *fs = req->work.fs;
1609
1610 spin_lock(&req->work.fs->lock);
1611 if (--fs->users)
1612 fs = NULL;
1613 spin_unlock(&req->work.fs->lock);
1614 if (fs)
1615 free_fs_struct(fs);
1616 req->work.fs = NULL;
1617 __io_free_req_finish(req);
1618}
1619
1620static void __io_free_req(struct io_kiocb *req)
1621{
1622 if (!io_dismantle_req(req)) {
1623 __io_free_req_finish(req);
1624 } else {
1625 int ret;
1626
1627 init_task_work(&req->task_work, io_req_task_file_table_put);
1628 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1629 if (unlikely(ret)) {
1630 struct task_struct *tsk;
1631
1632 tsk = io_wq_get_task(req->ctx->io_wq);
1633 task_work_add(tsk, &req->task_work, 0);
1634 }
1635 }
1636}
1637
Jackie Liua197f662019-11-08 08:09:12 -07001638static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001639{
Jackie Liua197f662019-11-08 08:09:12 -07001640 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001641 int ret;
1642
Jens Axboe2d283902019-12-04 11:08:05 -07001643 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001644 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001645 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001646 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001647 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001648 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001649 return true;
1650 }
1651
1652 return false;
1653}
1654
Jens Axboeab0b6452020-06-30 08:43:15 -06001655static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001656{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001657 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001658 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001659
1660 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001661 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001662 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1663 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001664 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001665
1666 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001667 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001668 wake_ev = io_link_cancel_timeout(link);
1669 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001670 return wake_ev;
1671}
1672
1673static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001674{
Jens Axboe2665abf2019-11-05 12:40:47 -07001675 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001676 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001677
Jens Axboeab0b6452020-06-30 08:43:15 -06001678 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1679 unsigned long flags;
1680
1681 spin_lock_irqsave(&ctx->completion_lock, flags);
1682 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001683 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001684 } else {
1685 wake_ev = __io_kill_linked_timeout(req);
1686 }
1687
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001688 if (wake_ev)
1689 io_cqring_ev_posted(ctx);
1690}
1691
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001692static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001693{
1694 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001695
Jens Axboe9e645e112019-05-10 16:07:28 -06001696 /*
1697 * The list should never be empty when we are called here. But could
1698 * potentially happen if the chain is messed up, check to be on the
1699 * safe side.
1700 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001701 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001702 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001703
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001704 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1705 list_del_init(&req->link_list);
1706 if (!list_empty(&nxt->link_list))
1707 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001708 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001709}
1710
1711/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001712 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001713 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001714static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001715{
Jens Axboe2665abf2019-11-05 12:40:47 -07001716 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001717
1718 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001719 struct io_kiocb *link = list_first_entry(&req->link_list,
1720 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001721
Pavel Begunkov44932332019-12-05 16:16:35 +03001722 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001723 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001724
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001725 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001726 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001727 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001728 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001729 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001730
1731 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001732 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001733}
1734
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001735static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001736{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001737 struct io_ring_ctx *ctx = req->ctx;
1738
1739 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1740 unsigned long flags;
1741
1742 spin_lock_irqsave(&ctx->completion_lock, flags);
1743 __io_fail_links(req);
1744 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1745 } else {
1746 __io_fail_links(req);
1747 }
1748
Jens Axboe9e645e112019-05-10 16:07:28 -06001749 io_cqring_ev_posted(ctx);
1750}
1751
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001752static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001753{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001754 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001755 if (req->flags & REQ_F_LINK_TIMEOUT)
1756 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001757
Jens Axboe9e645e112019-05-10 16:07:28 -06001758 /*
1759 * If LINK is set, we have dependent requests in this chain. If we
1760 * didn't fail this request, queue the first one up, moving any other
1761 * dependencies to the next request. In case of failure, fail the rest
1762 * of the chain.
1763 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001764 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1765 return io_req_link_next(req);
1766 io_fail_links(req);
1767 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001768}
Jens Axboe2665abf2019-11-05 12:40:47 -07001769
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001770static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1771{
1772 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1773 return NULL;
1774 return __io_req_find_next(req);
1775}
1776
Jens Axboefd7d6de2020-08-23 11:00:37 -06001777static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1778 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001779{
1780 struct task_struct *tsk = req->task;
1781 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001782 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001783
Jens Axboe6200b0a2020-09-13 14:38:30 -06001784 if (tsk->flags & PF_EXITING)
1785 return -ESRCH;
1786
Jens Axboec2c4c832020-07-01 15:37:11 -06001787 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001788 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1789 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1790 * processing task_work. There's no reliable way to tell if TWA_RESUME
1791 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001792 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001793 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001794 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001795 notify = TWA_SIGNAL;
1796
1797 ret = task_work_add(tsk, cb, notify);
1798 if (!ret)
1799 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001800
Jens Axboec2c4c832020-07-01 15:37:11 -06001801 return ret;
1802}
1803
Jens Axboec40f6372020-06-25 15:39:59 -06001804static void __io_req_task_cancel(struct io_kiocb *req, int error)
1805{
1806 struct io_ring_ctx *ctx = req->ctx;
1807
1808 spin_lock_irq(&ctx->completion_lock);
1809 io_cqring_fill_event(req, error);
1810 io_commit_cqring(ctx);
1811 spin_unlock_irq(&ctx->completion_lock);
1812
1813 io_cqring_ev_posted(ctx);
1814 req_set_fail_links(req);
1815 io_double_put_req(req);
1816}
1817
1818static void io_req_task_cancel(struct callback_head *cb)
1819{
1820 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001821 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001822
1823 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001824 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001825}
1826
1827static void __io_req_task_submit(struct io_kiocb *req)
1828{
1829 struct io_ring_ctx *ctx = req->ctx;
1830
Jens Axboec40f6372020-06-25 15:39:59 -06001831 if (!__io_sq_thread_acquire_mm(ctx)) {
1832 mutex_lock(&ctx->uring_lock);
1833 __io_queue_sqe(req, NULL, NULL);
1834 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001835 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001836 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001837 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001838}
1839
Jens Axboec40f6372020-06-25 15:39:59 -06001840static void io_req_task_submit(struct callback_head *cb)
1841{
1842 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001843 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001844
1845 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001846 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001847}
1848
1849static void io_req_task_queue(struct io_kiocb *req)
1850{
Jens Axboec40f6372020-06-25 15:39:59 -06001851 int ret;
1852
1853 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001854 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001855
Jens Axboefd7d6de2020-08-23 11:00:37 -06001856 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001857 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001858 struct task_struct *tsk;
1859
Jens Axboec40f6372020-06-25 15:39:59 -06001860 init_task_work(&req->task_work, io_req_task_cancel);
1861 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001862 task_work_add(tsk, &req->task_work, 0);
1863 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001864 }
Jens Axboec40f6372020-06-25 15:39:59 -06001865}
1866
Pavel Begunkovc3524382020-06-28 12:52:32 +03001867static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001868{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001869 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001870
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001871 if (nxt)
1872 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001873}
1874
Jens Axboe9e645e112019-05-10 16:07:28 -06001875static void io_free_req(struct io_kiocb *req)
1876{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001877 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001878 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001879}
1880
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001881struct req_batch {
1882 void *reqs[IO_IOPOLL_BATCH];
1883 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001884
1885 struct task_struct *task;
1886 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001887};
1888
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001889static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001890{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001891 rb->to_free = 0;
1892 rb->task_refs = 0;
1893 rb->task = NULL;
1894}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001895
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001896static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1897 struct req_batch *rb)
1898{
1899 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1900 percpu_ref_put_many(&ctx->refs, rb->to_free);
1901 rb->to_free = 0;
1902}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001903
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001904static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1905 struct req_batch *rb)
1906{
1907 if (rb->to_free)
1908 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001909 if (rb->task) {
1910 put_task_struct_many(rb->task, rb->task_refs);
1911 rb->task = NULL;
1912 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001913}
1914
1915static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1916{
1917 if (unlikely(io_is_fallback_req(req))) {
1918 io_free_req(req);
1919 return;
1920 }
1921 if (req->flags & REQ_F_LINK_HEAD)
1922 io_queue_next(req);
1923
Jens Axboee3bc8e92020-09-24 08:45:57 -06001924 if (req->task != rb->task) {
1925 if (rb->task)
1926 put_task_struct_many(rb->task, rb->task_refs);
1927 rb->task = req->task;
1928 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001929 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001930 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001931
Jens Axboe51a4cc12020-08-10 10:55:56 -06001932 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001933 rb->reqs[rb->to_free++] = req;
1934 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1935 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001936}
1937
Jens Axboeba816ad2019-09-28 11:36:45 -06001938/*
1939 * Drop reference to request, return next in chain (if there is one) if this
1940 * was the last reference to this request.
1941 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001942static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001943{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001944 struct io_kiocb *nxt = NULL;
1945
Jens Axboe2a44f462020-02-25 13:25:41 -07001946 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001947 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001948 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001949 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001950 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001951}
1952
Jens Axboe2b188cc2019-01-07 10:46:33 -07001953static void io_put_req(struct io_kiocb *req)
1954{
Jens Axboedef596e2019-01-09 08:59:42 -07001955 if (refcount_dec_and_test(&req->refs))
1956 io_free_req(req);
1957}
1958
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001959static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001960{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001961 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001962
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001963 /*
1964 * A ref is owned by io-wq in which context we're. So, if that's the
1965 * last one, it's safe to steal next work. False negatives are Ok,
1966 * it just will be re-punted async in io_put_work()
1967 */
1968 if (refcount_read(&req->refs) != 1)
1969 return NULL;
1970
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001971 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001972 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001973}
1974
Jens Axboe978db572019-11-14 22:39:04 -07001975/*
1976 * Must only be used if we don't need to care about links, usually from
1977 * within the completion handling itself.
1978 */
1979static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001980{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001981 /* drop both submit and complete references */
1982 if (refcount_sub_and_test(2, &req->refs))
1983 __io_free_req(req);
1984}
1985
Jens Axboe978db572019-11-14 22:39:04 -07001986static void io_double_put_req(struct io_kiocb *req)
1987{
1988 /* drop both submit and complete references */
1989 if (refcount_sub_and_test(2, &req->refs))
1990 io_free_req(req);
1991}
1992
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001993static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001994{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001995 struct io_rings *rings = ctx->rings;
1996
Jens Axboead3eb2c2019-12-18 17:12:20 -07001997 if (test_bit(0, &ctx->cq_check_overflow)) {
1998 /*
1999 * noflush == true is from the waitqueue handler, just ensure
2000 * we wake up the task, and the next invocation will flush the
2001 * entries. We cannot safely to it from here.
2002 */
2003 if (noflush && !list_empty(&ctx->cq_overflow_list))
2004 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002005
Jens Axboee6c8aa92020-09-28 13:10:13 -06002006 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002007 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002008
Jens Axboea3a0e432019-08-20 11:03:11 -06002009 /* See comment at the top of this file */
2010 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002011 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002012}
2013
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002014static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2015{
2016 struct io_rings *rings = ctx->rings;
2017
2018 /* make sure SQ entry isn't read before tail */
2019 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2020}
2021
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002022static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002023{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002024 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002025
Jens Axboebcda7ba2020-02-23 16:42:51 -07002026 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2027 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002028 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002029 kfree(kbuf);
2030 return cflags;
2031}
2032
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002033static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2034{
2035 struct io_buffer *kbuf;
2036
2037 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2038 return io_put_kbuf(req, kbuf);
2039}
2040
Jens Axboe4c6e2772020-07-01 11:29:10 -06002041static inline bool io_run_task_work(void)
2042{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002043 /*
2044 * Not safe to run on exiting task, and the task_work handling will
2045 * not add work to such a task.
2046 */
2047 if (unlikely(current->flags & PF_EXITING))
2048 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002049 if (current->task_works) {
2050 __set_current_state(TASK_RUNNING);
2051 task_work_run();
2052 return true;
2053 }
2054
2055 return false;
2056}
2057
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002058static void io_iopoll_queue(struct list_head *again)
2059{
2060 struct io_kiocb *req;
2061
2062 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002063 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2064 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002065 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002066 } while (!list_empty(again));
2067}
2068
Jens Axboedef596e2019-01-09 08:59:42 -07002069/*
2070 * Find and free completed poll iocbs
2071 */
2072static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2073 struct list_head *done)
2074{
Jens Axboe8237e042019-12-28 10:48:22 -07002075 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002076 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002077 LIST_HEAD(again);
2078
2079 /* order with ->result store in io_complete_rw_iopoll() */
2080 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002081
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002082 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002083 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002084 int cflags = 0;
2085
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002086 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002087 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002088 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002089 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002090 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002091 continue;
2092 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002093 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002094
Jens Axboebcda7ba2020-02-23 16:42:51 -07002095 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002096 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002097
2098 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002099 (*nr_events)++;
2100
Pavel Begunkovc3524382020-06-28 12:52:32 +03002101 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002102 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002103 }
Jens Axboedef596e2019-01-09 08:59:42 -07002104
Jens Axboe09bb8392019-03-13 12:39:28 -06002105 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002106 if (ctx->flags & IORING_SETUP_SQPOLL)
2107 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002108 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002109
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002110 if (!list_empty(&again))
2111 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002112}
2113
Jens Axboedef596e2019-01-09 08:59:42 -07002114static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2115 long min)
2116{
2117 struct io_kiocb *req, *tmp;
2118 LIST_HEAD(done);
2119 bool spin;
2120 int ret;
2121
2122 /*
2123 * Only spin for completions if we don't have multiple devices hanging
2124 * off our complete list, and we're under the requested amount.
2125 */
2126 spin = !ctx->poll_multi_file && *nr_events < min;
2127
2128 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002129 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002130 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002131
2132 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002133 * Move completed and retryable entries to our local lists.
2134 * If we find a request that requires polling, break out
2135 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002136 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002137 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002138 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002139 continue;
2140 }
2141 if (!list_empty(&done))
2142 break;
2143
2144 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2145 if (ret < 0)
2146 break;
2147
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002148 /* iopoll may have completed current req */
2149 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002150 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002151
Jens Axboedef596e2019-01-09 08:59:42 -07002152 if (ret && spin)
2153 spin = false;
2154 ret = 0;
2155 }
2156
2157 if (!list_empty(&done))
2158 io_iopoll_complete(ctx, nr_events, &done);
2159
2160 return ret;
2161}
2162
2163/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002164 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002165 * non-spinning poll check - we'll still enter the driver poll loop, but only
2166 * as a non-spinning completion check.
2167 */
2168static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2169 long min)
2170{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002171 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002172 int ret;
2173
2174 ret = io_do_iopoll(ctx, nr_events, min);
2175 if (ret < 0)
2176 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002177 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002178 return 0;
2179 }
2180
2181 return 1;
2182}
2183
2184/*
2185 * We can't just wait for polled events to come to us, we have to actively
2186 * find and complete them.
2187 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002188static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002189{
2190 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2191 return;
2192
2193 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002194 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002195 unsigned int nr_events = 0;
2196
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002197 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002198
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002199 /* let it sleep and repeat later if can't complete a request */
2200 if (nr_events == 0)
2201 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002202 /*
2203 * Ensure we allow local-to-the-cpu processing to take place,
2204 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002205 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002206 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002207 if (need_resched()) {
2208 mutex_unlock(&ctx->uring_lock);
2209 cond_resched();
2210 mutex_lock(&ctx->uring_lock);
2211 }
Jens Axboedef596e2019-01-09 08:59:42 -07002212 }
2213 mutex_unlock(&ctx->uring_lock);
2214}
2215
Pavel Begunkov7668b922020-07-07 16:36:21 +03002216static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002217{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002218 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002219 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002220
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002221 /*
2222 * We disallow the app entering submit/complete with polling, but we
2223 * still need to lock the ring to prevent racing with polled issue
2224 * that got punted to a workqueue.
2225 */
2226 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002227 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002228 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002229 * Don't enter poll loop if we already have events pending.
2230 * If we do, we can potentially be spinning for commands that
2231 * already triggered a CQE (eg in error).
2232 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002233 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002234 break;
2235
2236 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002237 * If a submit got punted to a workqueue, we can have the
2238 * application entering polling for a command before it gets
2239 * issued. That app will hold the uring_lock for the duration
2240 * of the poll right here, so we need to take a breather every
2241 * now and then to ensure that the issue has a chance to add
2242 * the poll to the issued list. Otherwise we can spin here
2243 * forever, while the workqueue is stuck trying to acquire the
2244 * very same mutex.
2245 */
2246 if (!(++iters & 7)) {
2247 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002248 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002249 mutex_lock(&ctx->uring_lock);
2250 }
2251
Pavel Begunkov7668b922020-07-07 16:36:21 +03002252 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002253 if (ret <= 0)
2254 break;
2255 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002256 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002257
Jens Axboe500f9fb2019-08-19 12:15:59 -06002258 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002259 return ret;
2260}
2261
Jens Axboe491381ce2019-10-17 09:20:46 -06002262static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002263{
Jens Axboe491381ce2019-10-17 09:20:46 -06002264 /*
2265 * Tell lockdep we inherited freeze protection from submission
2266 * thread.
2267 */
2268 if (req->flags & REQ_F_ISREG) {
2269 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002270
Jens Axboe491381ce2019-10-17 09:20:46 -06002271 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002272 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002273 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002274}
2275
Jens Axboea1d7c392020-06-22 11:09:46 -06002276static void io_complete_rw_common(struct kiocb *kiocb, long res,
2277 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002278{
Jens Axboe9adbd452019-12-20 08:45:55 -07002279 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002280 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281
Jens Axboe491381ce2019-10-17 09:20:46 -06002282 if (kiocb->ki_flags & IOCB_WRITE)
2283 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002284
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002285 if (res != req->result)
2286 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002287 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002288 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002289 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002290}
2291
Jens Axboeb63534c2020-06-04 11:28:00 -06002292#ifdef CONFIG_BLOCK
2293static bool io_resubmit_prep(struct io_kiocb *req, int error)
2294{
2295 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2296 ssize_t ret = -ECANCELED;
2297 struct iov_iter iter;
2298 int rw;
2299
2300 if (error) {
2301 ret = error;
2302 goto end_req;
2303 }
2304
2305 switch (req->opcode) {
2306 case IORING_OP_READV:
2307 case IORING_OP_READ_FIXED:
2308 case IORING_OP_READ:
2309 rw = READ;
2310 break;
2311 case IORING_OP_WRITEV:
2312 case IORING_OP_WRITE_FIXED:
2313 case IORING_OP_WRITE:
2314 rw = WRITE;
2315 break;
2316 default:
2317 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2318 req->opcode);
2319 goto end_req;
2320 }
2321
Jens Axboe8f3d7492020-09-14 09:28:14 -06002322 if (!req->io) {
2323 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2324 if (ret < 0)
2325 goto end_req;
2326 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2327 if (!ret)
2328 return true;
2329 kfree(iovec);
2330 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002331 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002332 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002333end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002334 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002335 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002336 return false;
2337}
Jens Axboeb63534c2020-06-04 11:28:00 -06002338#endif
2339
2340static bool io_rw_reissue(struct io_kiocb *req, long res)
2341{
2342#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002343 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002344 int ret;
2345
Jens Axboe355afae2020-09-02 09:30:31 -06002346 if (!S_ISBLK(mode) && !S_ISREG(mode))
2347 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002348 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2349 return false;
2350
Jens Axboefdee9462020-08-27 16:46:24 -06002351 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002352
Jens Axboefdee9462020-08-27 16:46:24 -06002353 if (io_resubmit_prep(req, ret)) {
2354 refcount_inc(&req->refs);
2355 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002356 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002357 }
2358
Jens Axboeb63534c2020-06-04 11:28:00 -06002359#endif
2360 return false;
2361}
2362
Jens Axboea1d7c392020-06-22 11:09:46 -06002363static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2364 struct io_comp_state *cs)
2365{
2366 if (!io_rw_reissue(req, res))
2367 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002368}
2369
2370static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2371{
Jens Axboe9adbd452019-12-20 08:45:55 -07002372 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002373
Jens Axboea1d7c392020-06-22 11:09:46 -06002374 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002375}
2376
Jens Axboedef596e2019-01-09 08:59:42 -07002377static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2378{
Jens Axboe9adbd452019-12-20 08:45:55 -07002379 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002380
Jens Axboe491381ce2019-10-17 09:20:46 -06002381 if (kiocb->ki_flags & IOCB_WRITE)
2382 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002383
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002384 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002385 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002386
2387 WRITE_ONCE(req->result, res);
2388 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002389 smp_wmb();
2390 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002391}
2392
2393/*
2394 * After the iocb has been issued, it's safe to be found on the poll list.
2395 * Adding the kiocb to the list AFTER submission ensures that we don't
2396 * find it from a io_iopoll_getevents() thread before the issuer is done
2397 * accessing the kiocb cookie.
2398 */
2399static void io_iopoll_req_issued(struct io_kiocb *req)
2400{
2401 struct io_ring_ctx *ctx = req->ctx;
2402
2403 /*
2404 * Track whether we have multiple files in our lists. This will impact
2405 * how we do polling eventually, not spinning if we're on potentially
2406 * different devices.
2407 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002408 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002409 ctx->poll_multi_file = false;
2410 } else if (!ctx->poll_multi_file) {
2411 struct io_kiocb *list_req;
2412
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002413 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002414 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002415 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002416 ctx->poll_multi_file = true;
2417 }
2418
2419 /*
2420 * For fast devices, IO may have already completed. If it has, add
2421 * it to the front so we find it first.
2422 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002423 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002424 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002425 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002426 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002427
2428 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2429 wq_has_sleeper(&ctx->sqo_wait))
2430 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002431}
2432
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002433static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002434{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002435 if (state->has_refs)
2436 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002437 state->file = NULL;
2438}
2439
2440static inline void io_state_file_put(struct io_submit_state *state)
2441{
2442 if (state->file)
2443 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002444}
2445
2446/*
2447 * Get as many references to a file as we have IOs left in this submission,
2448 * assuming most submissions are for one file, or at least that each file
2449 * has more than one submission.
2450 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002451static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002452{
2453 if (!state)
2454 return fget(fd);
2455
2456 if (state->file) {
2457 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002458 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002459 state->ios_left--;
2460 return state->file;
2461 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002462 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002463 }
2464 state->file = fget_many(fd, state->ios_left);
2465 if (!state->file)
2466 return NULL;
2467
2468 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002469 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002470 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002471 return state->file;
2472}
2473
Jens Axboe4503b762020-06-01 10:00:27 -06002474static bool io_bdev_nowait(struct block_device *bdev)
2475{
2476#ifdef CONFIG_BLOCK
2477 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2478#else
2479 return true;
2480#endif
2481}
2482
Jens Axboe2b188cc2019-01-07 10:46:33 -07002483/*
2484 * If we tracked the file through the SCM inflight mechanism, we could support
2485 * any file. For now, just ensure that anything potentially problematic is done
2486 * inline.
2487 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002488static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002489{
2490 umode_t mode = file_inode(file)->i_mode;
2491
Jens Axboe4503b762020-06-01 10:00:27 -06002492 if (S_ISBLK(mode)) {
2493 if (io_bdev_nowait(file->f_inode->i_bdev))
2494 return true;
2495 return false;
2496 }
2497 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002498 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002499 if (S_ISREG(mode)) {
2500 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2501 file->f_op != &io_uring_fops)
2502 return true;
2503 return false;
2504 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002505
Jens Axboec5b85622020-06-09 19:23:05 -06002506 /* any ->read/write should understand O_NONBLOCK */
2507 if (file->f_flags & O_NONBLOCK)
2508 return true;
2509
Jens Axboeaf197f52020-04-28 13:15:06 -06002510 if (!(file->f_mode & FMODE_NOWAIT))
2511 return false;
2512
2513 if (rw == READ)
2514 return file->f_op->read_iter != NULL;
2515
2516 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002517}
2518
Jens Axboe3529d8c2019-12-19 18:24:38 -07002519static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2520 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002521{
Jens Axboedef596e2019-01-09 08:59:42 -07002522 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002523 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002524 unsigned ioprio;
2525 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002526
Jens Axboe491381ce2019-10-17 09:20:46 -06002527 if (S_ISREG(file_inode(req->file)->i_mode))
2528 req->flags |= REQ_F_ISREG;
2529
Jens Axboe2b188cc2019-01-07 10:46:33 -07002530 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002531 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2532 req->flags |= REQ_F_CUR_POS;
2533 kiocb->ki_pos = req->file->f_pos;
2534 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002535 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002536 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2537 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2538 if (unlikely(ret))
2539 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540
2541 ioprio = READ_ONCE(sqe->ioprio);
2542 if (ioprio) {
2543 ret = ioprio_check_cap(ioprio);
2544 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002545 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002546
2547 kiocb->ki_ioprio = ioprio;
2548 } else
2549 kiocb->ki_ioprio = get_current_ioprio();
2550
Stefan Bühler8449eed2019-04-27 20:34:19 +02002551 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002552 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002553 req->flags |= REQ_F_NOWAIT;
2554
2555 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002556 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002557
Jens Axboedef596e2019-01-09 08:59:42 -07002558 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002559 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2560 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002561 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002562
Jens Axboedef596e2019-01-09 08:59:42 -07002563 kiocb->ki_flags |= IOCB_HIPRI;
2564 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002565 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002566 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002567 if (kiocb->ki_flags & IOCB_HIPRI)
2568 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002569 kiocb->ki_complete = io_complete_rw;
2570 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002571
Jens Axboe3529d8c2019-12-19 18:24:38 -07002572 req->rw.addr = READ_ONCE(sqe->addr);
2573 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002574 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002576}
2577
2578static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2579{
2580 switch (ret) {
2581 case -EIOCBQUEUED:
2582 break;
2583 case -ERESTARTSYS:
2584 case -ERESTARTNOINTR:
2585 case -ERESTARTNOHAND:
2586 case -ERESTART_RESTARTBLOCK:
2587 /*
2588 * We can't just restart the syscall, since previously
2589 * submitted sqes may already be in progress. Just fail this
2590 * IO with EINTR.
2591 */
2592 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002593 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002594 default:
2595 kiocb->ki_complete(kiocb, ret, 0);
2596 }
2597}
2598
Jens Axboea1d7c392020-06-22 11:09:46 -06002599static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2600 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002601{
Jens Axboeba042912019-12-25 16:33:42 -07002602 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2603
Jens Axboe227c0c92020-08-13 11:51:40 -06002604 /* add previously done IO, if any */
2605 if (req->io && req->io->rw.bytes_done > 0) {
2606 if (ret < 0)
2607 ret = req->io->rw.bytes_done;
2608 else
2609 ret += req->io->rw.bytes_done;
2610 }
2611
Jens Axboeba042912019-12-25 16:33:42 -07002612 if (req->flags & REQ_F_CUR_POS)
2613 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002614 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002615 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002616 else
2617 io_rw_done(kiocb, ret);
2618}
2619
Jens Axboe9adbd452019-12-20 08:45:55 -07002620static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002621 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002622{
Jens Axboe9adbd452019-12-20 08:45:55 -07002623 struct io_ring_ctx *ctx = req->ctx;
2624 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002625 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002626 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002627 size_t offset;
2628 u64 buf_addr;
2629
2630 /* attempt to use fixed buffers without having provided iovecs */
2631 if (unlikely(!ctx->user_bufs))
2632 return -EFAULT;
2633
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002634 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002635 if (unlikely(buf_index >= ctx->nr_user_bufs))
2636 return -EFAULT;
2637
2638 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2639 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002640 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002641
2642 /* overflow */
2643 if (buf_addr + len < buf_addr)
2644 return -EFAULT;
2645 /* not inside the mapped region */
2646 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2647 return -EFAULT;
2648
2649 /*
2650 * May not be a start of buffer, set size appropriately
2651 * and advance us to the beginning.
2652 */
2653 offset = buf_addr - imu->ubuf;
2654 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002655
2656 if (offset) {
2657 /*
2658 * Don't use iov_iter_advance() here, as it's really slow for
2659 * using the latter parts of a big fixed buffer - it iterates
2660 * over each segment manually. We can cheat a bit here, because
2661 * we know that:
2662 *
2663 * 1) it's a BVEC iter, we set it up
2664 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2665 * first and last bvec
2666 *
2667 * So just find our index, and adjust the iterator afterwards.
2668 * If the offset is within the first bvec (or the whole first
2669 * bvec, just use iov_iter_advance(). This makes it easier
2670 * since we can just skip the first segment, which may not
2671 * be PAGE_SIZE aligned.
2672 */
2673 const struct bio_vec *bvec = imu->bvec;
2674
2675 if (offset <= bvec->bv_len) {
2676 iov_iter_advance(iter, offset);
2677 } else {
2678 unsigned long seg_skip;
2679
2680 /* skip first vec */
2681 offset -= bvec->bv_len;
2682 seg_skip = 1 + (offset >> PAGE_SHIFT);
2683
2684 iter->bvec = bvec + seg_skip;
2685 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002686 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002687 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002688 }
2689 }
2690
Jens Axboe5e559562019-11-13 16:12:46 -07002691 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002692}
2693
Jens Axboebcda7ba2020-02-23 16:42:51 -07002694static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2695{
2696 if (needs_lock)
2697 mutex_unlock(&ctx->uring_lock);
2698}
2699
2700static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2701{
2702 /*
2703 * "Normal" inline submissions always hold the uring_lock, since we
2704 * grab it from the system call. Same is true for the SQPOLL offload.
2705 * The only exception is when we've detached the request and issue it
2706 * from an async worker thread, grab the lock for that case.
2707 */
2708 if (needs_lock)
2709 mutex_lock(&ctx->uring_lock);
2710}
2711
2712static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2713 int bgid, struct io_buffer *kbuf,
2714 bool needs_lock)
2715{
2716 struct io_buffer *head;
2717
2718 if (req->flags & REQ_F_BUFFER_SELECTED)
2719 return kbuf;
2720
2721 io_ring_submit_lock(req->ctx, needs_lock);
2722
2723 lockdep_assert_held(&req->ctx->uring_lock);
2724
2725 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2726 if (head) {
2727 if (!list_empty(&head->list)) {
2728 kbuf = list_last_entry(&head->list, struct io_buffer,
2729 list);
2730 list_del(&kbuf->list);
2731 } else {
2732 kbuf = head;
2733 idr_remove(&req->ctx->io_buffer_idr, bgid);
2734 }
2735 if (*len > kbuf->len)
2736 *len = kbuf->len;
2737 } else {
2738 kbuf = ERR_PTR(-ENOBUFS);
2739 }
2740
2741 io_ring_submit_unlock(req->ctx, needs_lock);
2742
2743 return kbuf;
2744}
2745
Jens Axboe4d954c22020-02-27 07:31:19 -07002746static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2747 bool needs_lock)
2748{
2749 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002750 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002751
2752 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002753 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002754 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2755 if (IS_ERR(kbuf))
2756 return kbuf;
2757 req->rw.addr = (u64) (unsigned long) kbuf;
2758 req->flags |= REQ_F_BUFFER_SELECTED;
2759 return u64_to_user_ptr(kbuf->addr);
2760}
2761
2762#ifdef CONFIG_COMPAT
2763static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2764 bool needs_lock)
2765{
2766 struct compat_iovec __user *uiov;
2767 compat_ssize_t clen;
2768 void __user *buf;
2769 ssize_t len;
2770
2771 uiov = u64_to_user_ptr(req->rw.addr);
2772 if (!access_ok(uiov, sizeof(*uiov)))
2773 return -EFAULT;
2774 if (__get_user(clen, &uiov->iov_len))
2775 return -EFAULT;
2776 if (clen < 0)
2777 return -EINVAL;
2778
2779 len = clen;
2780 buf = io_rw_buffer_select(req, &len, needs_lock);
2781 if (IS_ERR(buf))
2782 return PTR_ERR(buf);
2783 iov[0].iov_base = buf;
2784 iov[0].iov_len = (compat_size_t) len;
2785 return 0;
2786}
2787#endif
2788
2789static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2790 bool needs_lock)
2791{
2792 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2793 void __user *buf;
2794 ssize_t len;
2795
2796 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2797 return -EFAULT;
2798
2799 len = iov[0].iov_len;
2800 if (len < 0)
2801 return -EINVAL;
2802 buf = io_rw_buffer_select(req, &len, needs_lock);
2803 if (IS_ERR(buf))
2804 return PTR_ERR(buf);
2805 iov[0].iov_base = buf;
2806 iov[0].iov_len = len;
2807 return 0;
2808}
2809
2810static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2811 bool needs_lock)
2812{
Jens Axboedddb3e22020-06-04 11:27:01 -06002813 if (req->flags & REQ_F_BUFFER_SELECTED) {
2814 struct io_buffer *kbuf;
2815
2816 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2817 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2818 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002819 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002820 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002821 if (!req->rw.len)
2822 return 0;
2823 else if (req->rw.len > 1)
2824 return -EINVAL;
2825
2826#ifdef CONFIG_COMPAT
2827 if (req->ctx->compat)
2828 return io_compat_import(req, iov, needs_lock);
2829#endif
2830
2831 return __io_iov_buffer_select(req, iov, needs_lock);
2832}
2833
Jens Axboe8452fd02020-08-18 13:58:33 -07002834static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2835 struct iovec **iovec, struct iov_iter *iter,
2836 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002837{
Jens Axboe9adbd452019-12-20 08:45:55 -07002838 void __user *buf = u64_to_user_ptr(req->rw.addr);
2839 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002840 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002841 u8 opcode;
2842
Jens Axboed625c6e2019-12-17 19:53:05 -07002843 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002844 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002845 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002846 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002847 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848
Jens Axboebcda7ba2020-02-23 16:42:51 -07002849 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002850 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002851 return -EINVAL;
2852
Jens Axboe3a6820f2019-12-22 15:19:35 -07002853 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002854 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002855 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002856 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002857 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002858 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002859 }
2860
Jens Axboe3a6820f2019-12-22 15:19:35 -07002861 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2862 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002863 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002864 }
2865
Jens Axboe4d954c22020-02-27 07:31:19 -07002866 if (req->flags & REQ_F_BUFFER_SELECT) {
2867 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002868 if (!ret) {
2869 ret = (*iovec)->iov_len;
2870 iov_iter_init(iter, rw, *iovec, 1, ret);
2871 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002872 *iovec = NULL;
2873 return ret;
2874 }
2875
Jens Axboe2b188cc2019-01-07 10:46:33 -07002876#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002877 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002878 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2879 iovec, iter);
2880#endif
2881
2882 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2883}
2884
Jens Axboe8452fd02020-08-18 13:58:33 -07002885static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2886 struct iovec **iovec, struct iov_iter *iter,
2887 bool needs_lock)
2888{
2889 if (!req->io)
2890 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2891 *iovec = NULL;
2892 return iov_iter_count(&req->io->rw.iter);
2893}
2894
Jens Axboe0fef9482020-08-26 10:36:20 -06002895static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2896{
2897 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2898}
2899
Jens Axboe32960612019-09-23 11:05:34 -06002900/*
2901 * For files that don't have ->read_iter() and ->write_iter(), handle them
2902 * by looping over ->read() or ->write() manually.
2903 */
2904static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2905 struct iov_iter *iter)
2906{
2907 ssize_t ret = 0;
2908
2909 /*
2910 * Don't support polled IO through this interface, and we can't
2911 * support non-blocking either. For the latter, this just causes
2912 * the kiocb to be handled from an async context.
2913 */
2914 if (kiocb->ki_flags & IOCB_HIPRI)
2915 return -EOPNOTSUPP;
2916 if (kiocb->ki_flags & IOCB_NOWAIT)
2917 return -EAGAIN;
2918
2919 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002920 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002921 ssize_t nr;
2922
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002923 if (!iov_iter_is_bvec(iter)) {
2924 iovec = iov_iter_iovec(iter);
2925 } else {
2926 /* fixed buffers import bvec */
2927 iovec.iov_base = kmap(iter->bvec->bv_page)
2928 + iter->iov_offset;
2929 iovec.iov_len = min(iter->count,
2930 iter->bvec->bv_len - iter->iov_offset);
2931 }
2932
Jens Axboe32960612019-09-23 11:05:34 -06002933 if (rw == READ) {
2934 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002935 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002936 } else {
2937 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002938 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002939 }
2940
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002941 if (iov_iter_is_bvec(iter))
2942 kunmap(iter->bvec->bv_page);
2943
Jens Axboe32960612019-09-23 11:05:34 -06002944 if (nr < 0) {
2945 if (!ret)
2946 ret = nr;
2947 break;
2948 }
2949 ret += nr;
2950 if (nr != iovec.iov_len)
2951 break;
2952 iov_iter_advance(iter, nr);
2953 }
2954
2955 return ret;
2956}
2957
Jens Axboeff6165b2020-08-13 09:47:43 -06002958static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2959 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07002960{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002961 struct io_async_rw *rw = &req->io->rw;
2962
Jens Axboeff6165b2020-08-13 09:47:43 -06002963 memcpy(&rw->iter, iter, sizeof(*iter));
2964 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06002965 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06002966 /* can only be fixed buffers, no need to do anything */
2967 if (iter->type == ITER_BVEC)
2968 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002969 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06002970 unsigned iov_off = 0;
2971
2972 rw->iter.iov = rw->fast_iov;
2973 if (iter->iov != fast_iov) {
2974 iov_off = iter->iov - fast_iov;
2975 rw->iter.iov += iov_off;
2976 }
2977 if (rw->fast_iov != fast_iov)
2978 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002979 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002980 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06002981 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002982 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002983 }
2984}
2985
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002986static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2987{
2988 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2989 return req->io == NULL;
2990}
2991
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002992static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002993{
Jens Axboed3656342019-12-18 09:50:26 -07002994 if (!io_op_defs[req->opcode].async_ctx)
2995 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002996
2997 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002998}
2999
Jens Axboeff6165b2020-08-13 09:47:43 -06003000static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3001 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003002 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003003{
Jens Axboe227c0c92020-08-13 11:51:40 -06003004 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003005 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003006 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003007 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003008 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003009
Jens Axboeff6165b2020-08-13 09:47:43 -06003010 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003011 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003012 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003013}
3014
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003015static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3016 bool force_nonblock)
3017{
Jens Axboeff6165b2020-08-13 09:47:43 -06003018 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06003019 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003020 ssize_t ret;
3021
Jens Axboec183edf2020-09-04 22:36:52 -06003022 iorw->iter.iov = iov = iorw->fast_iov;
3023 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003024 if (unlikely(ret < 0))
3025 return ret;
3026
Jens Axboec183edf2020-09-04 22:36:52 -06003027 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003028 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003029 return 0;
3030}
3031
Jens Axboe3529d8c2019-12-19 18:24:38 -07003032static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3033 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003034{
3035 ssize_t ret;
3036
Jens Axboe3529d8c2019-12-19 18:24:38 -07003037 ret = io_prep_rw(req, sqe, force_nonblock);
3038 if (ret)
3039 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003040
Jens Axboe3529d8c2019-12-19 18:24:38 -07003041 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3042 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003043
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003044 /* either don't need iovec imported or already have it */
3045 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003046 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003047 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003048}
3049
Jens Axboec1dd91d2020-08-03 16:43:59 -06003050/*
3051 * This is our waitqueue callback handler, registered through lock_page_async()
3052 * when we initially tried to do the IO with the iocb armed our waitqueue.
3053 * This gets called when the page is unlocked, and we generally expect that to
3054 * happen when the page IO is completed and the page is now uptodate. This will
3055 * queue a task_work based retry of the operation, attempting to copy the data
3056 * again. If the latter fails because the page was NOT uptodate, then we will
3057 * do a thread based blocking retry of the operation. That's the unexpected
3058 * slow path.
3059 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003060static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3061 int sync, void *arg)
3062{
3063 struct wait_page_queue *wpq;
3064 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003065 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003066 int ret;
3067
3068 wpq = container_of(wait, struct wait_page_queue, wait);
3069
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003070 if (!wake_page_match(wpq, key))
3071 return 0;
3072
Hao Xuc8d317a2020-09-29 20:00:45 +08003073 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003074 list_del_init(&wait->entry);
3075
Pavel Begunkove7375122020-07-12 20:42:04 +03003076 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003077 percpu_ref_get(&req->ctx->refs);
3078
Jens Axboebcf5a062020-05-22 09:24:42 -06003079 /* submit ref gets dropped, acquire a new one */
3080 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003081 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003082 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003083 struct task_struct *tsk;
3084
Jens Axboebcf5a062020-05-22 09:24:42 -06003085 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003086 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003087 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003088 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003089 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003090 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003091 return 1;
3092}
3093
Jens Axboec1dd91d2020-08-03 16:43:59 -06003094/*
3095 * This controls whether a given IO request should be armed for async page
3096 * based retry. If we return false here, the request is handed to the async
3097 * worker threads for retry. If we're doing buffered reads on a regular file,
3098 * we prepare a private wait_page_queue entry and retry the operation. This
3099 * will either succeed because the page is now uptodate and unlocked, or it
3100 * will register a callback when the page is unlocked at IO completion. Through
3101 * that callback, io_uring uses task_work to setup a retry of the operation.
3102 * That retry will attempt the buffered read again. The retry will generally
3103 * succeed, or in rare cases where it fails, we then fall back to using the
3104 * async worker threads for a blocking retry.
3105 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003106static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003107{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003108 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003109 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003110
3111 /* never retry for NOWAIT, we just complete with -EAGAIN */
3112 if (req->flags & REQ_F_NOWAIT)
3113 return false;
3114
Jens Axboe227c0c92020-08-13 11:51:40 -06003115 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003116 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003117 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003118
Jens Axboebcf5a062020-05-22 09:24:42 -06003119 /*
3120 * just use poll if we can, and don't attempt if the fs doesn't
3121 * support callback based unlocks
3122 */
3123 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3124 return false;
3125
Jens Axboe3b2a4432020-08-16 10:58:43 -07003126 wait->wait.func = io_async_buf_func;
3127 wait->wait.private = req;
3128 wait->wait.flags = 0;
3129 INIT_LIST_HEAD(&wait->wait.entry);
3130 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003131 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003132 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003133 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003134}
3135
3136static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3137{
3138 if (req->file->f_op->read_iter)
3139 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003140 else if (req->file->f_op->read)
3141 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3142 else
3143 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003144}
3145
Jens Axboea1d7c392020-06-22 11:09:46 -06003146static int io_read(struct io_kiocb *req, bool force_nonblock,
3147 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148{
3149 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003150 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003151 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003152 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003153 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003154 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003155
Jens Axboeff6165b2020-08-13 09:47:43 -06003156 if (req->io)
3157 iter = &req->io->rw.iter;
3158
3159 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003160 if (ret < 0)
3161 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003162 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003163 io_size = ret;
3164 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003165 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003166
Jens Axboefd6c2e42019-12-18 12:19:41 -07003167 /* Ensure we clear previously set non-block flag */
3168 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003169 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003170
Pavel Begunkov24c74672020-06-21 13:09:51 +03003171 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003172 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3173 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003174 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003175
Jens Axboe0fef9482020-08-26 10:36:20 -06003176 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003177 if (unlikely(ret))
3178 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003179
Jens Axboe227c0c92020-08-13 11:51:40 -06003180 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003181
Jens Axboe227c0c92020-08-13 11:51:40 -06003182 if (!ret) {
3183 goto done;
3184 } else if (ret == -EIOCBQUEUED) {
3185 ret = 0;
3186 goto out_free;
3187 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003188 /* IOPOLL retry should happen for io-wq threads */
3189 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003190 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003191 /* no retry on NONBLOCK marked file */
3192 if (req->file->f_flags & O_NONBLOCK)
3193 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003194 /* some cases will consume bytes even on error returns */
3195 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003196 ret = 0;
3197 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003198 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003199 /* make sure -ERESTARTSYS -> -EINTR is done */
3200 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003201 }
3202
3203 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003204 if (!iov_iter_count(iter) || !force_nonblock ||
3205 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003206 goto done;
3207
3208 io_size -= ret;
3209copy_iov:
3210 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3211 if (ret2) {
3212 ret = ret2;
3213 goto out_free;
3214 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003215 if (no_async)
3216 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003217 /* it's copied and will be cleaned with ->io */
3218 iovec = NULL;
3219 /* now use our persistent iterator, if we aren't already */
3220 iter = &req->io->rw.iter;
3221retry:
3222 req->io->rw.bytes_done += ret;
3223 /* if we can retry, do so with the callbacks armed */
3224 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003225 kiocb->ki_flags &= ~IOCB_WAITQ;
3226 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003227 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003228
3229 /*
3230 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3231 * get -EIOCBQUEUED, then we'll get a notification when the desired
3232 * page gets unlocked. We can also get a partial read here, and if we
3233 * do, then just retry at the new offset.
3234 */
3235 ret = io_iter_do_read(req, iter);
3236 if (ret == -EIOCBQUEUED) {
3237 ret = 0;
3238 goto out_free;
3239 } else if (ret > 0 && ret < io_size) {
3240 /* we got some bytes, but not all. retry. */
3241 goto retry;
3242 }
3243done:
3244 kiocb_done(kiocb, ret, cs);
3245 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003246out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003247 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003248 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003249 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003250 return ret;
3251}
3252
Jens Axboe3529d8c2019-12-19 18:24:38 -07003253static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3254 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003255{
3256 ssize_t ret;
3257
Jens Axboe3529d8c2019-12-19 18:24:38 -07003258 ret = io_prep_rw(req, sqe, force_nonblock);
3259 if (ret)
3260 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003261
Jens Axboe3529d8c2019-12-19 18:24:38 -07003262 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3263 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003264
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003265 /* either don't need iovec imported or already have it */
3266 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003267 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003268 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003269}
3270
Jens Axboea1d7c392020-06-22 11:09:46 -06003271static int io_write(struct io_kiocb *req, bool force_nonblock,
3272 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003273{
3274 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003275 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003276 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003277 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003278 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003279
Jens Axboeff6165b2020-08-13 09:47:43 -06003280 if (req->io)
3281 iter = &req->io->rw.iter;
3282
3283 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003284 if (ret < 0)
3285 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003286 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003287 io_size = ret;
3288 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003289
Jens Axboefd6c2e42019-12-18 12:19:41 -07003290 /* Ensure we clear previously set non-block flag */
3291 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003292 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003293
Pavel Begunkov24c74672020-06-21 13:09:51 +03003294 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003295 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003296 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003297
Jens Axboe10d59342019-12-09 20:16:22 -07003298 /* file path doesn't support NOWAIT for non-direct_IO */
3299 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3300 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003301 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003302
Jens Axboe0fef9482020-08-26 10:36:20 -06003303 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003304 if (unlikely(ret))
3305 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003306
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003307 /*
3308 * Open-code file_start_write here to grab freeze protection,
3309 * which will be released by another thread in
3310 * io_complete_rw(). Fool lockdep by telling it the lock got
3311 * released so that it doesn't complain about the held lock when
3312 * we return to userspace.
3313 */
3314 if (req->flags & REQ_F_ISREG) {
3315 __sb_start_write(file_inode(req->file)->i_sb,
3316 SB_FREEZE_WRITE, true);
3317 __sb_writers_release(file_inode(req->file)->i_sb,
3318 SB_FREEZE_WRITE);
3319 }
3320 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003321
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003322 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003323 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003324 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003325 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003326 else
3327 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003328
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003329 /*
3330 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3331 * retry them without IOCB_NOWAIT.
3332 */
3333 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3334 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003335 /* no retry on NONBLOCK marked file */
3336 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3337 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003338 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003339 /* IOPOLL retry should happen for io-wq threads */
3340 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3341 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003342done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003343 kiocb_done(kiocb, ret2, cs);
3344 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003345copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003346 /* some cases will consume bytes even on error returns */
3347 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003348 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003349 if (!ret)
3350 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003351 }
Jens Axboe31b51512019-01-18 22:56:34 -07003352out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003353 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003354 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003355 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003356 return ret;
3357}
3358
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003359static int __io_splice_prep(struct io_kiocb *req,
3360 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003361{
3362 struct io_splice* sp = &req->splice;
3363 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3364 int ret;
3365
3366 if (req->flags & REQ_F_NEED_CLEANUP)
3367 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003368 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3369 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003370
3371 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003372 sp->len = READ_ONCE(sqe->len);
3373 sp->flags = READ_ONCE(sqe->splice_flags);
3374
3375 if (unlikely(sp->flags & ~valid_flags))
3376 return -EINVAL;
3377
3378 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3379 (sp->flags & SPLICE_F_FD_IN_FIXED));
3380 if (ret)
3381 return ret;
3382 req->flags |= REQ_F_NEED_CLEANUP;
3383
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003384 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3385 /*
3386 * Splice operation will be punted aync, and here need to
3387 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3388 */
3389 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003390 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003391 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003392
3393 return 0;
3394}
3395
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003396static int io_tee_prep(struct io_kiocb *req,
3397 const struct io_uring_sqe *sqe)
3398{
3399 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3400 return -EINVAL;
3401 return __io_splice_prep(req, sqe);
3402}
3403
3404static int io_tee(struct io_kiocb *req, bool force_nonblock)
3405{
3406 struct io_splice *sp = &req->splice;
3407 struct file *in = sp->file_in;
3408 struct file *out = sp->file_out;
3409 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3410 long ret = 0;
3411
3412 if (force_nonblock)
3413 return -EAGAIN;
3414 if (sp->len)
3415 ret = do_tee(in, out, sp->len, flags);
3416
3417 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3418 req->flags &= ~REQ_F_NEED_CLEANUP;
3419
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003420 if (ret != sp->len)
3421 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003422 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003423 return 0;
3424}
3425
3426static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3427{
3428 struct io_splice* sp = &req->splice;
3429
3430 sp->off_in = READ_ONCE(sqe->splice_off_in);
3431 sp->off_out = READ_ONCE(sqe->off);
3432 return __io_splice_prep(req, sqe);
3433}
3434
Pavel Begunkov014db002020-03-03 21:33:12 +03003435static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003436{
3437 struct io_splice *sp = &req->splice;
3438 struct file *in = sp->file_in;
3439 struct file *out = sp->file_out;
3440 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3441 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003442 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003443
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003444 if (force_nonblock)
3445 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003446
3447 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3448 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003449
Jens Axboe948a7742020-05-17 14:21:38 -06003450 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003451 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003452
3453 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3454 req->flags &= ~REQ_F_NEED_CLEANUP;
3455
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003456 if (ret != sp->len)
3457 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003458 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003459 return 0;
3460}
3461
Jens Axboe2b188cc2019-01-07 10:46:33 -07003462/*
3463 * IORING_OP_NOP just posts a completion event, nothing else.
3464 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003465static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003466{
3467 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003468
Jens Axboedef596e2019-01-09 08:59:42 -07003469 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3470 return -EINVAL;
3471
Jens Axboe229a7b62020-06-22 10:13:11 -06003472 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003473 return 0;
3474}
3475
Jens Axboe3529d8c2019-12-19 18:24:38 -07003476static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003477{
Jens Axboe6b063142019-01-10 22:13:58 -07003478 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003479
Jens Axboe09bb8392019-03-13 12:39:28 -06003480 if (!req->file)
3481 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003482
Jens Axboe6b063142019-01-10 22:13:58 -07003483 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003484 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003485 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003486 return -EINVAL;
3487
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003488 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3489 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3490 return -EINVAL;
3491
3492 req->sync.off = READ_ONCE(sqe->off);
3493 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003494 return 0;
3495}
3496
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003497static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003498{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003499 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003500 int ret;
3501
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003502 /* fsync always requires a blocking context */
3503 if (force_nonblock)
3504 return -EAGAIN;
3505
Jens Axboe9adbd452019-12-20 08:45:55 -07003506 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003507 end > 0 ? end : LLONG_MAX,
3508 req->sync.flags & IORING_FSYNC_DATASYNC);
3509 if (ret < 0)
3510 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003511 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003512 return 0;
3513}
3514
Jens Axboed63d1b52019-12-10 10:38:56 -07003515static int io_fallocate_prep(struct io_kiocb *req,
3516 const struct io_uring_sqe *sqe)
3517{
3518 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3519 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003520 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3521 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003522
3523 req->sync.off = READ_ONCE(sqe->off);
3524 req->sync.len = READ_ONCE(sqe->addr);
3525 req->sync.mode = READ_ONCE(sqe->len);
3526 return 0;
3527}
3528
Pavel Begunkov014db002020-03-03 21:33:12 +03003529static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003530{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003531 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003532
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003533 /* fallocate always requiring blocking context */
3534 if (force_nonblock)
3535 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003536 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3537 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003538 if (ret < 0)
3539 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003540 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003541 return 0;
3542}
3543
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003544static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003545{
Jens Axboef8748882020-01-08 17:47:02 -07003546 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003547 int ret;
3548
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003549 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003550 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003551 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003552 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003553
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003554 /* open.how should be already initialised */
3555 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003556 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003557
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003558 req->open.dfd = READ_ONCE(sqe->fd);
3559 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003560 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003561 if (IS_ERR(req->open.filename)) {
3562 ret = PTR_ERR(req->open.filename);
3563 req->open.filename = NULL;
3564 return ret;
3565 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003566 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003567 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003568 return 0;
3569}
3570
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003571static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3572{
3573 u64 flags, mode;
3574
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003575 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3576 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003577 if (req->flags & REQ_F_NEED_CLEANUP)
3578 return 0;
3579 mode = READ_ONCE(sqe->len);
3580 flags = READ_ONCE(sqe->open_flags);
3581 req->open.how = build_open_how(flags, mode);
3582 return __io_openat_prep(req, sqe);
3583}
3584
Jens Axboecebdb982020-01-08 17:59:24 -07003585static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3586{
3587 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003588 size_t len;
3589 int ret;
3590
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003591 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3592 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003593 if (req->flags & REQ_F_NEED_CLEANUP)
3594 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003595 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3596 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003597 if (len < OPEN_HOW_SIZE_VER0)
3598 return -EINVAL;
3599
3600 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3601 len);
3602 if (ret)
3603 return ret;
3604
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003605 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003606}
3607
Pavel Begunkov014db002020-03-03 21:33:12 +03003608static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003609{
3610 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003611 struct file *file;
3612 int ret;
3613
Jens Axboef86cd202020-01-29 13:46:44 -07003614 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003615 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003616
Jens Axboecebdb982020-01-08 17:59:24 -07003617 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003618 if (ret)
3619 goto err;
3620
Jens Axboe4022e7a2020-03-19 19:23:18 -06003621 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003622 if (ret < 0)
3623 goto err;
3624
3625 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3626 if (IS_ERR(file)) {
3627 put_unused_fd(ret);
3628 ret = PTR_ERR(file);
3629 } else {
3630 fsnotify_open(file);
3631 fd_install(ret, file);
3632 }
3633err:
3634 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003635 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003636 if (ret < 0)
3637 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003638 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003639 return 0;
3640}
3641
Pavel Begunkov014db002020-03-03 21:33:12 +03003642static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003643{
Pavel Begunkov014db002020-03-03 21:33:12 +03003644 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003645}
3646
Jens Axboe067524e2020-03-02 16:32:28 -07003647static int io_remove_buffers_prep(struct io_kiocb *req,
3648 const struct io_uring_sqe *sqe)
3649{
3650 struct io_provide_buf *p = &req->pbuf;
3651 u64 tmp;
3652
3653 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3654 return -EINVAL;
3655
3656 tmp = READ_ONCE(sqe->fd);
3657 if (!tmp || tmp > USHRT_MAX)
3658 return -EINVAL;
3659
3660 memset(p, 0, sizeof(*p));
3661 p->nbufs = tmp;
3662 p->bgid = READ_ONCE(sqe->buf_group);
3663 return 0;
3664}
3665
3666static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3667 int bgid, unsigned nbufs)
3668{
3669 unsigned i = 0;
3670
3671 /* shouldn't happen */
3672 if (!nbufs)
3673 return 0;
3674
3675 /* the head kbuf is the list itself */
3676 while (!list_empty(&buf->list)) {
3677 struct io_buffer *nxt;
3678
3679 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3680 list_del(&nxt->list);
3681 kfree(nxt);
3682 if (++i == nbufs)
3683 return i;
3684 }
3685 i++;
3686 kfree(buf);
3687 idr_remove(&ctx->io_buffer_idr, bgid);
3688
3689 return i;
3690}
3691
Jens Axboe229a7b62020-06-22 10:13:11 -06003692static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3693 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003694{
3695 struct io_provide_buf *p = &req->pbuf;
3696 struct io_ring_ctx *ctx = req->ctx;
3697 struct io_buffer *head;
3698 int ret = 0;
3699
3700 io_ring_submit_lock(ctx, !force_nonblock);
3701
3702 lockdep_assert_held(&ctx->uring_lock);
3703
3704 ret = -ENOENT;
3705 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3706 if (head)
3707 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3708
3709 io_ring_submit_lock(ctx, !force_nonblock);
3710 if (ret < 0)
3711 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003712 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003713 return 0;
3714}
3715
Jens Axboeddf0322d2020-02-23 16:41:33 -07003716static int io_provide_buffers_prep(struct io_kiocb *req,
3717 const struct io_uring_sqe *sqe)
3718{
3719 struct io_provide_buf *p = &req->pbuf;
3720 u64 tmp;
3721
3722 if (sqe->ioprio || sqe->rw_flags)
3723 return -EINVAL;
3724
3725 tmp = READ_ONCE(sqe->fd);
3726 if (!tmp || tmp > USHRT_MAX)
3727 return -E2BIG;
3728 p->nbufs = tmp;
3729 p->addr = READ_ONCE(sqe->addr);
3730 p->len = READ_ONCE(sqe->len);
3731
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003732 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003733 return -EFAULT;
3734
3735 p->bgid = READ_ONCE(sqe->buf_group);
3736 tmp = READ_ONCE(sqe->off);
3737 if (tmp > USHRT_MAX)
3738 return -E2BIG;
3739 p->bid = tmp;
3740 return 0;
3741}
3742
3743static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3744{
3745 struct io_buffer *buf;
3746 u64 addr = pbuf->addr;
3747 int i, bid = pbuf->bid;
3748
3749 for (i = 0; i < pbuf->nbufs; i++) {
3750 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3751 if (!buf)
3752 break;
3753
3754 buf->addr = addr;
3755 buf->len = pbuf->len;
3756 buf->bid = bid;
3757 addr += pbuf->len;
3758 bid++;
3759 if (!*head) {
3760 INIT_LIST_HEAD(&buf->list);
3761 *head = buf;
3762 } else {
3763 list_add_tail(&buf->list, &(*head)->list);
3764 }
3765 }
3766
3767 return i ? i : -ENOMEM;
3768}
3769
Jens Axboe229a7b62020-06-22 10:13:11 -06003770static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3771 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003772{
3773 struct io_provide_buf *p = &req->pbuf;
3774 struct io_ring_ctx *ctx = req->ctx;
3775 struct io_buffer *head, *list;
3776 int ret = 0;
3777
3778 io_ring_submit_lock(ctx, !force_nonblock);
3779
3780 lockdep_assert_held(&ctx->uring_lock);
3781
3782 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3783
3784 ret = io_add_buffers(p, &head);
3785 if (ret < 0)
3786 goto out;
3787
3788 if (!list) {
3789 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3790 GFP_KERNEL);
3791 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003792 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003793 goto out;
3794 }
3795 }
3796out:
3797 io_ring_submit_unlock(ctx, !force_nonblock);
3798 if (ret < 0)
3799 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003800 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003801 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003802}
3803
Jens Axboe3e4827b2020-01-08 15:18:09 -07003804static int io_epoll_ctl_prep(struct io_kiocb *req,
3805 const struct io_uring_sqe *sqe)
3806{
3807#if defined(CONFIG_EPOLL)
3808 if (sqe->ioprio || sqe->buf_index)
3809 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003810 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003811 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003812
3813 req->epoll.epfd = READ_ONCE(sqe->fd);
3814 req->epoll.op = READ_ONCE(sqe->len);
3815 req->epoll.fd = READ_ONCE(sqe->off);
3816
3817 if (ep_op_has_event(req->epoll.op)) {
3818 struct epoll_event __user *ev;
3819
3820 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3821 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3822 return -EFAULT;
3823 }
3824
3825 return 0;
3826#else
3827 return -EOPNOTSUPP;
3828#endif
3829}
3830
Jens Axboe229a7b62020-06-22 10:13:11 -06003831static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3832 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003833{
3834#if defined(CONFIG_EPOLL)
3835 struct io_epoll *ie = &req->epoll;
3836 int ret;
3837
3838 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3839 if (force_nonblock && ret == -EAGAIN)
3840 return -EAGAIN;
3841
3842 if (ret < 0)
3843 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003844 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003845 return 0;
3846#else
3847 return -EOPNOTSUPP;
3848#endif
3849}
3850
Jens Axboec1ca7572019-12-25 22:18:28 -07003851static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3852{
3853#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3854 if (sqe->ioprio || sqe->buf_index || sqe->off)
3855 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003856 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3857 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003858
3859 req->madvise.addr = READ_ONCE(sqe->addr);
3860 req->madvise.len = READ_ONCE(sqe->len);
3861 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3862 return 0;
3863#else
3864 return -EOPNOTSUPP;
3865#endif
3866}
3867
Pavel Begunkov014db002020-03-03 21:33:12 +03003868static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003869{
3870#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3871 struct io_madvise *ma = &req->madvise;
3872 int ret;
3873
3874 if (force_nonblock)
3875 return -EAGAIN;
3876
3877 ret = do_madvise(ma->addr, ma->len, ma->advice);
3878 if (ret < 0)
3879 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003880 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003881 return 0;
3882#else
3883 return -EOPNOTSUPP;
3884#endif
3885}
3886
Jens Axboe4840e412019-12-25 22:03:45 -07003887static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3888{
3889 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3890 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003891 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3892 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003893
3894 req->fadvise.offset = READ_ONCE(sqe->off);
3895 req->fadvise.len = READ_ONCE(sqe->len);
3896 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3897 return 0;
3898}
3899
Pavel Begunkov014db002020-03-03 21:33:12 +03003900static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003901{
3902 struct io_fadvise *fa = &req->fadvise;
3903 int ret;
3904
Jens Axboe3e694262020-02-01 09:22:49 -07003905 if (force_nonblock) {
3906 switch (fa->advice) {
3907 case POSIX_FADV_NORMAL:
3908 case POSIX_FADV_RANDOM:
3909 case POSIX_FADV_SEQUENTIAL:
3910 break;
3911 default:
3912 return -EAGAIN;
3913 }
3914 }
Jens Axboe4840e412019-12-25 22:03:45 -07003915
3916 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3917 if (ret < 0)
3918 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003919 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003920 return 0;
3921}
3922
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003923static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3924{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003925 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003926 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003927 if (sqe->ioprio || sqe->buf_index)
3928 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003929 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003930 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003931
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003932 req->statx.dfd = READ_ONCE(sqe->fd);
3933 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003934 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003935 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3936 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003937
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003938 return 0;
3939}
3940
Pavel Begunkov014db002020-03-03 21:33:12 +03003941static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003942{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003943 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003944 int ret;
3945
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003946 if (force_nonblock) {
3947 /* only need file table for an actual valid fd */
3948 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3949 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003950 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003951 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003952
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003953 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3954 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003955
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003956 if (ret < 0)
3957 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003958 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003959 return 0;
3960}
3961
Jens Axboeb5dba592019-12-11 14:02:38 -07003962static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3963{
3964 /*
3965 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003966 * leave the 'file' in an undeterminate state, and here need to modify
3967 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003968 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003969 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003970 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3971
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003972 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3973 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003974 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3975 sqe->rw_flags || sqe->buf_index)
3976 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003977 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003978 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003979
3980 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003981 if ((req->file && req->file->f_op == &io_uring_fops) ||
3982 req->close.fd == req->ctx->ring_fd)
3983 return -EBADF;
3984
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003985 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003986 return 0;
3987}
3988
Jens Axboe229a7b62020-06-22 10:13:11 -06003989static int io_close(struct io_kiocb *req, bool force_nonblock,
3990 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003991{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003992 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003993 int ret;
3994
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003995 /* might be already done during nonblock submission */
3996 if (!close->put_file) {
3997 ret = __close_fd_get_file(close->fd, &close->put_file);
3998 if (ret < 0)
3999 return (ret == -ENOENT) ? -EBADF : ret;
4000 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004001
4002 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004003 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004004 /* was never set, but play safe */
4005 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004006 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004007 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004008 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004009 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004010
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004011 /* No ->flush() or already async, safely close from here */
4012 ret = filp_close(close->put_file, req->work.files);
4013 if (ret < 0)
4014 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004015 fput(close->put_file);
4016 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004017 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004018 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004019}
4020
Jens Axboe3529d8c2019-12-19 18:24:38 -07004021static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004022{
4023 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004024
4025 if (!req->file)
4026 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004027
4028 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4029 return -EINVAL;
4030 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4031 return -EINVAL;
4032
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004033 req->sync.off = READ_ONCE(sqe->off);
4034 req->sync.len = READ_ONCE(sqe->len);
4035 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004036 return 0;
4037}
4038
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004039static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004040{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004041 int ret;
4042
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004043 /* sync_file_range always requires a blocking context */
4044 if (force_nonblock)
4045 return -EAGAIN;
4046
Jens Axboe9adbd452019-12-20 08:45:55 -07004047 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004048 req->sync.flags);
4049 if (ret < 0)
4050 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004051 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004052 return 0;
4053}
4054
YueHaibing469956e2020-03-04 15:53:52 +08004055#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004056static int io_setup_async_msg(struct io_kiocb *req,
4057 struct io_async_msghdr *kmsg)
4058{
4059 if (req->io)
4060 return -EAGAIN;
4061 if (io_alloc_async_ctx(req)) {
4062 if (kmsg->iov != kmsg->fast_iov)
4063 kfree(kmsg->iov);
4064 return -ENOMEM;
4065 }
4066 req->flags |= REQ_F_NEED_CLEANUP;
4067 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4068 return -EAGAIN;
4069}
4070
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004071static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4072 struct io_async_msghdr *iomsg)
4073{
4074 iomsg->iov = iomsg->fast_iov;
4075 iomsg->msg.msg_name = &iomsg->addr;
4076 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4077 req->sr_msg.msg_flags, &iomsg->iov);
4078}
4079
Jens Axboe3529d8c2019-12-19 18:24:38 -07004080static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004081{
Jens Axboee47293f2019-12-20 08:58:21 -07004082 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004083 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004084 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004085
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004086 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4087 return -EINVAL;
4088
Jens Axboee47293f2019-12-20 08:58:21 -07004089 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004090 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004091 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004092
Jens Axboed8768362020-02-27 14:17:49 -07004093#ifdef CONFIG_COMPAT
4094 if (req->ctx->compat)
4095 sr->msg_flags |= MSG_CMSG_COMPAT;
4096#endif
4097
Jens Axboefddafac2020-01-04 20:19:44 -07004098 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004099 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004100 /* iovec is already imported */
4101 if (req->flags & REQ_F_NEED_CLEANUP)
4102 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004103
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004104 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004105 if (!ret)
4106 req->flags |= REQ_F_NEED_CLEANUP;
4107 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004108}
4109
Jens Axboe229a7b62020-06-22 10:13:11 -06004110static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4111 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004112{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004113 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004114 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004115 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004116 int ret;
4117
Jens Axboe03b12302019-12-02 18:50:25 -07004118 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004119 if (unlikely(!sock))
4120 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004121
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004122 if (req->io) {
4123 kmsg = &req->io->msg;
4124 kmsg->msg.msg_name = &req->io->msg.addr;
4125 /* if iov is set, it's allocated already */
4126 if (!kmsg->iov)
4127 kmsg->iov = kmsg->fast_iov;
4128 kmsg->msg.msg_iter.iov = kmsg->iov;
4129 } else {
4130 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004131 if (ret)
4132 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004133 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004134 }
4135
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004136 flags = req->sr_msg.msg_flags;
4137 if (flags & MSG_DONTWAIT)
4138 req->flags |= REQ_F_NOWAIT;
4139 else if (force_nonblock)
4140 flags |= MSG_DONTWAIT;
4141
4142 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4143 if (force_nonblock && ret == -EAGAIN)
4144 return io_setup_async_msg(req, kmsg);
4145 if (ret == -ERESTARTSYS)
4146 ret = -EINTR;
4147
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004148 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004149 kfree(kmsg->iov);
4150 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004151 if (ret < 0)
4152 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004153 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004154 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004155}
4156
Jens Axboe229a7b62020-06-22 10:13:11 -06004157static int io_send(struct io_kiocb *req, bool force_nonblock,
4158 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004159{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004160 struct io_sr_msg *sr = &req->sr_msg;
4161 struct msghdr msg;
4162 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004163 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004164 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004165 int ret;
4166
4167 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004168 if (unlikely(!sock))
4169 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004170
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004171 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4172 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004173 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004174
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004175 msg.msg_name = NULL;
4176 msg.msg_control = NULL;
4177 msg.msg_controllen = 0;
4178 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004179
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004180 flags = req->sr_msg.msg_flags;
4181 if (flags & MSG_DONTWAIT)
4182 req->flags |= REQ_F_NOWAIT;
4183 else if (force_nonblock)
4184 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004185
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004186 msg.msg_flags = flags;
4187 ret = sock_sendmsg(sock, &msg);
4188 if (force_nonblock && ret == -EAGAIN)
4189 return -EAGAIN;
4190 if (ret == -ERESTARTSYS)
4191 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004192
Jens Axboe03b12302019-12-02 18:50:25 -07004193 if (ret < 0)
4194 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004195 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004196 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004197}
4198
Pavel Begunkov1400e692020-07-12 20:41:05 +03004199static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4200 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004201{
4202 struct io_sr_msg *sr = &req->sr_msg;
4203 struct iovec __user *uiov;
4204 size_t iov_len;
4205 int ret;
4206
Pavel Begunkov1400e692020-07-12 20:41:05 +03004207 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4208 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004209 if (ret)
4210 return ret;
4211
4212 if (req->flags & REQ_F_BUFFER_SELECT) {
4213 if (iov_len > 1)
4214 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004215 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004216 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004217 sr->len = iomsg->iov[0].iov_len;
4218 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004219 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004220 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004221 } else {
4222 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004223 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004224 if (ret > 0)
4225 ret = 0;
4226 }
4227
4228 return ret;
4229}
4230
4231#ifdef CONFIG_COMPAT
4232static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004233 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004234{
4235 struct compat_msghdr __user *msg_compat;
4236 struct io_sr_msg *sr = &req->sr_msg;
4237 struct compat_iovec __user *uiov;
4238 compat_uptr_t ptr;
4239 compat_size_t len;
4240 int ret;
4241
Pavel Begunkov270a5942020-07-12 20:41:04 +03004242 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004243 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004244 &ptr, &len);
4245 if (ret)
4246 return ret;
4247
4248 uiov = compat_ptr(ptr);
4249 if (req->flags & REQ_F_BUFFER_SELECT) {
4250 compat_ssize_t clen;
4251
4252 if (len > 1)
4253 return -EINVAL;
4254 if (!access_ok(uiov, sizeof(*uiov)))
4255 return -EFAULT;
4256 if (__get_user(clen, &uiov->iov_len))
4257 return -EFAULT;
4258 if (clen < 0)
4259 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004260 sr->len = iomsg->iov[0].iov_len;
4261 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004262 } else {
4263 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004264 &iomsg->iov,
4265 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004266 if (ret < 0)
4267 return ret;
4268 }
4269
4270 return 0;
4271}
Jens Axboe03b12302019-12-02 18:50:25 -07004272#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004273
Pavel Begunkov1400e692020-07-12 20:41:05 +03004274static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4275 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004276{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004277 iomsg->msg.msg_name = &iomsg->addr;
4278 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004279
4280#ifdef CONFIG_COMPAT
4281 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004282 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004283#endif
4284
Pavel Begunkov1400e692020-07-12 20:41:05 +03004285 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004286}
4287
Jens Axboebcda7ba2020-02-23 16:42:51 -07004288static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004289 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004290{
4291 struct io_sr_msg *sr = &req->sr_msg;
4292 struct io_buffer *kbuf;
4293
Jens Axboebcda7ba2020-02-23 16:42:51 -07004294 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4295 if (IS_ERR(kbuf))
4296 return kbuf;
4297
4298 sr->kbuf = kbuf;
4299 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004300 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004301}
4302
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004303static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4304{
4305 return io_put_kbuf(req, req->sr_msg.kbuf);
4306}
4307
Jens Axboe3529d8c2019-12-19 18:24:38 -07004308static int io_recvmsg_prep(struct io_kiocb *req,
4309 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004310{
Jens Axboee47293f2019-12-20 08:58:21 -07004311 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004312 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004313 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004314
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004315 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4316 return -EINVAL;
4317
Jens Axboe3529d8c2019-12-19 18:24:38 -07004318 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004319 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004320 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004321 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004322
Jens Axboed8768362020-02-27 14:17:49 -07004323#ifdef CONFIG_COMPAT
4324 if (req->ctx->compat)
4325 sr->msg_flags |= MSG_CMSG_COMPAT;
4326#endif
4327
Jens Axboefddafac2020-01-04 20:19:44 -07004328 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004329 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004330 /* iovec is already imported */
4331 if (req->flags & REQ_F_NEED_CLEANUP)
4332 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004333
Pavel Begunkov1400e692020-07-12 20:41:05 +03004334 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004335 if (!ret)
4336 req->flags |= REQ_F_NEED_CLEANUP;
4337 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004338}
4339
Jens Axboe229a7b62020-06-22 10:13:11 -06004340static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4341 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004342{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004343 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004344 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004345 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004346 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004347 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004348
Jens Axboe0fa03c62019-04-19 13:34:07 -06004349 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004350 if (unlikely(!sock))
4351 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004352
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004353 if (req->io) {
4354 kmsg = &req->io->msg;
4355 kmsg->msg.msg_name = &req->io->msg.addr;
4356 /* if iov is set, it's allocated already */
4357 if (!kmsg->iov)
4358 kmsg->iov = kmsg->fast_iov;
4359 kmsg->msg.msg_iter.iov = kmsg->iov;
4360 } else {
4361 ret = io_recvmsg_copy_hdr(req, &iomsg);
4362 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004363 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004364 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004365 }
4366
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004367 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004368 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004369 if (IS_ERR(kbuf))
4370 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004371 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4372 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4373 1, req->sr_msg.len);
4374 }
4375
4376 flags = req->sr_msg.msg_flags;
4377 if (flags & MSG_DONTWAIT)
4378 req->flags |= REQ_F_NOWAIT;
4379 else if (force_nonblock)
4380 flags |= MSG_DONTWAIT;
4381
4382 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4383 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004384 if (force_nonblock && ret == -EAGAIN)
4385 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004386 if (ret == -ERESTARTSYS)
4387 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004388
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004389 if (req->flags & REQ_F_BUFFER_SELECTED)
4390 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004391 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004392 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004393 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004394 if (ret < 0)
4395 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004396 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004397 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004398}
4399
Jens Axboe229a7b62020-06-22 10:13:11 -06004400static int io_recv(struct io_kiocb *req, bool force_nonblock,
4401 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004402{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004403 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004404 struct io_sr_msg *sr = &req->sr_msg;
4405 struct msghdr msg;
4406 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004407 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004408 struct iovec iov;
4409 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004410 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004411
Jens Axboefddafac2020-01-04 20:19:44 -07004412 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004413 if (unlikely(!sock))
4414 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004415
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004416 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004417 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004418 if (IS_ERR(kbuf))
4419 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004420 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004421 }
4422
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004423 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004424 if (unlikely(ret))
4425 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004426
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004427 msg.msg_name = NULL;
4428 msg.msg_control = NULL;
4429 msg.msg_controllen = 0;
4430 msg.msg_namelen = 0;
4431 msg.msg_iocb = NULL;
4432 msg.msg_flags = 0;
4433
4434 flags = req->sr_msg.msg_flags;
4435 if (flags & MSG_DONTWAIT)
4436 req->flags |= REQ_F_NOWAIT;
4437 else if (force_nonblock)
4438 flags |= MSG_DONTWAIT;
4439
4440 ret = sock_recvmsg(sock, &msg, flags);
4441 if (force_nonblock && ret == -EAGAIN)
4442 return -EAGAIN;
4443 if (ret == -ERESTARTSYS)
4444 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004445out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004446 if (req->flags & REQ_F_BUFFER_SELECTED)
4447 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004448 if (ret < 0)
4449 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004450 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004451 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004452}
4453
Jens Axboe3529d8c2019-12-19 18:24:38 -07004454static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004455{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004456 struct io_accept *accept = &req->accept;
4457
Jens Axboe17f2fe32019-10-17 14:42:58 -06004458 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4459 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004460 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004461 return -EINVAL;
4462
Jens Axboed55e5f52019-12-11 16:12:15 -07004463 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4464 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004465 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004466 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004467 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004468}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004469
Jens Axboe229a7b62020-06-22 10:13:11 -06004470static int io_accept(struct io_kiocb *req, bool force_nonblock,
4471 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004472{
4473 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004474 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004475 int ret;
4476
Jiufei Xuee697dee2020-06-10 13:41:59 +08004477 if (req->file->f_flags & O_NONBLOCK)
4478 req->flags |= REQ_F_NOWAIT;
4479
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004480 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004481 accept->addr_len, accept->flags,
4482 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004483 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004484 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004485 if (ret < 0) {
4486 if (ret == -ERESTARTSYS)
4487 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004488 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004489 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004490 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004491 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004492}
4493
Jens Axboe3529d8c2019-12-19 18:24:38 -07004494static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004495{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004496 struct io_connect *conn = &req->connect;
4497 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004498
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004499 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4500 return -EINVAL;
4501 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4502 return -EINVAL;
4503
Jens Axboe3529d8c2019-12-19 18:24:38 -07004504 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4505 conn->addr_len = READ_ONCE(sqe->addr2);
4506
4507 if (!io)
4508 return 0;
4509
4510 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004511 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004512}
4513
Jens Axboe229a7b62020-06-22 10:13:11 -06004514static int io_connect(struct io_kiocb *req, bool force_nonblock,
4515 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004516{
Jens Axboef499a022019-12-02 16:28:46 -07004517 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004518 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004519 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004520
Jens Axboef499a022019-12-02 16:28:46 -07004521 if (req->io) {
4522 io = req->io;
4523 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004524 ret = move_addr_to_kernel(req->connect.addr,
4525 req->connect.addr_len,
4526 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004527 if (ret)
4528 goto out;
4529 io = &__io;
4530 }
4531
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004532 file_flags = force_nonblock ? O_NONBLOCK : 0;
4533
4534 ret = __sys_connect_file(req->file, &io->connect.address,
4535 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004536 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004537 if (req->io)
4538 return -EAGAIN;
4539 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004540 ret = -ENOMEM;
4541 goto out;
4542 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004543 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004544 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004545 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004546 if (ret == -ERESTARTSYS)
4547 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004548out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004549 if (ret < 0)
4550 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004551 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004552 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004553}
YueHaibing469956e2020-03-04 15:53:52 +08004554#else /* !CONFIG_NET */
4555static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4556{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004557 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004558}
4559
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004560static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4561 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004562{
YueHaibing469956e2020-03-04 15:53:52 +08004563 return -EOPNOTSUPP;
4564}
4565
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004566static int io_send(struct io_kiocb *req, bool force_nonblock,
4567 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004568{
4569 return -EOPNOTSUPP;
4570}
4571
4572static int io_recvmsg_prep(struct io_kiocb *req,
4573 const struct io_uring_sqe *sqe)
4574{
4575 return -EOPNOTSUPP;
4576}
4577
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004578static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4579 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004580{
4581 return -EOPNOTSUPP;
4582}
4583
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004584static int io_recv(struct io_kiocb *req, bool force_nonblock,
4585 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004586{
4587 return -EOPNOTSUPP;
4588}
4589
4590static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4591{
4592 return -EOPNOTSUPP;
4593}
4594
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004595static int io_accept(struct io_kiocb *req, bool force_nonblock,
4596 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004597{
4598 return -EOPNOTSUPP;
4599}
4600
4601static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4602{
4603 return -EOPNOTSUPP;
4604}
4605
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004606static int io_connect(struct io_kiocb *req, bool force_nonblock,
4607 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004608{
4609 return -EOPNOTSUPP;
4610}
4611#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004612
Jens Axboed7718a92020-02-14 22:23:12 -07004613struct io_poll_table {
4614 struct poll_table_struct pt;
4615 struct io_kiocb *req;
4616 int error;
4617};
4618
Jens Axboed7718a92020-02-14 22:23:12 -07004619static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4620 __poll_t mask, task_work_func_t func)
4621{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004622 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004623 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004624
4625 /* for instances that support it check for an event match first: */
4626 if (mask && !(mask & poll->events))
4627 return 0;
4628
4629 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4630
4631 list_del_init(&poll->wait.entry);
4632
Jens Axboed7718a92020-02-14 22:23:12 -07004633 req->result = mask;
4634 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004635 percpu_ref_get(&req->ctx->refs);
4636
Jens Axboed7718a92020-02-14 22:23:12 -07004637 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004638 * If we using the signalfd wait_queue_head for this wakeup, then
4639 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4640 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4641 * either, as the normal wakeup will suffice.
4642 */
4643 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4644
4645 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004646 * If this fails, then the task is exiting. When a task exits, the
4647 * work gets canceled, so just cancel this request as well instead
4648 * of executing it. We can't safely execute it anyway, as we may not
4649 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004650 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004651 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004652 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004653 struct task_struct *tsk;
4654
Jens Axboee3aabf92020-05-18 11:04:17 -06004655 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004656 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004657 task_work_add(tsk, &req->task_work, 0);
4658 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004659 }
Jens Axboed7718a92020-02-14 22:23:12 -07004660 return 1;
4661}
4662
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004663static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4664 __acquires(&req->ctx->completion_lock)
4665{
4666 struct io_ring_ctx *ctx = req->ctx;
4667
4668 if (!req->result && !READ_ONCE(poll->canceled)) {
4669 struct poll_table_struct pt = { ._key = poll->events };
4670
4671 req->result = vfs_poll(req->file, &pt) & poll->events;
4672 }
4673
4674 spin_lock_irq(&ctx->completion_lock);
4675 if (!req->result && !READ_ONCE(poll->canceled)) {
4676 add_wait_queue(poll->head, &poll->wait);
4677 return true;
4678 }
4679
4680 return false;
4681}
4682
Jens Axboed4e7cd32020-08-15 11:44:50 -07004683static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004684{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004685 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4686 if (req->opcode == IORING_OP_POLL_ADD)
4687 return (struct io_poll_iocb *) req->io;
4688 return req->apoll->double_poll;
4689}
4690
4691static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4692{
4693 if (req->opcode == IORING_OP_POLL_ADD)
4694 return &req->poll;
4695 return &req->apoll->poll;
4696}
4697
4698static void io_poll_remove_double(struct io_kiocb *req)
4699{
4700 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004701
4702 lockdep_assert_held(&req->ctx->completion_lock);
4703
4704 if (poll && poll->head) {
4705 struct wait_queue_head *head = poll->head;
4706
4707 spin_lock(&head->lock);
4708 list_del_init(&poll->wait.entry);
4709 if (poll->wait.private)
4710 refcount_dec(&req->refs);
4711 poll->head = NULL;
4712 spin_unlock(&head->lock);
4713 }
4714}
4715
4716static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4717{
4718 struct io_ring_ctx *ctx = req->ctx;
4719
Jens Axboed4e7cd32020-08-15 11:44:50 -07004720 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004721 req->poll.done = true;
4722 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4723 io_commit_cqring(ctx);
4724}
4725
4726static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4727{
4728 struct io_ring_ctx *ctx = req->ctx;
4729
4730 if (io_poll_rewait(req, &req->poll)) {
4731 spin_unlock_irq(&ctx->completion_lock);
4732 return;
4733 }
4734
4735 hash_del(&req->hash_node);
4736 io_poll_complete(req, req->result, 0);
4737 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004738 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004739 spin_unlock_irq(&ctx->completion_lock);
4740
4741 io_cqring_ev_posted(ctx);
4742}
4743
4744static void io_poll_task_func(struct callback_head *cb)
4745{
4746 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004747 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004748 struct io_kiocb *nxt = NULL;
4749
4750 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004751 if (nxt)
4752 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004753 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004754}
4755
4756static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4757 int sync, void *key)
4758{
4759 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004760 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004761 __poll_t mask = key_to_poll(key);
4762
4763 /* for instances that support it check for an event match first: */
4764 if (mask && !(mask & poll->events))
4765 return 0;
4766
Jens Axboe8706e042020-09-28 08:38:54 -06004767 list_del_init(&wait->entry);
4768
Jens Axboe807abcb2020-07-17 17:09:27 -06004769 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004770 bool done;
4771
Jens Axboe807abcb2020-07-17 17:09:27 -06004772 spin_lock(&poll->head->lock);
4773 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004774 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004775 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004776 /* make sure double remove sees this as being gone */
4777 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004778 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004779 if (!done)
4780 __io_async_wake(req, poll, mask, io_poll_task_func);
4781 }
4782 refcount_dec(&req->refs);
4783 return 1;
4784}
4785
4786static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4787 wait_queue_func_t wake_func)
4788{
4789 poll->head = NULL;
4790 poll->done = false;
4791 poll->canceled = false;
4792 poll->events = events;
4793 INIT_LIST_HEAD(&poll->wait.entry);
4794 init_waitqueue_func_entry(&poll->wait, wake_func);
4795}
4796
4797static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004798 struct wait_queue_head *head,
4799 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004800{
4801 struct io_kiocb *req = pt->req;
4802
4803 /*
4804 * If poll->head is already set, it's because the file being polled
4805 * uses multiple waitqueues for poll handling (eg one for read, one
4806 * for write). Setup a separate io_poll_iocb if this happens.
4807 */
4808 if (unlikely(poll->head)) {
4809 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004810 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004811 pt->error = -EINVAL;
4812 return;
4813 }
4814 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4815 if (!poll) {
4816 pt->error = -ENOMEM;
4817 return;
4818 }
4819 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4820 refcount_inc(&req->refs);
4821 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004822 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004823 }
4824
4825 pt->error = 0;
4826 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004827
4828 if (poll->events & EPOLLEXCLUSIVE)
4829 add_wait_queue_exclusive(head, &poll->wait);
4830 else
4831 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004832}
4833
4834static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4835 struct poll_table_struct *p)
4836{
4837 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004838 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004839
Jens Axboe807abcb2020-07-17 17:09:27 -06004840 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004841}
4842
Jens Axboed7718a92020-02-14 22:23:12 -07004843static void io_async_task_func(struct callback_head *cb)
4844{
4845 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4846 struct async_poll *apoll = req->apoll;
4847 struct io_ring_ctx *ctx = req->ctx;
4848
4849 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4850
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004851 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004852 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004853 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004854 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004855 }
4856
Jens Axboe31067252020-05-17 17:43:31 -06004857 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004858 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004859 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004860
Jens Axboed4e7cd32020-08-15 11:44:50 -07004861 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004862 spin_unlock_irq(&ctx->completion_lock);
4863
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004864 if (!READ_ONCE(apoll->poll.canceled))
4865 __io_req_task_submit(req);
4866 else
4867 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004868
Jens Axboe6d816e02020-08-11 08:04:14 -06004869 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004870 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004871 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004872}
4873
4874static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4875 void *key)
4876{
4877 struct io_kiocb *req = wait->private;
4878 struct io_poll_iocb *poll = &req->apoll->poll;
4879
4880 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4881 key_to_poll(key));
4882
4883 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4884}
4885
4886static void io_poll_req_insert(struct io_kiocb *req)
4887{
4888 struct io_ring_ctx *ctx = req->ctx;
4889 struct hlist_head *list;
4890
4891 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4892 hlist_add_head(&req->hash_node, list);
4893}
4894
4895static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4896 struct io_poll_iocb *poll,
4897 struct io_poll_table *ipt, __poll_t mask,
4898 wait_queue_func_t wake_func)
4899 __acquires(&ctx->completion_lock)
4900{
4901 struct io_ring_ctx *ctx = req->ctx;
4902 bool cancel = false;
4903
Jens Axboe18bceab2020-05-15 11:56:54 -06004904 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004905 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004906 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004907
4908 ipt->pt._key = mask;
4909 ipt->req = req;
4910 ipt->error = -EINVAL;
4911
Jens Axboed7718a92020-02-14 22:23:12 -07004912 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4913
4914 spin_lock_irq(&ctx->completion_lock);
4915 if (likely(poll->head)) {
4916 spin_lock(&poll->head->lock);
4917 if (unlikely(list_empty(&poll->wait.entry))) {
4918 if (ipt->error)
4919 cancel = true;
4920 ipt->error = 0;
4921 mask = 0;
4922 }
4923 if (mask || ipt->error)
4924 list_del_init(&poll->wait.entry);
4925 else if (cancel)
4926 WRITE_ONCE(poll->canceled, true);
4927 else if (!poll->done) /* actually waiting for an event */
4928 io_poll_req_insert(req);
4929 spin_unlock(&poll->head->lock);
4930 }
4931
4932 return mask;
4933}
4934
4935static bool io_arm_poll_handler(struct io_kiocb *req)
4936{
4937 const struct io_op_def *def = &io_op_defs[req->opcode];
4938 struct io_ring_ctx *ctx = req->ctx;
4939 struct async_poll *apoll;
4940 struct io_poll_table ipt;
4941 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004942 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004943
4944 if (!req->file || !file_can_poll(req->file))
4945 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004946 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004947 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004948 if (def->pollin)
4949 rw = READ;
4950 else if (def->pollout)
4951 rw = WRITE;
4952 else
4953 return false;
4954 /* if we can't nonblock try, then no point in arming a poll handler */
4955 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004956 return false;
4957
4958 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4959 if (unlikely(!apoll))
4960 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004961 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004962
4963 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07004964 req->apoll = apoll;
4965 INIT_HLIST_NODE(&req->hash_node);
4966
Nathan Chancellor8755d972020-03-02 16:01:19 -07004967 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004968 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004969 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004970 if (def->pollout)
4971 mask |= POLLOUT | POLLWRNORM;
4972 mask |= POLLERR | POLLPRI;
4973
4974 ipt.pt._qproc = io_async_queue_proc;
4975
4976 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4977 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06004978 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07004979 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004980 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06004981 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004982 kfree(apoll);
4983 return false;
4984 }
4985 spin_unlock_irq(&ctx->completion_lock);
4986 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4987 apoll->poll.events);
4988 return true;
4989}
4990
4991static bool __io_poll_remove_one(struct io_kiocb *req,
4992 struct io_poll_iocb *poll)
4993{
Jens Axboeb41e9852020-02-17 09:52:41 -07004994 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004995
4996 spin_lock(&poll->head->lock);
4997 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004998 if (!list_empty(&poll->wait.entry)) {
4999 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005000 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005001 }
5002 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005003 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005004 return do_complete;
5005}
5006
5007static bool io_poll_remove_one(struct io_kiocb *req)
5008{
5009 bool do_complete;
5010
Jens Axboed4e7cd32020-08-15 11:44:50 -07005011 io_poll_remove_double(req);
5012
Jens Axboed7718a92020-02-14 22:23:12 -07005013 if (req->opcode == IORING_OP_POLL_ADD) {
5014 do_complete = __io_poll_remove_one(req, &req->poll);
5015 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005016 struct async_poll *apoll = req->apoll;
5017
Jens Axboed7718a92020-02-14 22:23:12 -07005018 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005019 do_complete = __io_poll_remove_one(req, &apoll->poll);
5020 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005021 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005022 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005023 kfree(apoll);
5024 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005025 }
5026
Jens Axboeb41e9852020-02-17 09:52:41 -07005027 if (do_complete) {
5028 io_cqring_fill_event(req, -ECANCELED);
5029 io_commit_cqring(req->ctx);
5030 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005031 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005032 io_put_req(req);
5033 }
5034
5035 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005036}
5037
Jens Axboe76e1b642020-09-26 15:05:03 -06005038/*
5039 * Returns true if we found and killed one or more poll requests
5040 */
5041static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005042{
Jens Axboe78076bb2019-12-04 19:56:40 -07005043 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005044 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005045 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005046
5047 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005048 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5049 struct hlist_head *list;
5050
5051 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005052 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5053 if (io_task_match(req, tsk))
5054 posted += io_poll_remove_one(req);
5055 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005056 }
5057 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005058
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005059 if (posted)
5060 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005061
5062 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005063}
5064
Jens Axboe47f46762019-11-09 17:43:02 -07005065static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5066{
Jens Axboe78076bb2019-12-04 19:56:40 -07005067 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005068 struct io_kiocb *req;
5069
Jens Axboe78076bb2019-12-04 19:56:40 -07005070 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5071 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005072 if (sqe_addr != req->user_data)
5073 continue;
5074 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005075 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005076 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005077 }
5078
5079 return -ENOENT;
5080}
5081
Jens Axboe3529d8c2019-12-19 18:24:38 -07005082static int io_poll_remove_prep(struct io_kiocb *req,
5083 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005084{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005085 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5086 return -EINVAL;
5087 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5088 sqe->poll_events)
5089 return -EINVAL;
5090
Jens Axboe0969e782019-12-17 18:40:57 -07005091 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005092 return 0;
5093}
5094
5095/*
5096 * Find a running poll command that matches one specified in sqe->addr,
5097 * and remove it if found.
5098 */
5099static int io_poll_remove(struct io_kiocb *req)
5100{
5101 struct io_ring_ctx *ctx = req->ctx;
5102 u64 addr;
5103 int ret;
5104
Jens Axboe0969e782019-12-17 18:40:57 -07005105 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005106 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005107 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005108 spin_unlock_irq(&ctx->completion_lock);
5109
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005110 if (ret < 0)
5111 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005112 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005113 return 0;
5114}
5115
Jens Axboe221c5eb2019-01-17 09:41:58 -07005116static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5117 void *key)
5118{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005119 struct io_kiocb *req = wait->private;
5120 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005121
Jens Axboed7718a92020-02-14 22:23:12 -07005122 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005123}
5124
Jens Axboe221c5eb2019-01-17 09:41:58 -07005125static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5126 struct poll_table_struct *p)
5127{
5128 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5129
Jens Axboe807abcb2020-07-17 17:09:27 -06005130 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005131}
5132
Jens Axboe3529d8c2019-12-19 18:24:38 -07005133static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005134{
5135 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005136 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005137
5138 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5139 return -EINVAL;
5140 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5141 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005142 if (!poll->file)
5143 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005144
Jiufei Xue5769a352020-06-17 17:53:55 +08005145 events = READ_ONCE(sqe->poll32_events);
5146#ifdef __BIG_ENDIAN
5147 events = swahw32(events);
5148#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005149 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5150 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005151 return 0;
5152}
5153
Pavel Begunkov014db002020-03-03 21:33:12 +03005154static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005155{
5156 struct io_poll_iocb *poll = &req->poll;
5157 struct io_ring_ctx *ctx = req->ctx;
5158 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005159 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005160
Jens Axboe78076bb2019-12-04 19:56:40 -07005161 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005162 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005163
Jens Axboed7718a92020-02-14 22:23:12 -07005164 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5165 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005166
Jens Axboe8c838782019-03-12 15:48:16 -06005167 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005168 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005169 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005170 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005171 spin_unlock_irq(&ctx->completion_lock);
5172
Jens Axboe8c838782019-03-12 15:48:16 -06005173 if (mask) {
5174 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005175 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005176 }
Jens Axboe8c838782019-03-12 15:48:16 -06005177 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005178}
5179
Jens Axboe5262f562019-09-17 12:26:57 -06005180static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5181{
Jens Axboead8a48a2019-11-15 08:49:11 -07005182 struct io_timeout_data *data = container_of(timer,
5183 struct io_timeout_data, timer);
5184 struct io_kiocb *req = data->req;
5185 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005186 unsigned long flags;
5187
Jens Axboe5262f562019-09-17 12:26:57 -06005188 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005189 atomic_set(&req->ctx->cq_timeouts,
5190 atomic_read(&req->ctx->cq_timeouts) + 1);
5191
zhangyi (F)ef036812019-10-23 15:10:08 +08005192 /*
Jens Axboe11365042019-10-16 09:08:32 -06005193 * We could be racing with timeout deletion. If the list is empty,
5194 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005195 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005196 if (!list_empty(&req->timeout.list))
5197 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005198
Jens Axboe78e19bb2019-11-06 15:21:34 -07005199 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005200 io_commit_cqring(ctx);
5201 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5202
5203 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005204 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005205 io_put_req(req);
5206 return HRTIMER_NORESTART;
5207}
5208
Jens Axboef254ac02020-08-12 17:33:30 -06005209static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005210{
Jens Axboef254ac02020-08-12 17:33:30 -06005211 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005212
Jens Axboef254ac02020-08-12 17:33:30 -06005213 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005214
Jens Axboe2d283902019-12-04 11:08:05 -07005215 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005216 if (ret == -1)
5217 return -EALREADY;
5218
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005219 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005220 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005221 io_cqring_fill_event(req, -ECANCELED);
5222 io_put_req(req);
5223 return 0;
5224}
5225
Jens Axboef254ac02020-08-12 17:33:30 -06005226static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5227{
5228 struct io_kiocb *req;
5229 int ret = -ENOENT;
5230
5231 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5232 if (user_data == req->user_data) {
5233 ret = 0;
5234 break;
5235 }
5236 }
5237
5238 if (ret == -ENOENT)
5239 return ret;
5240
5241 return __io_timeout_cancel(req);
5242}
5243
Jens Axboe3529d8c2019-12-19 18:24:38 -07005244static int io_timeout_remove_prep(struct io_kiocb *req,
5245 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005246{
Jens Axboeb29472e2019-12-17 18:50:29 -07005247 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5248 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005249 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5250 return -EINVAL;
5251 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005252 return -EINVAL;
5253
5254 req->timeout.addr = READ_ONCE(sqe->addr);
5255 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5256 if (req->timeout.flags)
5257 return -EINVAL;
5258
Jens Axboeb29472e2019-12-17 18:50:29 -07005259 return 0;
5260}
5261
Jens Axboe11365042019-10-16 09:08:32 -06005262/*
5263 * Remove or update an existing timeout command
5264 */
Jens Axboefc4df992019-12-10 14:38:45 -07005265static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005266{
5267 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005268 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005269
Jens Axboe11365042019-10-16 09:08:32 -06005270 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005271 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005272
Jens Axboe47f46762019-11-09 17:43:02 -07005273 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005274 io_commit_cqring(ctx);
5275 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005276 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005277 if (ret < 0)
5278 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005279 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005280 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005281}
5282
Jens Axboe3529d8c2019-12-19 18:24:38 -07005283static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005284 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005285{
Jens Axboead8a48a2019-11-15 08:49:11 -07005286 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005287 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005288 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005289
Jens Axboead8a48a2019-11-15 08:49:11 -07005290 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005291 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005292 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005293 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005294 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005295 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005296 flags = READ_ONCE(sqe->timeout_flags);
5297 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005298 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005299
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005300 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005301
Jens Axboe3529d8c2019-12-19 18:24:38 -07005302 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005303 return -ENOMEM;
5304
5305 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005306 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005307
5308 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005309 return -EFAULT;
5310
Jens Axboe11365042019-10-16 09:08:32 -06005311 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005312 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005313 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005314 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005315
Jens Axboead8a48a2019-11-15 08:49:11 -07005316 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5317 return 0;
5318}
5319
Jens Axboefc4df992019-12-10 14:38:45 -07005320static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005321{
Jens Axboead8a48a2019-11-15 08:49:11 -07005322 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005323 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005324 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005325 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005326
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005327 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005328
Jens Axboe5262f562019-09-17 12:26:57 -06005329 /*
5330 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005331 * timeout event to be satisfied. If it isn't set, then this is
5332 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005333 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005334 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005335 entry = ctx->timeout_list.prev;
5336 goto add;
5337 }
Jens Axboe5262f562019-09-17 12:26:57 -06005338
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005339 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5340 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005341
5342 /*
5343 * Insertion sort, ensuring the first entry in the list is always
5344 * the one we need first.
5345 */
Jens Axboe5262f562019-09-17 12:26:57 -06005346 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005347 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5348 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005349
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005350 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005351 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005352 /* nxt.seq is behind @tail, otherwise would've been completed */
5353 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005354 break;
5355 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005356add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005357 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005358 data->timer.function = io_timeout_fn;
5359 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005360 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005361 return 0;
5362}
5363
Jens Axboe62755e32019-10-28 21:49:21 -06005364static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005365{
Jens Axboe62755e32019-10-28 21:49:21 -06005366 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005367
Jens Axboe62755e32019-10-28 21:49:21 -06005368 return req->user_data == (unsigned long) data;
5369}
5370
Jens Axboee977d6d2019-11-05 12:39:45 -07005371static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005372{
Jens Axboe62755e32019-10-28 21:49:21 -06005373 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005374 int ret = 0;
5375
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005376 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005377 switch (cancel_ret) {
5378 case IO_WQ_CANCEL_OK:
5379 ret = 0;
5380 break;
5381 case IO_WQ_CANCEL_RUNNING:
5382 ret = -EALREADY;
5383 break;
5384 case IO_WQ_CANCEL_NOTFOUND:
5385 ret = -ENOENT;
5386 break;
5387 }
5388
Jens Axboee977d6d2019-11-05 12:39:45 -07005389 return ret;
5390}
5391
Jens Axboe47f46762019-11-09 17:43:02 -07005392static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5393 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005394 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005395{
5396 unsigned long flags;
5397 int ret;
5398
5399 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5400 if (ret != -ENOENT) {
5401 spin_lock_irqsave(&ctx->completion_lock, flags);
5402 goto done;
5403 }
5404
5405 spin_lock_irqsave(&ctx->completion_lock, flags);
5406 ret = io_timeout_cancel(ctx, sqe_addr);
5407 if (ret != -ENOENT)
5408 goto done;
5409 ret = io_poll_cancel(ctx, sqe_addr);
5410done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005411 if (!ret)
5412 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005413 io_cqring_fill_event(req, ret);
5414 io_commit_cqring(ctx);
5415 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5416 io_cqring_ev_posted(ctx);
5417
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005418 if (ret < 0)
5419 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005420 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005421}
5422
Jens Axboe3529d8c2019-12-19 18:24:38 -07005423static int io_async_cancel_prep(struct io_kiocb *req,
5424 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005425{
Jens Axboefbf23842019-12-17 18:45:56 -07005426 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005427 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005428 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5429 return -EINVAL;
5430 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005431 return -EINVAL;
5432
Jens Axboefbf23842019-12-17 18:45:56 -07005433 req->cancel.addr = READ_ONCE(sqe->addr);
5434 return 0;
5435}
5436
Pavel Begunkov014db002020-03-03 21:33:12 +03005437static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005438{
5439 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005440
Pavel Begunkov014db002020-03-03 21:33:12 +03005441 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005442 return 0;
5443}
5444
Jens Axboe05f3fb32019-12-09 11:22:50 -07005445static int io_files_update_prep(struct io_kiocb *req,
5446 const struct io_uring_sqe *sqe)
5447{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005448 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5449 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005450 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5451 return -EINVAL;
5452 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005453 return -EINVAL;
5454
5455 req->files_update.offset = READ_ONCE(sqe->off);
5456 req->files_update.nr_args = READ_ONCE(sqe->len);
5457 if (!req->files_update.nr_args)
5458 return -EINVAL;
5459 req->files_update.arg = READ_ONCE(sqe->addr);
5460 return 0;
5461}
5462
Jens Axboe229a7b62020-06-22 10:13:11 -06005463static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5464 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005465{
5466 struct io_ring_ctx *ctx = req->ctx;
5467 struct io_uring_files_update up;
5468 int ret;
5469
Jens Axboef86cd202020-01-29 13:46:44 -07005470 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005471 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005472
5473 up.offset = req->files_update.offset;
5474 up.fds = req->files_update.arg;
5475
5476 mutex_lock(&ctx->uring_lock);
5477 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5478 mutex_unlock(&ctx->uring_lock);
5479
5480 if (ret < 0)
5481 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005482 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005483 return 0;
5484}
5485
Jens Axboe3529d8c2019-12-19 18:24:38 -07005486static int io_req_defer_prep(struct io_kiocb *req,
5487 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005488{
Jens Axboee7815732019-12-17 19:45:06 -07005489 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005490
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005491 if (!sqe)
5492 return 0;
5493
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005494 if (io_alloc_async_ctx(req))
5495 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005496 ret = io_prep_work_files(req);
5497 if (unlikely(ret))
5498 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005499
Jens Axboe202700e12020-09-12 13:18:10 -06005500 io_prep_async_work(req);
5501
Jens Axboed625c6e2019-12-17 19:53:05 -07005502 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005503 case IORING_OP_NOP:
5504 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005505 case IORING_OP_READV:
5506 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005507 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005508 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005509 break;
5510 case IORING_OP_WRITEV:
5511 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005512 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005513 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005514 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005515 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005516 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005517 break;
5518 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005519 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005520 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005521 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005522 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005523 break;
5524 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005525 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005526 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005527 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005528 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005529 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005530 break;
5531 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005532 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005533 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005534 break;
Jens Axboef499a022019-12-02 16:28:46 -07005535 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005536 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005537 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005538 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005539 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005540 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005541 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005542 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005543 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005544 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005545 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005546 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005547 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005548 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005549 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005550 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005551 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005552 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005553 case IORING_OP_FALLOCATE:
5554 ret = io_fallocate_prep(req, sqe);
5555 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005556 case IORING_OP_OPENAT:
5557 ret = io_openat_prep(req, sqe);
5558 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005559 case IORING_OP_CLOSE:
5560 ret = io_close_prep(req, sqe);
5561 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005562 case IORING_OP_FILES_UPDATE:
5563 ret = io_files_update_prep(req, sqe);
5564 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005565 case IORING_OP_STATX:
5566 ret = io_statx_prep(req, sqe);
5567 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005568 case IORING_OP_FADVISE:
5569 ret = io_fadvise_prep(req, sqe);
5570 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005571 case IORING_OP_MADVISE:
5572 ret = io_madvise_prep(req, sqe);
5573 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005574 case IORING_OP_OPENAT2:
5575 ret = io_openat2_prep(req, sqe);
5576 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005577 case IORING_OP_EPOLL_CTL:
5578 ret = io_epoll_ctl_prep(req, sqe);
5579 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005580 case IORING_OP_SPLICE:
5581 ret = io_splice_prep(req, sqe);
5582 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005583 case IORING_OP_PROVIDE_BUFFERS:
5584 ret = io_provide_buffers_prep(req, sqe);
5585 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005586 case IORING_OP_REMOVE_BUFFERS:
5587 ret = io_remove_buffers_prep(req, sqe);
5588 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005589 case IORING_OP_TEE:
5590 ret = io_tee_prep(req, sqe);
5591 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005592 default:
Jens Axboee7815732019-12-17 19:45:06 -07005593 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5594 req->opcode);
5595 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005596 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005597 }
5598
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005599 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005600}
5601
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005602static u32 io_get_sequence(struct io_kiocb *req)
5603{
5604 struct io_kiocb *pos;
5605 struct io_ring_ctx *ctx = req->ctx;
5606 u32 total_submitted, nr_reqs = 1;
5607
5608 if (req->flags & REQ_F_LINK_HEAD)
5609 list_for_each_entry(pos, &req->link_list, link_list)
5610 nr_reqs++;
5611
5612 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5613 return total_submitted - nr_reqs;
5614}
5615
Jens Axboe3529d8c2019-12-19 18:24:38 -07005616static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005617{
Jackie Liua197f662019-11-08 08:09:12 -07005618 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005619 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005620 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005621 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005622
Bob Liu9d858b22019-11-13 18:06:25 +08005623 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005624 if (likely(list_empty_careful(&ctx->defer_list) &&
5625 !(req->flags & REQ_F_IO_DRAIN)))
5626 return 0;
5627
5628 seq = io_get_sequence(req);
5629 /* Still a chance to pass the sequence check */
5630 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005631 return 0;
5632
Pavel Begunkov650b5482020-05-17 14:02:11 +03005633 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005634 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005635 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005636 return ret;
5637 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005638 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005639 de = kmalloc(sizeof(*de), GFP_KERNEL);
5640 if (!de)
5641 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005642
Jens Axboede0617e2019-04-06 21:51:27 -06005643 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005644 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005645 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005646 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005647 io_queue_async_work(req);
5648 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005649 }
5650
Jens Axboe915967f2019-11-21 09:01:20 -07005651 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005652 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005653 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005654 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005655 spin_unlock_irq(&ctx->completion_lock);
5656 return -EIOCBQUEUED;
5657}
5658
Jens Axboef573d382020-09-22 10:19:24 -06005659static void io_req_drop_files(struct io_kiocb *req)
5660{
5661 struct io_ring_ctx *ctx = req->ctx;
5662 unsigned long flags;
5663
5664 spin_lock_irqsave(&ctx->inflight_lock, flags);
5665 list_del(&req->inflight_entry);
5666 if (waitqueue_active(&ctx->inflight_wait))
5667 wake_up(&ctx->inflight_wait);
5668 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5669 req->flags &= ~REQ_F_INFLIGHT;
5670 req->work.files = NULL;
5671}
5672
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005673static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005674{
5675 struct io_async_ctx *io = req->io;
5676
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005677 if (req->flags & REQ_F_BUFFER_SELECTED) {
5678 switch (req->opcode) {
5679 case IORING_OP_READV:
5680 case IORING_OP_READ_FIXED:
5681 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005682 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005683 break;
5684 case IORING_OP_RECVMSG:
5685 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005686 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005687 break;
5688 }
5689 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005690 }
5691
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005692 if (req->flags & REQ_F_NEED_CLEANUP) {
5693 switch (req->opcode) {
5694 case IORING_OP_READV:
5695 case IORING_OP_READ_FIXED:
5696 case IORING_OP_READ:
5697 case IORING_OP_WRITEV:
5698 case IORING_OP_WRITE_FIXED:
5699 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005700 if (io->rw.free_iovec)
5701 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005702 break;
5703 case IORING_OP_RECVMSG:
5704 case IORING_OP_SENDMSG:
5705 if (io->msg.iov != io->msg.fast_iov)
5706 kfree(io->msg.iov);
5707 break;
5708 case IORING_OP_SPLICE:
5709 case IORING_OP_TEE:
5710 io_put_file(req, req->splice.file_in,
5711 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5712 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005713 case IORING_OP_OPENAT:
5714 case IORING_OP_OPENAT2:
5715 if (req->open.filename)
5716 putname(req->open.filename);
5717 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005718 }
5719 req->flags &= ~REQ_F_NEED_CLEANUP;
5720 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005721
Jens Axboef573d382020-09-22 10:19:24 -06005722 if (req->flags & REQ_F_INFLIGHT)
5723 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005724}
5725
Jens Axboe3529d8c2019-12-19 18:24:38 -07005726static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005727 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005728{
Jackie Liua197f662019-11-08 08:09:12 -07005729 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005730 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005731
Jens Axboed625c6e2019-12-17 19:53:05 -07005732 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005733 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005734 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005735 break;
5736 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005737 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005738 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005739 if (sqe) {
5740 ret = io_read_prep(req, sqe, force_nonblock);
5741 if (ret < 0)
5742 break;
5743 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005744 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005745 break;
5746 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005747 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005748 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005749 if (sqe) {
5750 ret = io_write_prep(req, sqe, force_nonblock);
5751 if (ret < 0)
5752 break;
5753 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005754 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005755 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005756 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005757 if (sqe) {
5758 ret = io_prep_fsync(req, sqe);
5759 if (ret < 0)
5760 break;
5761 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005762 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005763 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005764 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005765 if (sqe) {
5766 ret = io_poll_add_prep(req, sqe);
5767 if (ret)
5768 break;
5769 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005770 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005771 break;
5772 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005773 if (sqe) {
5774 ret = io_poll_remove_prep(req, sqe);
5775 if (ret < 0)
5776 break;
5777 }
Jens Axboefc4df992019-12-10 14:38:45 -07005778 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005779 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005780 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005781 if (sqe) {
5782 ret = io_prep_sfr(req, sqe);
5783 if (ret < 0)
5784 break;
5785 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005786 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005787 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005788 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005789 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005790 if (sqe) {
5791 ret = io_sendmsg_prep(req, sqe);
5792 if (ret < 0)
5793 break;
5794 }
Jens Axboefddafac2020-01-04 20:19:44 -07005795 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005796 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005797 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005798 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005799 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005800 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005801 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005802 if (sqe) {
5803 ret = io_recvmsg_prep(req, sqe);
5804 if (ret)
5805 break;
5806 }
Jens Axboefddafac2020-01-04 20:19:44 -07005807 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005808 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005809 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005810 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005811 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005812 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005813 if (sqe) {
5814 ret = io_timeout_prep(req, sqe, false);
5815 if (ret)
5816 break;
5817 }
Jens Axboefc4df992019-12-10 14:38:45 -07005818 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005819 break;
Jens Axboe11365042019-10-16 09:08:32 -06005820 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005821 if (sqe) {
5822 ret = io_timeout_remove_prep(req, sqe);
5823 if (ret)
5824 break;
5825 }
Jens Axboefc4df992019-12-10 14:38:45 -07005826 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005827 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005828 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005829 if (sqe) {
5830 ret = io_accept_prep(req, sqe);
5831 if (ret)
5832 break;
5833 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005834 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005835 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005836 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005837 if (sqe) {
5838 ret = io_connect_prep(req, sqe);
5839 if (ret)
5840 break;
5841 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005842 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005843 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005844 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005845 if (sqe) {
5846 ret = io_async_cancel_prep(req, sqe);
5847 if (ret)
5848 break;
5849 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005850 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005851 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005852 case IORING_OP_FALLOCATE:
5853 if (sqe) {
5854 ret = io_fallocate_prep(req, sqe);
5855 if (ret)
5856 break;
5857 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005858 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005859 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005860 case IORING_OP_OPENAT:
5861 if (sqe) {
5862 ret = io_openat_prep(req, sqe);
5863 if (ret)
5864 break;
5865 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005866 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005867 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005868 case IORING_OP_CLOSE:
5869 if (sqe) {
5870 ret = io_close_prep(req, sqe);
5871 if (ret)
5872 break;
5873 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005874 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005875 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005876 case IORING_OP_FILES_UPDATE:
5877 if (sqe) {
5878 ret = io_files_update_prep(req, sqe);
5879 if (ret)
5880 break;
5881 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005882 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005883 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005884 case IORING_OP_STATX:
5885 if (sqe) {
5886 ret = io_statx_prep(req, sqe);
5887 if (ret)
5888 break;
5889 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005890 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005891 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005892 case IORING_OP_FADVISE:
5893 if (sqe) {
5894 ret = io_fadvise_prep(req, sqe);
5895 if (ret)
5896 break;
5897 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005898 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005899 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005900 case IORING_OP_MADVISE:
5901 if (sqe) {
5902 ret = io_madvise_prep(req, sqe);
5903 if (ret)
5904 break;
5905 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005906 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005907 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005908 case IORING_OP_OPENAT2:
5909 if (sqe) {
5910 ret = io_openat2_prep(req, sqe);
5911 if (ret)
5912 break;
5913 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005914 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005915 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005916 case IORING_OP_EPOLL_CTL:
5917 if (sqe) {
5918 ret = io_epoll_ctl_prep(req, sqe);
5919 if (ret)
5920 break;
5921 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005922 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005923 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005924 case IORING_OP_SPLICE:
5925 if (sqe) {
5926 ret = io_splice_prep(req, sqe);
5927 if (ret < 0)
5928 break;
5929 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005930 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005931 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005932 case IORING_OP_PROVIDE_BUFFERS:
5933 if (sqe) {
5934 ret = io_provide_buffers_prep(req, sqe);
5935 if (ret)
5936 break;
5937 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005938 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005939 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005940 case IORING_OP_REMOVE_BUFFERS:
5941 if (sqe) {
5942 ret = io_remove_buffers_prep(req, sqe);
5943 if (ret)
5944 break;
5945 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005946 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005947 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005948 case IORING_OP_TEE:
5949 if (sqe) {
5950 ret = io_tee_prep(req, sqe);
5951 if (ret < 0)
5952 break;
5953 }
5954 ret = io_tee(req, force_nonblock);
5955 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005956 default:
5957 ret = -EINVAL;
5958 break;
5959 }
5960
5961 if (ret)
5962 return ret;
5963
Jens Axboeb5325762020-05-19 21:20:27 -06005964 /* If the op doesn't have a file, we're not polling for it */
5965 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005966 const bool in_async = io_wq_current_is_worker();
5967
Jens Axboe11ba8202020-01-15 21:51:17 -07005968 /* workqueue context doesn't hold uring_lock, grab it now */
5969 if (in_async)
5970 mutex_lock(&ctx->uring_lock);
5971
Jens Axboe2b188cc2019-01-07 10:46:33 -07005972 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005973
5974 if (in_async)
5975 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005976 }
5977
5978 return 0;
5979}
5980
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005981static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005982{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005983 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005984 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005985 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005986
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005987 timeout = io_prep_linked_timeout(req);
5988 if (timeout)
5989 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005990
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005991 /* if NO_CANCEL is set, we must still run the work */
5992 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5993 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005994 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005995 }
Jens Axboe31b51512019-01-18 22:56:34 -07005996
Jens Axboe561fb042019-10-24 07:25:42 -06005997 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005998 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005999 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006000 /*
6001 * We can get EAGAIN for polled IO even though we're
6002 * forcing a sync submission from here, since we can't
6003 * wait for request slots on the block side.
6004 */
6005 if (ret != -EAGAIN)
6006 break;
6007 cond_resched();
6008 } while (1);
6009 }
Jens Axboe31b51512019-01-18 22:56:34 -07006010
Jens Axboe561fb042019-10-24 07:25:42 -06006011 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006012 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006013 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006014 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006015
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006016 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006017}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006018
Jens Axboe65e19f52019-10-26 07:20:21 -06006019static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6020 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006021{
Jens Axboe65e19f52019-10-26 07:20:21 -06006022 struct fixed_file_table *table;
6023
Jens Axboe05f3fb32019-12-09 11:22:50 -07006024 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006025 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006026}
6027
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006028static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6029 int fd, struct file **out_file, bool fixed)
6030{
6031 struct io_ring_ctx *ctx = req->ctx;
6032 struct file *file;
6033
6034 if (fixed) {
6035 if (unlikely(!ctx->file_data ||
6036 (unsigned) fd >= ctx->nr_user_files))
6037 return -EBADF;
6038 fd = array_index_nospec(fd, ctx->nr_user_files);
6039 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006040 if (file) {
6041 req->fixed_file_refs = ctx->file_data->cur_refs;
6042 percpu_ref_get(req->fixed_file_refs);
6043 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006044 } else {
6045 trace_io_uring_file_get(ctx, fd);
6046 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006047 }
6048
Jens Axboefd2206e2020-06-02 16:40:47 -06006049 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6050 *out_file = file;
6051 return 0;
6052 }
6053 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006054}
6055
Jens Axboe3529d8c2019-12-19 18:24:38 -07006056static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006057 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006058{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006059 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006060
Jens Axboe63ff8222020-05-07 14:56:15 -06006061 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006062 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006063 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006064
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006065 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006066}
6067
Jackie Liua197f662019-11-08 08:09:12 -07006068static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006069{
Jens Axboefcb323c2019-10-24 12:39:47 -06006070 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07006071 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006072
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006073 io_req_init_async(req);
6074
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006075 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006076 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006077 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07006078 return -EBADF;
6079
Jens Axboefcb323c2019-10-24 12:39:47 -06006080 rcu_read_lock();
6081 spin_lock_irq(&ctx->inflight_lock);
6082 /*
6083 * We use the f_ops->flush() handler to ensure that we can flush
6084 * out work accessing these files if the fd is closed. Check if
6085 * the fd has changed since we started down this path, and disallow
6086 * this operation if it has.
6087 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006088 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006089 list_add(&req->inflight_entry, &ctx->inflight_list);
6090 req->flags |= REQ_F_INFLIGHT;
6091 req->work.files = current->files;
6092 ret = 0;
6093 }
6094 spin_unlock_irq(&ctx->inflight_lock);
6095 rcu_read_unlock();
6096
6097 return ret;
6098}
6099
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006100static inline int io_prep_work_files(struct io_kiocb *req)
6101{
6102 if (!io_op_defs[req->opcode].file_table)
6103 return 0;
6104 return io_grab_files(req);
6105}
6106
Jens Axboe2665abf2019-11-05 12:40:47 -07006107static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6108{
Jens Axboead8a48a2019-11-15 08:49:11 -07006109 struct io_timeout_data *data = container_of(timer,
6110 struct io_timeout_data, timer);
6111 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006112 struct io_ring_ctx *ctx = req->ctx;
6113 struct io_kiocb *prev = NULL;
6114 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006115
6116 spin_lock_irqsave(&ctx->completion_lock, flags);
6117
6118 /*
6119 * We don't expect the list to be empty, that will only happen if we
6120 * race with the completion of the linked work.
6121 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006122 if (!list_empty(&req->link_list)) {
6123 prev = list_entry(req->link_list.prev, struct io_kiocb,
6124 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006125 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006126 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006127 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6128 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006129 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006130 }
6131
6132 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6133
6134 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006135 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006136 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006137 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006138 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006139 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006140 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006141 return HRTIMER_NORESTART;
6142}
6143
Jens Axboe7271ef32020-08-10 09:55:22 -06006144static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006145{
Jens Axboe76a46e02019-11-10 23:34:16 -07006146 /*
6147 * If the list is now empty, then our linked request finished before
6148 * we got a chance to setup the timer
6149 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006150 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006151 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006152
Jens Axboead8a48a2019-11-15 08:49:11 -07006153 data->timer.function = io_link_timeout_fn;
6154 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6155 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006156 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006157}
6158
6159static void io_queue_linked_timeout(struct io_kiocb *req)
6160{
6161 struct io_ring_ctx *ctx = req->ctx;
6162
6163 spin_lock_irq(&ctx->completion_lock);
6164 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006165 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006166
Jens Axboe2665abf2019-11-05 12:40:47 -07006167 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006168 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006169}
6170
Jens Axboead8a48a2019-11-15 08:49:11 -07006171static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006172{
6173 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006174
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006175 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006176 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006177 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006178 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006179
Pavel Begunkov44932332019-12-05 16:16:35 +03006180 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6181 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006182 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006183 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006184
Jens Axboe76a46e02019-11-10 23:34:16 -07006185 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006186 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006187}
6188
Jens Axboef13fad72020-06-22 09:34:30 -06006189static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6190 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006191{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006192 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006193 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006194 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006195 int ret;
6196
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006197again:
6198 linked_timeout = io_prep_linked_timeout(req);
6199
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006200 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6201 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006202 if (old_creds)
6203 revert_creds(old_creds);
6204 if (old_creds == req->work.creds)
6205 old_creds = NULL; /* restored original creds */
6206 else
6207 old_creds = override_creds(req->work.creds);
6208 }
6209
Jens Axboef13fad72020-06-22 09:34:30 -06006210 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006211
6212 /*
6213 * We async punt it if the file wasn't marked NOWAIT, or if the file
6214 * doesn't support non-blocking read/write attempts
6215 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006216 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006217 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006218punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006219 ret = io_prep_work_files(req);
6220 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006221 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006222 /*
6223 * Queued up for async execution, worker will release
6224 * submit reference when the iocb is actually submitted.
6225 */
6226 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006227 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006228
Pavel Begunkovf063c542020-07-25 14:41:59 +03006229 if (linked_timeout)
6230 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006231 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006232 }
Jens Axboee65ef562019-03-12 10:16:44 -06006233
Pavel Begunkov652532a2020-07-03 22:15:07 +03006234 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006235err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006236 /* un-prep timeout, so it'll be killed as any other linked */
6237 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006238 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006239 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006240 io_req_complete(req, ret);
6241 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006242 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006243
Jens Axboe6c271ce2019-01-10 11:22:30 -07006244 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006245 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006246 if (linked_timeout)
6247 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006248
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006249 if (nxt) {
6250 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006251
6252 if (req->flags & REQ_F_FORCE_ASYNC)
6253 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006254 goto again;
6255 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006256exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006257 if (old_creds)
6258 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006259}
6260
Jens Axboef13fad72020-06-22 09:34:30 -06006261static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6262 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006263{
6264 int ret;
6265
Jens Axboe3529d8c2019-12-19 18:24:38 -07006266 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006267 if (ret) {
6268 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006269fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006270 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006271 io_put_req(req);
6272 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006273 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006274 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006275 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006276 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006277 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006278 goto fail_req;
6279 }
6280
Jens Axboece35a472019-12-17 08:04:44 -07006281 /*
6282 * Never try inline submit of IOSQE_ASYNC is set, go straight
6283 * to async execution.
6284 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006285 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006286 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6287 io_queue_async_work(req);
6288 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006289 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006290 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006291}
6292
Jens Axboef13fad72020-06-22 09:34:30 -06006293static inline void io_queue_link_head(struct io_kiocb *req,
6294 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006295{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006296 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006297 io_put_req(req);
6298 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006299 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006300 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006301}
6302
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006303static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006304 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006305{
Jackie Liua197f662019-11-08 08:09:12 -07006306 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006307 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006308
Jens Axboe9e645e112019-05-10 16:07:28 -06006309 /*
6310 * If we already have a head request, queue this one for async
6311 * submittal once the head completes. If we don't have a head but
6312 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6313 * submitted sync once the chain is complete. If none of those
6314 * conditions are true (normal request), then just queue it.
6315 */
6316 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006317 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006318
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006319 /*
6320 * Taking sequential execution of a link, draining both sides
6321 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6322 * requests in the link. So, it drains the head and the
6323 * next after the link request. The last one is done via
6324 * drain_next flag to persist the effect across calls.
6325 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006326 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006327 head->flags |= REQ_F_IO_DRAIN;
6328 ctx->drain_next = 1;
6329 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006330 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006331 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006332 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006333 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006334 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006335 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006336 trace_io_uring_link(ctx, req, head);
6337 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006338
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006339 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006340 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006341 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006342 *link = NULL;
6343 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006344 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006345 if (unlikely(ctx->drain_next)) {
6346 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006347 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006348 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006349 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006350 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006351 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006352
Pavel Begunkov711be032020-01-17 03:57:59 +03006353 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006354 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006355 req->flags |= REQ_F_FAIL_LINK;
6356 *link = req;
6357 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006358 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006359 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006360 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006361
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006362 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006363}
6364
Jens Axboe9a56a232019-01-09 09:06:50 -07006365/*
6366 * Batched submission is done, ensure local IO is flushed out.
6367 */
6368static void io_submit_state_end(struct io_submit_state *state)
6369{
Jens Axboef13fad72020-06-22 09:34:30 -06006370 if (!list_empty(&state->comp.list))
6371 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006372 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006373 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006374 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006375 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006376}
6377
6378/*
6379 * Start submission side cache.
6380 */
6381static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006382 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006383{
6384 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006385 state->comp.nr = 0;
6386 INIT_LIST_HEAD(&state->comp.list);
6387 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006388 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006389 state->file = NULL;
6390 state->ios_left = max_ios;
6391}
6392
Jens Axboe2b188cc2019-01-07 10:46:33 -07006393static void io_commit_sqring(struct io_ring_ctx *ctx)
6394{
Hristo Venev75b28af2019-08-26 17:23:46 +00006395 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006396
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006397 /*
6398 * Ensure any loads from the SQEs are done at this point,
6399 * since once we write the new head, the application could
6400 * write new data to them.
6401 */
6402 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006403}
6404
6405/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006406 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006407 * that is mapped by userspace. This means that care needs to be taken to
6408 * ensure that reads are stable, as we cannot rely on userspace always
6409 * being a good citizen. If members of the sqe are validated and then later
6410 * used, it's important that those reads are done through READ_ONCE() to
6411 * prevent a re-load down the line.
6412 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006413static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006414{
Hristo Venev75b28af2019-08-26 17:23:46 +00006415 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006416 unsigned head;
6417
6418 /*
6419 * The cached sq head (or cq tail) serves two purposes:
6420 *
6421 * 1) allows us to batch the cost of updating the user visible
6422 * head updates.
6423 * 2) allows the kernel side to track the head on its own, even
6424 * though the application is the one updating it.
6425 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006426 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006427 if (likely(head < ctx->sq_entries))
6428 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006429
6430 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006431 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006432 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006433 return NULL;
6434}
6435
6436static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6437{
6438 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006439}
6440
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006441#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6442 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6443 IOSQE_BUFFER_SELECT)
6444
6445static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6446 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006447 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006448{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006449 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006450 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006451
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006452 req->opcode = READ_ONCE(sqe->opcode);
6453 req->user_data = READ_ONCE(sqe->user_data);
6454 req->io = NULL;
6455 req->file = NULL;
6456 req->ctx = ctx;
6457 req->flags = 0;
6458 /* one is dropped after submission, the other at completion */
6459 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006460 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006461 get_task_struct(req->task);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006462 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006463
6464 if (unlikely(req->opcode >= IORING_OP_LAST))
6465 return -EINVAL;
6466
Jens Axboe9d8426a2020-06-16 18:42:49 -06006467 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6468 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006469
6470 sqe_flags = READ_ONCE(sqe->flags);
6471 /* enforce forwards compatibility on users */
6472 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6473 return -EINVAL;
6474
6475 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6476 !io_op_defs[req->opcode].buffer_select)
6477 return -EOPNOTSUPP;
6478
6479 id = READ_ONCE(sqe->personality);
6480 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006481 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006482 req->work.creds = idr_find(&ctx->personality_idr, id);
6483 if (unlikely(!req->work.creds))
6484 return -EINVAL;
6485 get_cred(req->work.creds);
6486 }
6487
6488 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006489 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006490
Jens Axboe63ff8222020-05-07 14:56:15 -06006491 if (!io_op_defs[req->opcode].needs_file)
6492 return 0;
6493
6494 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006495}
6496
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006497static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006498 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006499{
Jens Axboeac8691c2020-06-01 08:30:41 -06006500 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006501 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006502 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006503
Jens Axboec4a2ed72019-11-21 21:01:26 -07006504 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006505 if (test_bit(0, &ctx->sq_check_overflow)) {
6506 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006507 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006508 return -EBUSY;
6509 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006510
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006511 /* make sure SQ entry isn't read before tail */
6512 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006513
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006514 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6515 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006516
Jens Axboe013538b2020-06-22 09:29:15 -06006517 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006518
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006519 ctx->ring_fd = ring_fd;
6520 ctx->ring_file = ring_file;
6521
Jens Axboe6c271ce2019-01-10 11:22:30 -07006522 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006523 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006524 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006525 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006526
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006527 sqe = io_get_sqe(ctx);
6528 if (unlikely(!sqe)) {
6529 io_consume_sqe(ctx);
6530 break;
6531 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006532 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006533 if (unlikely(!req)) {
6534 if (!submitted)
6535 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006536 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006537 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006538
Jens Axboeac8691c2020-06-01 08:30:41 -06006539 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006540 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006541 /* will complete beyond this point, count as submitted */
6542 submitted++;
6543
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006544 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006545fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006546 io_put_req(req);
6547 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006548 break;
6549 }
6550
Jens Axboe354420f2020-01-08 18:55:15 -07006551 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006552 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006553 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006554 if (err)
6555 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006556 }
6557
Pavel Begunkov9466f432020-01-25 22:34:01 +03006558 if (unlikely(submitted != nr)) {
6559 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6560
6561 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6562 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006563 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006564 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006565 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006566
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006567 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6568 io_commit_sqring(ctx);
6569
Jens Axboe6c271ce2019-01-10 11:22:30 -07006570 return submitted;
6571}
6572
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006573static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6574{
6575 /* Tell userspace we may need a wakeup call */
6576 spin_lock_irq(&ctx->completion_lock);
6577 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6578 spin_unlock_irq(&ctx->completion_lock);
6579}
6580
6581static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6582{
6583 spin_lock_irq(&ctx->completion_lock);
6584 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6585 spin_unlock_irq(&ctx->completion_lock);
6586}
6587
Jens Axboe6c271ce2019-01-10 11:22:30 -07006588static int io_sq_thread(void *data)
6589{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006590 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006591 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006592 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006593 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006594 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006595
Jens Axboe0f158b42020-05-14 17:18:39 -06006596 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006597
Jens Axboe181e4482019-11-25 08:52:30 -07006598 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006599
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006600 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006601 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006602 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006603
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006604 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006605 unsigned nr_events = 0;
6606
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006607 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006608 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006609 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006610 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006611 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006612 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006613 }
6614
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006615 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006616
6617 /*
6618 * If submit got -EBUSY, flag us as needing the application
6619 * to enter the kernel to reap and flush events.
6620 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006621 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006622 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006623 * Drop cur_mm before scheduling, we can't hold it for
6624 * long periods (or over schedule()). Do this before
6625 * adding ourselves to the waitqueue, as the unuse/drop
6626 * may sleep.
6627 */
Jens Axboe4349f302020-07-09 15:07:01 -06006628 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006629
6630 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006631 * We're polling. If we're within the defined idle
6632 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006633 * to sleep. The exception is if we got EBUSY doing
6634 * more IO, we should wait for the application to
6635 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006636 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006637 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006638 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6639 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006640 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006641 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006642 continue;
6643 }
6644
Jens Axboe6c271ce2019-01-10 11:22:30 -07006645 prepare_to_wait(&ctx->sqo_wait, &wait,
6646 TASK_INTERRUPTIBLE);
6647
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006648 /*
6649 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006650 * to check if there are new reqs added to iopoll_list,
6651 * it is because reqs may have been punted to io worker
6652 * and will be added to iopoll_list later, hence check
6653 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006654 */
6655 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006656 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006657 finish_wait(&ctx->sqo_wait, &wait);
6658 continue;
6659 }
6660
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006661 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006662
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006663 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006664 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006665 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006666 finish_wait(&ctx->sqo_wait, &wait);
6667 break;
6668 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006669 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006670 finish_wait(&ctx->sqo_wait, &wait);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006671 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006672 continue;
6673 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006674 if (signal_pending(current))
6675 flush_signals(current);
6676 schedule();
6677 finish_wait(&ctx->sqo_wait, &wait);
6678
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006679 io_ring_clear_wakeup_flag(ctx);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006680 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006681 continue;
6682 }
6683 finish_wait(&ctx->sqo_wait, &wait);
6684
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006685 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006686 }
6687
Jens Axboe8a4955f2019-12-09 14:52:35 -07006688 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006689 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6690 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006691 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006692 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006693 }
6694
Jens Axboe4c6e2772020-07-01 11:29:10 -06006695 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006696
Jens Axboe4349f302020-07-09 15:07:01 -06006697 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006698 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006699
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006700 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006701
Jens Axboe6c271ce2019-01-10 11:22:30 -07006702 return 0;
6703}
6704
Jens Axboebda52162019-09-24 13:47:15 -06006705struct io_wait_queue {
6706 struct wait_queue_entry wq;
6707 struct io_ring_ctx *ctx;
6708 unsigned to_wait;
6709 unsigned nr_timeouts;
6710};
6711
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006712static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006713{
6714 struct io_ring_ctx *ctx = iowq->ctx;
6715
6716 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006717 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006718 * started waiting. For timeouts, we always want to return to userspace,
6719 * regardless of event count.
6720 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006721 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006722 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6723}
6724
6725static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6726 int wake_flags, void *key)
6727{
6728 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6729 wq);
6730
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006731 /* use noflush == true, as we can't safely rely on locking context */
6732 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006733 return -1;
6734
6735 return autoremove_wake_function(curr, mode, wake_flags, key);
6736}
6737
Jens Axboe2b188cc2019-01-07 10:46:33 -07006738/*
6739 * Wait until events become available, if we don't already have some. The
6740 * application must reap them itself, as they reside on the shared cq ring.
6741 */
6742static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6743 const sigset_t __user *sig, size_t sigsz)
6744{
Jens Axboebda52162019-09-24 13:47:15 -06006745 struct io_wait_queue iowq = {
6746 .wq = {
6747 .private = current,
6748 .func = io_wake_function,
6749 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6750 },
6751 .ctx = ctx,
6752 .to_wait = min_events,
6753 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006754 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006755 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006756
Jens Axboeb41e9852020-02-17 09:52:41 -07006757 do {
6758 if (io_cqring_events(ctx, false) >= min_events)
6759 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006760 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006761 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006762 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763
6764 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006765#ifdef CONFIG_COMPAT
6766 if (in_compat_syscall())
6767 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006768 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006769 else
6770#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006771 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006772
Jens Axboe2b188cc2019-01-07 10:46:33 -07006773 if (ret)
6774 return ret;
6775 }
6776
Jens Axboebda52162019-09-24 13:47:15 -06006777 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006778 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006779 do {
6780 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6781 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006782 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006783 if (io_run_task_work())
6784 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006785 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006786 if (current->jobctl & JOBCTL_TASK_WORK) {
6787 spin_lock_irq(&current->sighand->siglock);
6788 current->jobctl &= ~JOBCTL_TASK_WORK;
6789 recalc_sigpending();
6790 spin_unlock_irq(&current->sighand->siglock);
6791 continue;
6792 }
6793 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006794 break;
6795 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006796 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006797 break;
6798 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006799 } while (1);
6800 finish_wait(&ctx->wait, &iowq.wq);
6801
Jens Axboeb7db41c2020-07-04 08:55:50 -06006802 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006803
Hristo Venev75b28af2019-08-26 17:23:46 +00006804 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006805}
6806
Jens Axboe6b063142019-01-10 22:13:58 -07006807static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6808{
6809#if defined(CONFIG_UNIX)
6810 if (ctx->ring_sock) {
6811 struct sock *sock = ctx->ring_sock->sk;
6812 struct sk_buff *skb;
6813
6814 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6815 kfree_skb(skb);
6816 }
6817#else
6818 int i;
6819
Jens Axboe65e19f52019-10-26 07:20:21 -06006820 for (i = 0; i < ctx->nr_user_files; i++) {
6821 struct file *file;
6822
6823 file = io_file_from_index(ctx, i);
6824 if (file)
6825 fput(file);
6826 }
Jens Axboe6b063142019-01-10 22:13:58 -07006827#endif
6828}
6829
Jens Axboe05f3fb32019-12-09 11:22:50 -07006830static void io_file_ref_kill(struct percpu_ref *ref)
6831{
6832 struct fixed_file_data *data;
6833
6834 data = container_of(ref, struct fixed_file_data, refs);
6835 complete(&data->done);
6836}
6837
Jens Axboe6b063142019-01-10 22:13:58 -07006838static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6839{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006840 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006841 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006842 unsigned nr_tables, i;
6843
Jens Axboe05f3fb32019-12-09 11:22:50 -07006844 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006845 return -ENXIO;
6846
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006847 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006848 if (!list_empty(&data->ref_list))
6849 ref_node = list_first_entry(&data->ref_list,
6850 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006851 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006852 if (ref_node)
6853 percpu_ref_kill(&ref_node->refs);
6854
6855 percpu_ref_kill(&data->refs);
6856
6857 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006858 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006859 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006860
Jens Axboe6b063142019-01-10 22:13:58 -07006861 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006862 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6863 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006864 kfree(data->table[i].files);
6865 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006866 percpu_ref_exit(&data->refs);
6867 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006868 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006869 ctx->nr_user_files = 0;
6870 return 0;
6871}
6872
Jens Axboe6c271ce2019-01-10 11:22:30 -07006873static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6874{
6875 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006876 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006877 /*
6878 * The park is a bit of a work-around, without it we get
6879 * warning spews on shutdown with SQPOLL set and affinity
6880 * set to a single CPU.
6881 */
Jens Axboe06058632019-04-13 09:26:03 -06006882 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006883 kthread_stop(ctx->sqo_thread);
6884 ctx->sqo_thread = NULL;
6885 }
6886}
6887
Jens Axboe6b063142019-01-10 22:13:58 -07006888static void io_finish_async(struct io_ring_ctx *ctx)
6889{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006890 io_sq_thread_stop(ctx);
6891
Jens Axboe561fb042019-10-24 07:25:42 -06006892 if (ctx->io_wq) {
6893 io_wq_destroy(ctx->io_wq);
6894 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006895 }
6896}
6897
6898#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006899/*
6900 * Ensure the UNIX gc is aware of our file set, so we are certain that
6901 * the io_uring can be safely unregistered on process exit, even if we have
6902 * loops in the file referencing.
6903 */
6904static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6905{
6906 struct sock *sk = ctx->ring_sock->sk;
6907 struct scm_fp_list *fpl;
6908 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006909 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006910
Jens Axboe6b063142019-01-10 22:13:58 -07006911 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6912 if (!fpl)
6913 return -ENOMEM;
6914
6915 skb = alloc_skb(0, GFP_KERNEL);
6916 if (!skb) {
6917 kfree(fpl);
6918 return -ENOMEM;
6919 }
6920
6921 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006922
Jens Axboe08a45172019-10-03 08:11:03 -06006923 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006924 fpl->user = get_uid(ctx->user);
6925 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006926 struct file *file = io_file_from_index(ctx, i + offset);
6927
6928 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006929 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006930 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006931 unix_inflight(fpl->user, fpl->fp[nr_files]);
6932 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006933 }
6934
Jens Axboe08a45172019-10-03 08:11:03 -06006935 if (nr_files) {
6936 fpl->max = SCM_MAX_FD;
6937 fpl->count = nr_files;
6938 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006939 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006940 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6941 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006942
Jens Axboe08a45172019-10-03 08:11:03 -06006943 for (i = 0; i < nr_files; i++)
6944 fput(fpl->fp[i]);
6945 } else {
6946 kfree_skb(skb);
6947 kfree(fpl);
6948 }
Jens Axboe6b063142019-01-10 22:13:58 -07006949
6950 return 0;
6951}
6952
6953/*
6954 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6955 * causes regular reference counting to break down. We rely on the UNIX
6956 * garbage collection to take care of this problem for us.
6957 */
6958static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6959{
6960 unsigned left, total;
6961 int ret = 0;
6962
6963 total = 0;
6964 left = ctx->nr_user_files;
6965 while (left) {
6966 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006967
6968 ret = __io_sqe_files_scm(ctx, this_files, total);
6969 if (ret)
6970 break;
6971 left -= this_files;
6972 total += this_files;
6973 }
6974
6975 if (!ret)
6976 return 0;
6977
6978 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006979 struct file *file = io_file_from_index(ctx, total);
6980
6981 if (file)
6982 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006983 total++;
6984 }
6985
6986 return ret;
6987}
6988#else
6989static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6990{
6991 return 0;
6992}
6993#endif
6994
Jens Axboe65e19f52019-10-26 07:20:21 -06006995static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6996 unsigned nr_files)
6997{
6998 int i;
6999
7000 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007001 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007002 unsigned this_files;
7003
7004 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7005 table->files = kcalloc(this_files, sizeof(struct file *),
7006 GFP_KERNEL);
7007 if (!table->files)
7008 break;
7009 nr_files -= this_files;
7010 }
7011
7012 if (i == nr_tables)
7013 return 0;
7014
7015 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007016 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007017 kfree(table->files);
7018 }
7019 return 1;
7020}
7021
Jens Axboe05f3fb32019-12-09 11:22:50 -07007022static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007023{
7024#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007025 struct sock *sock = ctx->ring_sock->sk;
7026 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7027 struct sk_buff *skb;
7028 int i;
7029
7030 __skb_queue_head_init(&list);
7031
7032 /*
7033 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7034 * remove this entry and rearrange the file array.
7035 */
7036 skb = skb_dequeue(head);
7037 while (skb) {
7038 struct scm_fp_list *fp;
7039
7040 fp = UNIXCB(skb).fp;
7041 for (i = 0; i < fp->count; i++) {
7042 int left;
7043
7044 if (fp->fp[i] != file)
7045 continue;
7046
7047 unix_notinflight(fp->user, fp->fp[i]);
7048 left = fp->count - 1 - i;
7049 if (left) {
7050 memmove(&fp->fp[i], &fp->fp[i + 1],
7051 left * sizeof(struct file *));
7052 }
7053 fp->count--;
7054 if (!fp->count) {
7055 kfree_skb(skb);
7056 skb = NULL;
7057 } else {
7058 __skb_queue_tail(&list, skb);
7059 }
7060 fput(file);
7061 file = NULL;
7062 break;
7063 }
7064
7065 if (!file)
7066 break;
7067
7068 __skb_queue_tail(&list, skb);
7069
7070 skb = skb_dequeue(head);
7071 }
7072
7073 if (skb_peek(&list)) {
7074 spin_lock_irq(&head->lock);
7075 while ((skb = __skb_dequeue(&list)) != NULL)
7076 __skb_queue_tail(head, skb);
7077 spin_unlock_irq(&head->lock);
7078 }
7079#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007080 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007081#endif
7082}
7083
Jens Axboe05f3fb32019-12-09 11:22:50 -07007084struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007085 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007086 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007087};
7088
Jens Axboe4a38aed22020-05-14 17:21:15 -06007089static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007090{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007091 struct fixed_file_data *file_data = ref_node->file_data;
7092 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007093 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007094
7095 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007096 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007097 io_ring_file_put(ctx, pfile->file);
7098 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007099 }
7100
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007101 spin_lock(&file_data->lock);
7102 list_del(&ref_node->node);
7103 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007104
Xiaoguang Wang05589552020-03-31 14:05:18 +08007105 percpu_ref_exit(&ref_node->refs);
7106 kfree(ref_node);
7107 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007108}
7109
Jens Axboe4a38aed22020-05-14 17:21:15 -06007110static void io_file_put_work(struct work_struct *work)
7111{
7112 struct io_ring_ctx *ctx;
7113 struct llist_node *node;
7114
7115 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7116 node = llist_del_all(&ctx->file_put_llist);
7117
7118 while (node) {
7119 struct fixed_file_ref_node *ref_node;
7120 struct llist_node *next = node->next;
7121
7122 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7123 __io_file_put_work(ref_node);
7124 node = next;
7125 }
7126}
7127
Jens Axboe05f3fb32019-12-09 11:22:50 -07007128static void io_file_data_ref_zero(struct percpu_ref *ref)
7129{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007130 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007131 struct io_ring_ctx *ctx;
7132 bool first_add;
7133 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007134
Xiaoguang Wang05589552020-03-31 14:05:18 +08007135 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007136 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007137
Jens Axboe4a38aed22020-05-14 17:21:15 -06007138 if (percpu_ref_is_dying(&ctx->file_data->refs))
7139 delay = 0;
7140
7141 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7142 if (!delay)
7143 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7144 else if (first_add)
7145 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007146}
7147
7148static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7149 struct io_ring_ctx *ctx)
7150{
7151 struct fixed_file_ref_node *ref_node;
7152
7153 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7154 if (!ref_node)
7155 return ERR_PTR(-ENOMEM);
7156
7157 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7158 0, GFP_KERNEL)) {
7159 kfree(ref_node);
7160 return ERR_PTR(-ENOMEM);
7161 }
7162 INIT_LIST_HEAD(&ref_node->node);
7163 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007164 ref_node->file_data = ctx->file_data;
7165 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007166}
7167
7168static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7169{
7170 percpu_ref_exit(&ref_node->refs);
7171 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007172}
7173
7174static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7175 unsigned nr_args)
7176{
7177 __s32 __user *fds = (__s32 __user *) arg;
7178 unsigned nr_tables;
7179 struct file *file;
7180 int fd, ret = 0;
7181 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007182 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007183
7184 if (ctx->file_data)
7185 return -EBUSY;
7186 if (!nr_args)
7187 return -EINVAL;
7188 if (nr_args > IORING_MAX_FIXED_FILES)
7189 return -EMFILE;
7190
7191 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7192 if (!ctx->file_data)
7193 return -ENOMEM;
7194 ctx->file_data->ctx = ctx;
7195 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007196 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007197 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007198
7199 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7200 ctx->file_data->table = kcalloc(nr_tables,
7201 sizeof(struct fixed_file_table),
7202 GFP_KERNEL);
7203 if (!ctx->file_data->table) {
7204 kfree(ctx->file_data);
7205 ctx->file_data = NULL;
7206 return -ENOMEM;
7207 }
7208
Xiaoguang Wang05589552020-03-31 14:05:18 +08007209 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007210 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7211 kfree(ctx->file_data->table);
7212 kfree(ctx->file_data);
7213 ctx->file_data = NULL;
7214 return -ENOMEM;
7215 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007216
7217 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7218 percpu_ref_exit(&ctx->file_data->refs);
7219 kfree(ctx->file_data->table);
7220 kfree(ctx->file_data);
7221 ctx->file_data = NULL;
7222 return -ENOMEM;
7223 }
7224
7225 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7226 struct fixed_file_table *table;
7227 unsigned index;
7228
7229 ret = -EFAULT;
7230 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7231 break;
7232 /* allow sparse sets */
7233 if (fd == -1) {
7234 ret = 0;
7235 continue;
7236 }
7237
7238 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7239 index = i & IORING_FILE_TABLE_MASK;
7240 file = fget(fd);
7241
7242 ret = -EBADF;
7243 if (!file)
7244 break;
7245
7246 /*
7247 * Don't allow io_uring instances to be registered. If UNIX
7248 * isn't enabled, then this causes a reference cycle and this
7249 * instance can never get freed. If UNIX is enabled we'll
7250 * handle it just fine, but there's still no point in allowing
7251 * a ring fd as it doesn't support regular read/write anyway.
7252 */
7253 if (file->f_op == &io_uring_fops) {
7254 fput(file);
7255 break;
7256 }
7257 ret = 0;
7258 table->files[index] = file;
7259 }
7260
7261 if (ret) {
7262 for (i = 0; i < ctx->nr_user_files; i++) {
7263 file = io_file_from_index(ctx, i);
7264 if (file)
7265 fput(file);
7266 }
7267 for (i = 0; i < nr_tables; i++)
7268 kfree(ctx->file_data->table[i].files);
7269
Yang Yingliang667e57d2020-07-10 14:14:20 +00007270 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007271 kfree(ctx->file_data->table);
7272 kfree(ctx->file_data);
7273 ctx->file_data = NULL;
7274 ctx->nr_user_files = 0;
7275 return ret;
7276 }
7277
7278 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007279 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007280 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007281 return ret;
7282 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007283
Xiaoguang Wang05589552020-03-31 14:05:18 +08007284 ref_node = alloc_fixed_file_ref_node(ctx);
7285 if (IS_ERR(ref_node)) {
7286 io_sqe_files_unregister(ctx);
7287 return PTR_ERR(ref_node);
7288 }
7289
7290 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007291 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007292 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007293 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007294 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007295 return ret;
7296}
7297
Jens Axboec3a31e62019-10-03 13:59:56 -06007298static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7299 int index)
7300{
7301#if defined(CONFIG_UNIX)
7302 struct sock *sock = ctx->ring_sock->sk;
7303 struct sk_buff_head *head = &sock->sk_receive_queue;
7304 struct sk_buff *skb;
7305
7306 /*
7307 * See if we can merge this file into an existing skb SCM_RIGHTS
7308 * file set. If there's no room, fall back to allocating a new skb
7309 * and filling it in.
7310 */
7311 spin_lock_irq(&head->lock);
7312 skb = skb_peek(head);
7313 if (skb) {
7314 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7315
7316 if (fpl->count < SCM_MAX_FD) {
7317 __skb_unlink(skb, head);
7318 spin_unlock_irq(&head->lock);
7319 fpl->fp[fpl->count] = get_file(file);
7320 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7321 fpl->count++;
7322 spin_lock_irq(&head->lock);
7323 __skb_queue_head(head, skb);
7324 } else {
7325 skb = NULL;
7326 }
7327 }
7328 spin_unlock_irq(&head->lock);
7329
7330 if (skb) {
7331 fput(file);
7332 return 0;
7333 }
7334
7335 return __io_sqe_files_scm(ctx, 1, index);
7336#else
7337 return 0;
7338#endif
7339}
7340
Hillf Dantona5318d32020-03-23 17:47:15 +08007341static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007342 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007343{
Hillf Dantona5318d32020-03-23 17:47:15 +08007344 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007345 struct percpu_ref *refs = data->cur_refs;
7346 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007347
Jens Axboe05f3fb32019-12-09 11:22:50 -07007348 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007349 if (!pfile)
7350 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007351
Xiaoguang Wang05589552020-03-31 14:05:18 +08007352 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007353 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007354 list_add(&pfile->list, &ref_node->file_list);
7355
Hillf Dantona5318d32020-03-23 17:47:15 +08007356 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007357}
7358
7359static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7360 struct io_uring_files_update *up,
7361 unsigned nr_args)
7362{
7363 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007364 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007365 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007366 __s32 __user *fds;
7367 int fd, i, err;
7368 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007369 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007370
Jens Axboe05f3fb32019-12-09 11:22:50 -07007371 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007372 return -EOVERFLOW;
7373 if (done > ctx->nr_user_files)
7374 return -EINVAL;
7375
Xiaoguang Wang05589552020-03-31 14:05:18 +08007376 ref_node = alloc_fixed_file_ref_node(ctx);
7377 if (IS_ERR(ref_node))
7378 return PTR_ERR(ref_node);
7379
Jens Axboec3a31e62019-10-03 13:59:56 -06007380 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007381 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007382 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007383 struct fixed_file_table *table;
7384 unsigned index;
7385
Jens Axboec3a31e62019-10-03 13:59:56 -06007386 err = 0;
7387 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7388 err = -EFAULT;
7389 break;
7390 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007391 i = array_index_nospec(up->offset, ctx->nr_user_files);
7392 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007393 index = i & IORING_FILE_TABLE_MASK;
7394 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007395 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007396 err = io_queue_file_removal(data, file);
7397 if (err)
7398 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007399 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007400 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007401 }
7402 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007403 file = fget(fd);
7404 if (!file) {
7405 err = -EBADF;
7406 break;
7407 }
7408 /*
7409 * Don't allow io_uring instances to be registered. If
7410 * UNIX isn't enabled, then this causes a reference
7411 * cycle and this instance can never get freed. If UNIX
7412 * is enabled we'll handle it just fine, but there's
7413 * still no point in allowing a ring fd as it doesn't
7414 * support regular read/write anyway.
7415 */
7416 if (file->f_op == &io_uring_fops) {
7417 fput(file);
7418 err = -EBADF;
7419 break;
7420 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007421 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007422 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007423 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007424 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007425 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007426 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007427 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007428 }
7429 nr_args--;
7430 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007431 up->offset++;
7432 }
7433
Xiaoguang Wang05589552020-03-31 14:05:18 +08007434 if (needs_switch) {
7435 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007436 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007437 list_add(&ref_node->node, &data->ref_list);
7438 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007439 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007440 percpu_ref_get(&ctx->file_data->refs);
7441 } else
7442 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007443
7444 return done ? done : err;
7445}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007446
Jens Axboe05f3fb32019-12-09 11:22:50 -07007447static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7448 unsigned nr_args)
7449{
7450 struct io_uring_files_update up;
7451
7452 if (!ctx->file_data)
7453 return -ENXIO;
7454 if (!nr_args)
7455 return -EINVAL;
7456 if (copy_from_user(&up, arg, sizeof(up)))
7457 return -EFAULT;
7458 if (up.resv)
7459 return -EINVAL;
7460
7461 return __io_sqe_files_update(ctx, &up, nr_args);
7462}
Jens Axboec3a31e62019-10-03 13:59:56 -06007463
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007464static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007465{
7466 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7467
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007468 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007469 io_put_req(req);
7470}
7471
Pavel Begunkov24369c22020-01-28 03:15:48 +03007472static int io_init_wq_offload(struct io_ring_ctx *ctx,
7473 struct io_uring_params *p)
7474{
7475 struct io_wq_data data;
7476 struct fd f;
7477 struct io_ring_ctx *ctx_attach;
7478 unsigned int concurrency;
7479 int ret = 0;
7480
7481 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007482 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007483 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007484
7485 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7486 /* Do QD, or 4 * CPUS, whatever is smallest */
7487 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7488
7489 ctx->io_wq = io_wq_create(concurrency, &data);
7490 if (IS_ERR(ctx->io_wq)) {
7491 ret = PTR_ERR(ctx->io_wq);
7492 ctx->io_wq = NULL;
7493 }
7494 return ret;
7495 }
7496
7497 f = fdget(p->wq_fd);
7498 if (!f.file)
7499 return -EBADF;
7500
7501 if (f.file->f_op != &io_uring_fops) {
7502 ret = -EINVAL;
7503 goto out_fput;
7504 }
7505
7506 ctx_attach = f.file->private_data;
7507 /* @io_wq is protected by holding the fd */
7508 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7509 ret = -EINVAL;
7510 goto out_fput;
7511 }
7512
7513 ctx->io_wq = ctx_attach->io_wq;
7514out_fput:
7515 fdput(f);
7516 return ret;
7517}
7518
Jens Axboe6c271ce2019-01-10 11:22:30 -07007519static int io_sq_offload_start(struct io_ring_ctx *ctx,
7520 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007521{
7522 int ret;
7523
Jens Axboe6c271ce2019-01-10 11:22:30 -07007524 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007525 ret = -EPERM;
7526 if (!capable(CAP_SYS_ADMIN))
7527 goto err;
7528
Jens Axboe917257d2019-04-13 09:28:55 -06007529 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7530 if (!ctx->sq_thread_idle)
7531 ctx->sq_thread_idle = HZ;
7532
Jens Axboe6c271ce2019-01-10 11:22:30 -07007533 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007534 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007535
Jens Axboe917257d2019-04-13 09:28:55 -06007536 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007537 if (cpu >= nr_cpu_ids)
7538 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007539 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007540 goto err;
7541
Jens Axboe6c271ce2019-01-10 11:22:30 -07007542 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7543 ctx, cpu,
7544 "io_uring-sq");
7545 } else {
7546 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7547 "io_uring-sq");
7548 }
7549 if (IS_ERR(ctx->sqo_thread)) {
7550 ret = PTR_ERR(ctx->sqo_thread);
7551 ctx->sqo_thread = NULL;
7552 goto err;
7553 }
7554 wake_up_process(ctx->sqo_thread);
7555 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7556 /* Can't have SQ_AFF without SQPOLL */
7557 ret = -EINVAL;
7558 goto err;
7559 }
7560
Pavel Begunkov24369c22020-01-28 03:15:48 +03007561 ret = io_init_wq_offload(ctx, p);
7562 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007563 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007564
7565 return 0;
7566err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007567 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007568 return ret;
7569}
7570
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007571static inline void __io_unaccount_mem(struct user_struct *user,
7572 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007573{
7574 atomic_long_sub(nr_pages, &user->locked_vm);
7575}
7576
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007577static inline int __io_account_mem(struct user_struct *user,
7578 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007579{
7580 unsigned long page_limit, cur_pages, new_pages;
7581
7582 /* Don't allow more pages than we can safely lock */
7583 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7584
7585 do {
7586 cur_pages = atomic_long_read(&user->locked_vm);
7587 new_pages = cur_pages + nr_pages;
7588 if (new_pages > page_limit)
7589 return -ENOMEM;
7590 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7591 new_pages) != cur_pages);
7592
7593 return 0;
7594}
7595
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007596static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7597 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007598{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007599 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007600 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007601
Jens Axboe2aede0e2020-09-14 10:45:53 -06007602 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007603 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007604 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007605 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007606 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007607 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007608}
7609
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007610static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7611 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007612{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007613 int ret;
7614
7615 if (ctx->limit_mem) {
7616 ret = __io_account_mem(ctx->user, nr_pages);
7617 if (ret)
7618 return ret;
7619 }
7620
Jens Axboe2aede0e2020-09-14 10:45:53 -06007621 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007622 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007623 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007624 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007625 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007626 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007627
7628 return 0;
7629}
7630
Jens Axboe2b188cc2019-01-07 10:46:33 -07007631static void io_mem_free(void *ptr)
7632{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007633 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007634
Mark Rutland52e04ef2019-04-30 17:30:21 +01007635 if (!ptr)
7636 return;
7637
7638 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007639 if (put_page_testzero(page))
7640 free_compound_page(page);
7641}
7642
7643static void *io_mem_alloc(size_t size)
7644{
7645 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7646 __GFP_NORETRY;
7647
7648 return (void *) __get_free_pages(gfp_flags, get_order(size));
7649}
7650
Hristo Venev75b28af2019-08-26 17:23:46 +00007651static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7652 size_t *sq_offset)
7653{
7654 struct io_rings *rings;
7655 size_t off, sq_array_size;
7656
7657 off = struct_size(rings, cqes, cq_entries);
7658 if (off == SIZE_MAX)
7659 return SIZE_MAX;
7660
7661#ifdef CONFIG_SMP
7662 off = ALIGN(off, SMP_CACHE_BYTES);
7663 if (off == 0)
7664 return SIZE_MAX;
7665#endif
7666
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007667 if (sq_offset)
7668 *sq_offset = off;
7669
Hristo Venev75b28af2019-08-26 17:23:46 +00007670 sq_array_size = array_size(sizeof(u32), sq_entries);
7671 if (sq_array_size == SIZE_MAX)
7672 return SIZE_MAX;
7673
7674 if (check_add_overflow(off, sq_array_size, &off))
7675 return SIZE_MAX;
7676
Hristo Venev75b28af2019-08-26 17:23:46 +00007677 return off;
7678}
7679
Jens Axboe2b188cc2019-01-07 10:46:33 -07007680static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7681{
Hristo Venev75b28af2019-08-26 17:23:46 +00007682 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007683
Hristo Venev75b28af2019-08-26 17:23:46 +00007684 pages = (size_t)1 << get_order(
7685 rings_size(sq_entries, cq_entries, NULL));
7686 pages += (size_t)1 << get_order(
7687 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007688
Hristo Venev75b28af2019-08-26 17:23:46 +00007689 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007690}
7691
Jens Axboeedafcce2019-01-09 09:16:05 -07007692static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7693{
7694 int i, j;
7695
7696 if (!ctx->user_bufs)
7697 return -ENXIO;
7698
7699 for (i = 0; i < ctx->nr_user_bufs; i++) {
7700 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7701
7702 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007703 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007704
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007705 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007706 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007707 imu->nr_bvecs = 0;
7708 }
7709
7710 kfree(ctx->user_bufs);
7711 ctx->user_bufs = NULL;
7712 ctx->nr_user_bufs = 0;
7713 return 0;
7714}
7715
7716static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7717 void __user *arg, unsigned index)
7718{
7719 struct iovec __user *src;
7720
7721#ifdef CONFIG_COMPAT
7722 if (ctx->compat) {
7723 struct compat_iovec __user *ciovs;
7724 struct compat_iovec ciov;
7725
7726 ciovs = (struct compat_iovec __user *) arg;
7727 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7728 return -EFAULT;
7729
Jens Axboed55e5f52019-12-11 16:12:15 -07007730 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007731 dst->iov_len = ciov.iov_len;
7732 return 0;
7733 }
7734#endif
7735 src = (struct iovec __user *) arg;
7736 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7737 return -EFAULT;
7738 return 0;
7739}
7740
7741static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7742 unsigned nr_args)
7743{
7744 struct vm_area_struct **vmas = NULL;
7745 struct page **pages = NULL;
7746 int i, j, got_pages = 0;
7747 int ret = -EINVAL;
7748
7749 if (ctx->user_bufs)
7750 return -EBUSY;
7751 if (!nr_args || nr_args > UIO_MAXIOV)
7752 return -EINVAL;
7753
7754 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7755 GFP_KERNEL);
7756 if (!ctx->user_bufs)
7757 return -ENOMEM;
7758
7759 for (i = 0; i < nr_args; i++) {
7760 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7761 unsigned long off, start, end, ubuf;
7762 int pret, nr_pages;
7763 struct iovec iov;
7764 size_t size;
7765
7766 ret = io_copy_iov(ctx, &iov, arg, i);
7767 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007768 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007769
7770 /*
7771 * Don't impose further limits on the size and buffer
7772 * constraints here, we'll -EINVAL later when IO is
7773 * submitted if they are wrong.
7774 */
7775 ret = -EFAULT;
7776 if (!iov.iov_base || !iov.iov_len)
7777 goto err;
7778
7779 /* arbitrary limit, but we need something */
7780 if (iov.iov_len > SZ_1G)
7781 goto err;
7782
7783 ubuf = (unsigned long) iov.iov_base;
7784 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7785 start = ubuf >> PAGE_SHIFT;
7786 nr_pages = end - start;
7787
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007788 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007789 if (ret)
7790 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007791
7792 ret = 0;
7793 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007794 kvfree(vmas);
7795 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007796 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007797 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007798 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007799 sizeof(struct vm_area_struct *),
7800 GFP_KERNEL);
7801 if (!pages || !vmas) {
7802 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007803 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007804 goto err;
7805 }
7806 got_pages = nr_pages;
7807 }
7808
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007809 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007810 GFP_KERNEL);
7811 ret = -ENOMEM;
7812 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007813 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007814 goto err;
7815 }
7816
7817 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007818 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007819 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007820 FOLL_WRITE | FOLL_LONGTERM,
7821 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007822 if (pret == nr_pages) {
7823 /* don't support file backed memory */
7824 for (j = 0; j < nr_pages; j++) {
7825 struct vm_area_struct *vma = vmas[j];
7826
7827 if (vma->vm_file &&
7828 !is_file_hugepages(vma->vm_file)) {
7829 ret = -EOPNOTSUPP;
7830 break;
7831 }
7832 }
7833 } else {
7834 ret = pret < 0 ? pret : -EFAULT;
7835 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007836 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007837 if (ret) {
7838 /*
7839 * if we did partial map, or found file backed vmas,
7840 * release any pages we did get
7841 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007842 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007843 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007844 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007845 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007846 goto err;
7847 }
7848
7849 off = ubuf & ~PAGE_MASK;
7850 size = iov.iov_len;
7851 for (j = 0; j < nr_pages; j++) {
7852 size_t vec_len;
7853
7854 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7855 imu->bvec[j].bv_page = pages[j];
7856 imu->bvec[j].bv_len = vec_len;
7857 imu->bvec[j].bv_offset = off;
7858 off = 0;
7859 size -= vec_len;
7860 }
7861 /* store original address for later verification */
7862 imu->ubuf = ubuf;
7863 imu->len = iov.iov_len;
7864 imu->nr_bvecs = nr_pages;
7865
7866 ctx->nr_user_bufs++;
7867 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007868 kvfree(pages);
7869 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007870 return 0;
7871err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007872 kvfree(pages);
7873 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007874 io_sqe_buffer_unregister(ctx);
7875 return ret;
7876}
7877
Jens Axboe9b402842019-04-11 11:45:41 -06007878static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7879{
7880 __s32 __user *fds = arg;
7881 int fd;
7882
7883 if (ctx->cq_ev_fd)
7884 return -EBUSY;
7885
7886 if (copy_from_user(&fd, fds, sizeof(*fds)))
7887 return -EFAULT;
7888
7889 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7890 if (IS_ERR(ctx->cq_ev_fd)) {
7891 int ret = PTR_ERR(ctx->cq_ev_fd);
7892 ctx->cq_ev_fd = NULL;
7893 return ret;
7894 }
7895
7896 return 0;
7897}
7898
7899static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7900{
7901 if (ctx->cq_ev_fd) {
7902 eventfd_ctx_put(ctx->cq_ev_fd);
7903 ctx->cq_ev_fd = NULL;
7904 return 0;
7905 }
7906
7907 return -ENXIO;
7908}
7909
Jens Axboe5a2e7452020-02-23 16:23:11 -07007910static int __io_destroy_buffers(int id, void *p, void *data)
7911{
7912 struct io_ring_ctx *ctx = data;
7913 struct io_buffer *buf = p;
7914
Jens Axboe067524e2020-03-02 16:32:28 -07007915 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007916 return 0;
7917}
7918
7919static void io_destroy_buffers(struct io_ring_ctx *ctx)
7920{
7921 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7922 idr_destroy(&ctx->io_buffer_idr);
7923}
7924
Jens Axboe2b188cc2019-01-07 10:46:33 -07007925static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7926{
Jens Axboe6b063142019-01-10 22:13:58 -07007927 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007928 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06007929
7930 if (ctx->sqo_task) {
7931 put_task_struct(ctx->sqo_task);
7932 ctx->sqo_task = NULL;
7933 mmdrop(ctx->mm_account);
7934 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007935 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007936
Jens Axboe6b063142019-01-10 22:13:58 -07007937 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007938 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007939 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007940 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007941
Jens Axboe2b188cc2019-01-07 10:46:33 -07007942#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007943 if (ctx->ring_sock) {
7944 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007945 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007946 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007947#endif
7948
Hristo Venev75b28af2019-08-26 17:23:46 +00007949 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007950 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007951
7952 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007953 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007954 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007955 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007956 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007957 kfree(ctx);
7958}
7959
7960static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7961{
7962 struct io_ring_ctx *ctx = file->private_data;
7963 __poll_t mask = 0;
7964
7965 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007966 /*
7967 * synchronizes with barrier from wq_has_sleeper call in
7968 * io_commit_cqring
7969 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007970 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007971 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7972 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007973 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007974 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007975 mask |= EPOLLIN | EPOLLRDNORM;
7976
7977 return mask;
7978}
7979
7980static int io_uring_fasync(int fd, struct file *file, int on)
7981{
7982 struct io_ring_ctx *ctx = file->private_data;
7983
7984 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7985}
7986
Jens Axboe071698e2020-01-28 10:04:42 -07007987static int io_remove_personalities(int id, void *p, void *data)
7988{
7989 struct io_ring_ctx *ctx = data;
7990 const struct cred *cred;
7991
7992 cred = idr_remove(&ctx->personality_idr, id);
7993 if (cred)
7994 put_cred(cred);
7995 return 0;
7996}
7997
Jens Axboe85faa7b2020-04-09 18:14:00 -06007998static void io_ring_exit_work(struct work_struct *work)
7999{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008000 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8001 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008002
Jens Axboe56952e92020-06-17 15:00:04 -06008003 /*
8004 * If we're doing polled IO and end up having requests being
8005 * submitted async (out-of-line), then completions can come in while
8006 * we're waiting for refs to drop. We need to reap these manually,
8007 * as nobody else will be looking for them.
8008 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008009 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008010 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008011 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008012 io_iopoll_try_reap_events(ctx);
8013 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008014 io_ring_ctx_free(ctx);
8015}
8016
Jens Axboe2b188cc2019-01-07 10:46:33 -07008017static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8018{
8019 mutex_lock(&ctx->uring_lock);
8020 percpu_ref_kill(&ctx->refs);
8021 mutex_unlock(&ctx->uring_lock);
8022
Jens Axboef3606e32020-09-22 08:18:24 -06008023 io_kill_timeouts(ctx, NULL);
8024 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008025
8026 if (ctx->io_wq)
8027 io_wq_cancel_all(ctx->io_wq);
8028
Jens Axboe15dff282019-11-13 09:09:23 -07008029 /* if we failed setting up the ctx, we might not have any rings */
8030 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008031 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008032 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008033 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008034
8035 /*
8036 * Do this upfront, so we won't have a grace period where the ring
8037 * is closed but resources aren't reaped yet. This can cause
8038 * spurious failure in setting up a new ring.
8039 */
Jens Axboe760618f2020-07-24 12:53:31 -06008040 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8041 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008042
Jens Axboe85faa7b2020-04-09 18:14:00 -06008043 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008044 /*
8045 * Use system_unbound_wq to avoid spawning tons of event kworkers
8046 * if we're exiting a ton of rings at the same time. It just adds
8047 * noise and overhead, there's no discernable change in runtime
8048 * over using system_wq.
8049 */
8050 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008051}
8052
8053static int io_uring_release(struct inode *inode, struct file *file)
8054{
8055 struct io_ring_ctx *ctx = file->private_data;
8056
8057 file->private_data = NULL;
8058 io_ring_ctx_wait_and_kill(ctx);
8059 return 0;
8060}
8061
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008062static bool io_wq_files_match(struct io_wq_work *work, void *data)
8063{
8064 struct files_struct *files = data;
8065
8066 return work->files == files;
8067}
8068
Jens Axboef254ac02020-08-12 17:33:30 -06008069/*
8070 * Returns true if 'preq' is the link parent of 'req'
8071 */
8072static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8073{
8074 struct io_kiocb *link;
8075
8076 if (!(preq->flags & REQ_F_LINK_HEAD))
8077 return false;
8078
8079 list_for_each_entry(link, &preq->link_list, link_list) {
8080 if (link == req)
8081 return true;
8082 }
8083
8084 return false;
8085}
8086
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008087static bool io_match_link_files(struct io_kiocb *req,
8088 struct files_struct *files)
8089{
8090 struct io_kiocb *link;
8091
8092 if (io_match_files(req, files))
8093 return true;
8094 if (req->flags & REQ_F_LINK_HEAD) {
8095 list_for_each_entry(link, &req->link_list, link_list) {
8096 if (io_match_files(link, files))
8097 return true;
8098 }
8099 }
8100 return false;
8101}
8102
Jens Axboef254ac02020-08-12 17:33:30 -06008103/*
8104 * We're looking to cancel 'req' because it's holding on to our files, but
8105 * 'req' could be a link to another request. See if it is, and cancel that
8106 * parent request if so.
8107 */
8108static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8109{
8110 struct hlist_node *tmp;
8111 struct io_kiocb *preq;
8112 bool found = false;
8113 int i;
8114
8115 spin_lock_irq(&ctx->completion_lock);
8116 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8117 struct hlist_head *list;
8118
8119 list = &ctx->cancel_hash[i];
8120 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8121 found = io_match_link(preq, req);
8122 if (found) {
8123 io_poll_remove_one(preq);
8124 break;
8125 }
8126 }
8127 }
8128 spin_unlock_irq(&ctx->completion_lock);
8129 return found;
8130}
8131
8132static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8133 struct io_kiocb *req)
8134{
8135 struct io_kiocb *preq;
8136 bool found = false;
8137
8138 spin_lock_irq(&ctx->completion_lock);
8139 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8140 found = io_match_link(preq, req);
8141 if (found) {
8142 __io_timeout_cancel(preq);
8143 break;
8144 }
8145 }
8146 spin_unlock_irq(&ctx->completion_lock);
8147 return found;
8148}
8149
Jens Axboeb711d4e2020-08-16 08:23:05 -07008150static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8151{
8152 return io_match_link(container_of(work, struct io_kiocb, work), data);
8153}
8154
8155static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8156{
8157 enum io_wq_cancel cret;
8158
8159 /* cancel this particular work, if it's running */
8160 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8161 if (cret != IO_WQ_CANCEL_NOTFOUND)
8162 return;
8163
8164 /* find links that hold this pending, cancel those */
8165 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8166 if (cret != IO_WQ_CANCEL_NOTFOUND)
8167 return;
8168
8169 /* if we have a poll link holding this pending, cancel that */
8170 if (io_poll_remove_link(ctx, req))
8171 return;
8172
8173 /* final option, timeout link is holding this req pending */
8174 io_timeout_remove_link(ctx, req);
8175}
8176
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008177static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8178 struct files_struct *files)
8179{
8180 struct io_defer_entry *de = NULL;
8181 LIST_HEAD(list);
8182
8183 spin_lock_irq(&ctx->completion_lock);
8184 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008185 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008186 list_cut_position(&list, &ctx->defer_list, &de->list);
8187 break;
8188 }
8189 }
8190 spin_unlock_irq(&ctx->completion_lock);
8191
8192 while (!list_empty(&list)) {
8193 de = list_first_entry(&list, struct io_defer_entry, list);
8194 list_del_init(&de->list);
8195 req_set_fail_links(de->req);
8196 io_put_req(de->req);
8197 io_req_complete(de->req, -ECANCELED);
8198 kfree(de);
8199 }
8200}
8201
Jens Axboe76e1b642020-09-26 15:05:03 -06008202/*
8203 * Returns true if we found and killed one or more files pinning requests
8204 */
8205static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008206 struct files_struct *files)
8207{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008208 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008209 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008210
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008211 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008212 /* cancel all at once, should be faster than doing it one by one*/
8213 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8214
Jens Axboefcb323c2019-10-24 12:39:47 -06008215 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008216 struct io_kiocb *cancel_req = NULL, *req;
8217 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008218
8219 spin_lock_irq(&ctx->inflight_lock);
8220 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07008221 if (req->work.files != files)
8222 continue;
8223 /* req is being completed, ignore */
8224 if (!refcount_inc_not_zero(&req->refs))
8225 continue;
8226 cancel_req = req;
8227 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008228 }
Jens Axboe768134d2019-11-10 20:30:53 -07008229 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008230 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008231 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008232 spin_unlock_irq(&ctx->inflight_lock);
8233
Jens Axboe768134d2019-11-10 20:30:53 -07008234 /* We need to keep going until we don't find a matching req */
8235 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008236 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008237 /* cancel this request, or head link requests */
8238 io_attempt_cancel(ctx, cancel_req);
8239 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008240 /* cancellations _may_ trigger task work */
8241 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008242 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008243 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008244 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008245
8246 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008247}
8248
Pavel Begunkov801dd572020-06-15 10:33:14 +03008249static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008250{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008251 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8252 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008253
Jens Axboef3606e32020-09-22 08:18:24 -06008254 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008255}
8256
Jens Axboefcb323c2019-10-24 12:39:47 -06008257static int io_uring_flush(struct file *file, void *data)
8258{
8259 struct io_ring_ctx *ctx = file->private_data;
8260
8261 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07008262
8263 /*
8264 * If the task is going away, cancel work it may have pending
8265 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008266 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
8267 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07008268
Jens Axboefcb323c2019-10-24 12:39:47 -06008269 return 0;
8270}
8271
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008272static void *io_uring_validate_mmap_request(struct file *file,
8273 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008274{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008275 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008276 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008277 struct page *page;
8278 void *ptr;
8279
8280 switch (offset) {
8281 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008282 case IORING_OFF_CQ_RING:
8283 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008284 break;
8285 case IORING_OFF_SQES:
8286 ptr = ctx->sq_sqes;
8287 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008288 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008289 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008290 }
8291
8292 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008293 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008294 return ERR_PTR(-EINVAL);
8295
8296 return ptr;
8297}
8298
8299#ifdef CONFIG_MMU
8300
8301static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8302{
8303 size_t sz = vma->vm_end - vma->vm_start;
8304 unsigned long pfn;
8305 void *ptr;
8306
8307 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8308 if (IS_ERR(ptr))
8309 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008310
8311 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8312 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8313}
8314
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008315#else /* !CONFIG_MMU */
8316
8317static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8318{
8319 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8320}
8321
8322static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8323{
8324 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8325}
8326
8327static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8328 unsigned long addr, unsigned long len,
8329 unsigned long pgoff, unsigned long flags)
8330{
8331 void *ptr;
8332
8333 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8334 if (IS_ERR(ptr))
8335 return PTR_ERR(ptr);
8336
8337 return (unsigned long) ptr;
8338}
8339
8340#endif /* !CONFIG_MMU */
8341
Jens Axboe2b188cc2019-01-07 10:46:33 -07008342SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8343 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8344 size_t, sigsz)
8345{
8346 struct io_ring_ctx *ctx;
8347 long ret = -EBADF;
8348 int submitted = 0;
8349 struct fd f;
8350
Jens Axboe4c6e2772020-07-01 11:29:10 -06008351 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008352
Jens Axboe6c271ce2019-01-10 11:22:30 -07008353 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008354 return -EINVAL;
8355
8356 f = fdget(fd);
8357 if (!f.file)
8358 return -EBADF;
8359
8360 ret = -EOPNOTSUPP;
8361 if (f.file->f_op != &io_uring_fops)
8362 goto out_fput;
8363
8364 ret = -ENXIO;
8365 ctx = f.file->private_data;
8366 if (!percpu_ref_tryget(&ctx->refs))
8367 goto out_fput;
8368
Jens Axboe6c271ce2019-01-10 11:22:30 -07008369 /*
8370 * For SQ polling, the thread will do all submissions and completions.
8371 * Just return the requested submit count, and wake the thread if
8372 * we were asked to.
8373 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008374 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008375 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008376 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008377 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008378 if (flags & IORING_ENTER_SQ_WAKEUP)
8379 wake_up(&ctx->sqo_wait);
8380 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008381 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07008382 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03008383 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008384 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008385
8386 if (submitted != to_submit)
8387 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008388 }
8389 if (flags & IORING_ENTER_GETEVENTS) {
8390 min_complete = min(min_complete, ctx->cq_entries);
8391
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008392 /*
8393 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8394 * space applications don't need to do io completion events
8395 * polling again, they can rely on io_sq_thread to do polling
8396 * work, which can reduce cpu usage and uring_lock contention.
8397 */
8398 if (ctx->flags & IORING_SETUP_IOPOLL &&
8399 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008400 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008401 } else {
8402 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8403 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008404 }
8405
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008406out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008407 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008408out_fput:
8409 fdput(f);
8410 return submitted ? submitted : ret;
8411}
8412
Tobias Klauserbebdb652020-02-26 18:38:32 +01008413#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008414static int io_uring_show_cred(int id, void *p, void *data)
8415{
8416 const struct cred *cred = p;
8417 struct seq_file *m = data;
8418 struct user_namespace *uns = seq_user_ns(m);
8419 struct group_info *gi;
8420 kernel_cap_t cap;
8421 unsigned __capi;
8422 int g;
8423
8424 seq_printf(m, "%5d\n", id);
8425 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8426 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8427 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8428 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8429 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8430 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8431 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8432 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8433 seq_puts(m, "\n\tGroups:\t");
8434 gi = cred->group_info;
8435 for (g = 0; g < gi->ngroups; g++) {
8436 seq_put_decimal_ull(m, g ? " " : "",
8437 from_kgid_munged(uns, gi->gid[g]));
8438 }
8439 seq_puts(m, "\n\tCapEff:\t");
8440 cap = cred->cap_effective;
8441 CAP_FOR_EACH_U32(__capi)
8442 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8443 seq_putc(m, '\n');
8444 return 0;
8445}
8446
8447static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8448{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008449 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008450 int i;
8451
Jens Axboefad8e0d2020-09-28 08:57:48 -06008452 /*
8453 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8454 * since fdinfo case grabs it in the opposite direction of normal use
8455 * cases. If we fail to get the lock, we just don't iterate any
8456 * structures that could be going away outside the io_uring mutex.
8457 */
8458 has_lock = mutex_trylock(&ctx->uring_lock);
8459
Jens Axboe87ce9552020-01-30 08:25:34 -07008460 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008461 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008462 struct fixed_file_table *table;
8463 struct file *f;
8464
8465 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8466 f = table->files[i & IORING_FILE_TABLE_MASK];
8467 if (f)
8468 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8469 else
8470 seq_printf(m, "%5u: <none>\n", i);
8471 }
8472 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008473 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008474 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8475
8476 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8477 (unsigned int) buf->len);
8478 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008479 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008480 seq_printf(m, "Personalities:\n");
8481 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8482 }
Jens Axboed7718a92020-02-14 22:23:12 -07008483 seq_printf(m, "PollList:\n");
8484 spin_lock_irq(&ctx->completion_lock);
8485 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8486 struct hlist_head *list = &ctx->cancel_hash[i];
8487 struct io_kiocb *req;
8488
8489 hlist_for_each_entry(req, list, hash_node)
8490 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8491 req->task->task_works != NULL);
8492 }
8493 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008494 if (has_lock)
8495 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008496}
8497
8498static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8499{
8500 struct io_ring_ctx *ctx = f->private_data;
8501
8502 if (percpu_ref_tryget(&ctx->refs)) {
8503 __io_uring_show_fdinfo(ctx, m);
8504 percpu_ref_put(&ctx->refs);
8505 }
8506}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008507#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008508
Jens Axboe2b188cc2019-01-07 10:46:33 -07008509static const struct file_operations io_uring_fops = {
8510 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008511 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008512 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008513#ifndef CONFIG_MMU
8514 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8515 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8516#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008517 .poll = io_uring_poll,
8518 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008519#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008520 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008521#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008522};
8523
8524static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8525 struct io_uring_params *p)
8526{
Hristo Venev75b28af2019-08-26 17:23:46 +00008527 struct io_rings *rings;
8528 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008529
Jens Axboebd740482020-08-05 12:58:23 -06008530 /* make sure these are sane, as we already accounted them */
8531 ctx->sq_entries = p->sq_entries;
8532 ctx->cq_entries = p->cq_entries;
8533
Hristo Venev75b28af2019-08-26 17:23:46 +00008534 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8535 if (size == SIZE_MAX)
8536 return -EOVERFLOW;
8537
8538 rings = io_mem_alloc(size);
8539 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008540 return -ENOMEM;
8541
Hristo Venev75b28af2019-08-26 17:23:46 +00008542 ctx->rings = rings;
8543 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8544 rings->sq_ring_mask = p->sq_entries - 1;
8545 rings->cq_ring_mask = p->cq_entries - 1;
8546 rings->sq_ring_entries = p->sq_entries;
8547 rings->cq_ring_entries = p->cq_entries;
8548 ctx->sq_mask = rings->sq_ring_mask;
8549 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008550
8551 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008552 if (size == SIZE_MAX) {
8553 io_mem_free(ctx->rings);
8554 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008555 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008556 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008557
8558 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008559 if (!ctx->sq_sqes) {
8560 io_mem_free(ctx->rings);
8561 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008562 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008563 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008564
Jens Axboe2b188cc2019-01-07 10:46:33 -07008565 return 0;
8566}
8567
8568/*
8569 * Allocate an anonymous fd, this is what constitutes the application
8570 * visible backing of an io_uring instance. The application mmaps this
8571 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8572 * we have to tie this fd to a socket for file garbage collection purposes.
8573 */
8574static int io_uring_get_fd(struct io_ring_ctx *ctx)
8575{
8576 struct file *file;
8577 int ret;
8578
8579#if defined(CONFIG_UNIX)
8580 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8581 &ctx->ring_sock);
8582 if (ret)
8583 return ret;
8584#endif
8585
8586 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8587 if (ret < 0)
8588 goto err;
8589
8590 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8591 O_RDWR | O_CLOEXEC);
8592 if (IS_ERR(file)) {
8593 put_unused_fd(ret);
8594 ret = PTR_ERR(file);
8595 goto err;
8596 }
8597
8598#if defined(CONFIG_UNIX)
8599 ctx->ring_sock->file = file;
8600#endif
8601 fd_install(ret, file);
8602 return ret;
8603err:
8604#if defined(CONFIG_UNIX)
8605 sock_release(ctx->ring_sock);
8606 ctx->ring_sock = NULL;
8607#endif
8608 return ret;
8609}
8610
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008611static int io_uring_create(unsigned entries, struct io_uring_params *p,
8612 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008613{
8614 struct user_struct *user = NULL;
8615 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008616 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008617 int ret;
8618
Jens Axboe8110c1a2019-12-28 15:39:54 -07008619 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008620 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008621 if (entries > IORING_MAX_ENTRIES) {
8622 if (!(p->flags & IORING_SETUP_CLAMP))
8623 return -EINVAL;
8624 entries = IORING_MAX_ENTRIES;
8625 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008626
8627 /*
8628 * Use twice as many entries for the CQ ring. It's possible for the
8629 * application to drive a higher depth than the size of the SQ ring,
8630 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008631 * some flexibility in overcommitting a bit. If the application has
8632 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8633 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008634 */
8635 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008636 if (p->flags & IORING_SETUP_CQSIZE) {
8637 /*
8638 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8639 * to a power-of-two, if it isn't already. We do NOT impose
8640 * any cq vs sq ring sizing.
8641 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008642 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008643 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008644 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8645 if (!(p->flags & IORING_SETUP_CLAMP))
8646 return -EINVAL;
8647 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8648 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008649 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8650 } else {
8651 p->cq_entries = 2 * p->sq_entries;
8652 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008653
8654 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008655 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008656
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008657 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008658 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008659 ring_pages(p->sq_entries, p->cq_entries));
8660 if (ret) {
8661 free_uid(user);
8662 return ret;
8663 }
8664 }
8665
8666 ctx = io_ring_ctx_alloc(p);
8667 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008668 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008669 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008670 p->cq_entries));
8671 free_uid(user);
8672 return -ENOMEM;
8673 }
8674 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008675 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008676 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008677
Jens Axboe2aede0e2020-09-14 10:45:53 -06008678 ctx->sqo_task = get_task_struct(current);
8679
8680 /*
8681 * This is just grabbed for accounting purposes. When a process exits,
8682 * the mm is exited and dropped before the files, hence we need to hang
8683 * on to this mm purely for the purposes of being able to unaccount
8684 * memory (locked/pinned vm). It's not used for anything else.
8685 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06008686 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008687 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06008688
Jens Axboef74441e2020-08-05 13:00:44 -06008689 /*
8690 * Account memory _before_ installing the file descriptor. Once
8691 * the descriptor is installed, it can get closed at any time. Also
8692 * do this before hitting the general error path, as ring freeing
8693 * will un-account as well.
8694 */
8695 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8696 ACCT_LOCKED);
8697 ctx->limit_mem = limit_mem;
8698
Jens Axboe2b188cc2019-01-07 10:46:33 -07008699 ret = io_allocate_scq_urings(ctx, p);
8700 if (ret)
8701 goto err;
8702
Jens Axboe6c271ce2019-01-10 11:22:30 -07008703 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008704 if (ret)
8705 goto err;
8706
Jens Axboe2b188cc2019-01-07 10:46:33 -07008707 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008708 p->sq_off.head = offsetof(struct io_rings, sq.head);
8709 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8710 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8711 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8712 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8713 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8714 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008715
8716 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008717 p->cq_off.head = offsetof(struct io_rings, cq.head);
8718 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8719 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8720 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8721 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8722 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008723 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008724
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008725 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8726 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008727 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8728 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008729
8730 if (copy_to_user(params, p, sizeof(*p))) {
8731 ret = -EFAULT;
8732 goto err;
8733 }
Jens Axboed1719f72020-07-30 13:43:53 -06008734
8735 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06008736 * Install ring fd as the very last thing, so we don't risk someone
8737 * having closed it before we finish setup
8738 */
8739 ret = io_uring_get_fd(ctx);
8740 if (ret < 0)
8741 goto err;
8742
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008743 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008744 return ret;
8745err:
8746 io_ring_ctx_wait_and_kill(ctx);
8747 return ret;
8748}
8749
8750/*
8751 * Sets up an aio uring context, and returns the fd. Applications asks for a
8752 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8753 * params structure passed in.
8754 */
8755static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8756{
8757 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008758 int i;
8759
8760 if (copy_from_user(&p, params, sizeof(p)))
8761 return -EFAULT;
8762 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8763 if (p.resv[i])
8764 return -EINVAL;
8765 }
8766
Jens Axboe6c271ce2019-01-10 11:22:30 -07008767 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008768 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008769 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008770 return -EINVAL;
8771
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008772 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008773}
8774
8775SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8776 struct io_uring_params __user *, params)
8777{
8778 return io_uring_setup(entries, params);
8779}
8780
Jens Axboe66f4af92020-01-16 15:36:52 -07008781static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8782{
8783 struct io_uring_probe *p;
8784 size_t size;
8785 int i, ret;
8786
8787 size = struct_size(p, ops, nr_args);
8788 if (size == SIZE_MAX)
8789 return -EOVERFLOW;
8790 p = kzalloc(size, GFP_KERNEL);
8791 if (!p)
8792 return -ENOMEM;
8793
8794 ret = -EFAULT;
8795 if (copy_from_user(p, arg, size))
8796 goto out;
8797 ret = -EINVAL;
8798 if (memchr_inv(p, 0, size))
8799 goto out;
8800
8801 p->last_op = IORING_OP_LAST - 1;
8802 if (nr_args > IORING_OP_LAST)
8803 nr_args = IORING_OP_LAST;
8804
8805 for (i = 0; i < nr_args; i++) {
8806 p->ops[i].op = i;
8807 if (!io_op_defs[i].not_supported)
8808 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8809 }
8810 p->ops_len = i;
8811
8812 ret = 0;
8813 if (copy_to_user(arg, p, size))
8814 ret = -EFAULT;
8815out:
8816 kfree(p);
8817 return ret;
8818}
8819
Jens Axboe071698e2020-01-28 10:04:42 -07008820static int io_register_personality(struct io_ring_ctx *ctx)
8821{
8822 const struct cred *creds = get_current_cred();
8823 int id;
8824
8825 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8826 USHRT_MAX, GFP_KERNEL);
8827 if (id < 0)
8828 put_cred(creds);
8829 return id;
8830}
8831
8832static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8833{
8834 const struct cred *old_creds;
8835
8836 old_creds = idr_remove(&ctx->personality_idr, id);
8837 if (old_creds) {
8838 put_cred(old_creds);
8839 return 0;
8840 }
8841
8842 return -EINVAL;
8843}
8844
8845static bool io_register_op_must_quiesce(int op)
8846{
8847 switch (op) {
8848 case IORING_UNREGISTER_FILES:
8849 case IORING_REGISTER_FILES_UPDATE:
8850 case IORING_REGISTER_PROBE:
8851 case IORING_REGISTER_PERSONALITY:
8852 case IORING_UNREGISTER_PERSONALITY:
8853 return false;
8854 default:
8855 return true;
8856 }
8857}
8858
Jens Axboeedafcce2019-01-09 09:16:05 -07008859static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8860 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008861 __releases(ctx->uring_lock)
8862 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008863{
8864 int ret;
8865
Jens Axboe35fa71a2019-04-22 10:23:23 -06008866 /*
8867 * We're inside the ring mutex, if the ref is already dying, then
8868 * someone else killed the ctx or is already going through
8869 * io_uring_register().
8870 */
8871 if (percpu_ref_is_dying(&ctx->refs))
8872 return -ENXIO;
8873
Jens Axboe071698e2020-01-28 10:04:42 -07008874 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008875 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008876
Jens Axboe05f3fb32019-12-09 11:22:50 -07008877 /*
8878 * Drop uring mutex before waiting for references to exit. If
8879 * another thread is currently inside io_uring_enter() it might
8880 * need to grab the uring_lock to make progress. If we hold it
8881 * here across the drain wait, then we can deadlock. It's safe
8882 * to drop the mutex here, since no new references will come in
8883 * after we've killed the percpu ref.
8884 */
8885 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008886 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008887 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008888 if (ret) {
8889 percpu_ref_resurrect(&ctx->refs);
8890 ret = -EINTR;
8891 goto out;
8892 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008893 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008894
8895 switch (opcode) {
8896 case IORING_REGISTER_BUFFERS:
8897 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8898 break;
8899 case IORING_UNREGISTER_BUFFERS:
8900 ret = -EINVAL;
8901 if (arg || nr_args)
8902 break;
8903 ret = io_sqe_buffer_unregister(ctx);
8904 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008905 case IORING_REGISTER_FILES:
8906 ret = io_sqe_files_register(ctx, arg, nr_args);
8907 break;
8908 case IORING_UNREGISTER_FILES:
8909 ret = -EINVAL;
8910 if (arg || nr_args)
8911 break;
8912 ret = io_sqe_files_unregister(ctx);
8913 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008914 case IORING_REGISTER_FILES_UPDATE:
8915 ret = io_sqe_files_update(ctx, arg, nr_args);
8916 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008917 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008918 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008919 ret = -EINVAL;
8920 if (nr_args != 1)
8921 break;
8922 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008923 if (ret)
8924 break;
8925 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8926 ctx->eventfd_async = 1;
8927 else
8928 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008929 break;
8930 case IORING_UNREGISTER_EVENTFD:
8931 ret = -EINVAL;
8932 if (arg || nr_args)
8933 break;
8934 ret = io_eventfd_unregister(ctx);
8935 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008936 case IORING_REGISTER_PROBE:
8937 ret = -EINVAL;
8938 if (!arg || nr_args > 256)
8939 break;
8940 ret = io_probe(ctx, arg, nr_args);
8941 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008942 case IORING_REGISTER_PERSONALITY:
8943 ret = -EINVAL;
8944 if (arg || nr_args)
8945 break;
8946 ret = io_register_personality(ctx);
8947 break;
8948 case IORING_UNREGISTER_PERSONALITY:
8949 ret = -EINVAL;
8950 if (arg)
8951 break;
8952 ret = io_unregister_personality(ctx, nr_args);
8953 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008954 default:
8955 ret = -EINVAL;
8956 break;
8957 }
8958
Jens Axboe071698e2020-01-28 10:04:42 -07008959 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008960 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008961 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008962out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008963 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008964 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008965 return ret;
8966}
8967
8968SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8969 void __user *, arg, unsigned int, nr_args)
8970{
8971 struct io_ring_ctx *ctx;
8972 long ret = -EBADF;
8973 struct fd f;
8974
8975 f = fdget(fd);
8976 if (!f.file)
8977 return -EBADF;
8978
8979 ret = -EOPNOTSUPP;
8980 if (f.file->f_op != &io_uring_fops)
8981 goto out_fput;
8982
8983 ctx = f.file->private_data;
8984
8985 mutex_lock(&ctx->uring_lock);
8986 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8987 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008988 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8989 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008990out_fput:
8991 fdput(f);
8992 return ret;
8993}
8994
Jens Axboe2b188cc2019-01-07 10:46:33 -07008995static int __init io_uring_init(void)
8996{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008997#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8998 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8999 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9000} while (0)
9001
9002#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9003 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9004 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9005 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9006 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9007 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9008 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9009 BUILD_BUG_SQE_ELEM(8, __u64, off);
9010 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9011 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009012 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009013 BUILD_BUG_SQE_ELEM(24, __u32, len);
9014 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9015 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9016 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9017 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009018 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9019 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009020 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9021 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9022 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9023 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9024 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9025 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9026 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9027 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009028 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009029 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9030 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9031 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009032 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009033
Jens Axboed3656342019-12-18 09:50:26 -07009034 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009035 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009036 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9037 return 0;
9038};
9039__initcall(io_uring_init);