blob: 5ddb8b2fe3e5f43583249c0f7521fb6b4a4d9b6e [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 Axboec4a2ed72019-11-21 21:01:26 -07001347/* Returns true if there are no backlogged entries after the flush */
1348static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001350 struct io_rings *rings = ctx->rings;
1351 struct io_uring_cqe *cqe;
1352 struct io_kiocb *req;
1353 unsigned long flags;
1354 LIST_HEAD(list);
1355
1356 if (!force) {
1357 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001358 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001359 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1360 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001361 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001362 }
1363
1364 spin_lock_irqsave(&ctx->completion_lock, flags);
1365
1366 /* if force is set, the ring is going away. always drop after that */
1367 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001368 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001369
Jens Axboec4a2ed72019-11-21 21:01:26 -07001370 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001371 while (!list_empty(&ctx->cq_overflow_list)) {
1372 cqe = io_get_cqring(ctx);
1373 if (!cqe && !force)
1374 break;
1375
1376 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001377 compl.list);
1378 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001379 if (cqe) {
1380 WRITE_ONCE(cqe->user_data, req->user_data);
1381 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001382 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001383 } else {
1384 WRITE_ONCE(ctx->rings->cq_overflow,
1385 atomic_inc_return(&ctx->cached_cq_overflow));
1386 }
1387 }
1388
1389 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001390 io_cqring_mark_overflow(ctx);
1391
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001392 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1393 io_cqring_ev_posted(ctx);
1394
1395 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001396 req = list_first_entry(&list, struct io_kiocb, compl.list);
1397 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001398 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001399 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001400
1401 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001402}
1403
Jens Axboebcda7ba2020-02-23 16:42:51 -07001404static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001406 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001407 struct io_uring_cqe *cqe;
1408
Jens Axboe78e19bb2019-11-06 15:21:34 -07001409 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001410
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411 /*
1412 * If we can't get a cq entry, userspace overflowed the
1413 * submission (by quite a lot). Increment the overflow count in
1414 * the ring.
1415 */
1416 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001417 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001418 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001420 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001421 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 WRITE_ONCE(ctx->rings->cq_overflow,
1423 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001424 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001425 if (list_empty(&ctx->cq_overflow_list)) {
1426 set_bit(0, &ctx->sq_check_overflow);
1427 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001428 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001429 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001430 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001431 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001432 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001433 refcount_inc(&req->refs);
1434 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001435 }
1436}
1437
Jens Axboebcda7ba2020-02-23 16:42:51 -07001438static void io_cqring_fill_event(struct io_kiocb *req, long res)
1439{
1440 __io_cqring_fill_event(req, res, 0);
1441}
1442
Jens Axboee1e16092020-06-22 09:17:17 -06001443static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001444{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001445 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446 unsigned long flags;
1447
1448 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001449 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001450 io_commit_cqring(ctx);
1451 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1452
Jens Axboe8c838782019-03-12 15:48:16 -06001453 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001454}
1455
Jens Axboe229a7b62020-06-22 10:13:11 -06001456static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001457{
Jens Axboe229a7b62020-06-22 10:13:11 -06001458 struct io_ring_ctx *ctx = cs->ctx;
1459
1460 spin_lock_irq(&ctx->completion_lock);
1461 while (!list_empty(&cs->list)) {
1462 struct io_kiocb *req;
1463
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001464 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1465 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001466 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001467 if (!(req->flags & REQ_F_LINK_HEAD)) {
1468 req->flags |= REQ_F_COMP_LOCKED;
1469 io_put_req(req);
1470 } else {
1471 spin_unlock_irq(&ctx->completion_lock);
1472 io_put_req(req);
1473 spin_lock_irq(&ctx->completion_lock);
1474 }
1475 }
1476 io_commit_cqring(ctx);
1477 spin_unlock_irq(&ctx->completion_lock);
1478
1479 io_cqring_ev_posted(ctx);
1480 cs->nr = 0;
1481}
1482
1483static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1484 struct io_comp_state *cs)
1485{
1486 if (!cs) {
1487 io_cqring_add_event(req, res, cflags);
1488 io_put_req(req);
1489 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001490 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001491 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001492 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001493 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001494 if (++cs->nr >= 32)
1495 io_submit_flush_completions(cs);
1496 }
Jens Axboee1e16092020-06-22 09:17:17 -06001497}
1498
1499static void io_req_complete(struct io_kiocb *req, long res)
1500{
Jens Axboe229a7b62020-06-22 10:13:11 -06001501 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001502}
1503
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001504static inline bool io_is_fallback_req(struct io_kiocb *req)
1505{
1506 return req == (struct io_kiocb *)
1507 ((unsigned long) req->ctx->fallback_req & ~1UL);
1508}
1509
1510static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1511{
1512 struct io_kiocb *req;
1513
1514 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001515 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001516 return req;
1517
1518 return NULL;
1519}
1520
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001521static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1522 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001523{
Jens Axboefd6fab22019-03-14 16:30:06 -06001524 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001525 struct io_kiocb *req;
1526
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001527 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001528 size_t sz;
1529 int ret;
1530
1531 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001532 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1533
1534 /*
1535 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1536 * retry single alloc to be on the safe side.
1537 */
1538 if (unlikely(ret <= 0)) {
1539 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1540 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001541 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001542 ret = 1;
1543 }
Jens Axboe2579f912019-01-09 09:10:43 -07001544 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001545 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001546 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001547 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001548 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001549 }
1550
Jens Axboe2579f912019-01-09 09:10:43 -07001551 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001552fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001553 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001554}
1555
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001556static inline void io_put_file(struct io_kiocb *req, struct file *file,
1557 bool fixed)
1558{
1559 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001560 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001561 else
1562 fput(file);
1563}
1564
Jens Axboe51a4cc12020-08-10 10:55:56 -06001565static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001566{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001567 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001568
Jens Axboe5acbbc82020-07-08 15:15:26 -06001569 if (req->io)
1570 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001571 if (req->file)
1572 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001573
Jens Axboe51a4cc12020-08-10 10:55:56 -06001574 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001575}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001576
Jens Axboe51a4cc12020-08-10 10:55:56 -06001577static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001578{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001579 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001580
Jens Axboee3bc8e92020-09-24 08:45:57 -06001581 put_task_struct(req->task);
1582
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001583 if (likely(!io_is_fallback_req(req)))
1584 kmem_cache_free(req_cachep, req);
1585 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001586 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1587 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001588}
1589
Jens Axboe51a4cc12020-08-10 10:55:56 -06001590static void io_req_task_file_table_put(struct callback_head *cb)
1591{
1592 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1593 struct fs_struct *fs = req->work.fs;
1594
1595 spin_lock(&req->work.fs->lock);
1596 if (--fs->users)
1597 fs = NULL;
1598 spin_unlock(&req->work.fs->lock);
1599 if (fs)
1600 free_fs_struct(fs);
1601 req->work.fs = NULL;
1602 __io_free_req_finish(req);
1603}
1604
1605static void __io_free_req(struct io_kiocb *req)
1606{
1607 if (!io_dismantle_req(req)) {
1608 __io_free_req_finish(req);
1609 } else {
1610 int ret;
1611
1612 init_task_work(&req->task_work, io_req_task_file_table_put);
1613 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1614 if (unlikely(ret)) {
1615 struct task_struct *tsk;
1616
1617 tsk = io_wq_get_task(req->ctx->io_wq);
1618 task_work_add(tsk, &req->task_work, 0);
1619 }
1620 }
1621}
1622
Jackie Liua197f662019-11-08 08:09:12 -07001623static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001624{
Jackie Liua197f662019-11-08 08:09:12 -07001625 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001626 int ret;
1627
Jens Axboe2d283902019-12-04 11:08:05 -07001628 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001629 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001630 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001631 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001632 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001633 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001634 return true;
1635 }
1636
1637 return false;
1638}
1639
Jens Axboeab0b6452020-06-30 08:43:15 -06001640static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001641{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001642 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001643 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001644
1645 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001646 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001647 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1648 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001649 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001650
1651 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001652 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001653 wake_ev = io_link_cancel_timeout(link);
1654 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001655 return wake_ev;
1656}
1657
1658static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001659{
Jens Axboe2665abf2019-11-05 12:40:47 -07001660 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001661 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001662
Jens Axboeab0b6452020-06-30 08:43:15 -06001663 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1664 unsigned long flags;
1665
1666 spin_lock_irqsave(&ctx->completion_lock, flags);
1667 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001668 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001669 } else {
1670 wake_ev = __io_kill_linked_timeout(req);
1671 }
1672
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001673 if (wake_ev)
1674 io_cqring_ev_posted(ctx);
1675}
1676
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001677static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001678{
1679 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001680
Jens Axboe9e645e112019-05-10 16:07:28 -06001681 /*
1682 * The list should never be empty when we are called here. But could
1683 * potentially happen if the chain is messed up, check to be on the
1684 * safe side.
1685 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001686 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001687 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001688
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001689 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1690 list_del_init(&req->link_list);
1691 if (!list_empty(&nxt->link_list))
1692 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001693 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001694}
1695
1696/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001697 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001698 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001699static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001700{
Jens Axboe2665abf2019-11-05 12:40:47 -07001701 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001702
1703 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001704 struct io_kiocb *link = list_first_entry(&req->link_list,
1705 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001706
Pavel Begunkov44932332019-12-05 16:16:35 +03001707 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001708 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001709
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001710 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001711 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001712 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001713 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001714 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001715
1716 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001717 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001718}
1719
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001720static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001721{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001722 struct io_ring_ctx *ctx = req->ctx;
1723
1724 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1725 unsigned long flags;
1726
1727 spin_lock_irqsave(&ctx->completion_lock, flags);
1728 __io_fail_links(req);
1729 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1730 } else {
1731 __io_fail_links(req);
1732 }
1733
Jens Axboe9e645e112019-05-10 16:07:28 -06001734 io_cqring_ev_posted(ctx);
1735}
1736
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001737static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001738{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001739 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001740 if (req->flags & REQ_F_LINK_TIMEOUT)
1741 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001742
Jens Axboe9e645e112019-05-10 16:07:28 -06001743 /*
1744 * If LINK is set, we have dependent requests in this chain. If we
1745 * didn't fail this request, queue the first one up, moving any other
1746 * dependencies to the next request. In case of failure, fail the rest
1747 * of the chain.
1748 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001749 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1750 return io_req_link_next(req);
1751 io_fail_links(req);
1752 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001753}
Jens Axboe2665abf2019-11-05 12:40:47 -07001754
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001755static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1756{
1757 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1758 return NULL;
1759 return __io_req_find_next(req);
1760}
1761
Jens Axboefd7d6de2020-08-23 11:00:37 -06001762static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1763 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001764{
1765 struct task_struct *tsk = req->task;
1766 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001767 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001768
Jens Axboe6200b0a2020-09-13 14:38:30 -06001769 if (tsk->flags & PF_EXITING)
1770 return -ESRCH;
1771
Jens Axboec2c4c832020-07-01 15:37:11 -06001772 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001773 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1774 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1775 * processing task_work. There's no reliable way to tell if TWA_RESUME
1776 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001777 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001778 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001779 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001780 notify = TWA_SIGNAL;
1781
1782 ret = task_work_add(tsk, cb, notify);
1783 if (!ret)
1784 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001785
Jens Axboec2c4c832020-07-01 15:37:11 -06001786 return ret;
1787}
1788
Jens Axboec40f6372020-06-25 15:39:59 -06001789static void __io_req_task_cancel(struct io_kiocb *req, int error)
1790{
1791 struct io_ring_ctx *ctx = req->ctx;
1792
1793 spin_lock_irq(&ctx->completion_lock);
1794 io_cqring_fill_event(req, error);
1795 io_commit_cqring(ctx);
1796 spin_unlock_irq(&ctx->completion_lock);
1797
1798 io_cqring_ev_posted(ctx);
1799 req_set_fail_links(req);
1800 io_double_put_req(req);
1801}
1802
1803static void io_req_task_cancel(struct callback_head *cb)
1804{
1805 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001806 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001807
1808 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001809 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001810}
1811
1812static void __io_req_task_submit(struct io_kiocb *req)
1813{
1814 struct io_ring_ctx *ctx = req->ctx;
1815
Jens Axboec40f6372020-06-25 15:39:59 -06001816 if (!__io_sq_thread_acquire_mm(ctx)) {
1817 mutex_lock(&ctx->uring_lock);
1818 __io_queue_sqe(req, NULL, NULL);
1819 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001820 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001821 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001822 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001823}
1824
Jens Axboec40f6372020-06-25 15:39:59 -06001825static void io_req_task_submit(struct callback_head *cb)
1826{
1827 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001828 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001829
1830 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001831 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001832}
1833
1834static void io_req_task_queue(struct io_kiocb *req)
1835{
Jens Axboec40f6372020-06-25 15:39:59 -06001836 int ret;
1837
1838 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001839 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001840
Jens Axboefd7d6de2020-08-23 11:00:37 -06001841 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001842 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001843 struct task_struct *tsk;
1844
Jens Axboec40f6372020-06-25 15:39:59 -06001845 init_task_work(&req->task_work, io_req_task_cancel);
1846 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001847 task_work_add(tsk, &req->task_work, 0);
1848 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001849 }
Jens Axboec40f6372020-06-25 15:39:59 -06001850}
1851
Pavel Begunkovc3524382020-06-28 12:52:32 +03001852static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001853{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001854 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001855
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001856 if (nxt)
1857 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001858}
1859
Jens Axboe9e645e112019-05-10 16:07:28 -06001860static void io_free_req(struct io_kiocb *req)
1861{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001862 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001863 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001864}
1865
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001866struct req_batch {
1867 void *reqs[IO_IOPOLL_BATCH];
1868 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001869
1870 struct task_struct *task;
1871 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001872};
1873
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001874static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001875{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001876 rb->to_free = 0;
1877 rb->task_refs = 0;
1878 rb->task = NULL;
1879}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001880
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001881static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1882 struct req_batch *rb)
1883{
1884 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1885 percpu_ref_put_many(&ctx->refs, rb->to_free);
1886 rb->to_free = 0;
1887}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001888
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001889static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1890 struct req_batch *rb)
1891{
1892 if (rb->to_free)
1893 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001894 if (rb->task) {
1895 put_task_struct_many(rb->task, rb->task_refs);
1896 rb->task = NULL;
1897 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001898}
1899
1900static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1901{
1902 if (unlikely(io_is_fallback_req(req))) {
1903 io_free_req(req);
1904 return;
1905 }
1906 if (req->flags & REQ_F_LINK_HEAD)
1907 io_queue_next(req);
1908
Jens Axboee3bc8e92020-09-24 08:45:57 -06001909 if (req->task != rb->task) {
1910 if (rb->task)
1911 put_task_struct_many(rb->task, rb->task_refs);
1912 rb->task = req->task;
1913 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001914 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001915 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001916
Jens Axboe51a4cc12020-08-10 10:55:56 -06001917 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001918 rb->reqs[rb->to_free++] = req;
1919 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1920 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001921}
1922
Jens Axboeba816ad2019-09-28 11:36:45 -06001923/*
1924 * Drop reference to request, return next in chain (if there is one) if this
1925 * was the last reference to this request.
1926 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001927static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001928{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001929 struct io_kiocb *nxt = NULL;
1930
Jens Axboe2a44f462020-02-25 13:25:41 -07001931 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001932 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001933 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001934 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001935 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001936}
1937
Jens Axboe2b188cc2019-01-07 10:46:33 -07001938static void io_put_req(struct io_kiocb *req)
1939{
Jens Axboedef596e2019-01-09 08:59:42 -07001940 if (refcount_dec_and_test(&req->refs))
1941 io_free_req(req);
1942}
1943
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001944static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001945{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001946 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001947
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001948 /*
1949 * A ref is owned by io-wq in which context we're. So, if that's the
1950 * last one, it's safe to steal next work. False negatives are Ok,
1951 * it just will be re-punted async in io_put_work()
1952 */
1953 if (refcount_read(&req->refs) != 1)
1954 return NULL;
1955
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001956 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001957 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001958}
1959
Jens Axboe978db572019-11-14 22:39:04 -07001960/*
1961 * Must only be used if we don't need to care about links, usually from
1962 * within the completion handling itself.
1963 */
1964static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001965{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001966 /* drop both submit and complete references */
1967 if (refcount_sub_and_test(2, &req->refs))
1968 __io_free_req(req);
1969}
1970
Jens Axboe978db572019-11-14 22:39:04 -07001971static void io_double_put_req(struct io_kiocb *req)
1972{
1973 /* drop both submit and complete references */
1974 if (refcount_sub_and_test(2, &req->refs))
1975 io_free_req(req);
1976}
1977
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001978static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001979{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001980 struct io_rings *rings = ctx->rings;
1981
Jens Axboead3eb2c2019-12-18 17:12:20 -07001982 if (test_bit(0, &ctx->cq_check_overflow)) {
1983 /*
1984 * noflush == true is from the waitqueue handler, just ensure
1985 * we wake up the task, and the next invocation will flush the
1986 * entries. We cannot safely to it from here.
1987 */
1988 if (noflush && !list_empty(&ctx->cq_overflow_list))
1989 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001990
Jens Axboead3eb2c2019-12-18 17:12:20 -07001991 io_cqring_overflow_flush(ctx, false);
1992 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001993
Jens Axboea3a0e432019-08-20 11:03:11 -06001994 /* See comment at the top of this file */
1995 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001996 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001997}
1998
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001999static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2000{
2001 struct io_rings *rings = ctx->rings;
2002
2003 /* make sure SQ entry isn't read before tail */
2004 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2005}
2006
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002007static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002008{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002009 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002010
Jens Axboebcda7ba2020-02-23 16:42:51 -07002011 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2012 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002013 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002014 kfree(kbuf);
2015 return cflags;
2016}
2017
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002018static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2019{
2020 struct io_buffer *kbuf;
2021
2022 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2023 return io_put_kbuf(req, kbuf);
2024}
2025
Jens Axboe4c6e2772020-07-01 11:29:10 -06002026static inline bool io_run_task_work(void)
2027{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002028 /*
2029 * Not safe to run on exiting task, and the task_work handling will
2030 * not add work to such a task.
2031 */
2032 if (unlikely(current->flags & PF_EXITING))
2033 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002034 if (current->task_works) {
2035 __set_current_state(TASK_RUNNING);
2036 task_work_run();
2037 return true;
2038 }
2039
2040 return false;
2041}
2042
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002043static void io_iopoll_queue(struct list_head *again)
2044{
2045 struct io_kiocb *req;
2046
2047 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002048 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2049 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002050 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002051 } while (!list_empty(again));
2052}
2053
Jens Axboedef596e2019-01-09 08:59:42 -07002054/*
2055 * Find and free completed poll iocbs
2056 */
2057static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2058 struct list_head *done)
2059{
Jens Axboe8237e042019-12-28 10:48:22 -07002060 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002061 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002062 LIST_HEAD(again);
2063
2064 /* order with ->result store in io_complete_rw_iopoll() */
2065 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002066
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002067 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002068 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002069 int cflags = 0;
2070
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002071 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002072 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002073 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002074 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002075 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002076 continue;
2077 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002078 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002079
Jens Axboebcda7ba2020-02-23 16:42:51 -07002080 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002081 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002082
2083 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002084 (*nr_events)++;
2085
Pavel Begunkovc3524382020-06-28 12:52:32 +03002086 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002087 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002088 }
Jens Axboedef596e2019-01-09 08:59:42 -07002089
Jens Axboe09bb8392019-03-13 12:39:28 -06002090 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002091 if (ctx->flags & IORING_SETUP_SQPOLL)
2092 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002093 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002094
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002095 if (!list_empty(&again))
2096 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002097}
2098
Jens Axboedef596e2019-01-09 08:59:42 -07002099static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2100 long min)
2101{
2102 struct io_kiocb *req, *tmp;
2103 LIST_HEAD(done);
2104 bool spin;
2105 int ret;
2106
2107 /*
2108 * Only spin for completions if we don't have multiple devices hanging
2109 * off our complete list, and we're under the requested amount.
2110 */
2111 spin = !ctx->poll_multi_file && *nr_events < min;
2112
2113 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002114 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002115 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002116
2117 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002118 * Move completed and retryable entries to our local lists.
2119 * If we find a request that requires polling, break out
2120 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002121 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002122 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002123 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002124 continue;
2125 }
2126 if (!list_empty(&done))
2127 break;
2128
2129 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2130 if (ret < 0)
2131 break;
2132
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002133 /* iopoll may have completed current req */
2134 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002135 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002136
Jens Axboedef596e2019-01-09 08:59:42 -07002137 if (ret && spin)
2138 spin = false;
2139 ret = 0;
2140 }
2141
2142 if (!list_empty(&done))
2143 io_iopoll_complete(ctx, nr_events, &done);
2144
2145 return ret;
2146}
2147
2148/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002149 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002150 * non-spinning poll check - we'll still enter the driver poll loop, but only
2151 * as a non-spinning completion check.
2152 */
2153static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2154 long min)
2155{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002156 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002157 int ret;
2158
2159 ret = io_do_iopoll(ctx, nr_events, min);
2160 if (ret < 0)
2161 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002162 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002163 return 0;
2164 }
2165
2166 return 1;
2167}
2168
2169/*
2170 * We can't just wait for polled events to come to us, we have to actively
2171 * find and complete them.
2172 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002173static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002174{
2175 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2176 return;
2177
2178 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002179 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002180 unsigned int nr_events = 0;
2181
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002182 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002183
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002184 /* let it sleep and repeat later if can't complete a request */
2185 if (nr_events == 0)
2186 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002187 /*
2188 * Ensure we allow local-to-the-cpu processing to take place,
2189 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002190 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002191 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002192 if (need_resched()) {
2193 mutex_unlock(&ctx->uring_lock);
2194 cond_resched();
2195 mutex_lock(&ctx->uring_lock);
2196 }
Jens Axboedef596e2019-01-09 08:59:42 -07002197 }
2198 mutex_unlock(&ctx->uring_lock);
2199}
2200
Pavel Begunkov7668b922020-07-07 16:36:21 +03002201static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002202{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002203 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002204 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002205
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002206 /*
2207 * We disallow the app entering submit/complete with polling, but we
2208 * still need to lock the ring to prevent racing with polled issue
2209 * that got punted to a workqueue.
2210 */
2211 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002212 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002213 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002214 * Don't enter poll loop if we already have events pending.
2215 * If we do, we can potentially be spinning for commands that
2216 * already triggered a CQE (eg in error).
2217 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002218 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002219 break;
2220
2221 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002222 * If a submit got punted to a workqueue, we can have the
2223 * application entering polling for a command before it gets
2224 * issued. That app will hold the uring_lock for the duration
2225 * of the poll right here, so we need to take a breather every
2226 * now and then to ensure that the issue has a chance to add
2227 * the poll to the issued list. Otherwise we can spin here
2228 * forever, while the workqueue is stuck trying to acquire the
2229 * very same mutex.
2230 */
2231 if (!(++iters & 7)) {
2232 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002233 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002234 mutex_lock(&ctx->uring_lock);
2235 }
2236
Pavel Begunkov7668b922020-07-07 16:36:21 +03002237 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002238 if (ret <= 0)
2239 break;
2240 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002241 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002242
Jens Axboe500f9fb2019-08-19 12:15:59 -06002243 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002244 return ret;
2245}
2246
Jens Axboe491381ce2019-10-17 09:20:46 -06002247static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248{
Jens Axboe491381ce2019-10-17 09:20:46 -06002249 /*
2250 * Tell lockdep we inherited freeze protection from submission
2251 * thread.
2252 */
2253 if (req->flags & REQ_F_ISREG) {
2254 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002255
Jens Axboe491381ce2019-10-17 09:20:46 -06002256 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002257 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002258 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002259}
2260
Jens Axboea1d7c392020-06-22 11:09:46 -06002261static void io_complete_rw_common(struct kiocb *kiocb, long res,
2262 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002263{
Jens Axboe9adbd452019-12-20 08:45:55 -07002264 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002265 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002266
Jens Axboe491381ce2019-10-17 09:20:46 -06002267 if (kiocb->ki_flags & IOCB_WRITE)
2268 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002269
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002270 if (res != req->result)
2271 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002272 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002273 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002274 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002275}
2276
Jens Axboeb63534c2020-06-04 11:28:00 -06002277#ifdef CONFIG_BLOCK
2278static bool io_resubmit_prep(struct io_kiocb *req, int error)
2279{
2280 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2281 ssize_t ret = -ECANCELED;
2282 struct iov_iter iter;
2283 int rw;
2284
2285 if (error) {
2286 ret = error;
2287 goto end_req;
2288 }
2289
2290 switch (req->opcode) {
2291 case IORING_OP_READV:
2292 case IORING_OP_READ_FIXED:
2293 case IORING_OP_READ:
2294 rw = READ;
2295 break;
2296 case IORING_OP_WRITEV:
2297 case IORING_OP_WRITE_FIXED:
2298 case IORING_OP_WRITE:
2299 rw = WRITE;
2300 break;
2301 default:
2302 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2303 req->opcode);
2304 goto end_req;
2305 }
2306
Jens Axboe8f3d7492020-09-14 09:28:14 -06002307 if (!req->io) {
2308 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2309 if (ret < 0)
2310 goto end_req;
2311 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2312 if (!ret)
2313 return true;
2314 kfree(iovec);
2315 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002316 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002317 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002318end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002319 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002320 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002321 return false;
2322}
Jens Axboeb63534c2020-06-04 11:28:00 -06002323#endif
2324
2325static bool io_rw_reissue(struct io_kiocb *req, long res)
2326{
2327#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002328 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002329 int ret;
2330
Jens Axboe355afae2020-09-02 09:30:31 -06002331 if (!S_ISBLK(mode) && !S_ISREG(mode))
2332 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002333 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2334 return false;
2335
Jens Axboefdee9462020-08-27 16:46:24 -06002336 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002337
Jens Axboefdee9462020-08-27 16:46:24 -06002338 if (io_resubmit_prep(req, ret)) {
2339 refcount_inc(&req->refs);
2340 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002341 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002342 }
2343
Jens Axboeb63534c2020-06-04 11:28:00 -06002344#endif
2345 return false;
2346}
2347
Jens Axboea1d7c392020-06-22 11:09:46 -06002348static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2349 struct io_comp_state *cs)
2350{
2351 if (!io_rw_reissue(req, res))
2352 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002353}
2354
2355static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2356{
Jens Axboe9adbd452019-12-20 08:45:55 -07002357 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002358
Jens Axboea1d7c392020-06-22 11:09:46 -06002359 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360}
2361
Jens Axboedef596e2019-01-09 08:59:42 -07002362static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2363{
Jens Axboe9adbd452019-12-20 08:45:55 -07002364 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002365
Jens Axboe491381ce2019-10-17 09:20:46 -06002366 if (kiocb->ki_flags & IOCB_WRITE)
2367 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002368
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002369 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002370 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002371
2372 WRITE_ONCE(req->result, res);
2373 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002374 smp_wmb();
2375 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002376}
2377
2378/*
2379 * After the iocb has been issued, it's safe to be found on the poll list.
2380 * Adding the kiocb to the list AFTER submission ensures that we don't
2381 * find it from a io_iopoll_getevents() thread before the issuer is done
2382 * accessing the kiocb cookie.
2383 */
2384static void io_iopoll_req_issued(struct io_kiocb *req)
2385{
2386 struct io_ring_ctx *ctx = req->ctx;
2387
2388 /*
2389 * Track whether we have multiple files in our lists. This will impact
2390 * how we do polling eventually, not spinning if we're on potentially
2391 * different devices.
2392 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002393 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002394 ctx->poll_multi_file = false;
2395 } else if (!ctx->poll_multi_file) {
2396 struct io_kiocb *list_req;
2397
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002398 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002399 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002400 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002401 ctx->poll_multi_file = true;
2402 }
2403
2404 /*
2405 * For fast devices, IO may have already completed. If it has, add
2406 * it to the front so we find it first.
2407 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002408 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002409 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002410 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002411 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002412
2413 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2414 wq_has_sleeper(&ctx->sqo_wait))
2415 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002416}
2417
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002418static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002419{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002420 if (state->has_refs)
2421 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002422 state->file = NULL;
2423}
2424
2425static inline void io_state_file_put(struct io_submit_state *state)
2426{
2427 if (state->file)
2428 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002429}
2430
2431/*
2432 * Get as many references to a file as we have IOs left in this submission,
2433 * assuming most submissions are for one file, or at least that each file
2434 * has more than one submission.
2435 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002436static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002437{
2438 if (!state)
2439 return fget(fd);
2440
2441 if (state->file) {
2442 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002443 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002444 state->ios_left--;
2445 return state->file;
2446 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002447 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002448 }
2449 state->file = fget_many(fd, state->ios_left);
2450 if (!state->file)
2451 return NULL;
2452
2453 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002454 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002455 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002456 return state->file;
2457}
2458
Jens Axboe4503b762020-06-01 10:00:27 -06002459static bool io_bdev_nowait(struct block_device *bdev)
2460{
2461#ifdef CONFIG_BLOCK
2462 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2463#else
2464 return true;
2465#endif
2466}
2467
Jens Axboe2b188cc2019-01-07 10:46:33 -07002468/*
2469 * If we tracked the file through the SCM inflight mechanism, we could support
2470 * any file. For now, just ensure that anything potentially problematic is done
2471 * inline.
2472 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002473static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002474{
2475 umode_t mode = file_inode(file)->i_mode;
2476
Jens Axboe4503b762020-06-01 10:00:27 -06002477 if (S_ISBLK(mode)) {
2478 if (io_bdev_nowait(file->f_inode->i_bdev))
2479 return true;
2480 return false;
2481 }
2482 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002483 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002484 if (S_ISREG(mode)) {
2485 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2486 file->f_op != &io_uring_fops)
2487 return true;
2488 return false;
2489 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002490
Jens Axboec5b85622020-06-09 19:23:05 -06002491 /* any ->read/write should understand O_NONBLOCK */
2492 if (file->f_flags & O_NONBLOCK)
2493 return true;
2494
Jens Axboeaf197f52020-04-28 13:15:06 -06002495 if (!(file->f_mode & FMODE_NOWAIT))
2496 return false;
2497
2498 if (rw == READ)
2499 return file->f_op->read_iter != NULL;
2500
2501 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002502}
2503
Jens Axboe3529d8c2019-12-19 18:24:38 -07002504static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2505 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002506{
Jens Axboedef596e2019-01-09 08:59:42 -07002507 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002508 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002509 unsigned ioprio;
2510 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002511
Jens Axboe491381ce2019-10-17 09:20:46 -06002512 if (S_ISREG(file_inode(req->file)->i_mode))
2513 req->flags |= REQ_F_ISREG;
2514
Jens Axboe2b188cc2019-01-07 10:46:33 -07002515 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002516 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2517 req->flags |= REQ_F_CUR_POS;
2518 kiocb->ki_pos = req->file->f_pos;
2519 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002521 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2522 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2523 if (unlikely(ret))
2524 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002525
2526 ioprio = READ_ONCE(sqe->ioprio);
2527 if (ioprio) {
2528 ret = ioprio_check_cap(ioprio);
2529 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002530 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002531
2532 kiocb->ki_ioprio = ioprio;
2533 } else
2534 kiocb->ki_ioprio = get_current_ioprio();
2535
Stefan Bühler8449eed2019-04-27 20:34:19 +02002536 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002537 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002538 req->flags |= REQ_F_NOWAIT;
2539
2540 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002541 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002542
Jens Axboedef596e2019-01-09 08:59:42 -07002543 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002544 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2545 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002546 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547
Jens Axboedef596e2019-01-09 08:59:42 -07002548 kiocb->ki_flags |= IOCB_HIPRI;
2549 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002550 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002551 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002552 if (kiocb->ki_flags & IOCB_HIPRI)
2553 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002554 kiocb->ki_complete = io_complete_rw;
2555 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002556
Jens Axboe3529d8c2019-12-19 18:24:38 -07002557 req->rw.addr = READ_ONCE(sqe->addr);
2558 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002559 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002560 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002561}
2562
2563static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2564{
2565 switch (ret) {
2566 case -EIOCBQUEUED:
2567 break;
2568 case -ERESTARTSYS:
2569 case -ERESTARTNOINTR:
2570 case -ERESTARTNOHAND:
2571 case -ERESTART_RESTARTBLOCK:
2572 /*
2573 * We can't just restart the syscall, since previously
2574 * submitted sqes may already be in progress. Just fail this
2575 * IO with EINTR.
2576 */
2577 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002578 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002579 default:
2580 kiocb->ki_complete(kiocb, ret, 0);
2581 }
2582}
2583
Jens Axboea1d7c392020-06-22 11:09:46 -06002584static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2585 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002586{
Jens Axboeba042912019-12-25 16:33:42 -07002587 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2588
Jens Axboe227c0c92020-08-13 11:51:40 -06002589 /* add previously done IO, if any */
2590 if (req->io && req->io->rw.bytes_done > 0) {
2591 if (ret < 0)
2592 ret = req->io->rw.bytes_done;
2593 else
2594 ret += req->io->rw.bytes_done;
2595 }
2596
Jens Axboeba042912019-12-25 16:33:42 -07002597 if (req->flags & REQ_F_CUR_POS)
2598 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002599 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002600 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002601 else
2602 io_rw_done(kiocb, ret);
2603}
2604
Jens Axboe9adbd452019-12-20 08:45:55 -07002605static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002606 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002607{
Jens Axboe9adbd452019-12-20 08:45:55 -07002608 struct io_ring_ctx *ctx = req->ctx;
2609 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002610 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002611 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002612 size_t offset;
2613 u64 buf_addr;
2614
2615 /* attempt to use fixed buffers without having provided iovecs */
2616 if (unlikely(!ctx->user_bufs))
2617 return -EFAULT;
2618
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002619 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002620 if (unlikely(buf_index >= ctx->nr_user_bufs))
2621 return -EFAULT;
2622
2623 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2624 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002625 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002626
2627 /* overflow */
2628 if (buf_addr + len < buf_addr)
2629 return -EFAULT;
2630 /* not inside the mapped region */
2631 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2632 return -EFAULT;
2633
2634 /*
2635 * May not be a start of buffer, set size appropriately
2636 * and advance us to the beginning.
2637 */
2638 offset = buf_addr - imu->ubuf;
2639 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002640
2641 if (offset) {
2642 /*
2643 * Don't use iov_iter_advance() here, as it's really slow for
2644 * using the latter parts of a big fixed buffer - it iterates
2645 * over each segment manually. We can cheat a bit here, because
2646 * we know that:
2647 *
2648 * 1) it's a BVEC iter, we set it up
2649 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2650 * first and last bvec
2651 *
2652 * So just find our index, and adjust the iterator afterwards.
2653 * If the offset is within the first bvec (or the whole first
2654 * bvec, just use iov_iter_advance(). This makes it easier
2655 * since we can just skip the first segment, which may not
2656 * be PAGE_SIZE aligned.
2657 */
2658 const struct bio_vec *bvec = imu->bvec;
2659
2660 if (offset <= bvec->bv_len) {
2661 iov_iter_advance(iter, offset);
2662 } else {
2663 unsigned long seg_skip;
2664
2665 /* skip first vec */
2666 offset -= bvec->bv_len;
2667 seg_skip = 1 + (offset >> PAGE_SHIFT);
2668
2669 iter->bvec = bvec + seg_skip;
2670 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002671 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002672 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002673 }
2674 }
2675
Jens Axboe5e559562019-11-13 16:12:46 -07002676 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002677}
2678
Jens Axboebcda7ba2020-02-23 16:42:51 -07002679static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2680{
2681 if (needs_lock)
2682 mutex_unlock(&ctx->uring_lock);
2683}
2684
2685static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2686{
2687 /*
2688 * "Normal" inline submissions always hold the uring_lock, since we
2689 * grab it from the system call. Same is true for the SQPOLL offload.
2690 * The only exception is when we've detached the request and issue it
2691 * from an async worker thread, grab the lock for that case.
2692 */
2693 if (needs_lock)
2694 mutex_lock(&ctx->uring_lock);
2695}
2696
2697static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2698 int bgid, struct io_buffer *kbuf,
2699 bool needs_lock)
2700{
2701 struct io_buffer *head;
2702
2703 if (req->flags & REQ_F_BUFFER_SELECTED)
2704 return kbuf;
2705
2706 io_ring_submit_lock(req->ctx, needs_lock);
2707
2708 lockdep_assert_held(&req->ctx->uring_lock);
2709
2710 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2711 if (head) {
2712 if (!list_empty(&head->list)) {
2713 kbuf = list_last_entry(&head->list, struct io_buffer,
2714 list);
2715 list_del(&kbuf->list);
2716 } else {
2717 kbuf = head;
2718 idr_remove(&req->ctx->io_buffer_idr, bgid);
2719 }
2720 if (*len > kbuf->len)
2721 *len = kbuf->len;
2722 } else {
2723 kbuf = ERR_PTR(-ENOBUFS);
2724 }
2725
2726 io_ring_submit_unlock(req->ctx, needs_lock);
2727
2728 return kbuf;
2729}
2730
Jens Axboe4d954c22020-02-27 07:31:19 -07002731static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2732 bool needs_lock)
2733{
2734 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002735 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002736
2737 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002738 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002739 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2740 if (IS_ERR(kbuf))
2741 return kbuf;
2742 req->rw.addr = (u64) (unsigned long) kbuf;
2743 req->flags |= REQ_F_BUFFER_SELECTED;
2744 return u64_to_user_ptr(kbuf->addr);
2745}
2746
2747#ifdef CONFIG_COMPAT
2748static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2749 bool needs_lock)
2750{
2751 struct compat_iovec __user *uiov;
2752 compat_ssize_t clen;
2753 void __user *buf;
2754 ssize_t len;
2755
2756 uiov = u64_to_user_ptr(req->rw.addr);
2757 if (!access_ok(uiov, sizeof(*uiov)))
2758 return -EFAULT;
2759 if (__get_user(clen, &uiov->iov_len))
2760 return -EFAULT;
2761 if (clen < 0)
2762 return -EINVAL;
2763
2764 len = clen;
2765 buf = io_rw_buffer_select(req, &len, needs_lock);
2766 if (IS_ERR(buf))
2767 return PTR_ERR(buf);
2768 iov[0].iov_base = buf;
2769 iov[0].iov_len = (compat_size_t) len;
2770 return 0;
2771}
2772#endif
2773
2774static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2775 bool needs_lock)
2776{
2777 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2778 void __user *buf;
2779 ssize_t len;
2780
2781 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2782 return -EFAULT;
2783
2784 len = iov[0].iov_len;
2785 if (len < 0)
2786 return -EINVAL;
2787 buf = io_rw_buffer_select(req, &len, needs_lock);
2788 if (IS_ERR(buf))
2789 return PTR_ERR(buf);
2790 iov[0].iov_base = buf;
2791 iov[0].iov_len = len;
2792 return 0;
2793}
2794
2795static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2796 bool needs_lock)
2797{
Jens Axboedddb3e22020-06-04 11:27:01 -06002798 if (req->flags & REQ_F_BUFFER_SELECTED) {
2799 struct io_buffer *kbuf;
2800
2801 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2802 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2803 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002804 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002805 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002806 if (!req->rw.len)
2807 return 0;
2808 else if (req->rw.len > 1)
2809 return -EINVAL;
2810
2811#ifdef CONFIG_COMPAT
2812 if (req->ctx->compat)
2813 return io_compat_import(req, iov, needs_lock);
2814#endif
2815
2816 return __io_iov_buffer_select(req, iov, needs_lock);
2817}
2818
Jens Axboe8452fd02020-08-18 13:58:33 -07002819static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2820 struct iovec **iovec, struct iov_iter *iter,
2821 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002822{
Jens Axboe9adbd452019-12-20 08:45:55 -07002823 void __user *buf = u64_to_user_ptr(req->rw.addr);
2824 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002825 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002826 u8 opcode;
2827
Jens Axboed625c6e2019-12-17 19:53:05 -07002828 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002829 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002830 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002831 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002832 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002833
Jens Axboebcda7ba2020-02-23 16:42:51 -07002834 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002835 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002836 return -EINVAL;
2837
Jens Axboe3a6820f2019-12-22 15:19:35 -07002838 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002839 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002840 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002841 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002842 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002843 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002844 }
2845
Jens Axboe3a6820f2019-12-22 15:19:35 -07002846 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2847 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002848 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002849 }
2850
Jens Axboe4d954c22020-02-27 07:31:19 -07002851 if (req->flags & REQ_F_BUFFER_SELECT) {
2852 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002853 if (!ret) {
2854 ret = (*iovec)->iov_len;
2855 iov_iter_init(iter, rw, *iovec, 1, ret);
2856 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002857 *iovec = NULL;
2858 return ret;
2859 }
2860
Jens Axboe2b188cc2019-01-07 10:46:33 -07002861#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002862 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002863 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2864 iovec, iter);
2865#endif
2866
2867 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2868}
2869
Jens Axboe8452fd02020-08-18 13:58:33 -07002870static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2871 struct iovec **iovec, struct iov_iter *iter,
2872 bool needs_lock)
2873{
2874 if (!req->io)
2875 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2876 *iovec = NULL;
2877 return iov_iter_count(&req->io->rw.iter);
2878}
2879
Jens Axboe0fef9482020-08-26 10:36:20 -06002880static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2881{
2882 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2883}
2884
Jens Axboe32960612019-09-23 11:05:34 -06002885/*
2886 * For files that don't have ->read_iter() and ->write_iter(), handle them
2887 * by looping over ->read() or ->write() manually.
2888 */
2889static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2890 struct iov_iter *iter)
2891{
2892 ssize_t ret = 0;
2893
2894 /*
2895 * Don't support polled IO through this interface, and we can't
2896 * support non-blocking either. For the latter, this just causes
2897 * the kiocb to be handled from an async context.
2898 */
2899 if (kiocb->ki_flags & IOCB_HIPRI)
2900 return -EOPNOTSUPP;
2901 if (kiocb->ki_flags & IOCB_NOWAIT)
2902 return -EAGAIN;
2903
2904 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002905 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002906 ssize_t nr;
2907
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002908 if (!iov_iter_is_bvec(iter)) {
2909 iovec = iov_iter_iovec(iter);
2910 } else {
2911 /* fixed buffers import bvec */
2912 iovec.iov_base = kmap(iter->bvec->bv_page)
2913 + iter->iov_offset;
2914 iovec.iov_len = min(iter->count,
2915 iter->bvec->bv_len - iter->iov_offset);
2916 }
2917
Jens Axboe32960612019-09-23 11:05:34 -06002918 if (rw == READ) {
2919 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002920 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002921 } else {
2922 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002923 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002924 }
2925
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002926 if (iov_iter_is_bvec(iter))
2927 kunmap(iter->bvec->bv_page);
2928
Jens Axboe32960612019-09-23 11:05:34 -06002929 if (nr < 0) {
2930 if (!ret)
2931 ret = nr;
2932 break;
2933 }
2934 ret += nr;
2935 if (nr != iovec.iov_len)
2936 break;
2937 iov_iter_advance(iter, nr);
2938 }
2939
2940 return ret;
2941}
2942
Jens Axboeff6165b2020-08-13 09:47:43 -06002943static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2944 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07002945{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002946 struct io_async_rw *rw = &req->io->rw;
2947
Jens Axboeff6165b2020-08-13 09:47:43 -06002948 memcpy(&rw->iter, iter, sizeof(*iter));
2949 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06002950 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06002951 /* can only be fixed buffers, no need to do anything */
2952 if (iter->type == ITER_BVEC)
2953 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002954 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06002955 unsigned iov_off = 0;
2956
2957 rw->iter.iov = rw->fast_iov;
2958 if (iter->iov != fast_iov) {
2959 iov_off = iter->iov - fast_iov;
2960 rw->iter.iov += iov_off;
2961 }
2962 if (rw->fast_iov != fast_iov)
2963 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002964 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002965 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06002966 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002967 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002968 }
2969}
2970
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002971static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2972{
2973 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2974 return req->io == NULL;
2975}
2976
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002977static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002978{
Jens Axboed3656342019-12-18 09:50:26 -07002979 if (!io_op_defs[req->opcode].async_ctx)
2980 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002981
2982 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002983}
2984
Jens Axboeff6165b2020-08-13 09:47:43 -06002985static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
2986 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06002987 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002988{
Jens Axboe227c0c92020-08-13 11:51:40 -06002989 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002990 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002991 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002992 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002993 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002994
Jens Axboeff6165b2020-08-13 09:47:43 -06002995 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07002996 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002997 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002998}
2999
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003000static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3001 bool force_nonblock)
3002{
Jens Axboeff6165b2020-08-13 09:47:43 -06003003 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06003004 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003005 ssize_t ret;
3006
Jens Axboec183edf2020-09-04 22:36:52 -06003007 iorw->iter.iov = iov = iorw->fast_iov;
3008 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003009 if (unlikely(ret < 0))
3010 return ret;
3011
Jens Axboec183edf2020-09-04 22:36:52 -06003012 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003013 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003014 return 0;
3015}
3016
Jens Axboe3529d8c2019-12-19 18:24:38 -07003017static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3018 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003019{
3020 ssize_t ret;
3021
Jens Axboe3529d8c2019-12-19 18:24:38 -07003022 ret = io_prep_rw(req, sqe, force_nonblock);
3023 if (ret)
3024 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003025
Jens Axboe3529d8c2019-12-19 18:24:38 -07003026 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3027 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003028
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003029 /* either don't need iovec imported or already have it */
3030 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003031 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003032 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003033}
3034
Jens Axboec1dd91d2020-08-03 16:43:59 -06003035/*
3036 * This is our waitqueue callback handler, registered through lock_page_async()
3037 * when we initially tried to do the IO with the iocb armed our waitqueue.
3038 * This gets called when the page is unlocked, and we generally expect that to
3039 * happen when the page IO is completed and the page is now uptodate. This will
3040 * queue a task_work based retry of the operation, attempting to copy the data
3041 * again. If the latter fails because the page was NOT uptodate, then we will
3042 * do a thread based blocking retry of the operation. That's the unexpected
3043 * slow path.
3044 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003045static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3046 int sync, void *arg)
3047{
3048 struct wait_page_queue *wpq;
3049 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003050 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003051 int ret;
3052
3053 wpq = container_of(wait, struct wait_page_queue, wait);
3054
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003055 if (!wake_page_match(wpq, key))
3056 return 0;
3057
Hao Xuc8d317a2020-09-29 20:00:45 +08003058 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003059 list_del_init(&wait->entry);
3060
Pavel Begunkove7375122020-07-12 20:42:04 +03003061 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003062 percpu_ref_get(&req->ctx->refs);
3063
Jens Axboebcf5a062020-05-22 09:24:42 -06003064 /* submit ref gets dropped, acquire a new one */
3065 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003066 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003067 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003068 struct task_struct *tsk;
3069
Jens Axboebcf5a062020-05-22 09:24:42 -06003070 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003071 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003072 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003073 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003074 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003075 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003076 return 1;
3077}
3078
Jens Axboec1dd91d2020-08-03 16:43:59 -06003079/*
3080 * This controls whether a given IO request should be armed for async page
3081 * based retry. If we return false here, the request is handed to the async
3082 * worker threads for retry. If we're doing buffered reads on a regular file,
3083 * we prepare a private wait_page_queue entry and retry the operation. This
3084 * will either succeed because the page is now uptodate and unlocked, or it
3085 * will register a callback when the page is unlocked at IO completion. Through
3086 * that callback, io_uring uses task_work to setup a retry of the operation.
3087 * That retry will attempt the buffered read again. The retry will generally
3088 * succeed, or in rare cases where it fails, we then fall back to using the
3089 * async worker threads for a blocking retry.
3090 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003091static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003092{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003093 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003094 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003095
3096 /* never retry for NOWAIT, we just complete with -EAGAIN */
3097 if (req->flags & REQ_F_NOWAIT)
3098 return false;
3099
Jens Axboe227c0c92020-08-13 11:51:40 -06003100 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003101 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003102 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003103
Jens Axboebcf5a062020-05-22 09:24:42 -06003104 /*
3105 * just use poll if we can, and don't attempt if the fs doesn't
3106 * support callback based unlocks
3107 */
3108 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3109 return false;
3110
Jens Axboe3b2a4432020-08-16 10:58:43 -07003111 wait->wait.func = io_async_buf_func;
3112 wait->wait.private = req;
3113 wait->wait.flags = 0;
3114 INIT_LIST_HEAD(&wait->wait.entry);
3115 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003116 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003117 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003118 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003119}
3120
3121static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3122{
3123 if (req->file->f_op->read_iter)
3124 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003125 else if (req->file->f_op->read)
3126 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3127 else
3128 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003129}
3130
Jens Axboea1d7c392020-06-22 11:09:46 -06003131static int io_read(struct io_kiocb *req, bool force_nonblock,
3132 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003133{
3134 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003135 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003136 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003137 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003138 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003139 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003140
Jens Axboeff6165b2020-08-13 09:47:43 -06003141 if (req->io)
3142 iter = &req->io->rw.iter;
3143
3144 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003145 if (ret < 0)
3146 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003147 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003148 io_size = ret;
3149 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003150 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003151
Jens Axboefd6c2e42019-12-18 12:19:41 -07003152 /* Ensure we clear previously set non-block flag */
3153 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003154 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003155
Pavel Begunkov24c74672020-06-21 13:09:51 +03003156 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003157 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3158 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003159 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003160
Jens Axboe0fef9482020-08-26 10:36:20 -06003161 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003162 if (unlikely(ret))
3163 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003164
Jens Axboe227c0c92020-08-13 11:51:40 -06003165 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003166
Jens Axboe227c0c92020-08-13 11:51:40 -06003167 if (!ret) {
3168 goto done;
3169 } else if (ret == -EIOCBQUEUED) {
3170 ret = 0;
3171 goto out_free;
3172 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003173 /* IOPOLL retry should happen for io-wq threads */
3174 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003175 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003176 /* no retry on NONBLOCK marked file */
3177 if (req->file->f_flags & O_NONBLOCK)
3178 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003179 /* some cases will consume bytes even on error returns */
3180 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003181 ret = 0;
3182 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003183 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003184 /* make sure -ERESTARTSYS -> -EINTR is done */
3185 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003186 }
3187
3188 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003189 if (!iov_iter_count(iter) || !force_nonblock ||
3190 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003191 goto done;
3192
3193 io_size -= ret;
3194copy_iov:
3195 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3196 if (ret2) {
3197 ret = ret2;
3198 goto out_free;
3199 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003200 if (no_async)
3201 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003202 /* it's copied and will be cleaned with ->io */
3203 iovec = NULL;
3204 /* now use our persistent iterator, if we aren't already */
3205 iter = &req->io->rw.iter;
3206retry:
3207 req->io->rw.bytes_done += ret;
3208 /* if we can retry, do so with the callbacks armed */
3209 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003210 kiocb->ki_flags &= ~IOCB_WAITQ;
3211 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003212 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003213
3214 /*
3215 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3216 * get -EIOCBQUEUED, then we'll get a notification when the desired
3217 * page gets unlocked. We can also get a partial read here, and if we
3218 * do, then just retry at the new offset.
3219 */
3220 ret = io_iter_do_read(req, iter);
3221 if (ret == -EIOCBQUEUED) {
3222 ret = 0;
3223 goto out_free;
3224 } else if (ret > 0 && ret < io_size) {
3225 /* we got some bytes, but not all. retry. */
3226 goto retry;
3227 }
3228done:
3229 kiocb_done(kiocb, ret, cs);
3230 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003231out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003232 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003233 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003234 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003235 return ret;
3236}
3237
Jens Axboe3529d8c2019-12-19 18:24:38 -07003238static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3239 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003240{
3241 ssize_t ret;
3242
Jens Axboe3529d8c2019-12-19 18:24:38 -07003243 ret = io_prep_rw(req, sqe, force_nonblock);
3244 if (ret)
3245 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003246
Jens Axboe3529d8c2019-12-19 18:24:38 -07003247 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3248 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003249
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003250 /* either don't need iovec imported or already have it */
3251 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003252 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003253 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003254}
3255
Jens Axboea1d7c392020-06-22 11:09:46 -06003256static int io_write(struct io_kiocb *req, bool force_nonblock,
3257 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003258{
3259 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003260 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003261 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003262 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003263 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003264
Jens Axboeff6165b2020-08-13 09:47:43 -06003265 if (req->io)
3266 iter = &req->io->rw.iter;
3267
3268 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003269 if (ret < 0)
3270 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003271 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003272 io_size = ret;
3273 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003274
Jens Axboefd6c2e42019-12-18 12:19:41 -07003275 /* Ensure we clear previously set non-block flag */
3276 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003277 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003278
Pavel Begunkov24c74672020-06-21 13:09:51 +03003279 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003280 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003281 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003282
Jens Axboe10d59342019-12-09 20:16:22 -07003283 /* file path doesn't support NOWAIT for non-direct_IO */
3284 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3285 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003286 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003287
Jens Axboe0fef9482020-08-26 10:36:20 -06003288 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003289 if (unlikely(ret))
3290 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003291
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003292 /*
3293 * Open-code file_start_write here to grab freeze protection,
3294 * which will be released by another thread in
3295 * io_complete_rw(). Fool lockdep by telling it the lock got
3296 * released so that it doesn't complain about the held lock when
3297 * we return to userspace.
3298 */
3299 if (req->flags & REQ_F_ISREG) {
3300 __sb_start_write(file_inode(req->file)->i_sb,
3301 SB_FREEZE_WRITE, true);
3302 __sb_writers_release(file_inode(req->file)->i_sb,
3303 SB_FREEZE_WRITE);
3304 }
3305 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003306
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003307 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003308 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003309 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003310 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003311 else
3312 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003313
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003314 /*
3315 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3316 * retry them without IOCB_NOWAIT.
3317 */
3318 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3319 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003320 /* no retry on NONBLOCK marked file */
3321 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3322 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003323 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003324 /* IOPOLL retry should happen for io-wq threads */
3325 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3326 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003327done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003328 kiocb_done(kiocb, ret2, cs);
3329 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003330copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003331 /* some cases will consume bytes even on error returns */
3332 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003333 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003334 if (!ret)
3335 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003336 }
Jens Axboe31b51512019-01-18 22:56:34 -07003337out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003338 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003339 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003340 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003341 return ret;
3342}
3343
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003344static int __io_splice_prep(struct io_kiocb *req,
3345 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003346{
3347 struct io_splice* sp = &req->splice;
3348 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3349 int ret;
3350
3351 if (req->flags & REQ_F_NEED_CLEANUP)
3352 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003353 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3354 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003355
3356 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003357 sp->len = READ_ONCE(sqe->len);
3358 sp->flags = READ_ONCE(sqe->splice_flags);
3359
3360 if (unlikely(sp->flags & ~valid_flags))
3361 return -EINVAL;
3362
3363 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3364 (sp->flags & SPLICE_F_FD_IN_FIXED));
3365 if (ret)
3366 return ret;
3367 req->flags |= REQ_F_NEED_CLEANUP;
3368
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003369 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3370 /*
3371 * Splice operation will be punted aync, and here need to
3372 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3373 */
3374 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003375 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003376 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003377
3378 return 0;
3379}
3380
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003381static int io_tee_prep(struct io_kiocb *req,
3382 const struct io_uring_sqe *sqe)
3383{
3384 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3385 return -EINVAL;
3386 return __io_splice_prep(req, sqe);
3387}
3388
3389static int io_tee(struct io_kiocb *req, bool force_nonblock)
3390{
3391 struct io_splice *sp = &req->splice;
3392 struct file *in = sp->file_in;
3393 struct file *out = sp->file_out;
3394 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3395 long ret = 0;
3396
3397 if (force_nonblock)
3398 return -EAGAIN;
3399 if (sp->len)
3400 ret = do_tee(in, out, sp->len, flags);
3401
3402 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3403 req->flags &= ~REQ_F_NEED_CLEANUP;
3404
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003405 if (ret != sp->len)
3406 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003407 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003408 return 0;
3409}
3410
3411static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3412{
3413 struct io_splice* sp = &req->splice;
3414
3415 sp->off_in = READ_ONCE(sqe->splice_off_in);
3416 sp->off_out = READ_ONCE(sqe->off);
3417 return __io_splice_prep(req, sqe);
3418}
3419
Pavel Begunkov014db002020-03-03 21:33:12 +03003420static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003421{
3422 struct io_splice *sp = &req->splice;
3423 struct file *in = sp->file_in;
3424 struct file *out = sp->file_out;
3425 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3426 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003427 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003428
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003429 if (force_nonblock)
3430 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003431
3432 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3433 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003434
Jens Axboe948a7742020-05-17 14:21:38 -06003435 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003436 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003437
3438 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3439 req->flags &= ~REQ_F_NEED_CLEANUP;
3440
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003441 if (ret != sp->len)
3442 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003443 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003444 return 0;
3445}
3446
Jens Axboe2b188cc2019-01-07 10:46:33 -07003447/*
3448 * IORING_OP_NOP just posts a completion event, nothing else.
3449 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003450static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003451{
3452 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003453
Jens Axboedef596e2019-01-09 08:59:42 -07003454 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3455 return -EINVAL;
3456
Jens Axboe229a7b62020-06-22 10:13:11 -06003457 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003458 return 0;
3459}
3460
Jens Axboe3529d8c2019-12-19 18:24:38 -07003461static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003462{
Jens Axboe6b063142019-01-10 22:13:58 -07003463 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003464
Jens Axboe09bb8392019-03-13 12:39:28 -06003465 if (!req->file)
3466 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003467
Jens Axboe6b063142019-01-10 22:13:58 -07003468 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003469 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003470 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003471 return -EINVAL;
3472
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003473 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3474 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3475 return -EINVAL;
3476
3477 req->sync.off = READ_ONCE(sqe->off);
3478 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003479 return 0;
3480}
3481
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003482static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003483{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003484 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003485 int ret;
3486
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003487 /* fsync always requires a blocking context */
3488 if (force_nonblock)
3489 return -EAGAIN;
3490
Jens Axboe9adbd452019-12-20 08:45:55 -07003491 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003492 end > 0 ? end : LLONG_MAX,
3493 req->sync.flags & IORING_FSYNC_DATASYNC);
3494 if (ret < 0)
3495 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003496 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003497 return 0;
3498}
3499
Jens Axboed63d1b52019-12-10 10:38:56 -07003500static int io_fallocate_prep(struct io_kiocb *req,
3501 const struct io_uring_sqe *sqe)
3502{
3503 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3504 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003505 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3506 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003507
3508 req->sync.off = READ_ONCE(sqe->off);
3509 req->sync.len = READ_ONCE(sqe->addr);
3510 req->sync.mode = READ_ONCE(sqe->len);
3511 return 0;
3512}
3513
Pavel Begunkov014db002020-03-03 21:33:12 +03003514static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003515{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003516 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003517
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003518 /* fallocate always requiring blocking context */
3519 if (force_nonblock)
3520 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003521 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3522 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003523 if (ret < 0)
3524 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003525 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003526 return 0;
3527}
3528
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003529static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003530{
Jens Axboef8748882020-01-08 17:47:02 -07003531 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003532 int ret;
3533
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003534 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003535 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003536 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003537 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003538
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003539 /* open.how should be already initialised */
3540 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003541 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003542
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003543 req->open.dfd = READ_ONCE(sqe->fd);
3544 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003545 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003546 if (IS_ERR(req->open.filename)) {
3547 ret = PTR_ERR(req->open.filename);
3548 req->open.filename = NULL;
3549 return ret;
3550 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003551 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003552 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003553 return 0;
3554}
3555
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003556static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3557{
3558 u64 flags, mode;
3559
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003560 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3561 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003562 if (req->flags & REQ_F_NEED_CLEANUP)
3563 return 0;
3564 mode = READ_ONCE(sqe->len);
3565 flags = READ_ONCE(sqe->open_flags);
3566 req->open.how = build_open_how(flags, mode);
3567 return __io_openat_prep(req, sqe);
3568}
3569
Jens Axboecebdb982020-01-08 17:59:24 -07003570static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3571{
3572 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003573 size_t len;
3574 int ret;
3575
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003576 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3577 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003578 if (req->flags & REQ_F_NEED_CLEANUP)
3579 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003580 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3581 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003582 if (len < OPEN_HOW_SIZE_VER0)
3583 return -EINVAL;
3584
3585 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3586 len);
3587 if (ret)
3588 return ret;
3589
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003590 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003591}
3592
Pavel Begunkov014db002020-03-03 21:33:12 +03003593static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003594{
3595 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003596 struct file *file;
3597 int ret;
3598
Jens Axboef86cd202020-01-29 13:46:44 -07003599 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003600 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003601
Jens Axboecebdb982020-01-08 17:59:24 -07003602 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003603 if (ret)
3604 goto err;
3605
Jens Axboe4022e7a2020-03-19 19:23:18 -06003606 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003607 if (ret < 0)
3608 goto err;
3609
3610 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3611 if (IS_ERR(file)) {
3612 put_unused_fd(ret);
3613 ret = PTR_ERR(file);
3614 } else {
3615 fsnotify_open(file);
3616 fd_install(ret, file);
3617 }
3618err:
3619 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003620 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003621 if (ret < 0)
3622 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003623 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003624 return 0;
3625}
3626
Pavel Begunkov014db002020-03-03 21:33:12 +03003627static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003628{
Pavel Begunkov014db002020-03-03 21:33:12 +03003629 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003630}
3631
Jens Axboe067524e2020-03-02 16:32:28 -07003632static int io_remove_buffers_prep(struct io_kiocb *req,
3633 const struct io_uring_sqe *sqe)
3634{
3635 struct io_provide_buf *p = &req->pbuf;
3636 u64 tmp;
3637
3638 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3639 return -EINVAL;
3640
3641 tmp = READ_ONCE(sqe->fd);
3642 if (!tmp || tmp > USHRT_MAX)
3643 return -EINVAL;
3644
3645 memset(p, 0, sizeof(*p));
3646 p->nbufs = tmp;
3647 p->bgid = READ_ONCE(sqe->buf_group);
3648 return 0;
3649}
3650
3651static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3652 int bgid, unsigned nbufs)
3653{
3654 unsigned i = 0;
3655
3656 /* shouldn't happen */
3657 if (!nbufs)
3658 return 0;
3659
3660 /* the head kbuf is the list itself */
3661 while (!list_empty(&buf->list)) {
3662 struct io_buffer *nxt;
3663
3664 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3665 list_del(&nxt->list);
3666 kfree(nxt);
3667 if (++i == nbufs)
3668 return i;
3669 }
3670 i++;
3671 kfree(buf);
3672 idr_remove(&ctx->io_buffer_idr, bgid);
3673
3674 return i;
3675}
3676
Jens Axboe229a7b62020-06-22 10:13:11 -06003677static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3678 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003679{
3680 struct io_provide_buf *p = &req->pbuf;
3681 struct io_ring_ctx *ctx = req->ctx;
3682 struct io_buffer *head;
3683 int ret = 0;
3684
3685 io_ring_submit_lock(ctx, !force_nonblock);
3686
3687 lockdep_assert_held(&ctx->uring_lock);
3688
3689 ret = -ENOENT;
3690 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3691 if (head)
3692 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3693
3694 io_ring_submit_lock(ctx, !force_nonblock);
3695 if (ret < 0)
3696 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003697 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003698 return 0;
3699}
3700
Jens Axboeddf0322d2020-02-23 16:41:33 -07003701static int io_provide_buffers_prep(struct io_kiocb *req,
3702 const struct io_uring_sqe *sqe)
3703{
3704 struct io_provide_buf *p = &req->pbuf;
3705 u64 tmp;
3706
3707 if (sqe->ioprio || sqe->rw_flags)
3708 return -EINVAL;
3709
3710 tmp = READ_ONCE(sqe->fd);
3711 if (!tmp || tmp > USHRT_MAX)
3712 return -E2BIG;
3713 p->nbufs = tmp;
3714 p->addr = READ_ONCE(sqe->addr);
3715 p->len = READ_ONCE(sqe->len);
3716
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003717 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003718 return -EFAULT;
3719
3720 p->bgid = READ_ONCE(sqe->buf_group);
3721 tmp = READ_ONCE(sqe->off);
3722 if (tmp > USHRT_MAX)
3723 return -E2BIG;
3724 p->bid = tmp;
3725 return 0;
3726}
3727
3728static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3729{
3730 struct io_buffer *buf;
3731 u64 addr = pbuf->addr;
3732 int i, bid = pbuf->bid;
3733
3734 for (i = 0; i < pbuf->nbufs; i++) {
3735 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3736 if (!buf)
3737 break;
3738
3739 buf->addr = addr;
3740 buf->len = pbuf->len;
3741 buf->bid = bid;
3742 addr += pbuf->len;
3743 bid++;
3744 if (!*head) {
3745 INIT_LIST_HEAD(&buf->list);
3746 *head = buf;
3747 } else {
3748 list_add_tail(&buf->list, &(*head)->list);
3749 }
3750 }
3751
3752 return i ? i : -ENOMEM;
3753}
3754
Jens Axboe229a7b62020-06-22 10:13:11 -06003755static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3756 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003757{
3758 struct io_provide_buf *p = &req->pbuf;
3759 struct io_ring_ctx *ctx = req->ctx;
3760 struct io_buffer *head, *list;
3761 int ret = 0;
3762
3763 io_ring_submit_lock(ctx, !force_nonblock);
3764
3765 lockdep_assert_held(&ctx->uring_lock);
3766
3767 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3768
3769 ret = io_add_buffers(p, &head);
3770 if (ret < 0)
3771 goto out;
3772
3773 if (!list) {
3774 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3775 GFP_KERNEL);
3776 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003777 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003778 goto out;
3779 }
3780 }
3781out:
3782 io_ring_submit_unlock(ctx, !force_nonblock);
3783 if (ret < 0)
3784 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003785 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003786 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003787}
3788
Jens Axboe3e4827b2020-01-08 15:18:09 -07003789static int io_epoll_ctl_prep(struct io_kiocb *req,
3790 const struct io_uring_sqe *sqe)
3791{
3792#if defined(CONFIG_EPOLL)
3793 if (sqe->ioprio || sqe->buf_index)
3794 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003795 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003796 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003797
3798 req->epoll.epfd = READ_ONCE(sqe->fd);
3799 req->epoll.op = READ_ONCE(sqe->len);
3800 req->epoll.fd = READ_ONCE(sqe->off);
3801
3802 if (ep_op_has_event(req->epoll.op)) {
3803 struct epoll_event __user *ev;
3804
3805 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3806 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3807 return -EFAULT;
3808 }
3809
3810 return 0;
3811#else
3812 return -EOPNOTSUPP;
3813#endif
3814}
3815
Jens Axboe229a7b62020-06-22 10:13:11 -06003816static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3817 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003818{
3819#if defined(CONFIG_EPOLL)
3820 struct io_epoll *ie = &req->epoll;
3821 int ret;
3822
3823 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3824 if (force_nonblock && ret == -EAGAIN)
3825 return -EAGAIN;
3826
3827 if (ret < 0)
3828 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003829 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003830 return 0;
3831#else
3832 return -EOPNOTSUPP;
3833#endif
3834}
3835
Jens Axboec1ca7572019-12-25 22:18:28 -07003836static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3837{
3838#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3839 if (sqe->ioprio || sqe->buf_index || sqe->off)
3840 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003841 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3842 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003843
3844 req->madvise.addr = READ_ONCE(sqe->addr);
3845 req->madvise.len = READ_ONCE(sqe->len);
3846 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3847 return 0;
3848#else
3849 return -EOPNOTSUPP;
3850#endif
3851}
3852
Pavel Begunkov014db002020-03-03 21:33:12 +03003853static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003854{
3855#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3856 struct io_madvise *ma = &req->madvise;
3857 int ret;
3858
3859 if (force_nonblock)
3860 return -EAGAIN;
3861
3862 ret = do_madvise(ma->addr, ma->len, ma->advice);
3863 if (ret < 0)
3864 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003865 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003866 return 0;
3867#else
3868 return -EOPNOTSUPP;
3869#endif
3870}
3871
Jens Axboe4840e412019-12-25 22:03:45 -07003872static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3873{
3874 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3875 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003876 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3877 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003878
3879 req->fadvise.offset = READ_ONCE(sqe->off);
3880 req->fadvise.len = READ_ONCE(sqe->len);
3881 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3882 return 0;
3883}
3884
Pavel Begunkov014db002020-03-03 21:33:12 +03003885static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003886{
3887 struct io_fadvise *fa = &req->fadvise;
3888 int ret;
3889
Jens Axboe3e694262020-02-01 09:22:49 -07003890 if (force_nonblock) {
3891 switch (fa->advice) {
3892 case POSIX_FADV_NORMAL:
3893 case POSIX_FADV_RANDOM:
3894 case POSIX_FADV_SEQUENTIAL:
3895 break;
3896 default:
3897 return -EAGAIN;
3898 }
3899 }
Jens Axboe4840e412019-12-25 22:03:45 -07003900
3901 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3902 if (ret < 0)
3903 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003904 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003905 return 0;
3906}
3907
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003908static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3909{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003910 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003911 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003912 if (sqe->ioprio || sqe->buf_index)
3913 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003914 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003915 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003916
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003917 req->statx.dfd = READ_ONCE(sqe->fd);
3918 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003919 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003920 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3921 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003922
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003923 return 0;
3924}
3925
Pavel Begunkov014db002020-03-03 21:33:12 +03003926static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003927{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003928 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003929 int ret;
3930
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003931 if (force_nonblock) {
3932 /* only need file table for an actual valid fd */
3933 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3934 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003935 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003936 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003937
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003938 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3939 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003940
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003941 if (ret < 0)
3942 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003943 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003944 return 0;
3945}
3946
Jens Axboeb5dba592019-12-11 14:02:38 -07003947static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3948{
3949 /*
3950 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003951 * leave the 'file' in an undeterminate state, and here need to modify
3952 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003953 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003954 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003955 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3956
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003957 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3958 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003959 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3960 sqe->rw_flags || sqe->buf_index)
3961 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003962 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003963 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003964
3965 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003966 if ((req->file && req->file->f_op == &io_uring_fops) ||
3967 req->close.fd == req->ctx->ring_fd)
3968 return -EBADF;
3969
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003970 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003971 return 0;
3972}
3973
Jens Axboe229a7b62020-06-22 10:13:11 -06003974static int io_close(struct io_kiocb *req, bool force_nonblock,
3975 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003976{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003977 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003978 int ret;
3979
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003980 /* might be already done during nonblock submission */
3981 if (!close->put_file) {
3982 ret = __close_fd_get_file(close->fd, &close->put_file);
3983 if (ret < 0)
3984 return (ret == -ENOENT) ? -EBADF : ret;
3985 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003986
3987 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003988 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003989 /* was never set, but play safe */
3990 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003991 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003992 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003993 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003994 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003995
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003996 /* No ->flush() or already async, safely close from here */
3997 ret = filp_close(close->put_file, req->work.files);
3998 if (ret < 0)
3999 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004000 fput(close->put_file);
4001 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004002 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004003 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004004}
4005
Jens Axboe3529d8c2019-12-19 18:24:38 -07004006static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004007{
4008 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004009
4010 if (!req->file)
4011 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004012
4013 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4014 return -EINVAL;
4015 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4016 return -EINVAL;
4017
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004018 req->sync.off = READ_ONCE(sqe->off);
4019 req->sync.len = READ_ONCE(sqe->len);
4020 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004021 return 0;
4022}
4023
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004024static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004025{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004026 int ret;
4027
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004028 /* sync_file_range always requires a blocking context */
4029 if (force_nonblock)
4030 return -EAGAIN;
4031
Jens Axboe9adbd452019-12-20 08:45:55 -07004032 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004033 req->sync.flags);
4034 if (ret < 0)
4035 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004036 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004037 return 0;
4038}
4039
YueHaibing469956e2020-03-04 15:53:52 +08004040#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004041static int io_setup_async_msg(struct io_kiocb *req,
4042 struct io_async_msghdr *kmsg)
4043{
4044 if (req->io)
4045 return -EAGAIN;
4046 if (io_alloc_async_ctx(req)) {
4047 if (kmsg->iov != kmsg->fast_iov)
4048 kfree(kmsg->iov);
4049 return -ENOMEM;
4050 }
4051 req->flags |= REQ_F_NEED_CLEANUP;
4052 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4053 return -EAGAIN;
4054}
4055
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004056static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4057 struct io_async_msghdr *iomsg)
4058{
4059 iomsg->iov = iomsg->fast_iov;
4060 iomsg->msg.msg_name = &iomsg->addr;
4061 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4062 req->sr_msg.msg_flags, &iomsg->iov);
4063}
4064
Jens Axboe3529d8c2019-12-19 18:24:38 -07004065static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004066{
Jens Axboee47293f2019-12-20 08:58:21 -07004067 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004068 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004069 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004070
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004071 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4072 return -EINVAL;
4073
Jens Axboee47293f2019-12-20 08:58:21 -07004074 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004075 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004076 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004077
Jens Axboed8768362020-02-27 14:17:49 -07004078#ifdef CONFIG_COMPAT
4079 if (req->ctx->compat)
4080 sr->msg_flags |= MSG_CMSG_COMPAT;
4081#endif
4082
Jens Axboefddafac2020-01-04 20:19:44 -07004083 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004084 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004085 /* iovec is already imported */
4086 if (req->flags & REQ_F_NEED_CLEANUP)
4087 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004088
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004089 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004090 if (!ret)
4091 req->flags |= REQ_F_NEED_CLEANUP;
4092 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004093}
4094
Jens Axboe229a7b62020-06-22 10:13:11 -06004095static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4096 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004097{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004098 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004099 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004100 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004101 int ret;
4102
Jens Axboe03b12302019-12-02 18:50:25 -07004103 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004104 if (unlikely(!sock))
4105 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004106
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004107 if (req->io) {
4108 kmsg = &req->io->msg;
4109 kmsg->msg.msg_name = &req->io->msg.addr;
4110 /* if iov is set, it's allocated already */
4111 if (!kmsg->iov)
4112 kmsg->iov = kmsg->fast_iov;
4113 kmsg->msg.msg_iter.iov = kmsg->iov;
4114 } else {
4115 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004116 if (ret)
4117 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004118 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004119 }
4120
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004121 flags = req->sr_msg.msg_flags;
4122 if (flags & MSG_DONTWAIT)
4123 req->flags |= REQ_F_NOWAIT;
4124 else if (force_nonblock)
4125 flags |= MSG_DONTWAIT;
4126
4127 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4128 if (force_nonblock && ret == -EAGAIN)
4129 return io_setup_async_msg(req, kmsg);
4130 if (ret == -ERESTARTSYS)
4131 ret = -EINTR;
4132
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004133 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004134 kfree(kmsg->iov);
4135 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004136 if (ret < 0)
4137 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004138 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004139 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004140}
4141
Jens Axboe229a7b62020-06-22 10:13:11 -06004142static int io_send(struct io_kiocb *req, bool force_nonblock,
4143 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004144{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004145 struct io_sr_msg *sr = &req->sr_msg;
4146 struct msghdr msg;
4147 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004148 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004149 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004150 int ret;
4151
4152 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004153 if (unlikely(!sock))
4154 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004155
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004156 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4157 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004158 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004159
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004160 msg.msg_name = NULL;
4161 msg.msg_control = NULL;
4162 msg.msg_controllen = 0;
4163 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004164
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004165 flags = req->sr_msg.msg_flags;
4166 if (flags & MSG_DONTWAIT)
4167 req->flags |= REQ_F_NOWAIT;
4168 else if (force_nonblock)
4169 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004170
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004171 msg.msg_flags = flags;
4172 ret = sock_sendmsg(sock, &msg);
4173 if (force_nonblock && ret == -EAGAIN)
4174 return -EAGAIN;
4175 if (ret == -ERESTARTSYS)
4176 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004177
Jens Axboe03b12302019-12-02 18:50:25 -07004178 if (ret < 0)
4179 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004180 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004181 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004182}
4183
Pavel Begunkov1400e692020-07-12 20:41:05 +03004184static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4185 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004186{
4187 struct io_sr_msg *sr = &req->sr_msg;
4188 struct iovec __user *uiov;
4189 size_t iov_len;
4190 int ret;
4191
Pavel Begunkov1400e692020-07-12 20:41:05 +03004192 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4193 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004194 if (ret)
4195 return ret;
4196
4197 if (req->flags & REQ_F_BUFFER_SELECT) {
4198 if (iov_len > 1)
4199 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004200 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004201 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004202 sr->len = iomsg->iov[0].iov_len;
4203 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004204 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004205 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004206 } else {
4207 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004208 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004209 if (ret > 0)
4210 ret = 0;
4211 }
4212
4213 return ret;
4214}
4215
4216#ifdef CONFIG_COMPAT
4217static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004218 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004219{
4220 struct compat_msghdr __user *msg_compat;
4221 struct io_sr_msg *sr = &req->sr_msg;
4222 struct compat_iovec __user *uiov;
4223 compat_uptr_t ptr;
4224 compat_size_t len;
4225 int ret;
4226
Pavel Begunkov270a5942020-07-12 20:41:04 +03004227 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004228 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004229 &ptr, &len);
4230 if (ret)
4231 return ret;
4232
4233 uiov = compat_ptr(ptr);
4234 if (req->flags & REQ_F_BUFFER_SELECT) {
4235 compat_ssize_t clen;
4236
4237 if (len > 1)
4238 return -EINVAL;
4239 if (!access_ok(uiov, sizeof(*uiov)))
4240 return -EFAULT;
4241 if (__get_user(clen, &uiov->iov_len))
4242 return -EFAULT;
4243 if (clen < 0)
4244 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004245 sr->len = iomsg->iov[0].iov_len;
4246 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004247 } else {
4248 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004249 &iomsg->iov,
4250 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004251 if (ret < 0)
4252 return ret;
4253 }
4254
4255 return 0;
4256}
Jens Axboe03b12302019-12-02 18:50:25 -07004257#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004258
Pavel Begunkov1400e692020-07-12 20:41:05 +03004259static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4260 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004261{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004262 iomsg->msg.msg_name = &iomsg->addr;
4263 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004264
4265#ifdef CONFIG_COMPAT
4266 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004267 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004268#endif
4269
Pavel Begunkov1400e692020-07-12 20:41:05 +03004270 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004271}
4272
Jens Axboebcda7ba2020-02-23 16:42:51 -07004273static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004274 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004275{
4276 struct io_sr_msg *sr = &req->sr_msg;
4277 struct io_buffer *kbuf;
4278
Jens Axboebcda7ba2020-02-23 16:42:51 -07004279 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4280 if (IS_ERR(kbuf))
4281 return kbuf;
4282
4283 sr->kbuf = kbuf;
4284 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004285 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004286}
4287
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004288static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4289{
4290 return io_put_kbuf(req, req->sr_msg.kbuf);
4291}
4292
Jens Axboe3529d8c2019-12-19 18:24:38 -07004293static int io_recvmsg_prep(struct io_kiocb *req,
4294 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004295{
Jens Axboee47293f2019-12-20 08:58:21 -07004296 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004297 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004298 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004299
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004300 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4301 return -EINVAL;
4302
Jens Axboe3529d8c2019-12-19 18:24:38 -07004303 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004304 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004305 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004306 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004307
Jens Axboed8768362020-02-27 14:17:49 -07004308#ifdef CONFIG_COMPAT
4309 if (req->ctx->compat)
4310 sr->msg_flags |= MSG_CMSG_COMPAT;
4311#endif
4312
Jens Axboefddafac2020-01-04 20:19:44 -07004313 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004314 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004315 /* iovec is already imported */
4316 if (req->flags & REQ_F_NEED_CLEANUP)
4317 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004318
Pavel Begunkov1400e692020-07-12 20:41:05 +03004319 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004320 if (!ret)
4321 req->flags |= REQ_F_NEED_CLEANUP;
4322 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004323}
4324
Jens Axboe229a7b62020-06-22 10:13:11 -06004325static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4326 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004327{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004328 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004329 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004330 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004331 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004332 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004333
Jens Axboe0fa03c62019-04-19 13:34:07 -06004334 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004335 if (unlikely(!sock))
4336 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004337
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004338 if (req->io) {
4339 kmsg = &req->io->msg;
4340 kmsg->msg.msg_name = &req->io->msg.addr;
4341 /* if iov is set, it's allocated already */
4342 if (!kmsg->iov)
4343 kmsg->iov = kmsg->fast_iov;
4344 kmsg->msg.msg_iter.iov = kmsg->iov;
4345 } else {
4346 ret = io_recvmsg_copy_hdr(req, &iomsg);
4347 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004348 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004349 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004350 }
4351
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004352 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004353 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004354 if (IS_ERR(kbuf))
4355 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004356 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4357 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4358 1, req->sr_msg.len);
4359 }
4360
4361 flags = req->sr_msg.msg_flags;
4362 if (flags & MSG_DONTWAIT)
4363 req->flags |= REQ_F_NOWAIT;
4364 else if (force_nonblock)
4365 flags |= MSG_DONTWAIT;
4366
4367 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4368 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004369 if (force_nonblock && ret == -EAGAIN)
4370 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004371 if (ret == -ERESTARTSYS)
4372 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004373
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004374 if (req->flags & REQ_F_BUFFER_SELECTED)
4375 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004376 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004377 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004378 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004379 if (ret < 0)
4380 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004381 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004382 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004383}
4384
Jens Axboe229a7b62020-06-22 10:13:11 -06004385static int io_recv(struct io_kiocb *req, bool force_nonblock,
4386 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004387{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004388 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004389 struct io_sr_msg *sr = &req->sr_msg;
4390 struct msghdr msg;
4391 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004392 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004393 struct iovec iov;
4394 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004395 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004396
Jens Axboefddafac2020-01-04 20:19:44 -07004397 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004398 if (unlikely(!sock))
4399 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004400
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004401 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004402 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004403 if (IS_ERR(kbuf))
4404 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004405 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004406 }
4407
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004408 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004409 if (unlikely(ret))
4410 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004411
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004412 msg.msg_name = NULL;
4413 msg.msg_control = NULL;
4414 msg.msg_controllen = 0;
4415 msg.msg_namelen = 0;
4416 msg.msg_iocb = NULL;
4417 msg.msg_flags = 0;
4418
4419 flags = req->sr_msg.msg_flags;
4420 if (flags & MSG_DONTWAIT)
4421 req->flags |= REQ_F_NOWAIT;
4422 else if (force_nonblock)
4423 flags |= MSG_DONTWAIT;
4424
4425 ret = sock_recvmsg(sock, &msg, flags);
4426 if (force_nonblock && ret == -EAGAIN)
4427 return -EAGAIN;
4428 if (ret == -ERESTARTSYS)
4429 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004430out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004431 if (req->flags & REQ_F_BUFFER_SELECTED)
4432 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004433 if (ret < 0)
4434 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004435 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004436 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004437}
4438
Jens Axboe3529d8c2019-12-19 18:24:38 -07004439static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004440{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004441 struct io_accept *accept = &req->accept;
4442
Jens Axboe17f2fe32019-10-17 14:42:58 -06004443 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4444 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004445 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004446 return -EINVAL;
4447
Jens Axboed55e5f52019-12-11 16:12:15 -07004448 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4449 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004450 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004451 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004452 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004453}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004454
Jens Axboe229a7b62020-06-22 10:13:11 -06004455static int io_accept(struct io_kiocb *req, bool force_nonblock,
4456 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004457{
4458 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004459 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004460 int ret;
4461
Jiufei Xuee697dee2020-06-10 13:41:59 +08004462 if (req->file->f_flags & O_NONBLOCK)
4463 req->flags |= REQ_F_NOWAIT;
4464
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004465 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004466 accept->addr_len, accept->flags,
4467 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004468 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004469 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004470 if (ret < 0) {
4471 if (ret == -ERESTARTSYS)
4472 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004473 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004474 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004475 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004476 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004477}
4478
Jens Axboe3529d8c2019-12-19 18:24:38 -07004479static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004480{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004481 struct io_connect *conn = &req->connect;
4482 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004483
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004484 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4485 return -EINVAL;
4486 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4487 return -EINVAL;
4488
Jens Axboe3529d8c2019-12-19 18:24:38 -07004489 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4490 conn->addr_len = READ_ONCE(sqe->addr2);
4491
4492 if (!io)
4493 return 0;
4494
4495 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004496 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004497}
4498
Jens Axboe229a7b62020-06-22 10:13:11 -06004499static int io_connect(struct io_kiocb *req, bool force_nonblock,
4500 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004501{
Jens Axboef499a022019-12-02 16:28:46 -07004502 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004503 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004504 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004505
Jens Axboef499a022019-12-02 16:28:46 -07004506 if (req->io) {
4507 io = req->io;
4508 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004509 ret = move_addr_to_kernel(req->connect.addr,
4510 req->connect.addr_len,
4511 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004512 if (ret)
4513 goto out;
4514 io = &__io;
4515 }
4516
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004517 file_flags = force_nonblock ? O_NONBLOCK : 0;
4518
4519 ret = __sys_connect_file(req->file, &io->connect.address,
4520 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004521 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004522 if (req->io)
4523 return -EAGAIN;
4524 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004525 ret = -ENOMEM;
4526 goto out;
4527 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004528 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004529 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004530 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004531 if (ret == -ERESTARTSYS)
4532 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004533out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004534 if (ret < 0)
4535 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004536 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004537 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004538}
YueHaibing469956e2020-03-04 15:53:52 +08004539#else /* !CONFIG_NET */
4540static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4541{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004542 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004543}
4544
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004545static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4546 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004547{
YueHaibing469956e2020-03-04 15:53:52 +08004548 return -EOPNOTSUPP;
4549}
4550
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004551static int io_send(struct io_kiocb *req, bool force_nonblock,
4552 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004553{
4554 return -EOPNOTSUPP;
4555}
4556
4557static int io_recvmsg_prep(struct io_kiocb *req,
4558 const struct io_uring_sqe *sqe)
4559{
4560 return -EOPNOTSUPP;
4561}
4562
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004563static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4564 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004565{
4566 return -EOPNOTSUPP;
4567}
4568
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004569static int io_recv(struct io_kiocb *req, bool force_nonblock,
4570 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004571{
4572 return -EOPNOTSUPP;
4573}
4574
4575static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4576{
4577 return -EOPNOTSUPP;
4578}
4579
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004580static int io_accept(struct io_kiocb *req, bool force_nonblock,
4581 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004582{
4583 return -EOPNOTSUPP;
4584}
4585
4586static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4587{
4588 return -EOPNOTSUPP;
4589}
4590
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004591static int io_connect(struct io_kiocb *req, bool force_nonblock,
4592 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004593{
4594 return -EOPNOTSUPP;
4595}
4596#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004597
Jens Axboed7718a92020-02-14 22:23:12 -07004598struct io_poll_table {
4599 struct poll_table_struct pt;
4600 struct io_kiocb *req;
4601 int error;
4602};
4603
Jens Axboed7718a92020-02-14 22:23:12 -07004604static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4605 __poll_t mask, task_work_func_t func)
4606{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004607 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004608 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004609
4610 /* for instances that support it check for an event match first: */
4611 if (mask && !(mask & poll->events))
4612 return 0;
4613
4614 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4615
4616 list_del_init(&poll->wait.entry);
4617
Jens Axboed7718a92020-02-14 22:23:12 -07004618 req->result = mask;
4619 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004620 percpu_ref_get(&req->ctx->refs);
4621
Jens Axboed7718a92020-02-14 22:23:12 -07004622 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004623 * If we using the signalfd wait_queue_head for this wakeup, then
4624 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4625 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4626 * either, as the normal wakeup will suffice.
4627 */
4628 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4629
4630 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004631 * If this fails, then the task is exiting. When a task exits, the
4632 * work gets canceled, so just cancel this request as well instead
4633 * of executing it. We can't safely execute it anyway, as we may not
4634 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004635 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004636 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004637 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004638 struct task_struct *tsk;
4639
Jens Axboee3aabf92020-05-18 11:04:17 -06004640 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004641 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004642 task_work_add(tsk, &req->task_work, 0);
4643 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004644 }
Jens Axboed7718a92020-02-14 22:23:12 -07004645 return 1;
4646}
4647
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004648static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4649 __acquires(&req->ctx->completion_lock)
4650{
4651 struct io_ring_ctx *ctx = req->ctx;
4652
4653 if (!req->result && !READ_ONCE(poll->canceled)) {
4654 struct poll_table_struct pt = { ._key = poll->events };
4655
4656 req->result = vfs_poll(req->file, &pt) & poll->events;
4657 }
4658
4659 spin_lock_irq(&ctx->completion_lock);
4660 if (!req->result && !READ_ONCE(poll->canceled)) {
4661 add_wait_queue(poll->head, &poll->wait);
4662 return true;
4663 }
4664
4665 return false;
4666}
4667
Jens Axboed4e7cd32020-08-15 11:44:50 -07004668static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004669{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004670 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4671 if (req->opcode == IORING_OP_POLL_ADD)
4672 return (struct io_poll_iocb *) req->io;
4673 return req->apoll->double_poll;
4674}
4675
4676static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4677{
4678 if (req->opcode == IORING_OP_POLL_ADD)
4679 return &req->poll;
4680 return &req->apoll->poll;
4681}
4682
4683static void io_poll_remove_double(struct io_kiocb *req)
4684{
4685 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004686
4687 lockdep_assert_held(&req->ctx->completion_lock);
4688
4689 if (poll && poll->head) {
4690 struct wait_queue_head *head = poll->head;
4691
4692 spin_lock(&head->lock);
4693 list_del_init(&poll->wait.entry);
4694 if (poll->wait.private)
4695 refcount_dec(&req->refs);
4696 poll->head = NULL;
4697 spin_unlock(&head->lock);
4698 }
4699}
4700
4701static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4702{
4703 struct io_ring_ctx *ctx = req->ctx;
4704
Jens Axboed4e7cd32020-08-15 11:44:50 -07004705 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004706 req->poll.done = true;
4707 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4708 io_commit_cqring(ctx);
4709}
4710
4711static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4712{
4713 struct io_ring_ctx *ctx = req->ctx;
4714
4715 if (io_poll_rewait(req, &req->poll)) {
4716 spin_unlock_irq(&ctx->completion_lock);
4717 return;
4718 }
4719
4720 hash_del(&req->hash_node);
4721 io_poll_complete(req, req->result, 0);
4722 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004723 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004724 spin_unlock_irq(&ctx->completion_lock);
4725
4726 io_cqring_ev_posted(ctx);
4727}
4728
4729static void io_poll_task_func(struct callback_head *cb)
4730{
4731 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004732 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004733 struct io_kiocb *nxt = NULL;
4734
4735 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004736 if (nxt)
4737 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004738 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004739}
4740
4741static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4742 int sync, void *key)
4743{
4744 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004745 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004746 __poll_t mask = key_to_poll(key);
4747
4748 /* for instances that support it check for an event match first: */
4749 if (mask && !(mask & poll->events))
4750 return 0;
4751
Jens Axboe8706e042020-09-28 08:38:54 -06004752 list_del_init(&wait->entry);
4753
Jens Axboe807abcb2020-07-17 17:09:27 -06004754 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004755 bool done;
4756
Jens Axboe807abcb2020-07-17 17:09:27 -06004757 spin_lock(&poll->head->lock);
4758 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004759 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004760 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004761 /* make sure double remove sees this as being gone */
4762 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004763 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004764 if (!done)
4765 __io_async_wake(req, poll, mask, io_poll_task_func);
4766 }
4767 refcount_dec(&req->refs);
4768 return 1;
4769}
4770
4771static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4772 wait_queue_func_t wake_func)
4773{
4774 poll->head = NULL;
4775 poll->done = false;
4776 poll->canceled = false;
4777 poll->events = events;
4778 INIT_LIST_HEAD(&poll->wait.entry);
4779 init_waitqueue_func_entry(&poll->wait, wake_func);
4780}
4781
4782static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004783 struct wait_queue_head *head,
4784 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004785{
4786 struct io_kiocb *req = pt->req;
4787
4788 /*
4789 * If poll->head is already set, it's because the file being polled
4790 * uses multiple waitqueues for poll handling (eg one for read, one
4791 * for write). Setup a separate io_poll_iocb if this happens.
4792 */
4793 if (unlikely(poll->head)) {
4794 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004795 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004796 pt->error = -EINVAL;
4797 return;
4798 }
4799 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4800 if (!poll) {
4801 pt->error = -ENOMEM;
4802 return;
4803 }
4804 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4805 refcount_inc(&req->refs);
4806 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004807 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004808 }
4809
4810 pt->error = 0;
4811 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004812
4813 if (poll->events & EPOLLEXCLUSIVE)
4814 add_wait_queue_exclusive(head, &poll->wait);
4815 else
4816 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004817}
4818
4819static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4820 struct poll_table_struct *p)
4821{
4822 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004823 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004824
Jens Axboe807abcb2020-07-17 17:09:27 -06004825 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004826}
4827
Jens Axboed7718a92020-02-14 22:23:12 -07004828static void io_async_task_func(struct callback_head *cb)
4829{
4830 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4831 struct async_poll *apoll = req->apoll;
4832 struct io_ring_ctx *ctx = req->ctx;
4833
4834 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4835
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004836 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004837 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004838 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004839 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004840 }
4841
Jens Axboe31067252020-05-17 17:43:31 -06004842 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004843 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004844 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004845
Jens Axboed4e7cd32020-08-15 11:44:50 -07004846 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004847 spin_unlock_irq(&ctx->completion_lock);
4848
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004849 if (!READ_ONCE(apoll->poll.canceled))
4850 __io_req_task_submit(req);
4851 else
4852 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004853
Jens Axboe6d816e02020-08-11 08:04:14 -06004854 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004855 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004856 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004857}
4858
4859static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4860 void *key)
4861{
4862 struct io_kiocb *req = wait->private;
4863 struct io_poll_iocb *poll = &req->apoll->poll;
4864
4865 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4866 key_to_poll(key));
4867
4868 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4869}
4870
4871static void io_poll_req_insert(struct io_kiocb *req)
4872{
4873 struct io_ring_ctx *ctx = req->ctx;
4874 struct hlist_head *list;
4875
4876 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4877 hlist_add_head(&req->hash_node, list);
4878}
4879
4880static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4881 struct io_poll_iocb *poll,
4882 struct io_poll_table *ipt, __poll_t mask,
4883 wait_queue_func_t wake_func)
4884 __acquires(&ctx->completion_lock)
4885{
4886 struct io_ring_ctx *ctx = req->ctx;
4887 bool cancel = false;
4888
Jens Axboe18bceab2020-05-15 11:56:54 -06004889 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004890 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004891 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004892
4893 ipt->pt._key = mask;
4894 ipt->req = req;
4895 ipt->error = -EINVAL;
4896
Jens Axboed7718a92020-02-14 22:23:12 -07004897 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4898
4899 spin_lock_irq(&ctx->completion_lock);
4900 if (likely(poll->head)) {
4901 spin_lock(&poll->head->lock);
4902 if (unlikely(list_empty(&poll->wait.entry))) {
4903 if (ipt->error)
4904 cancel = true;
4905 ipt->error = 0;
4906 mask = 0;
4907 }
4908 if (mask || ipt->error)
4909 list_del_init(&poll->wait.entry);
4910 else if (cancel)
4911 WRITE_ONCE(poll->canceled, true);
4912 else if (!poll->done) /* actually waiting for an event */
4913 io_poll_req_insert(req);
4914 spin_unlock(&poll->head->lock);
4915 }
4916
4917 return mask;
4918}
4919
4920static bool io_arm_poll_handler(struct io_kiocb *req)
4921{
4922 const struct io_op_def *def = &io_op_defs[req->opcode];
4923 struct io_ring_ctx *ctx = req->ctx;
4924 struct async_poll *apoll;
4925 struct io_poll_table ipt;
4926 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004927 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004928
4929 if (!req->file || !file_can_poll(req->file))
4930 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004931 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004932 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004933 if (def->pollin)
4934 rw = READ;
4935 else if (def->pollout)
4936 rw = WRITE;
4937 else
4938 return false;
4939 /* if we can't nonblock try, then no point in arming a poll handler */
4940 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004941 return false;
4942
4943 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4944 if (unlikely(!apoll))
4945 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004946 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004947
4948 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07004949 req->apoll = apoll;
4950 INIT_HLIST_NODE(&req->hash_node);
4951
Nathan Chancellor8755d972020-03-02 16:01:19 -07004952 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004953 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004954 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004955 if (def->pollout)
4956 mask |= POLLOUT | POLLWRNORM;
4957 mask |= POLLERR | POLLPRI;
4958
4959 ipt.pt._qproc = io_async_queue_proc;
4960
4961 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4962 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06004963 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07004964 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004965 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06004966 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004967 kfree(apoll);
4968 return false;
4969 }
4970 spin_unlock_irq(&ctx->completion_lock);
4971 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4972 apoll->poll.events);
4973 return true;
4974}
4975
4976static bool __io_poll_remove_one(struct io_kiocb *req,
4977 struct io_poll_iocb *poll)
4978{
Jens Axboeb41e9852020-02-17 09:52:41 -07004979 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004980
4981 spin_lock(&poll->head->lock);
4982 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004983 if (!list_empty(&poll->wait.entry)) {
4984 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004985 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004986 }
4987 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004988 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004989 return do_complete;
4990}
4991
4992static bool io_poll_remove_one(struct io_kiocb *req)
4993{
4994 bool do_complete;
4995
Jens Axboed4e7cd32020-08-15 11:44:50 -07004996 io_poll_remove_double(req);
4997
Jens Axboed7718a92020-02-14 22:23:12 -07004998 if (req->opcode == IORING_OP_POLL_ADD) {
4999 do_complete = __io_poll_remove_one(req, &req->poll);
5000 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005001 struct async_poll *apoll = req->apoll;
5002
Jens Axboed7718a92020-02-14 22:23:12 -07005003 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005004 do_complete = __io_poll_remove_one(req, &apoll->poll);
5005 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005006 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005007 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005008 kfree(apoll);
5009 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005010 }
5011
Jens Axboeb41e9852020-02-17 09:52:41 -07005012 if (do_complete) {
5013 io_cqring_fill_event(req, -ECANCELED);
5014 io_commit_cqring(req->ctx);
5015 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005016 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005017 io_put_req(req);
5018 }
5019
5020 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005021}
5022
Jens Axboe76e1b642020-09-26 15:05:03 -06005023/*
5024 * Returns true if we found and killed one or more poll requests
5025 */
5026static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005027{
Jens Axboe78076bb2019-12-04 19:56:40 -07005028 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005029 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005030 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005031
5032 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005033 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5034 struct hlist_head *list;
5035
5036 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005037 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5038 if (io_task_match(req, tsk))
5039 posted += io_poll_remove_one(req);
5040 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005041 }
5042 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005043
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005044 if (posted)
5045 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005046
5047 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005048}
5049
Jens Axboe47f46762019-11-09 17:43:02 -07005050static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5051{
Jens Axboe78076bb2019-12-04 19:56:40 -07005052 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005053 struct io_kiocb *req;
5054
Jens Axboe78076bb2019-12-04 19:56:40 -07005055 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5056 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005057 if (sqe_addr != req->user_data)
5058 continue;
5059 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005060 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005061 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005062 }
5063
5064 return -ENOENT;
5065}
5066
Jens Axboe3529d8c2019-12-19 18:24:38 -07005067static int io_poll_remove_prep(struct io_kiocb *req,
5068 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005069{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005070 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5071 return -EINVAL;
5072 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5073 sqe->poll_events)
5074 return -EINVAL;
5075
Jens Axboe0969e782019-12-17 18:40:57 -07005076 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005077 return 0;
5078}
5079
5080/*
5081 * Find a running poll command that matches one specified in sqe->addr,
5082 * and remove it if found.
5083 */
5084static int io_poll_remove(struct io_kiocb *req)
5085{
5086 struct io_ring_ctx *ctx = req->ctx;
5087 u64 addr;
5088 int ret;
5089
Jens Axboe0969e782019-12-17 18:40:57 -07005090 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005091 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005092 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005093 spin_unlock_irq(&ctx->completion_lock);
5094
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005095 if (ret < 0)
5096 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005097 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005098 return 0;
5099}
5100
Jens Axboe221c5eb2019-01-17 09:41:58 -07005101static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5102 void *key)
5103{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005104 struct io_kiocb *req = wait->private;
5105 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005106
Jens Axboed7718a92020-02-14 22:23:12 -07005107 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005108}
5109
Jens Axboe221c5eb2019-01-17 09:41:58 -07005110static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5111 struct poll_table_struct *p)
5112{
5113 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5114
Jens Axboe807abcb2020-07-17 17:09:27 -06005115 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005116}
5117
Jens Axboe3529d8c2019-12-19 18:24:38 -07005118static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005119{
5120 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005121 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005122
5123 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5124 return -EINVAL;
5125 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5126 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005127 if (!poll->file)
5128 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005129
Jiufei Xue5769a352020-06-17 17:53:55 +08005130 events = READ_ONCE(sqe->poll32_events);
5131#ifdef __BIG_ENDIAN
5132 events = swahw32(events);
5133#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005134 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5135 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005136 return 0;
5137}
5138
Pavel Begunkov014db002020-03-03 21:33:12 +03005139static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005140{
5141 struct io_poll_iocb *poll = &req->poll;
5142 struct io_ring_ctx *ctx = req->ctx;
5143 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005144 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005145
Jens Axboe78076bb2019-12-04 19:56:40 -07005146 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005147 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005148
Jens Axboed7718a92020-02-14 22:23:12 -07005149 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5150 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005151
Jens Axboe8c838782019-03-12 15:48:16 -06005152 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005153 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005154 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005155 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005156 spin_unlock_irq(&ctx->completion_lock);
5157
Jens Axboe8c838782019-03-12 15:48:16 -06005158 if (mask) {
5159 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005160 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005161 }
Jens Axboe8c838782019-03-12 15:48:16 -06005162 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005163}
5164
Jens Axboe5262f562019-09-17 12:26:57 -06005165static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5166{
Jens Axboead8a48a2019-11-15 08:49:11 -07005167 struct io_timeout_data *data = container_of(timer,
5168 struct io_timeout_data, timer);
5169 struct io_kiocb *req = data->req;
5170 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005171 unsigned long flags;
5172
Jens Axboe5262f562019-09-17 12:26:57 -06005173 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005174 atomic_set(&req->ctx->cq_timeouts,
5175 atomic_read(&req->ctx->cq_timeouts) + 1);
5176
zhangyi (F)ef036812019-10-23 15:10:08 +08005177 /*
Jens Axboe11365042019-10-16 09:08:32 -06005178 * We could be racing with timeout deletion. If the list is empty,
5179 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005180 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005181 if (!list_empty(&req->timeout.list))
5182 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005183
Jens Axboe78e19bb2019-11-06 15:21:34 -07005184 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005185 io_commit_cqring(ctx);
5186 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5187
5188 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005189 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005190 io_put_req(req);
5191 return HRTIMER_NORESTART;
5192}
5193
Jens Axboef254ac02020-08-12 17:33:30 -06005194static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005195{
Jens Axboef254ac02020-08-12 17:33:30 -06005196 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005197
Jens Axboef254ac02020-08-12 17:33:30 -06005198 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005199
Jens Axboe2d283902019-12-04 11:08:05 -07005200 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005201 if (ret == -1)
5202 return -EALREADY;
5203
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005204 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005205 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005206 io_cqring_fill_event(req, -ECANCELED);
5207 io_put_req(req);
5208 return 0;
5209}
5210
Jens Axboef254ac02020-08-12 17:33:30 -06005211static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5212{
5213 struct io_kiocb *req;
5214 int ret = -ENOENT;
5215
5216 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5217 if (user_data == req->user_data) {
5218 ret = 0;
5219 break;
5220 }
5221 }
5222
5223 if (ret == -ENOENT)
5224 return ret;
5225
5226 return __io_timeout_cancel(req);
5227}
5228
Jens Axboe3529d8c2019-12-19 18:24:38 -07005229static int io_timeout_remove_prep(struct io_kiocb *req,
5230 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005231{
Jens Axboeb29472e2019-12-17 18:50:29 -07005232 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5233 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005234 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5235 return -EINVAL;
5236 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005237 return -EINVAL;
5238
5239 req->timeout.addr = READ_ONCE(sqe->addr);
5240 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5241 if (req->timeout.flags)
5242 return -EINVAL;
5243
Jens Axboeb29472e2019-12-17 18:50:29 -07005244 return 0;
5245}
5246
Jens Axboe11365042019-10-16 09:08:32 -06005247/*
5248 * Remove or update an existing timeout command
5249 */
Jens Axboefc4df992019-12-10 14:38:45 -07005250static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005251{
5252 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005253 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005254
Jens Axboe11365042019-10-16 09:08:32 -06005255 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005256 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005257
Jens Axboe47f46762019-11-09 17:43:02 -07005258 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005259 io_commit_cqring(ctx);
5260 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005261 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005262 if (ret < 0)
5263 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005264 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005265 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005266}
5267
Jens Axboe3529d8c2019-12-19 18:24:38 -07005268static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005269 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005270{
Jens Axboead8a48a2019-11-15 08:49:11 -07005271 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005272 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005273 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005274
Jens Axboead8a48a2019-11-15 08:49:11 -07005275 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005276 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005277 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005278 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005279 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005280 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005281 flags = READ_ONCE(sqe->timeout_flags);
5282 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005283 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005284
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005285 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005286
Jens Axboe3529d8c2019-12-19 18:24:38 -07005287 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005288 return -ENOMEM;
5289
5290 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005291 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005292
5293 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005294 return -EFAULT;
5295
Jens Axboe11365042019-10-16 09:08:32 -06005296 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005297 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005298 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005299 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005300
Jens Axboead8a48a2019-11-15 08:49:11 -07005301 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5302 return 0;
5303}
5304
Jens Axboefc4df992019-12-10 14:38:45 -07005305static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005306{
Jens Axboead8a48a2019-11-15 08:49:11 -07005307 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005308 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005309 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005310 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005311
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005312 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005313
Jens Axboe5262f562019-09-17 12:26:57 -06005314 /*
5315 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005316 * timeout event to be satisfied. If it isn't set, then this is
5317 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005318 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005319 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005320 entry = ctx->timeout_list.prev;
5321 goto add;
5322 }
Jens Axboe5262f562019-09-17 12:26:57 -06005323
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005324 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5325 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005326
5327 /*
5328 * Insertion sort, ensuring the first entry in the list is always
5329 * the one we need first.
5330 */
Jens Axboe5262f562019-09-17 12:26:57 -06005331 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005332 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5333 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005334
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005335 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005336 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005337 /* nxt.seq is behind @tail, otherwise would've been completed */
5338 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005339 break;
5340 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005341add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005342 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005343 data->timer.function = io_timeout_fn;
5344 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005345 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005346 return 0;
5347}
5348
Jens Axboe62755e32019-10-28 21:49:21 -06005349static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005350{
Jens Axboe62755e32019-10-28 21:49:21 -06005351 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005352
Jens Axboe62755e32019-10-28 21:49:21 -06005353 return req->user_data == (unsigned long) data;
5354}
5355
Jens Axboee977d6d2019-11-05 12:39:45 -07005356static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005357{
Jens Axboe62755e32019-10-28 21:49:21 -06005358 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005359 int ret = 0;
5360
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005361 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005362 switch (cancel_ret) {
5363 case IO_WQ_CANCEL_OK:
5364 ret = 0;
5365 break;
5366 case IO_WQ_CANCEL_RUNNING:
5367 ret = -EALREADY;
5368 break;
5369 case IO_WQ_CANCEL_NOTFOUND:
5370 ret = -ENOENT;
5371 break;
5372 }
5373
Jens Axboee977d6d2019-11-05 12:39:45 -07005374 return ret;
5375}
5376
Jens Axboe47f46762019-11-09 17:43:02 -07005377static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5378 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005379 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005380{
5381 unsigned long flags;
5382 int ret;
5383
5384 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5385 if (ret != -ENOENT) {
5386 spin_lock_irqsave(&ctx->completion_lock, flags);
5387 goto done;
5388 }
5389
5390 spin_lock_irqsave(&ctx->completion_lock, flags);
5391 ret = io_timeout_cancel(ctx, sqe_addr);
5392 if (ret != -ENOENT)
5393 goto done;
5394 ret = io_poll_cancel(ctx, sqe_addr);
5395done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005396 if (!ret)
5397 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005398 io_cqring_fill_event(req, ret);
5399 io_commit_cqring(ctx);
5400 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5401 io_cqring_ev_posted(ctx);
5402
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005403 if (ret < 0)
5404 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005405 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005406}
5407
Jens Axboe3529d8c2019-12-19 18:24:38 -07005408static int io_async_cancel_prep(struct io_kiocb *req,
5409 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005410{
Jens Axboefbf23842019-12-17 18:45:56 -07005411 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005412 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005413 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5414 return -EINVAL;
5415 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005416 return -EINVAL;
5417
Jens Axboefbf23842019-12-17 18:45:56 -07005418 req->cancel.addr = READ_ONCE(sqe->addr);
5419 return 0;
5420}
5421
Pavel Begunkov014db002020-03-03 21:33:12 +03005422static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005423{
5424 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005425
Pavel Begunkov014db002020-03-03 21:33:12 +03005426 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005427 return 0;
5428}
5429
Jens Axboe05f3fb32019-12-09 11:22:50 -07005430static int io_files_update_prep(struct io_kiocb *req,
5431 const struct io_uring_sqe *sqe)
5432{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005433 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5434 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005435 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5436 return -EINVAL;
5437 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005438 return -EINVAL;
5439
5440 req->files_update.offset = READ_ONCE(sqe->off);
5441 req->files_update.nr_args = READ_ONCE(sqe->len);
5442 if (!req->files_update.nr_args)
5443 return -EINVAL;
5444 req->files_update.arg = READ_ONCE(sqe->addr);
5445 return 0;
5446}
5447
Jens Axboe229a7b62020-06-22 10:13:11 -06005448static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5449 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005450{
5451 struct io_ring_ctx *ctx = req->ctx;
5452 struct io_uring_files_update up;
5453 int ret;
5454
Jens Axboef86cd202020-01-29 13:46:44 -07005455 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005456 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005457
5458 up.offset = req->files_update.offset;
5459 up.fds = req->files_update.arg;
5460
5461 mutex_lock(&ctx->uring_lock);
5462 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5463 mutex_unlock(&ctx->uring_lock);
5464
5465 if (ret < 0)
5466 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005467 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005468 return 0;
5469}
5470
Jens Axboe3529d8c2019-12-19 18:24:38 -07005471static int io_req_defer_prep(struct io_kiocb *req,
5472 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005473{
Jens Axboee7815732019-12-17 19:45:06 -07005474 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005475
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005476 if (!sqe)
5477 return 0;
5478
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005479 if (io_alloc_async_ctx(req))
5480 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005481 ret = io_prep_work_files(req);
5482 if (unlikely(ret))
5483 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005484
Jens Axboe202700e12020-09-12 13:18:10 -06005485 io_prep_async_work(req);
5486
Jens Axboed625c6e2019-12-17 19:53:05 -07005487 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005488 case IORING_OP_NOP:
5489 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005490 case IORING_OP_READV:
5491 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005492 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005493 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005494 break;
5495 case IORING_OP_WRITEV:
5496 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005497 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005498 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005499 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005500 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005501 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005502 break;
5503 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005504 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005505 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005506 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005507 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005508 break;
5509 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005510 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005511 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005512 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005513 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005514 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005515 break;
5516 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005517 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005518 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005519 break;
Jens Axboef499a022019-12-02 16:28:46 -07005520 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005521 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005522 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005523 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005524 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005525 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005526 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005527 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005528 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005529 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005530 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005531 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005532 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005533 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005534 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005535 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005536 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005537 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005538 case IORING_OP_FALLOCATE:
5539 ret = io_fallocate_prep(req, sqe);
5540 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005541 case IORING_OP_OPENAT:
5542 ret = io_openat_prep(req, sqe);
5543 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005544 case IORING_OP_CLOSE:
5545 ret = io_close_prep(req, sqe);
5546 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005547 case IORING_OP_FILES_UPDATE:
5548 ret = io_files_update_prep(req, sqe);
5549 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005550 case IORING_OP_STATX:
5551 ret = io_statx_prep(req, sqe);
5552 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005553 case IORING_OP_FADVISE:
5554 ret = io_fadvise_prep(req, sqe);
5555 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005556 case IORING_OP_MADVISE:
5557 ret = io_madvise_prep(req, sqe);
5558 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005559 case IORING_OP_OPENAT2:
5560 ret = io_openat2_prep(req, sqe);
5561 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005562 case IORING_OP_EPOLL_CTL:
5563 ret = io_epoll_ctl_prep(req, sqe);
5564 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005565 case IORING_OP_SPLICE:
5566 ret = io_splice_prep(req, sqe);
5567 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005568 case IORING_OP_PROVIDE_BUFFERS:
5569 ret = io_provide_buffers_prep(req, sqe);
5570 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005571 case IORING_OP_REMOVE_BUFFERS:
5572 ret = io_remove_buffers_prep(req, sqe);
5573 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005574 case IORING_OP_TEE:
5575 ret = io_tee_prep(req, sqe);
5576 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005577 default:
Jens Axboee7815732019-12-17 19:45:06 -07005578 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5579 req->opcode);
5580 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005581 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005582 }
5583
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005584 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005585}
5586
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005587static u32 io_get_sequence(struct io_kiocb *req)
5588{
5589 struct io_kiocb *pos;
5590 struct io_ring_ctx *ctx = req->ctx;
5591 u32 total_submitted, nr_reqs = 1;
5592
5593 if (req->flags & REQ_F_LINK_HEAD)
5594 list_for_each_entry(pos, &req->link_list, link_list)
5595 nr_reqs++;
5596
5597 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5598 return total_submitted - nr_reqs;
5599}
5600
Jens Axboe3529d8c2019-12-19 18:24:38 -07005601static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005602{
Jackie Liua197f662019-11-08 08:09:12 -07005603 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005604 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005605 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005606 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005607
Bob Liu9d858b22019-11-13 18:06:25 +08005608 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005609 if (likely(list_empty_careful(&ctx->defer_list) &&
5610 !(req->flags & REQ_F_IO_DRAIN)))
5611 return 0;
5612
5613 seq = io_get_sequence(req);
5614 /* Still a chance to pass the sequence check */
5615 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005616 return 0;
5617
Pavel Begunkov650b5482020-05-17 14:02:11 +03005618 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005619 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005620 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005621 return ret;
5622 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005623 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005624 de = kmalloc(sizeof(*de), GFP_KERNEL);
5625 if (!de)
5626 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005627
Jens Axboede0617e2019-04-06 21:51:27 -06005628 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005629 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005630 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005631 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005632 io_queue_async_work(req);
5633 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005634 }
5635
Jens Axboe915967f2019-11-21 09:01:20 -07005636 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005637 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005638 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005639 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005640 spin_unlock_irq(&ctx->completion_lock);
5641 return -EIOCBQUEUED;
5642}
5643
Jens Axboef573d382020-09-22 10:19:24 -06005644static void io_req_drop_files(struct io_kiocb *req)
5645{
5646 struct io_ring_ctx *ctx = req->ctx;
5647 unsigned long flags;
5648
5649 spin_lock_irqsave(&ctx->inflight_lock, flags);
5650 list_del(&req->inflight_entry);
5651 if (waitqueue_active(&ctx->inflight_wait))
5652 wake_up(&ctx->inflight_wait);
5653 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5654 req->flags &= ~REQ_F_INFLIGHT;
5655 req->work.files = NULL;
5656}
5657
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005658static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005659{
5660 struct io_async_ctx *io = req->io;
5661
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005662 if (req->flags & REQ_F_BUFFER_SELECTED) {
5663 switch (req->opcode) {
5664 case IORING_OP_READV:
5665 case IORING_OP_READ_FIXED:
5666 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005667 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005668 break;
5669 case IORING_OP_RECVMSG:
5670 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005671 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005672 break;
5673 }
5674 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005675 }
5676
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005677 if (req->flags & REQ_F_NEED_CLEANUP) {
5678 switch (req->opcode) {
5679 case IORING_OP_READV:
5680 case IORING_OP_READ_FIXED:
5681 case IORING_OP_READ:
5682 case IORING_OP_WRITEV:
5683 case IORING_OP_WRITE_FIXED:
5684 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005685 if (io->rw.free_iovec)
5686 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005687 break;
5688 case IORING_OP_RECVMSG:
5689 case IORING_OP_SENDMSG:
5690 if (io->msg.iov != io->msg.fast_iov)
5691 kfree(io->msg.iov);
5692 break;
5693 case IORING_OP_SPLICE:
5694 case IORING_OP_TEE:
5695 io_put_file(req, req->splice.file_in,
5696 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5697 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005698 case IORING_OP_OPENAT:
5699 case IORING_OP_OPENAT2:
5700 if (req->open.filename)
5701 putname(req->open.filename);
5702 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005703 }
5704 req->flags &= ~REQ_F_NEED_CLEANUP;
5705 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005706
Jens Axboef573d382020-09-22 10:19:24 -06005707 if (req->flags & REQ_F_INFLIGHT)
5708 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005709}
5710
Jens Axboe3529d8c2019-12-19 18:24:38 -07005711static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005712 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005713{
Jackie Liua197f662019-11-08 08:09:12 -07005714 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005715 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005716
Jens Axboed625c6e2019-12-17 19:53:05 -07005717 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005718 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005719 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005720 break;
5721 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005722 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005723 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005724 if (sqe) {
5725 ret = io_read_prep(req, sqe, force_nonblock);
5726 if (ret < 0)
5727 break;
5728 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005729 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005730 break;
5731 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005732 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005733 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005734 if (sqe) {
5735 ret = io_write_prep(req, sqe, force_nonblock);
5736 if (ret < 0)
5737 break;
5738 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005739 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005740 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005741 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005742 if (sqe) {
5743 ret = io_prep_fsync(req, sqe);
5744 if (ret < 0)
5745 break;
5746 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005747 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005748 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005749 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005750 if (sqe) {
5751 ret = io_poll_add_prep(req, sqe);
5752 if (ret)
5753 break;
5754 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005755 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005756 break;
5757 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005758 if (sqe) {
5759 ret = io_poll_remove_prep(req, sqe);
5760 if (ret < 0)
5761 break;
5762 }
Jens Axboefc4df992019-12-10 14:38:45 -07005763 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005764 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005765 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005766 if (sqe) {
5767 ret = io_prep_sfr(req, sqe);
5768 if (ret < 0)
5769 break;
5770 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005771 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005772 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005773 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005774 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005775 if (sqe) {
5776 ret = io_sendmsg_prep(req, sqe);
5777 if (ret < 0)
5778 break;
5779 }
Jens Axboefddafac2020-01-04 20:19:44 -07005780 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005781 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005782 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005783 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005784 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005785 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005786 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005787 if (sqe) {
5788 ret = io_recvmsg_prep(req, sqe);
5789 if (ret)
5790 break;
5791 }
Jens Axboefddafac2020-01-04 20:19:44 -07005792 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005793 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005794 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005795 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005796 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005797 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005798 if (sqe) {
5799 ret = io_timeout_prep(req, sqe, false);
5800 if (ret)
5801 break;
5802 }
Jens Axboefc4df992019-12-10 14:38:45 -07005803 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005804 break;
Jens Axboe11365042019-10-16 09:08:32 -06005805 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005806 if (sqe) {
5807 ret = io_timeout_remove_prep(req, sqe);
5808 if (ret)
5809 break;
5810 }
Jens Axboefc4df992019-12-10 14:38:45 -07005811 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005812 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005813 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005814 if (sqe) {
5815 ret = io_accept_prep(req, sqe);
5816 if (ret)
5817 break;
5818 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005819 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005820 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005821 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005822 if (sqe) {
5823 ret = io_connect_prep(req, sqe);
5824 if (ret)
5825 break;
5826 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005827 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005828 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005829 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005830 if (sqe) {
5831 ret = io_async_cancel_prep(req, sqe);
5832 if (ret)
5833 break;
5834 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005835 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005836 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005837 case IORING_OP_FALLOCATE:
5838 if (sqe) {
5839 ret = io_fallocate_prep(req, sqe);
5840 if (ret)
5841 break;
5842 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005843 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005844 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005845 case IORING_OP_OPENAT:
5846 if (sqe) {
5847 ret = io_openat_prep(req, sqe);
5848 if (ret)
5849 break;
5850 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005851 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005852 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005853 case IORING_OP_CLOSE:
5854 if (sqe) {
5855 ret = io_close_prep(req, sqe);
5856 if (ret)
5857 break;
5858 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005859 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005860 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005861 case IORING_OP_FILES_UPDATE:
5862 if (sqe) {
5863 ret = io_files_update_prep(req, sqe);
5864 if (ret)
5865 break;
5866 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005867 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005868 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005869 case IORING_OP_STATX:
5870 if (sqe) {
5871 ret = io_statx_prep(req, sqe);
5872 if (ret)
5873 break;
5874 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005875 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005876 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005877 case IORING_OP_FADVISE:
5878 if (sqe) {
5879 ret = io_fadvise_prep(req, sqe);
5880 if (ret)
5881 break;
5882 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005883 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005884 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005885 case IORING_OP_MADVISE:
5886 if (sqe) {
5887 ret = io_madvise_prep(req, sqe);
5888 if (ret)
5889 break;
5890 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005891 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005892 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005893 case IORING_OP_OPENAT2:
5894 if (sqe) {
5895 ret = io_openat2_prep(req, sqe);
5896 if (ret)
5897 break;
5898 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005899 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005900 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005901 case IORING_OP_EPOLL_CTL:
5902 if (sqe) {
5903 ret = io_epoll_ctl_prep(req, sqe);
5904 if (ret)
5905 break;
5906 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005907 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005908 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005909 case IORING_OP_SPLICE:
5910 if (sqe) {
5911 ret = io_splice_prep(req, sqe);
5912 if (ret < 0)
5913 break;
5914 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005915 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005916 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005917 case IORING_OP_PROVIDE_BUFFERS:
5918 if (sqe) {
5919 ret = io_provide_buffers_prep(req, sqe);
5920 if (ret)
5921 break;
5922 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005923 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005924 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005925 case IORING_OP_REMOVE_BUFFERS:
5926 if (sqe) {
5927 ret = io_remove_buffers_prep(req, sqe);
5928 if (ret)
5929 break;
5930 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005931 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005932 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005933 case IORING_OP_TEE:
5934 if (sqe) {
5935 ret = io_tee_prep(req, sqe);
5936 if (ret < 0)
5937 break;
5938 }
5939 ret = io_tee(req, force_nonblock);
5940 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005941 default:
5942 ret = -EINVAL;
5943 break;
5944 }
5945
5946 if (ret)
5947 return ret;
5948
Jens Axboeb5325762020-05-19 21:20:27 -06005949 /* If the op doesn't have a file, we're not polling for it */
5950 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005951 const bool in_async = io_wq_current_is_worker();
5952
Jens Axboe11ba8202020-01-15 21:51:17 -07005953 /* workqueue context doesn't hold uring_lock, grab it now */
5954 if (in_async)
5955 mutex_lock(&ctx->uring_lock);
5956
Jens Axboe2b188cc2019-01-07 10:46:33 -07005957 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005958
5959 if (in_async)
5960 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005961 }
5962
5963 return 0;
5964}
5965
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005966static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005967{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005968 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005969 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005970 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005971
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005972 timeout = io_prep_linked_timeout(req);
5973 if (timeout)
5974 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005975
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005976 /* if NO_CANCEL is set, we must still run the work */
5977 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5978 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005979 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005980 }
Jens Axboe31b51512019-01-18 22:56:34 -07005981
Jens Axboe561fb042019-10-24 07:25:42 -06005982 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005983 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005984 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005985 /*
5986 * We can get EAGAIN for polled IO even though we're
5987 * forcing a sync submission from here, since we can't
5988 * wait for request slots on the block side.
5989 */
5990 if (ret != -EAGAIN)
5991 break;
5992 cond_resched();
5993 } while (1);
5994 }
Jens Axboe31b51512019-01-18 22:56:34 -07005995
Jens Axboe561fb042019-10-24 07:25:42 -06005996 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005997 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005998 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005999 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006000
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006001 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006002}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006003
Jens Axboe65e19f52019-10-26 07:20:21 -06006004static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6005 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006006{
Jens Axboe65e19f52019-10-26 07:20:21 -06006007 struct fixed_file_table *table;
6008
Jens Axboe05f3fb32019-12-09 11:22:50 -07006009 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006010 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006011}
6012
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006013static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6014 int fd, struct file **out_file, bool fixed)
6015{
6016 struct io_ring_ctx *ctx = req->ctx;
6017 struct file *file;
6018
6019 if (fixed) {
6020 if (unlikely(!ctx->file_data ||
6021 (unsigned) fd >= ctx->nr_user_files))
6022 return -EBADF;
6023 fd = array_index_nospec(fd, ctx->nr_user_files);
6024 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006025 if (file) {
6026 req->fixed_file_refs = ctx->file_data->cur_refs;
6027 percpu_ref_get(req->fixed_file_refs);
6028 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006029 } else {
6030 trace_io_uring_file_get(ctx, fd);
6031 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006032 }
6033
Jens Axboefd2206e2020-06-02 16:40:47 -06006034 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6035 *out_file = file;
6036 return 0;
6037 }
6038 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006039}
6040
Jens Axboe3529d8c2019-12-19 18:24:38 -07006041static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006042 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006043{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006044 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006045
Jens Axboe63ff8222020-05-07 14:56:15 -06006046 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006047 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006048 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006049
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006050 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006051}
6052
Jackie Liua197f662019-11-08 08:09:12 -07006053static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006054{
Jens Axboefcb323c2019-10-24 12:39:47 -06006055 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07006056 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006057
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006058 io_req_init_async(req);
6059
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006060 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006061 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006062 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07006063 return -EBADF;
6064
Jens Axboefcb323c2019-10-24 12:39:47 -06006065 rcu_read_lock();
6066 spin_lock_irq(&ctx->inflight_lock);
6067 /*
6068 * We use the f_ops->flush() handler to ensure that we can flush
6069 * out work accessing these files if the fd is closed. Check if
6070 * the fd has changed since we started down this path, and disallow
6071 * this operation if it has.
6072 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006073 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006074 list_add(&req->inflight_entry, &ctx->inflight_list);
6075 req->flags |= REQ_F_INFLIGHT;
6076 req->work.files = current->files;
6077 ret = 0;
6078 }
6079 spin_unlock_irq(&ctx->inflight_lock);
6080 rcu_read_unlock();
6081
6082 return ret;
6083}
6084
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006085static inline int io_prep_work_files(struct io_kiocb *req)
6086{
6087 if (!io_op_defs[req->opcode].file_table)
6088 return 0;
6089 return io_grab_files(req);
6090}
6091
Jens Axboe2665abf2019-11-05 12:40:47 -07006092static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6093{
Jens Axboead8a48a2019-11-15 08:49:11 -07006094 struct io_timeout_data *data = container_of(timer,
6095 struct io_timeout_data, timer);
6096 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006097 struct io_ring_ctx *ctx = req->ctx;
6098 struct io_kiocb *prev = NULL;
6099 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006100
6101 spin_lock_irqsave(&ctx->completion_lock, flags);
6102
6103 /*
6104 * We don't expect the list to be empty, that will only happen if we
6105 * race with the completion of the linked work.
6106 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006107 if (!list_empty(&req->link_list)) {
6108 prev = list_entry(req->link_list.prev, struct io_kiocb,
6109 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006110 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006111 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006112 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6113 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006114 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006115 }
6116
6117 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6118
6119 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006120 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006121 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006122 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006123 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006124 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006125 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006126 return HRTIMER_NORESTART;
6127}
6128
Jens Axboe7271ef32020-08-10 09:55:22 -06006129static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006130{
Jens Axboe76a46e02019-11-10 23:34:16 -07006131 /*
6132 * If the list is now empty, then our linked request finished before
6133 * we got a chance to setup the timer
6134 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006135 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006136 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006137
Jens Axboead8a48a2019-11-15 08:49:11 -07006138 data->timer.function = io_link_timeout_fn;
6139 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6140 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006141 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006142}
6143
6144static void io_queue_linked_timeout(struct io_kiocb *req)
6145{
6146 struct io_ring_ctx *ctx = req->ctx;
6147
6148 spin_lock_irq(&ctx->completion_lock);
6149 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006150 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006151
Jens Axboe2665abf2019-11-05 12:40:47 -07006152 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006153 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006154}
6155
Jens Axboead8a48a2019-11-15 08:49:11 -07006156static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006157{
6158 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006159
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006160 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006161 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006162 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006163 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006164
Pavel Begunkov44932332019-12-05 16:16:35 +03006165 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6166 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006167 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006168 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006169
Jens Axboe76a46e02019-11-10 23:34:16 -07006170 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006171 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006172}
6173
Jens Axboef13fad72020-06-22 09:34:30 -06006174static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6175 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006176{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006177 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006178 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006179 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180 int ret;
6181
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006182again:
6183 linked_timeout = io_prep_linked_timeout(req);
6184
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006185 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6186 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006187 if (old_creds)
6188 revert_creds(old_creds);
6189 if (old_creds == req->work.creds)
6190 old_creds = NULL; /* restored original creds */
6191 else
6192 old_creds = override_creds(req->work.creds);
6193 }
6194
Jens Axboef13fad72020-06-22 09:34:30 -06006195 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006196
6197 /*
6198 * We async punt it if the file wasn't marked NOWAIT, or if the file
6199 * doesn't support non-blocking read/write attempts
6200 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006201 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006202 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006203punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006204 ret = io_prep_work_files(req);
6205 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006206 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006207 /*
6208 * Queued up for async execution, worker will release
6209 * submit reference when the iocb is actually submitted.
6210 */
6211 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006212 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006213
Pavel Begunkovf063c542020-07-25 14:41:59 +03006214 if (linked_timeout)
6215 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006216 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006217 }
Jens Axboee65ef562019-03-12 10:16:44 -06006218
Pavel Begunkov652532a2020-07-03 22:15:07 +03006219 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006220err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006221 /* un-prep timeout, so it'll be killed as any other linked */
6222 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006223 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006224 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006225 io_req_complete(req, ret);
6226 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006227 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006228
Jens Axboe6c271ce2019-01-10 11:22:30 -07006229 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006230 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006231 if (linked_timeout)
6232 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006233
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006234 if (nxt) {
6235 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006236
6237 if (req->flags & REQ_F_FORCE_ASYNC)
6238 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006239 goto again;
6240 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006241exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006242 if (old_creds)
6243 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006244}
6245
Jens Axboef13fad72020-06-22 09:34:30 -06006246static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6247 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006248{
6249 int ret;
6250
Jens Axboe3529d8c2019-12-19 18:24:38 -07006251 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006252 if (ret) {
6253 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006254fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006255 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006256 io_put_req(req);
6257 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006258 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006259 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006260 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006261 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006262 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006263 goto fail_req;
6264 }
6265
Jens Axboece35a472019-12-17 08:04:44 -07006266 /*
6267 * Never try inline submit of IOSQE_ASYNC is set, go straight
6268 * to async execution.
6269 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006270 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006271 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6272 io_queue_async_work(req);
6273 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006274 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006275 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006276}
6277
Jens Axboef13fad72020-06-22 09:34:30 -06006278static inline void io_queue_link_head(struct io_kiocb *req,
6279 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006280{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006281 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006282 io_put_req(req);
6283 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006284 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006285 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006286}
6287
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006288static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006289 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006290{
Jackie Liua197f662019-11-08 08:09:12 -07006291 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006292 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006293
Jens Axboe9e645e112019-05-10 16:07:28 -06006294 /*
6295 * If we already have a head request, queue this one for async
6296 * submittal once the head completes. If we don't have a head but
6297 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6298 * submitted sync once the chain is complete. If none of those
6299 * conditions are true (normal request), then just queue it.
6300 */
6301 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006302 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006303
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006304 /*
6305 * Taking sequential execution of a link, draining both sides
6306 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6307 * requests in the link. So, it drains the head and the
6308 * next after the link request. The last one is done via
6309 * drain_next flag to persist the effect across calls.
6310 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006311 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006312 head->flags |= REQ_F_IO_DRAIN;
6313 ctx->drain_next = 1;
6314 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006315 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006316 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006317 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006318 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006319 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006320 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006321 trace_io_uring_link(ctx, req, head);
6322 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006323
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006324 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006325 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006326 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006327 *link = NULL;
6328 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006329 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006330 if (unlikely(ctx->drain_next)) {
6331 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006332 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006333 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006334 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006335 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006336 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006337
Pavel Begunkov711be032020-01-17 03:57:59 +03006338 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006339 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006340 req->flags |= REQ_F_FAIL_LINK;
6341 *link = req;
6342 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006343 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006344 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006345 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006346
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006347 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006348}
6349
Jens Axboe9a56a232019-01-09 09:06:50 -07006350/*
6351 * Batched submission is done, ensure local IO is flushed out.
6352 */
6353static void io_submit_state_end(struct io_submit_state *state)
6354{
Jens Axboef13fad72020-06-22 09:34:30 -06006355 if (!list_empty(&state->comp.list))
6356 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006357 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006358 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006359 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006360 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006361}
6362
6363/*
6364 * Start submission side cache.
6365 */
6366static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006367 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006368{
6369 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006370 state->comp.nr = 0;
6371 INIT_LIST_HEAD(&state->comp.list);
6372 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006373 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006374 state->file = NULL;
6375 state->ios_left = max_ios;
6376}
6377
Jens Axboe2b188cc2019-01-07 10:46:33 -07006378static void io_commit_sqring(struct io_ring_ctx *ctx)
6379{
Hristo Venev75b28af2019-08-26 17:23:46 +00006380 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006381
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006382 /*
6383 * Ensure any loads from the SQEs are done at this point,
6384 * since once we write the new head, the application could
6385 * write new data to them.
6386 */
6387 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006388}
6389
6390/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006391 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006392 * that is mapped by userspace. This means that care needs to be taken to
6393 * ensure that reads are stable, as we cannot rely on userspace always
6394 * being a good citizen. If members of the sqe are validated and then later
6395 * used, it's important that those reads are done through READ_ONCE() to
6396 * prevent a re-load down the line.
6397 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006398static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006399{
Hristo Venev75b28af2019-08-26 17:23:46 +00006400 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006401 unsigned head;
6402
6403 /*
6404 * The cached sq head (or cq tail) serves two purposes:
6405 *
6406 * 1) allows us to batch the cost of updating the user visible
6407 * head updates.
6408 * 2) allows the kernel side to track the head on its own, even
6409 * though the application is the one updating it.
6410 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006411 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006412 if (likely(head < ctx->sq_entries))
6413 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006414
6415 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006416 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006417 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006418 return NULL;
6419}
6420
6421static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6422{
6423 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006424}
6425
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006426#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6427 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6428 IOSQE_BUFFER_SELECT)
6429
6430static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6431 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006432 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006433{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006434 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006435 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006436
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006437 req->opcode = READ_ONCE(sqe->opcode);
6438 req->user_data = READ_ONCE(sqe->user_data);
6439 req->io = NULL;
6440 req->file = NULL;
6441 req->ctx = ctx;
6442 req->flags = 0;
6443 /* one is dropped after submission, the other at completion */
6444 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006445 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006446 get_task_struct(req->task);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006447 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006448
6449 if (unlikely(req->opcode >= IORING_OP_LAST))
6450 return -EINVAL;
6451
Jens Axboe9d8426a2020-06-16 18:42:49 -06006452 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6453 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006454
6455 sqe_flags = READ_ONCE(sqe->flags);
6456 /* enforce forwards compatibility on users */
6457 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6458 return -EINVAL;
6459
6460 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6461 !io_op_defs[req->opcode].buffer_select)
6462 return -EOPNOTSUPP;
6463
6464 id = READ_ONCE(sqe->personality);
6465 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006466 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006467 req->work.creds = idr_find(&ctx->personality_idr, id);
6468 if (unlikely(!req->work.creds))
6469 return -EINVAL;
6470 get_cred(req->work.creds);
6471 }
6472
6473 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006474 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006475
Jens Axboe63ff8222020-05-07 14:56:15 -06006476 if (!io_op_defs[req->opcode].needs_file)
6477 return 0;
6478
6479 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006480}
6481
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006482static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006483 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006484{
Jens Axboeac8691c2020-06-01 08:30:41 -06006485 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006486 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006487 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006488
Jens Axboec4a2ed72019-11-21 21:01:26 -07006489 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006490 if (test_bit(0, &ctx->sq_check_overflow)) {
6491 if (!list_empty(&ctx->cq_overflow_list) &&
6492 !io_cqring_overflow_flush(ctx, false))
6493 return -EBUSY;
6494 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006495
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006496 /* make sure SQ entry isn't read before tail */
6497 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006498
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006499 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6500 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006501
Jens Axboe013538b2020-06-22 09:29:15 -06006502 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006503
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006504 ctx->ring_fd = ring_fd;
6505 ctx->ring_file = ring_file;
6506
Jens Axboe6c271ce2019-01-10 11:22:30 -07006507 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006508 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006509 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006510 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006511
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006512 sqe = io_get_sqe(ctx);
6513 if (unlikely(!sqe)) {
6514 io_consume_sqe(ctx);
6515 break;
6516 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006517 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006518 if (unlikely(!req)) {
6519 if (!submitted)
6520 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006521 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006522 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006523
Jens Axboeac8691c2020-06-01 08:30:41 -06006524 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006525 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006526 /* will complete beyond this point, count as submitted */
6527 submitted++;
6528
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006529 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006530fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006531 io_put_req(req);
6532 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006533 break;
6534 }
6535
Jens Axboe354420f2020-01-08 18:55:15 -07006536 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006537 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006538 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006539 if (err)
6540 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006541 }
6542
Pavel Begunkov9466f432020-01-25 22:34:01 +03006543 if (unlikely(submitted != nr)) {
6544 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6545
6546 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6547 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006548 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006549 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006550 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006551
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006552 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6553 io_commit_sqring(ctx);
6554
Jens Axboe6c271ce2019-01-10 11:22:30 -07006555 return submitted;
6556}
6557
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006558static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6559{
6560 /* Tell userspace we may need a wakeup call */
6561 spin_lock_irq(&ctx->completion_lock);
6562 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6563 spin_unlock_irq(&ctx->completion_lock);
6564}
6565
6566static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6567{
6568 spin_lock_irq(&ctx->completion_lock);
6569 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6570 spin_unlock_irq(&ctx->completion_lock);
6571}
6572
Jens Axboe6c271ce2019-01-10 11:22:30 -07006573static int io_sq_thread(void *data)
6574{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006575 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006576 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006577 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006578 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006579 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006580
Jens Axboe0f158b42020-05-14 17:18:39 -06006581 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006582
Jens Axboe181e4482019-11-25 08:52:30 -07006583 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006584
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006585 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006586 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006587 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006588
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006589 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006590 unsigned nr_events = 0;
6591
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006592 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006593 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006594 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006595 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006596 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006597 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006598 }
6599
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006600 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006601
6602 /*
6603 * If submit got -EBUSY, flag us as needing the application
6604 * to enter the kernel to reap and flush events.
6605 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006606 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006607 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006608 * Drop cur_mm before scheduling, we can't hold it for
6609 * long periods (or over schedule()). Do this before
6610 * adding ourselves to the waitqueue, as the unuse/drop
6611 * may sleep.
6612 */
Jens Axboe4349f302020-07-09 15:07:01 -06006613 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006614
6615 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006616 * We're polling. If we're within the defined idle
6617 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006618 * to sleep. The exception is if we got EBUSY doing
6619 * more IO, we should wait for the application to
6620 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006621 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006622 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006623 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6624 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006625 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006626 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006627 continue;
6628 }
6629
Jens Axboe6c271ce2019-01-10 11:22:30 -07006630 prepare_to_wait(&ctx->sqo_wait, &wait,
6631 TASK_INTERRUPTIBLE);
6632
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006633 /*
6634 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006635 * to check if there are new reqs added to iopoll_list,
6636 * it is because reqs may have been punted to io worker
6637 * and will be added to iopoll_list later, hence check
6638 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006639 */
6640 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006641 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006642 finish_wait(&ctx->sqo_wait, &wait);
6643 continue;
6644 }
6645
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006646 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006647
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006648 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006649 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006650 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006651 finish_wait(&ctx->sqo_wait, &wait);
6652 break;
6653 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006654 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006655 finish_wait(&ctx->sqo_wait, &wait);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006656 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006657 continue;
6658 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006659 if (signal_pending(current))
6660 flush_signals(current);
6661 schedule();
6662 finish_wait(&ctx->sqo_wait, &wait);
6663
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006664 io_ring_clear_wakeup_flag(ctx);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006665 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006666 continue;
6667 }
6668 finish_wait(&ctx->sqo_wait, &wait);
6669
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006670 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006671 }
6672
Jens Axboe8a4955f2019-12-09 14:52:35 -07006673 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006674 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6675 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006676 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006677 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006678 }
6679
Jens Axboe4c6e2772020-07-01 11:29:10 -06006680 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006681
Jens Axboe4349f302020-07-09 15:07:01 -06006682 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006683 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006684
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006685 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006686
Jens Axboe6c271ce2019-01-10 11:22:30 -07006687 return 0;
6688}
6689
Jens Axboebda52162019-09-24 13:47:15 -06006690struct io_wait_queue {
6691 struct wait_queue_entry wq;
6692 struct io_ring_ctx *ctx;
6693 unsigned to_wait;
6694 unsigned nr_timeouts;
6695};
6696
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006697static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006698{
6699 struct io_ring_ctx *ctx = iowq->ctx;
6700
6701 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006702 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006703 * started waiting. For timeouts, we always want to return to userspace,
6704 * regardless of event count.
6705 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006706 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006707 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6708}
6709
6710static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6711 int wake_flags, void *key)
6712{
6713 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6714 wq);
6715
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006716 /* use noflush == true, as we can't safely rely on locking context */
6717 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006718 return -1;
6719
6720 return autoremove_wake_function(curr, mode, wake_flags, key);
6721}
6722
Jens Axboe2b188cc2019-01-07 10:46:33 -07006723/*
6724 * Wait until events become available, if we don't already have some. The
6725 * application must reap them itself, as they reside on the shared cq ring.
6726 */
6727static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6728 const sigset_t __user *sig, size_t sigsz)
6729{
Jens Axboebda52162019-09-24 13:47:15 -06006730 struct io_wait_queue iowq = {
6731 .wq = {
6732 .private = current,
6733 .func = io_wake_function,
6734 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6735 },
6736 .ctx = ctx,
6737 .to_wait = min_events,
6738 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006739 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006740 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006741
Jens Axboeb41e9852020-02-17 09:52:41 -07006742 do {
6743 if (io_cqring_events(ctx, false) >= min_events)
6744 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006745 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006746 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006747 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006748
6749 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006750#ifdef CONFIG_COMPAT
6751 if (in_compat_syscall())
6752 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006753 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006754 else
6755#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006756 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006757
Jens Axboe2b188cc2019-01-07 10:46:33 -07006758 if (ret)
6759 return ret;
6760 }
6761
Jens Axboebda52162019-09-24 13:47:15 -06006762 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006763 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006764 do {
6765 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6766 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006767 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006768 if (io_run_task_work())
6769 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006770 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006771 if (current->jobctl & JOBCTL_TASK_WORK) {
6772 spin_lock_irq(&current->sighand->siglock);
6773 current->jobctl &= ~JOBCTL_TASK_WORK;
6774 recalc_sigpending();
6775 spin_unlock_irq(&current->sighand->siglock);
6776 continue;
6777 }
6778 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006779 break;
6780 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006781 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006782 break;
6783 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006784 } while (1);
6785 finish_wait(&ctx->wait, &iowq.wq);
6786
Jens Axboeb7db41c2020-07-04 08:55:50 -06006787 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006788
Hristo Venev75b28af2019-08-26 17:23:46 +00006789 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006790}
6791
Jens Axboe6b063142019-01-10 22:13:58 -07006792static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6793{
6794#if defined(CONFIG_UNIX)
6795 if (ctx->ring_sock) {
6796 struct sock *sock = ctx->ring_sock->sk;
6797 struct sk_buff *skb;
6798
6799 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6800 kfree_skb(skb);
6801 }
6802#else
6803 int i;
6804
Jens Axboe65e19f52019-10-26 07:20:21 -06006805 for (i = 0; i < ctx->nr_user_files; i++) {
6806 struct file *file;
6807
6808 file = io_file_from_index(ctx, i);
6809 if (file)
6810 fput(file);
6811 }
Jens Axboe6b063142019-01-10 22:13:58 -07006812#endif
6813}
6814
Jens Axboe05f3fb32019-12-09 11:22:50 -07006815static void io_file_ref_kill(struct percpu_ref *ref)
6816{
6817 struct fixed_file_data *data;
6818
6819 data = container_of(ref, struct fixed_file_data, refs);
6820 complete(&data->done);
6821}
6822
Jens Axboe6b063142019-01-10 22:13:58 -07006823static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6824{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006825 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006826 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006827 unsigned nr_tables, i;
6828
Jens Axboe05f3fb32019-12-09 11:22:50 -07006829 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006830 return -ENXIO;
6831
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006832 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006833 if (!list_empty(&data->ref_list))
6834 ref_node = list_first_entry(&data->ref_list,
6835 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006836 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006837 if (ref_node)
6838 percpu_ref_kill(&ref_node->refs);
6839
6840 percpu_ref_kill(&data->refs);
6841
6842 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006843 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006844 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006845
Jens Axboe6b063142019-01-10 22:13:58 -07006846 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006847 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6848 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006849 kfree(data->table[i].files);
6850 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006851 percpu_ref_exit(&data->refs);
6852 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006853 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006854 ctx->nr_user_files = 0;
6855 return 0;
6856}
6857
Jens Axboe6c271ce2019-01-10 11:22:30 -07006858static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6859{
6860 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006861 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006862 /*
6863 * The park is a bit of a work-around, without it we get
6864 * warning spews on shutdown with SQPOLL set and affinity
6865 * set to a single CPU.
6866 */
Jens Axboe06058632019-04-13 09:26:03 -06006867 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006868 kthread_stop(ctx->sqo_thread);
6869 ctx->sqo_thread = NULL;
6870 }
6871}
6872
Jens Axboe6b063142019-01-10 22:13:58 -07006873static void io_finish_async(struct io_ring_ctx *ctx)
6874{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006875 io_sq_thread_stop(ctx);
6876
Jens Axboe561fb042019-10-24 07:25:42 -06006877 if (ctx->io_wq) {
6878 io_wq_destroy(ctx->io_wq);
6879 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006880 }
6881}
6882
6883#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006884/*
6885 * Ensure the UNIX gc is aware of our file set, so we are certain that
6886 * the io_uring can be safely unregistered on process exit, even if we have
6887 * loops in the file referencing.
6888 */
6889static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6890{
6891 struct sock *sk = ctx->ring_sock->sk;
6892 struct scm_fp_list *fpl;
6893 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006894 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006895
Jens Axboe6b063142019-01-10 22:13:58 -07006896 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6897 if (!fpl)
6898 return -ENOMEM;
6899
6900 skb = alloc_skb(0, GFP_KERNEL);
6901 if (!skb) {
6902 kfree(fpl);
6903 return -ENOMEM;
6904 }
6905
6906 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006907
Jens Axboe08a45172019-10-03 08:11:03 -06006908 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006909 fpl->user = get_uid(ctx->user);
6910 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006911 struct file *file = io_file_from_index(ctx, i + offset);
6912
6913 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006914 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006915 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006916 unix_inflight(fpl->user, fpl->fp[nr_files]);
6917 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006918 }
6919
Jens Axboe08a45172019-10-03 08:11:03 -06006920 if (nr_files) {
6921 fpl->max = SCM_MAX_FD;
6922 fpl->count = nr_files;
6923 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006924 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006925 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6926 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006927
Jens Axboe08a45172019-10-03 08:11:03 -06006928 for (i = 0; i < nr_files; i++)
6929 fput(fpl->fp[i]);
6930 } else {
6931 kfree_skb(skb);
6932 kfree(fpl);
6933 }
Jens Axboe6b063142019-01-10 22:13:58 -07006934
6935 return 0;
6936}
6937
6938/*
6939 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6940 * causes regular reference counting to break down. We rely on the UNIX
6941 * garbage collection to take care of this problem for us.
6942 */
6943static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6944{
6945 unsigned left, total;
6946 int ret = 0;
6947
6948 total = 0;
6949 left = ctx->nr_user_files;
6950 while (left) {
6951 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006952
6953 ret = __io_sqe_files_scm(ctx, this_files, total);
6954 if (ret)
6955 break;
6956 left -= this_files;
6957 total += this_files;
6958 }
6959
6960 if (!ret)
6961 return 0;
6962
6963 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006964 struct file *file = io_file_from_index(ctx, total);
6965
6966 if (file)
6967 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006968 total++;
6969 }
6970
6971 return ret;
6972}
6973#else
6974static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6975{
6976 return 0;
6977}
6978#endif
6979
Jens Axboe65e19f52019-10-26 07:20:21 -06006980static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6981 unsigned nr_files)
6982{
6983 int i;
6984
6985 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006986 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006987 unsigned this_files;
6988
6989 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6990 table->files = kcalloc(this_files, sizeof(struct file *),
6991 GFP_KERNEL);
6992 if (!table->files)
6993 break;
6994 nr_files -= this_files;
6995 }
6996
6997 if (i == nr_tables)
6998 return 0;
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 kfree(table->files);
7003 }
7004 return 1;
7005}
7006
Jens Axboe05f3fb32019-12-09 11:22:50 -07007007static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007008{
7009#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007010 struct sock *sock = ctx->ring_sock->sk;
7011 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7012 struct sk_buff *skb;
7013 int i;
7014
7015 __skb_queue_head_init(&list);
7016
7017 /*
7018 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7019 * remove this entry and rearrange the file array.
7020 */
7021 skb = skb_dequeue(head);
7022 while (skb) {
7023 struct scm_fp_list *fp;
7024
7025 fp = UNIXCB(skb).fp;
7026 for (i = 0; i < fp->count; i++) {
7027 int left;
7028
7029 if (fp->fp[i] != file)
7030 continue;
7031
7032 unix_notinflight(fp->user, fp->fp[i]);
7033 left = fp->count - 1 - i;
7034 if (left) {
7035 memmove(&fp->fp[i], &fp->fp[i + 1],
7036 left * sizeof(struct file *));
7037 }
7038 fp->count--;
7039 if (!fp->count) {
7040 kfree_skb(skb);
7041 skb = NULL;
7042 } else {
7043 __skb_queue_tail(&list, skb);
7044 }
7045 fput(file);
7046 file = NULL;
7047 break;
7048 }
7049
7050 if (!file)
7051 break;
7052
7053 __skb_queue_tail(&list, skb);
7054
7055 skb = skb_dequeue(head);
7056 }
7057
7058 if (skb_peek(&list)) {
7059 spin_lock_irq(&head->lock);
7060 while ((skb = __skb_dequeue(&list)) != NULL)
7061 __skb_queue_tail(head, skb);
7062 spin_unlock_irq(&head->lock);
7063 }
7064#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007065 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007066#endif
7067}
7068
Jens Axboe05f3fb32019-12-09 11:22:50 -07007069struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007070 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007071 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007072};
7073
Jens Axboe4a38aed22020-05-14 17:21:15 -06007074static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007075{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007076 struct fixed_file_data *file_data = ref_node->file_data;
7077 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007078 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007079
7080 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007081 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007082 io_ring_file_put(ctx, pfile->file);
7083 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007084 }
7085
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007086 spin_lock(&file_data->lock);
7087 list_del(&ref_node->node);
7088 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007089
Xiaoguang Wang05589552020-03-31 14:05:18 +08007090 percpu_ref_exit(&ref_node->refs);
7091 kfree(ref_node);
7092 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007093}
7094
Jens Axboe4a38aed22020-05-14 17:21:15 -06007095static void io_file_put_work(struct work_struct *work)
7096{
7097 struct io_ring_ctx *ctx;
7098 struct llist_node *node;
7099
7100 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7101 node = llist_del_all(&ctx->file_put_llist);
7102
7103 while (node) {
7104 struct fixed_file_ref_node *ref_node;
7105 struct llist_node *next = node->next;
7106
7107 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7108 __io_file_put_work(ref_node);
7109 node = next;
7110 }
7111}
7112
Jens Axboe05f3fb32019-12-09 11:22:50 -07007113static void io_file_data_ref_zero(struct percpu_ref *ref)
7114{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007115 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007116 struct io_ring_ctx *ctx;
7117 bool first_add;
7118 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007119
Xiaoguang Wang05589552020-03-31 14:05:18 +08007120 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007121 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007122
Jens Axboe4a38aed22020-05-14 17:21:15 -06007123 if (percpu_ref_is_dying(&ctx->file_data->refs))
7124 delay = 0;
7125
7126 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7127 if (!delay)
7128 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7129 else if (first_add)
7130 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007131}
7132
7133static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7134 struct io_ring_ctx *ctx)
7135{
7136 struct fixed_file_ref_node *ref_node;
7137
7138 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7139 if (!ref_node)
7140 return ERR_PTR(-ENOMEM);
7141
7142 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7143 0, GFP_KERNEL)) {
7144 kfree(ref_node);
7145 return ERR_PTR(-ENOMEM);
7146 }
7147 INIT_LIST_HEAD(&ref_node->node);
7148 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007149 ref_node->file_data = ctx->file_data;
7150 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007151}
7152
7153static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7154{
7155 percpu_ref_exit(&ref_node->refs);
7156 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007157}
7158
7159static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7160 unsigned nr_args)
7161{
7162 __s32 __user *fds = (__s32 __user *) arg;
7163 unsigned nr_tables;
7164 struct file *file;
7165 int fd, ret = 0;
7166 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007167 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007168
7169 if (ctx->file_data)
7170 return -EBUSY;
7171 if (!nr_args)
7172 return -EINVAL;
7173 if (nr_args > IORING_MAX_FIXED_FILES)
7174 return -EMFILE;
7175
7176 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7177 if (!ctx->file_data)
7178 return -ENOMEM;
7179 ctx->file_data->ctx = ctx;
7180 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007181 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007182 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007183
7184 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7185 ctx->file_data->table = kcalloc(nr_tables,
7186 sizeof(struct fixed_file_table),
7187 GFP_KERNEL);
7188 if (!ctx->file_data->table) {
7189 kfree(ctx->file_data);
7190 ctx->file_data = NULL;
7191 return -ENOMEM;
7192 }
7193
Xiaoguang Wang05589552020-03-31 14:05:18 +08007194 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007195 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7196 kfree(ctx->file_data->table);
7197 kfree(ctx->file_data);
7198 ctx->file_data = NULL;
7199 return -ENOMEM;
7200 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007201
7202 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7203 percpu_ref_exit(&ctx->file_data->refs);
7204 kfree(ctx->file_data->table);
7205 kfree(ctx->file_data);
7206 ctx->file_data = NULL;
7207 return -ENOMEM;
7208 }
7209
7210 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7211 struct fixed_file_table *table;
7212 unsigned index;
7213
7214 ret = -EFAULT;
7215 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7216 break;
7217 /* allow sparse sets */
7218 if (fd == -1) {
7219 ret = 0;
7220 continue;
7221 }
7222
7223 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7224 index = i & IORING_FILE_TABLE_MASK;
7225 file = fget(fd);
7226
7227 ret = -EBADF;
7228 if (!file)
7229 break;
7230
7231 /*
7232 * Don't allow io_uring instances to be registered. If UNIX
7233 * isn't enabled, then this causes a reference cycle and this
7234 * instance can never get freed. If UNIX is enabled we'll
7235 * handle it just fine, but there's still no point in allowing
7236 * a ring fd as it doesn't support regular read/write anyway.
7237 */
7238 if (file->f_op == &io_uring_fops) {
7239 fput(file);
7240 break;
7241 }
7242 ret = 0;
7243 table->files[index] = file;
7244 }
7245
7246 if (ret) {
7247 for (i = 0; i < ctx->nr_user_files; i++) {
7248 file = io_file_from_index(ctx, i);
7249 if (file)
7250 fput(file);
7251 }
7252 for (i = 0; i < nr_tables; i++)
7253 kfree(ctx->file_data->table[i].files);
7254
Yang Yingliang667e57d2020-07-10 14:14:20 +00007255 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007256 kfree(ctx->file_data->table);
7257 kfree(ctx->file_data);
7258 ctx->file_data = NULL;
7259 ctx->nr_user_files = 0;
7260 return ret;
7261 }
7262
7263 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007264 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007265 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007266 return ret;
7267 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007268
Xiaoguang Wang05589552020-03-31 14:05:18 +08007269 ref_node = alloc_fixed_file_ref_node(ctx);
7270 if (IS_ERR(ref_node)) {
7271 io_sqe_files_unregister(ctx);
7272 return PTR_ERR(ref_node);
7273 }
7274
7275 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007276 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007277 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007278 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007279 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007280 return ret;
7281}
7282
Jens Axboec3a31e62019-10-03 13:59:56 -06007283static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7284 int index)
7285{
7286#if defined(CONFIG_UNIX)
7287 struct sock *sock = ctx->ring_sock->sk;
7288 struct sk_buff_head *head = &sock->sk_receive_queue;
7289 struct sk_buff *skb;
7290
7291 /*
7292 * See if we can merge this file into an existing skb SCM_RIGHTS
7293 * file set. If there's no room, fall back to allocating a new skb
7294 * and filling it in.
7295 */
7296 spin_lock_irq(&head->lock);
7297 skb = skb_peek(head);
7298 if (skb) {
7299 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7300
7301 if (fpl->count < SCM_MAX_FD) {
7302 __skb_unlink(skb, head);
7303 spin_unlock_irq(&head->lock);
7304 fpl->fp[fpl->count] = get_file(file);
7305 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7306 fpl->count++;
7307 spin_lock_irq(&head->lock);
7308 __skb_queue_head(head, skb);
7309 } else {
7310 skb = NULL;
7311 }
7312 }
7313 spin_unlock_irq(&head->lock);
7314
7315 if (skb) {
7316 fput(file);
7317 return 0;
7318 }
7319
7320 return __io_sqe_files_scm(ctx, 1, index);
7321#else
7322 return 0;
7323#endif
7324}
7325
Hillf Dantona5318d32020-03-23 17:47:15 +08007326static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007327 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007328{
Hillf Dantona5318d32020-03-23 17:47:15 +08007329 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007330 struct percpu_ref *refs = data->cur_refs;
7331 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007332
Jens Axboe05f3fb32019-12-09 11:22:50 -07007333 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007334 if (!pfile)
7335 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007336
Xiaoguang Wang05589552020-03-31 14:05:18 +08007337 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007338 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007339 list_add(&pfile->list, &ref_node->file_list);
7340
Hillf Dantona5318d32020-03-23 17:47:15 +08007341 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007342}
7343
7344static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7345 struct io_uring_files_update *up,
7346 unsigned nr_args)
7347{
7348 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007349 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007350 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007351 __s32 __user *fds;
7352 int fd, i, err;
7353 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007354 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007355
Jens Axboe05f3fb32019-12-09 11:22:50 -07007356 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007357 return -EOVERFLOW;
7358 if (done > ctx->nr_user_files)
7359 return -EINVAL;
7360
Xiaoguang Wang05589552020-03-31 14:05:18 +08007361 ref_node = alloc_fixed_file_ref_node(ctx);
7362 if (IS_ERR(ref_node))
7363 return PTR_ERR(ref_node);
7364
Jens Axboec3a31e62019-10-03 13:59:56 -06007365 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007366 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007367 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007368 struct fixed_file_table *table;
7369 unsigned index;
7370
Jens Axboec3a31e62019-10-03 13:59:56 -06007371 err = 0;
7372 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7373 err = -EFAULT;
7374 break;
7375 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007376 i = array_index_nospec(up->offset, ctx->nr_user_files);
7377 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007378 index = i & IORING_FILE_TABLE_MASK;
7379 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007380 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007381 err = io_queue_file_removal(data, file);
7382 if (err)
7383 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007384 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007385 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007386 }
7387 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007388 file = fget(fd);
7389 if (!file) {
7390 err = -EBADF;
7391 break;
7392 }
7393 /*
7394 * Don't allow io_uring instances to be registered. If
7395 * UNIX isn't enabled, then this causes a reference
7396 * cycle and this instance can never get freed. If UNIX
7397 * is enabled we'll handle it just fine, but there's
7398 * still no point in allowing a ring fd as it doesn't
7399 * support regular read/write anyway.
7400 */
7401 if (file->f_op == &io_uring_fops) {
7402 fput(file);
7403 err = -EBADF;
7404 break;
7405 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007406 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007407 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007408 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007409 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007410 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007411 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007412 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007413 }
7414 nr_args--;
7415 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007416 up->offset++;
7417 }
7418
Xiaoguang Wang05589552020-03-31 14:05:18 +08007419 if (needs_switch) {
7420 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007421 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007422 list_add(&ref_node->node, &data->ref_list);
7423 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007424 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007425 percpu_ref_get(&ctx->file_data->refs);
7426 } else
7427 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007428
7429 return done ? done : err;
7430}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007431
Jens Axboe05f3fb32019-12-09 11:22:50 -07007432static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7433 unsigned nr_args)
7434{
7435 struct io_uring_files_update up;
7436
7437 if (!ctx->file_data)
7438 return -ENXIO;
7439 if (!nr_args)
7440 return -EINVAL;
7441 if (copy_from_user(&up, arg, sizeof(up)))
7442 return -EFAULT;
7443 if (up.resv)
7444 return -EINVAL;
7445
7446 return __io_sqe_files_update(ctx, &up, nr_args);
7447}
Jens Axboec3a31e62019-10-03 13:59:56 -06007448
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007449static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007450{
7451 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7452
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007453 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007454 io_put_req(req);
7455}
7456
Pavel Begunkov24369c22020-01-28 03:15:48 +03007457static int io_init_wq_offload(struct io_ring_ctx *ctx,
7458 struct io_uring_params *p)
7459{
7460 struct io_wq_data data;
7461 struct fd f;
7462 struct io_ring_ctx *ctx_attach;
7463 unsigned int concurrency;
7464 int ret = 0;
7465
7466 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007467 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007468 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007469
7470 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7471 /* Do QD, or 4 * CPUS, whatever is smallest */
7472 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7473
7474 ctx->io_wq = io_wq_create(concurrency, &data);
7475 if (IS_ERR(ctx->io_wq)) {
7476 ret = PTR_ERR(ctx->io_wq);
7477 ctx->io_wq = NULL;
7478 }
7479 return ret;
7480 }
7481
7482 f = fdget(p->wq_fd);
7483 if (!f.file)
7484 return -EBADF;
7485
7486 if (f.file->f_op != &io_uring_fops) {
7487 ret = -EINVAL;
7488 goto out_fput;
7489 }
7490
7491 ctx_attach = f.file->private_data;
7492 /* @io_wq is protected by holding the fd */
7493 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7494 ret = -EINVAL;
7495 goto out_fput;
7496 }
7497
7498 ctx->io_wq = ctx_attach->io_wq;
7499out_fput:
7500 fdput(f);
7501 return ret;
7502}
7503
Jens Axboe6c271ce2019-01-10 11:22:30 -07007504static int io_sq_offload_start(struct io_ring_ctx *ctx,
7505 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007506{
7507 int ret;
7508
Jens Axboe6c271ce2019-01-10 11:22:30 -07007509 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007510 ret = -EPERM;
7511 if (!capable(CAP_SYS_ADMIN))
7512 goto err;
7513
Jens Axboe917257d2019-04-13 09:28:55 -06007514 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7515 if (!ctx->sq_thread_idle)
7516 ctx->sq_thread_idle = HZ;
7517
Jens Axboe6c271ce2019-01-10 11:22:30 -07007518 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007519 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007520
Jens Axboe917257d2019-04-13 09:28:55 -06007521 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007522 if (cpu >= nr_cpu_ids)
7523 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007524 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007525 goto err;
7526
Jens Axboe6c271ce2019-01-10 11:22:30 -07007527 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7528 ctx, cpu,
7529 "io_uring-sq");
7530 } else {
7531 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7532 "io_uring-sq");
7533 }
7534 if (IS_ERR(ctx->sqo_thread)) {
7535 ret = PTR_ERR(ctx->sqo_thread);
7536 ctx->sqo_thread = NULL;
7537 goto err;
7538 }
7539 wake_up_process(ctx->sqo_thread);
7540 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7541 /* Can't have SQ_AFF without SQPOLL */
7542 ret = -EINVAL;
7543 goto err;
7544 }
7545
Pavel Begunkov24369c22020-01-28 03:15:48 +03007546 ret = io_init_wq_offload(ctx, p);
7547 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007548 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007549
7550 return 0;
7551err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007552 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007553 return ret;
7554}
7555
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007556static inline void __io_unaccount_mem(struct user_struct *user,
7557 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007558{
7559 atomic_long_sub(nr_pages, &user->locked_vm);
7560}
7561
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007562static inline int __io_account_mem(struct user_struct *user,
7563 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007564{
7565 unsigned long page_limit, cur_pages, new_pages;
7566
7567 /* Don't allow more pages than we can safely lock */
7568 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7569
7570 do {
7571 cur_pages = atomic_long_read(&user->locked_vm);
7572 new_pages = cur_pages + nr_pages;
7573 if (new_pages > page_limit)
7574 return -ENOMEM;
7575 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7576 new_pages) != cur_pages);
7577
7578 return 0;
7579}
7580
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007581static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7582 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007583{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007584 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007585 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007586
Jens Axboe2aede0e2020-09-14 10:45:53 -06007587 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007588 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007589 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007590 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007591 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007592 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007593}
7594
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007595static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7596 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007597{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007598 int ret;
7599
7600 if (ctx->limit_mem) {
7601 ret = __io_account_mem(ctx->user, nr_pages);
7602 if (ret)
7603 return ret;
7604 }
7605
Jens Axboe2aede0e2020-09-14 10:45:53 -06007606 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007607 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007608 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007609 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007610 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007611 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007612
7613 return 0;
7614}
7615
Jens Axboe2b188cc2019-01-07 10:46:33 -07007616static void io_mem_free(void *ptr)
7617{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007618 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007619
Mark Rutland52e04ef2019-04-30 17:30:21 +01007620 if (!ptr)
7621 return;
7622
7623 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007624 if (put_page_testzero(page))
7625 free_compound_page(page);
7626}
7627
7628static void *io_mem_alloc(size_t size)
7629{
7630 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7631 __GFP_NORETRY;
7632
7633 return (void *) __get_free_pages(gfp_flags, get_order(size));
7634}
7635
Hristo Venev75b28af2019-08-26 17:23:46 +00007636static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7637 size_t *sq_offset)
7638{
7639 struct io_rings *rings;
7640 size_t off, sq_array_size;
7641
7642 off = struct_size(rings, cqes, cq_entries);
7643 if (off == SIZE_MAX)
7644 return SIZE_MAX;
7645
7646#ifdef CONFIG_SMP
7647 off = ALIGN(off, SMP_CACHE_BYTES);
7648 if (off == 0)
7649 return SIZE_MAX;
7650#endif
7651
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007652 if (sq_offset)
7653 *sq_offset = off;
7654
Hristo Venev75b28af2019-08-26 17:23:46 +00007655 sq_array_size = array_size(sizeof(u32), sq_entries);
7656 if (sq_array_size == SIZE_MAX)
7657 return SIZE_MAX;
7658
7659 if (check_add_overflow(off, sq_array_size, &off))
7660 return SIZE_MAX;
7661
Hristo Venev75b28af2019-08-26 17:23:46 +00007662 return off;
7663}
7664
Jens Axboe2b188cc2019-01-07 10:46:33 -07007665static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7666{
Hristo Venev75b28af2019-08-26 17:23:46 +00007667 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007668
Hristo Venev75b28af2019-08-26 17:23:46 +00007669 pages = (size_t)1 << get_order(
7670 rings_size(sq_entries, cq_entries, NULL));
7671 pages += (size_t)1 << get_order(
7672 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007673
Hristo Venev75b28af2019-08-26 17:23:46 +00007674 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007675}
7676
Jens Axboeedafcce2019-01-09 09:16:05 -07007677static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7678{
7679 int i, j;
7680
7681 if (!ctx->user_bufs)
7682 return -ENXIO;
7683
7684 for (i = 0; i < ctx->nr_user_bufs; i++) {
7685 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7686
7687 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007688 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007689
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007690 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007691 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007692 imu->nr_bvecs = 0;
7693 }
7694
7695 kfree(ctx->user_bufs);
7696 ctx->user_bufs = NULL;
7697 ctx->nr_user_bufs = 0;
7698 return 0;
7699}
7700
7701static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7702 void __user *arg, unsigned index)
7703{
7704 struct iovec __user *src;
7705
7706#ifdef CONFIG_COMPAT
7707 if (ctx->compat) {
7708 struct compat_iovec __user *ciovs;
7709 struct compat_iovec ciov;
7710
7711 ciovs = (struct compat_iovec __user *) arg;
7712 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7713 return -EFAULT;
7714
Jens Axboed55e5f52019-12-11 16:12:15 -07007715 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007716 dst->iov_len = ciov.iov_len;
7717 return 0;
7718 }
7719#endif
7720 src = (struct iovec __user *) arg;
7721 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7722 return -EFAULT;
7723 return 0;
7724}
7725
7726static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7727 unsigned nr_args)
7728{
7729 struct vm_area_struct **vmas = NULL;
7730 struct page **pages = NULL;
7731 int i, j, got_pages = 0;
7732 int ret = -EINVAL;
7733
7734 if (ctx->user_bufs)
7735 return -EBUSY;
7736 if (!nr_args || nr_args > UIO_MAXIOV)
7737 return -EINVAL;
7738
7739 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7740 GFP_KERNEL);
7741 if (!ctx->user_bufs)
7742 return -ENOMEM;
7743
7744 for (i = 0; i < nr_args; i++) {
7745 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7746 unsigned long off, start, end, ubuf;
7747 int pret, nr_pages;
7748 struct iovec iov;
7749 size_t size;
7750
7751 ret = io_copy_iov(ctx, &iov, arg, i);
7752 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007753 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007754
7755 /*
7756 * Don't impose further limits on the size and buffer
7757 * constraints here, we'll -EINVAL later when IO is
7758 * submitted if they are wrong.
7759 */
7760 ret = -EFAULT;
7761 if (!iov.iov_base || !iov.iov_len)
7762 goto err;
7763
7764 /* arbitrary limit, but we need something */
7765 if (iov.iov_len > SZ_1G)
7766 goto err;
7767
7768 ubuf = (unsigned long) iov.iov_base;
7769 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7770 start = ubuf >> PAGE_SHIFT;
7771 nr_pages = end - start;
7772
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007773 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007774 if (ret)
7775 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007776
7777 ret = 0;
7778 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007779 kvfree(vmas);
7780 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007781 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007782 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007783 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007784 sizeof(struct vm_area_struct *),
7785 GFP_KERNEL);
7786 if (!pages || !vmas) {
7787 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007788 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007789 goto err;
7790 }
7791 got_pages = nr_pages;
7792 }
7793
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007794 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007795 GFP_KERNEL);
7796 ret = -ENOMEM;
7797 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007798 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007799 goto err;
7800 }
7801
7802 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007803 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007804 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007805 FOLL_WRITE | FOLL_LONGTERM,
7806 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007807 if (pret == nr_pages) {
7808 /* don't support file backed memory */
7809 for (j = 0; j < nr_pages; j++) {
7810 struct vm_area_struct *vma = vmas[j];
7811
7812 if (vma->vm_file &&
7813 !is_file_hugepages(vma->vm_file)) {
7814 ret = -EOPNOTSUPP;
7815 break;
7816 }
7817 }
7818 } else {
7819 ret = pret < 0 ? pret : -EFAULT;
7820 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007821 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007822 if (ret) {
7823 /*
7824 * if we did partial map, or found file backed vmas,
7825 * release any pages we did get
7826 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007827 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007828 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007829 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007830 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007831 goto err;
7832 }
7833
7834 off = ubuf & ~PAGE_MASK;
7835 size = iov.iov_len;
7836 for (j = 0; j < nr_pages; j++) {
7837 size_t vec_len;
7838
7839 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7840 imu->bvec[j].bv_page = pages[j];
7841 imu->bvec[j].bv_len = vec_len;
7842 imu->bvec[j].bv_offset = off;
7843 off = 0;
7844 size -= vec_len;
7845 }
7846 /* store original address for later verification */
7847 imu->ubuf = ubuf;
7848 imu->len = iov.iov_len;
7849 imu->nr_bvecs = nr_pages;
7850
7851 ctx->nr_user_bufs++;
7852 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007853 kvfree(pages);
7854 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007855 return 0;
7856err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007857 kvfree(pages);
7858 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007859 io_sqe_buffer_unregister(ctx);
7860 return ret;
7861}
7862
Jens Axboe9b402842019-04-11 11:45:41 -06007863static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7864{
7865 __s32 __user *fds = arg;
7866 int fd;
7867
7868 if (ctx->cq_ev_fd)
7869 return -EBUSY;
7870
7871 if (copy_from_user(&fd, fds, sizeof(*fds)))
7872 return -EFAULT;
7873
7874 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7875 if (IS_ERR(ctx->cq_ev_fd)) {
7876 int ret = PTR_ERR(ctx->cq_ev_fd);
7877 ctx->cq_ev_fd = NULL;
7878 return ret;
7879 }
7880
7881 return 0;
7882}
7883
7884static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7885{
7886 if (ctx->cq_ev_fd) {
7887 eventfd_ctx_put(ctx->cq_ev_fd);
7888 ctx->cq_ev_fd = NULL;
7889 return 0;
7890 }
7891
7892 return -ENXIO;
7893}
7894
Jens Axboe5a2e7452020-02-23 16:23:11 -07007895static int __io_destroy_buffers(int id, void *p, void *data)
7896{
7897 struct io_ring_ctx *ctx = data;
7898 struct io_buffer *buf = p;
7899
Jens Axboe067524e2020-03-02 16:32:28 -07007900 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007901 return 0;
7902}
7903
7904static void io_destroy_buffers(struct io_ring_ctx *ctx)
7905{
7906 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7907 idr_destroy(&ctx->io_buffer_idr);
7908}
7909
Jens Axboe2b188cc2019-01-07 10:46:33 -07007910static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7911{
Jens Axboe6b063142019-01-10 22:13:58 -07007912 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007913 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06007914
7915 if (ctx->sqo_task) {
7916 put_task_struct(ctx->sqo_task);
7917 ctx->sqo_task = NULL;
7918 mmdrop(ctx->mm_account);
7919 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007920 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007921
Jens Axboe6b063142019-01-10 22:13:58 -07007922 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007923 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007924 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007925 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007926
Jens Axboe2b188cc2019-01-07 10:46:33 -07007927#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007928 if (ctx->ring_sock) {
7929 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007930 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007931 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007932#endif
7933
Hristo Venev75b28af2019-08-26 17:23:46 +00007934 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007935 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007936
7937 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007938 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007939 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007940 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007941 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007942 kfree(ctx);
7943}
7944
7945static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7946{
7947 struct io_ring_ctx *ctx = file->private_data;
7948 __poll_t mask = 0;
7949
7950 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007951 /*
7952 * synchronizes with barrier from wq_has_sleeper call in
7953 * io_commit_cqring
7954 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007955 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007956 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7957 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007958 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007959 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007960 mask |= EPOLLIN | EPOLLRDNORM;
7961
7962 return mask;
7963}
7964
7965static int io_uring_fasync(int fd, struct file *file, int on)
7966{
7967 struct io_ring_ctx *ctx = file->private_data;
7968
7969 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7970}
7971
Jens Axboe071698e2020-01-28 10:04:42 -07007972static int io_remove_personalities(int id, void *p, void *data)
7973{
7974 struct io_ring_ctx *ctx = data;
7975 const struct cred *cred;
7976
7977 cred = idr_remove(&ctx->personality_idr, id);
7978 if (cred)
7979 put_cred(cred);
7980 return 0;
7981}
7982
Jens Axboe85faa7b2020-04-09 18:14:00 -06007983static void io_ring_exit_work(struct work_struct *work)
7984{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007985 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
7986 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007987
Jens Axboe56952e92020-06-17 15:00:04 -06007988 /*
7989 * If we're doing polled IO and end up having requests being
7990 * submitted async (out-of-line), then completions can come in while
7991 * we're waiting for refs to drop. We need to reap these manually,
7992 * as nobody else will be looking for them.
7993 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007994 do {
Jens Axboe56952e92020-06-17 15:00:04 -06007995 if (ctx->rings)
7996 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007997 io_iopoll_try_reap_events(ctx);
7998 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06007999 io_ring_ctx_free(ctx);
8000}
8001
Jens Axboe2b188cc2019-01-07 10:46:33 -07008002static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8003{
8004 mutex_lock(&ctx->uring_lock);
8005 percpu_ref_kill(&ctx->refs);
8006 mutex_unlock(&ctx->uring_lock);
8007
Jens Axboef3606e32020-09-22 08:18:24 -06008008 io_kill_timeouts(ctx, NULL);
8009 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008010
8011 if (ctx->io_wq)
8012 io_wq_cancel_all(ctx->io_wq);
8013
Jens Axboe15dff282019-11-13 09:09:23 -07008014 /* if we failed setting up the ctx, we might not have any rings */
8015 if (ctx->rings)
8016 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008017 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008018 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008019
8020 /*
8021 * Do this upfront, so we won't have a grace period where the ring
8022 * is closed but resources aren't reaped yet. This can cause
8023 * spurious failure in setting up a new ring.
8024 */
Jens Axboe760618f2020-07-24 12:53:31 -06008025 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8026 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008027
Jens Axboe85faa7b2020-04-09 18:14:00 -06008028 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008029 /*
8030 * Use system_unbound_wq to avoid spawning tons of event kworkers
8031 * if we're exiting a ton of rings at the same time. It just adds
8032 * noise and overhead, there's no discernable change in runtime
8033 * over using system_wq.
8034 */
8035 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008036}
8037
8038static int io_uring_release(struct inode *inode, struct file *file)
8039{
8040 struct io_ring_ctx *ctx = file->private_data;
8041
8042 file->private_data = NULL;
8043 io_ring_ctx_wait_and_kill(ctx);
8044 return 0;
8045}
8046
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008047static bool io_wq_files_match(struct io_wq_work *work, void *data)
8048{
8049 struct files_struct *files = data;
8050
8051 return work->files == files;
8052}
8053
Jens Axboef254ac02020-08-12 17:33:30 -06008054/*
8055 * Returns true if 'preq' is the link parent of 'req'
8056 */
8057static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8058{
8059 struct io_kiocb *link;
8060
8061 if (!(preq->flags & REQ_F_LINK_HEAD))
8062 return false;
8063
8064 list_for_each_entry(link, &preq->link_list, link_list) {
8065 if (link == req)
8066 return true;
8067 }
8068
8069 return false;
8070}
8071
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008072static inline bool io_match_files(struct io_kiocb *req,
8073 struct files_struct *files)
8074{
8075 return (req->flags & REQ_F_WORK_INITIALIZED) && req->work.files == files;
8076}
8077
8078static bool io_match_link_files(struct io_kiocb *req,
8079 struct files_struct *files)
8080{
8081 struct io_kiocb *link;
8082
8083 if (io_match_files(req, files))
8084 return true;
8085 if (req->flags & REQ_F_LINK_HEAD) {
8086 list_for_each_entry(link, &req->link_list, link_list) {
8087 if (io_match_files(link, files))
8088 return true;
8089 }
8090 }
8091 return false;
8092}
8093
Jens Axboef254ac02020-08-12 17:33:30 -06008094/*
8095 * We're looking to cancel 'req' because it's holding on to our files, but
8096 * 'req' could be a link to another request. See if it is, and cancel that
8097 * parent request if so.
8098 */
8099static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8100{
8101 struct hlist_node *tmp;
8102 struct io_kiocb *preq;
8103 bool found = false;
8104 int i;
8105
8106 spin_lock_irq(&ctx->completion_lock);
8107 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8108 struct hlist_head *list;
8109
8110 list = &ctx->cancel_hash[i];
8111 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8112 found = io_match_link(preq, req);
8113 if (found) {
8114 io_poll_remove_one(preq);
8115 break;
8116 }
8117 }
8118 }
8119 spin_unlock_irq(&ctx->completion_lock);
8120 return found;
8121}
8122
8123static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8124 struct io_kiocb *req)
8125{
8126 struct io_kiocb *preq;
8127 bool found = false;
8128
8129 spin_lock_irq(&ctx->completion_lock);
8130 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8131 found = io_match_link(preq, req);
8132 if (found) {
8133 __io_timeout_cancel(preq);
8134 break;
8135 }
8136 }
8137 spin_unlock_irq(&ctx->completion_lock);
8138 return found;
8139}
8140
Jens Axboeb711d4e2020-08-16 08:23:05 -07008141static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8142{
8143 return io_match_link(container_of(work, struct io_kiocb, work), data);
8144}
8145
8146static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8147{
8148 enum io_wq_cancel cret;
8149
8150 /* cancel this particular work, if it's running */
8151 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8152 if (cret != IO_WQ_CANCEL_NOTFOUND)
8153 return;
8154
8155 /* find links that hold this pending, cancel those */
8156 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8157 if (cret != IO_WQ_CANCEL_NOTFOUND)
8158 return;
8159
8160 /* if we have a poll link holding this pending, cancel that */
8161 if (io_poll_remove_link(ctx, req))
8162 return;
8163
8164 /* final option, timeout link is holding this req pending */
8165 io_timeout_remove_link(ctx, req);
8166}
8167
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008168static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8169 struct files_struct *files)
8170{
8171 struct io_defer_entry *de = NULL;
8172 LIST_HEAD(list);
8173
8174 spin_lock_irq(&ctx->completion_lock);
8175 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008176 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008177 list_cut_position(&list, &ctx->defer_list, &de->list);
8178 break;
8179 }
8180 }
8181 spin_unlock_irq(&ctx->completion_lock);
8182
8183 while (!list_empty(&list)) {
8184 de = list_first_entry(&list, struct io_defer_entry, list);
8185 list_del_init(&de->list);
8186 req_set_fail_links(de->req);
8187 io_put_req(de->req);
8188 io_req_complete(de->req, -ECANCELED);
8189 kfree(de);
8190 }
8191}
8192
Jens Axboe76e1b642020-09-26 15:05:03 -06008193/*
8194 * Returns true if we found and killed one or more files pinning requests
8195 */
8196static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008197 struct files_struct *files)
8198{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008199 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008200 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008201
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008202 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008203 /* cancel all at once, should be faster than doing it one by one*/
8204 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8205
Jens Axboefcb323c2019-10-24 12:39:47 -06008206 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008207 struct io_kiocb *cancel_req = NULL, *req;
8208 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008209
8210 spin_lock_irq(&ctx->inflight_lock);
8211 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07008212 if (req->work.files != files)
8213 continue;
8214 /* req is being completed, ignore */
8215 if (!refcount_inc_not_zero(&req->refs))
8216 continue;
8217 cancel_req = req;
8218 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008219 }
Jens Axboe768134d2019-11-10 20:30:53 -07008220 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008221 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008222 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008223 spin_unlock_irq(&ctx->inflight_lock);
8224
Jens Axboe768134d2019-11-10 20:30:53 -07008225 /* We need to keep going until we don't find a matching req */
8226 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008227 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008228 /* cancel this request, or head link requests */
8229 io_attempt_cancel(ctx, cancel_req);
8230 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008231 /* cancellations _may_ trigger task work */
8232 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008233 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008234 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008235 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008236
8237 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008238}
8239
Pavel Begunkov801dd572020-06-15 10:33:14 +03008240static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008241{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008242 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8243 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008244
Jens Axboef3606e32020-09-22 08:18:24 -06008245 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008246}
8247
Jens Axboefcb323c2019-10-24 12:39:47 -06008248static int io_uring_flush(struct file *file, void *data)
8249{
8250 struct io_ring_ctx *ctx = file->private_data;
8251
8252 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07008253
8254 /*
8255 * If the task is going away, cancel work it may have pending
8256 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008257 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
8258 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07008259
Jens Axboefcb323c2019-10-24 12:39:47 -06008260 return 0;
8261}
8262
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008263static void *io_uring_validate_mmap_request(struct file *file,
8264 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008265{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008266 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008267 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008268 struct page *page;
8269 void *ptr;
8270
8271 switch (offset) {
8272 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008273 case IORING_OFF_CQ_RING:
8274 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008275 break;
8276 case IORING_OFF_SQES:
8277 ptr = ctx->sq_sqes;
8278 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008279 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008280 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008281 }
8282
8283 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008284 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008285 return ERR_PTR(-EINVAL);
8286
8287 return ptr;
8288}
8289
8290#ifdef CONFIG_MMU
8291
8292static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8293{
8294 size_t sz = vma->vm_end - vma->vm_start;
8295 unsigned long pfn;
8296 void *ptr;
8297
8298 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8299 if (IS_ERR(ptr))
8300 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008301
8302 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8303 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8304}
8305
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008306#else /* !CONFIG_MMU */
8307
8308static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8309{
8310 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8311}
8312
8313static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8314{
8315 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8316}
8317
8318static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8319 unsigned long addr, unsigned long len,
8320 unsigned long pgoff, unsigned long flags)
8321{
8322 void *ptr;
8323
8324 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8325 if (IS_ERR(ptr))
8326 return PTR_ERR(ptr);
8327
8328 return (unsigned long) ptr;
8329}
8330
8331#endif /* !CONFIG_MMU */
8332
Jens Axboe2b188cc2019-01-07 10:46:33 -07008333SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8334 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8335 size_t, sigsz)
8336{
8337 struct io_ring_ctx *ctx;
8338 long ret = -EBADF;
8339 int submitted = 0;
8340 struct fd f;
8341
Jens Axboe4c6e2772020-07-01 11:29:10 -06008342 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008343
Jens Axboe6c271ce2019-01-10 11:22:30 -07008344 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008345 return -EINVAL;
8346
8347 f = fdget(fd);
8348 if (!f.file)
8349 return -EBADF;
8350
8351 ret = -EOPNOTSUPP;
8352 if (f.file->f_op != &io_uring_fops)
8353 goto out_fput;
8354
8355 ret = -ENXIO;
8356 ctx = f.file->private_data;
8357 if (!percpu_ref_tryget(&ctx->refs))
8358 goto out_fput;
8359
Jens Axboe6c271ce2019-01-10 11:22:30 -07008360 /*
8361 * For SQ polling, the thread will do all submissions and completions.
8362 * Just return the requested submit count, and wake the thread if
8363 * we were asked to.
8364 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008365 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008366 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008367 if (!list_empty_careful(&ctx->cq_overflow_list))
8368 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008369 if (flags & IORING_ENTER_SQ_WAKEUP)
8370 wake_up(&ctx->sqo_wait);
8371 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008372 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07008373 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03008374 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008375 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008376
8377 if (submitted != to_submit)
8378 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008379 }
8380 if (flags & IORING_ENTER_GETEVENTS) {
8381 min_complete = min(min_complete, ctx->cq_entries);
8382
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008383 /*
8384 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8385 * space applications don't need to do io completion events
8386 * polling again, they can rely on io_sq_thread to do polling
8387 * work, which can reduce cpu usage and uring_lock contention.
8388 */
8389 if (ctx->flags & IORING_SETUP_IOPOLL &&
8390 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008391 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008392 } else {
8393 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8394 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008395 }
8396
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008397out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008398 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008399out_fput:
8400 fdput(f);
8401 return submitted ? submitted : ret;
8402}
8403
Tobias Klauserbebdb652020-02-26 18:38:32 +01008404#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008405static int io_uring_show_cred(int id, void *p, void *data)
8406{
8407 const struct cred *cred = p;
8408 struct seq_file *m = data;
8409 struct user_namespace *uns = seq_user_ns(m);
8410 struct group_info *gi;
8411 kernel_cap_t cap;
8412 unsigned __capi;
8413 int g;
8414
8415 seq_printf(m, "%5d\n", id);
8416 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8417 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8418 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8419 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8420 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8421 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8422 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8423 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8424 seq_puts(m, "\n\tGroups:\t");
8425 gi = cred->group_info;
8426 for (g = 0; g < gi->ngroups; g++) {
8427 seq_put_decimal_ull(m, g ? " " : "",
8428 from_kgid_munged(uns, gi->gid[g]));
8429 }
8430 seq_puts(m, "\n\tCapEff:\t");
8431 cap = cred->cap_effective;
8432 CAP_FOR_EACH_U32(__capi)
8433 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8434 seq_putc(m, '\n');
8435 return 0;
8436}
8437
8438static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8439{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008440 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008441 int i;
8442
Jens Axboefad8e0d2020-09-28 08:57:48 -06008443 /*
8444 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8445 * since fdinfo case grabs it in the opposite direction of normal use
8446 * cases. If we fail to get the lock, we just don't iterate any
8447 * structures that could be going away outside the io_uring mutex.
8448 */
8449 has_lock = mutex_trylock(&ctx->uring_lock);
8450
Jens Axboe87ce9552020-01-30 08:25:34 -07008451 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008452 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008453 struct fixed_file_table *table;
8454 struct file *f;
8455
8456 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8457 f = table->files[i & IORING_FILE_TABLE_MASK];
8458 if (f)
8459 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8460 else
8461 seq_printf(m, "%5u: <none>\n", i);
8462 }
8463 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008464 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008465 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8466
8467 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8468 (unsigned int) buf->len);
8469 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008470 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008471 seq_printf(m, "Personalities:\n");
8472 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8473 }
Jens Axboed7718a92020-02-14 22:23:12 -07008474 seq_printf(m, "PollList:\n");
8475 spin_lock_irq(&ctx->completion_lock);
8476 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8477 struct hlist_head *list = &ctx->cancel_hash[i];
8478 struct io_kiocb *req;
8479
8480 hlist_for_each_entry(req, list, hash_node)
8481 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8482 req->task->task_works != NULL);
8483 }
8484 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008485 if (has_lock)
8486 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008487}
8488
8489static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8490{
8491 struct io_ring_ctx *ctx = f->private_data;
8492
8493 if (percpu_ref_tryget(&ctx->refs)) {
8494 __io_uring_show_fdinfo(ctx, m);
8495 percpu_ref_put(&ctx->refs);
8496 }
8497}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008498#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008499
Jens Axboe2b188cc2019-01-07 10:46:33 -07008500static const struct file_operations io_uring_fops = {
8501 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008502 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008503 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008504#ifndef CONFIG_MMU
8505 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8506 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8507#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008508 .poll = io_uring_poll,
8509 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008510#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008511 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008512#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008513};
8514
8515static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8516 struct io_uring_params *p)
8517{
Hristo Venev75b28af2019-08-26 17:23:46 +00008518 struct io_rings *rings;
8519 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008520
Jens Axboebd740482020-08-05 12:58:23 -06008521 /* make sure these are sane, as we already accounted them */
8522 ctx->sq_entries = p->sq_entries;
8523 ctx->cq_entries = p->cq_entries;
8524
Hristo Venev75b28af2019-08-26 17:23:46 +00008525 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8526 if (size == SIZE_MAX)
8527 return -EOVERFLOW;
8528
8529 rings = io_mem_alloc(size);
8530 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008531 return -ENOMEM;
8532
Hristo Venev75b28af2019-08-26 17:23:46 +00008533 ctx->rings = rings;
8534 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8535 rings->sq_ring_mask = p->sq_entries - 1;
8536 rings->cq_ring_mask = p->cq_entries - 1;
8537 rings->sq_ring_entries = p->sq_entries;
8538 rings->cq_ring_entries = p->cq_entries;
8539 ctx->sq_mask = rings->sq_ring_mask;
8540 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008541
8542 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008543 if (size == SIZE_MAX) {
8544 io_mem_free(ctx->rings);
8545 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008546 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008547 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008548
8549 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008550 if (!ctx->sq_sqes) {
8551 io_mem_free(ctx->rings);
8552 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008553 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008554 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008555
Jens Axboe2b188cc2019-01-07 10:46:33 -07008556 return 0;
8557}
8558
8559/*
8560 * Allocate an anonymous fd, this is what constitutes the application
8561 * visible backing of an io_uring instance. The application mmaps this
8562 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8563 * we have to tie this fd to a socket for file garbage collection purposes.
8564 */
8565static int io_uring_get_fd(struct io_ring_ctx *ctx)
8566{
8567 struct file *file;
8568 int ret;
8569
8570#if defined(CONFIG_UNIX)
8571 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8572 &ctx->ring_sock);
8573 if (ret)
8574 return ret;
8575#endif
8576
8577 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8578 if (ret < 0)
8579 goto err;
8580
8581 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8582 O_RDWR | O_CLOEXEC);
8583 if (IS_ERR(file)) {
8584 put_unused_fd(ret);
8585 ret = PTR_ERR(file);
8586 goto err;
8587 }
8588
8589#if defined(CONFIG_UNIX)
8590 ctx->ring_sock->file = file;
8591#endif
8592 fd_install(ret, file);
8593 return ret;
8594err:
8595#if defined(CONFIG_UNIX)
8596 sock_release(ctx->ring_sock);
8597 ctx->ring_sock = NULL;
8598#endif
8599 return ret;
8600}
8601
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008602static int io_uring_create(unsigned entries, struct io_uring_params *p,
8603 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008604{
8605 struct user_struct *user = NULL;
8606 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008607 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008608 int ret;
8609
Jens Axboe8110c1a2019-12-28 15:39:54 -07008610 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008611 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008612 if (entries > IORING_MAX_ENTRIES) {
8613 if (!(p->flags & IORING_SETUP_CLAMP))
8614 return -EINVAL;
8615 entries = IORING_MAX_ENTRIES;
8616 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008617
8618 /*
8619 * Use twice as many entries for the CQ ring. It's possible for the
8620 * application to drive a higher depth than the size of the SQ ring,
8621 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008622 * some flexibility in overcommitting a bit. If the application has
8623 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8624 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008625 */
8626 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008627 if (p->flags & IORING_SETUP_CQSIZE) {
8628 /*
8629 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8630 * to a power-of-two, if it isn't already. We do NOT impose
8631 * any cq vs sq ring sizing.
8632 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008633 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008634 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008635 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8636 if (!(p->flags & IORING_SETUP_CLAMP))
8637 return -EINVAL;
8638 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8639 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008640 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8641 } else {
8642 p->cq_entries = 2 * p->sq_entries;
8643 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008644
8645 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008646 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008647
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008648 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008649 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008650 ring_pages(p->sq_entries, p->cq_entries));
8651 if (ret) {
8652 free_uid(user);
8653 return ret;
8654 }
8655 }
8656
8657 ctx = io_ring_ctx_alloc(p);
8658 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008659 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008660 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008661 p->cq_entries));
8662 free_uid(user);
8663 return -ENOMEM;
8664 }
8665 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008666 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008667 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008668
Jens Axboe2aede0e2020-09-14 10:45:53 -06008669 ctx->sqo_task = get_task_struct(current);
8670
8671 /*
8672 * This is just grabbed for accounting purposes. When a process exits,
8673 * the mm is exited and dropped before the files, hence we need to hang
8674 * on to this mm purely for the purposes of being able to unaccount
8675 * memory (locked/pinned vm). It's not used for anything else.
8676 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06008677 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008678 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06008679
Jens Axboef74441e2020-08-05 13:00:44 -06008680 /*
8681 * Account memory _before_ installing the file descriptor. Once
8682 * the descriptor is installed, it can get closed at any time. Also
8683 * do this before hitting the general error path, as ring freeing
8684 * will un-account as well.
8685 */
8686 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8687 ACCT_LOCKED);
8688 ctx->limit_mem = limit_mem;
8689
Jens Axboe2b188cc2019-01-07 10:46:33 -07008690 ret = io_allocate_scq_urings(ctx, p);
8691 if (ret)
8692 goto err;
8693
Jens Axboe6c271ce2019-01-10 11:22:30 -07008694 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008695 if (ret)
8696 goto err;
8697
Jens Axboe2b188cc2019-01-07 10:46:33 -07008698 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008699 p->sq_off.head = offsetof(struct io_rings, sq.head);
8700 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8701 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8702 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8703 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8704 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8705 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008706
8707 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008708 p->cq_off.head = offsetof(struct io_rings, cq.head);
8709 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8710 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8711 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8712 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8713 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008714 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008715
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008716 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8717 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008718 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8719 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008720
8721 if (copy_to_user(params, p, sizeof(*p))) {
8722 ret = -EFAULT;
8723 goto err;
8724 }
Jens Axboed1719f72020-07-30 13:43:53 -06008725
8726 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06008727 * Install ring fd as the very last thing, so we don't risk someone
8728 * having closed it before we finish setup
8729 */
8730 ret = io_uring_get_fd(ctx);
8731 if (ret < 0)
8732 goto err;
8733
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008734 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008735 return ret;
8736err:
8737 io_ring_ctx_wait_and_kill(ctx);
8738 return ret;
8739}
8740
8741/*
8742 * Sets up an aio uring context, and returns the fd. Applications asks for a
8743 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8744 * params structure passed in.
8745 */
8746static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8747{
8748 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008749 int i;
8750
8751 if (copy_from_user(&p, params, sizeof(p)))
8752 return -EFAULT;
8753 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8754 if (p.resv[i])
8755 return -EINVAL;
8756 }
8757
Jens Axboe6c271ce2019-01-10 11:22:30 -07008758 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008759 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008760 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008761 return -EINVAL;
8762
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008763 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008764}
8765
8766SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8767 struct io_uring_params __user *, params)
8768{
8769 return io_uring_setup(entries, params);
8770}
8771
Jens Axboe66f4af92020-01-16 15:36:52 -07008772static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8773{
8774 struct io_uring_probe *p;
8775 size_t size;
8776 int i, ret;
8777
8778 size = struct_size(p, ops, nr_args);
8779 if (size == SIZE_MAX)
8780 return -EOVERFLOW;
8781 p = kzalloc(size, GFP_KERNEL);
8782 if (!p)
8783 return -ENOMEM;
8784
8785 ret = -EFAULT;
8786 if (copy_from_user(p, arg, size))
8787 goto out;
8788 ret = -EINVAL;
8789 if (memchr_inv(p, 0, size))
8790 goto out;
8791
8792 p->last_op = IORING_OP_LAST - 1;
8793 if (nr_args > IORING_OP_LAST)
8794 nr_args = IORING_OP_LAST;
8795
8796 for (i = 0; i < nr_args; i++) {
8797 p->ops[i].op = i;
8798 if (!io_op_defs[i].not_supported)
8799 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8800 }
8801 p->ops_len = i;
8802
8803 ret = 0;
8804 if (copy_to_user(arg, p, size))
8805 ret = -EFAULT;
8806out:
8807 kfree(p);
8808 return ret;
8809}
8810
Jens Axboe071698e2020-01-28 10:04:42 -07008811static int io_register_personality(struct io_ring_ctx *ctx)
8812{
8813 const struct cred *creds = get_current_cred();
8814 int id;
8815
8816 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8817 USHRT_MAX, GFP_KERNEL);
8818 if (id < 0)
8819 put_cred(creds);
8820 return id;
8821}
8822
8823static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8824{
8825 const struct cred *old_creds;
8826
8827 old_creds = idr_remove(&ctx->personality_idr, id);
8828 if (old_creds) {
8829 put_cred(old_creds);
8830 return 0;
8831 }
8832
8833 return -EINVAL;
8834}
8835
8836static bool io_register_op_must_quiesce(int op)
8837{
8838 switch (op) {
8839 case IORING_UNREGISTER_FILES:
8840 case IORING_REGISTER_FILES_UPDATE:
8841 case IORING_REGISTER_PROBE:
8842 case IORING_REGISTER_PERSONALITY:
8843 case IORING_UNREGISTER_PERSONALITY:
8844 return false;
8845 default:
8846 return true;
8847 }
8848}
8849
Jens Axboeedafcce2019-01-09 09:16:05 -07008850static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8851 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008852 __releases(ctx->uring_lock)
8853 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008854{
8855 int ret;
8856
Jens Axboe35fa71a2019-04-22 10:23:23 -06008857 /*
8858 * We're inside the ring mutex, if the ref is already dying, then
8859 * someone else killed the ctx or is already going through
8860 * io_uring_register().
8861 */
8862 if (percpu_ref_is_dying(&ctx->refs))
8863 return -ENXIO;
8864
Jens Axboe071698e2020-01-28 10:04:42 -07008865 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008866 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008867
Jens Axboe05f3fb32019-12-09 11:22:50 -07008868 /*
8869 * Drop uring mutex before waiting for references to exit. If
8870 * another thread is currently inside io_uring_enter() it might
8871 * need to grab the uring_lock to make progress. If we hold it
8872 * here across the drain wait, then we can deadlock. It's safe
8873 * to drop the mutex here, since no new references will come in
8874 * after we've killed the percpu ref.
8875 */
8876 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008877 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008878 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008879 if (ret) {
8880 percpu_ref_resurrect(&ctx->refs);
8881 ret = -EINTR;
8882 goto out;
8883 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008884 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008885
8886 switch (opcode) {
8887 case IORING_REGISTER_BUFFERS:
8888 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8889 break;
8890 case IORING_UNREGISTER_BUFFERS:
8891 ret = -EINVAL;
8892 if (arg || nr_args)
8893 break;
8894 ret = io_sqe_buffer_unregister(ctx);
8895 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008896 case IORING_REGISTER_FILES:
8897 ret = io_sqe_files_register(ctx, arg, nr_args);
8898 break;
8899 case IORING_UNREGISTER_FILES:
8900 ret = -EINVAL;
8901 if (arg || nr_args)
8902 break;
8903 ret = io_sqe_files_unregister(ctx);
8904 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008905 case IORING_REGISTER_FILES_UPDATE:
8906 ret = io_sqe_files_update(ctx, arg, nr_args);
8907 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008908 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008909 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008910 ret = -EINVAL;
8911 if (nr_args != 1)
8912 break;
8913 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008914 if (ret)
8915 break;
8916 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8917 ctx->eventfd_async = 1;
8918 else
8919 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008920 break;
8921 case IORING_UNREGISTER_EVENTFD:
8922 ret = -EINVAL;
8923 if (arg || nr_args)
8924 break;
8925 ret = io_eventfd_unregister(ctx);
8926 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008927 case IORING_REGISTER_PROBE:
8928 ret = -EINVAL;
8929 if (!arg || nr_args > 256)
8930 break;
8931 ret = io_probe(ctx, arg, nr_args);
8932 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008933 case IORING_REGISTER_PERSONALITY:
8934 ret = -EINVAL;
8935 if (arg || nr_args)
8936 break;
8937 ret = io_register_personality(ctx);
8938 break;
8939 case IORING_UNREGISTER_PERSONALITY:
8940 ret = -EINVAL;
8941 if (arg)
8942 break;
8943 ret = io_unregister_personality(ctx, nr_args);
8944 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008945 default:
8946 ret = -EINVAL;
8947 break;
8948 }
8949
Jens Axboe071698e2020-01-28 10:04:42 -07008950 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008951 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008952 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008953out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008954 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008955 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008956 return ret;
8957}
8958
8959SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8960 void __user *, arg, unsigned int, nr_args)
8961{
8962 struct io_ring_ctx *ctx;
8963 long ret = -EBADF;
8964 struct fd f;
8965
8966 f = fdget(fd);
8967 if (!f.file)
8968 return -EBADF;
8969
8970 ret = -EOPNOTSUPP;
8971 if (f.file->f_op != &io_uring_fops)
8972 goto out_fput;
8973
8974 ctx = f.file->private_data;
8975
8976 mutex_lock(&ctx->uring_lock);
8977 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8978 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008979 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8980 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008981out_fput:
8982 fdput(f);
8983 return ret;
8984}
8985
Jens Axboe2b188cc2019-01-07 10:46:33 -07008986static int __init io_uring_init(void)
8987{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008988#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8989 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8990 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8991} while (0)
8992
8993#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8994 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8995 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8996 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8997 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8998 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8999 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9000 BUILD_BUG_SQE_ELEM(8, __u64, off);
9001 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9002 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009003 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009004 BUILD_BUG_SQE_ELEM(24, __u32, len);
9005 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9006 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9007 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9008 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009009 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9010 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009011 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9012 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9013 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9014 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9015 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9016 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9017 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9018 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009019 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009020 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9021 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9022 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009023 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009024
Jens Axboed3656342019-12-18 09:50:26 -07009025 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009026 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009027 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9028 return 0;
9029};
9030__initcall(io_uring_init);