blob: 73c5dbb1591d3fcde734d6feaa2144cc5794284f [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200145 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application.
149 *
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
152 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000153 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200155 * Runtime CQ flags
156 *
157 * Written by the application, shouldn't be modified by the
158 * kernel.
159 */
160 u32 cq_flags;
161 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 * Number of completion events lost because the queue was full;
163 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800164 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * the completion queue.
166 *
167 * Written by the kernel, shouldn't be modified by the
168 * application (i.e. get number of "new events" by comparing to
169 * cached value).
170 *
171 * As completion events come in out of order this counter is not
172 * ordered with any other data.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200175 /*
176 * Ring buffer of completion events.
177 *
178 * The kernel writes completion events fresh every time they are
179 * produced, so the application is allowed to modify pending
180 * entries.
181 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000182 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700183};
184
Jens Axboeedafcce2019-01-09 09:16:05 -0700185struct io_mapped_ubuf {
186 u64 ubuf;
187 size_t len;
188 struct bio_vec *bvec;
189 unsigned int nr_bvecs;
190};
191
Jens Axboe65e19f52019-10-26 07:20:21 -0600192struct fixed_file_table {
193 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700194};
195
Xiaoguang Wang05589552020-03-31 14:05:18 +0800196struct fixed_file_ref_node {
197 struct percpu_ref refs;
198 struct list_head node;
199 struct list_head file_list;
200 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600201 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202};
203
Jens Axboe05f3fb32019-12-09 11:22:50 -0700204struct fixed_file_data {
205 struct fixed_file_table *table;
206 struct io_ring_ctx *ctx;
207
Xiaoguang Wang05589552020-03-31 14:05:18 +0800208 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700210 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct list_head ref_list;
212 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213};
214
Jens Axboe5a2e7452020-02-23 16:23:11 -0700215struct io_buffer {
216 struct list_head list;
217 __u64 addr;
218 __s32 len;
219 __u16 bid;
220};
221
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222struct io_ring_ctx {
223 struct {
224 struct percpu_ref refs;
225 } ____cacheline_aligned_in_smp;
226
227 struct {
228 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800229 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700230 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800231 unsigned int cq_overflow_flushed: 1;
232 unsigned int drain_next: 1;
233 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Hristo Venev75b28af2019-08-26 17:23:46 +0000235 /*
236 * Ring buffer of indices into array of io_uring_sqe, which is
237 * mmapped by the application using the IORING_OFF_SQES offset.
238 *
239 * This indirection could e.g. be used to assign fixed
240 * io_uring_sqe entries to operations and only submit them to
241 * the queue when needed.
242 *
243 * The kernel modifies neither the indices array nor the entries
244 * array.
245 */
246 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700247 unsigned cached_sq_head;
248 unsigned sq_entries;
249 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700250 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600251 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700252 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700253 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600254
255 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600256 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700257 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258
Jens Axboefcb323c2019-10-24 12:39:47 -0600259 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700260 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 } ____cacheline_aligned_in_smp;
262
Hristo Venev75b28af2019-08-26 17:23:46 +0000263 struct io_rings *rings;
264
Jens Axboe2b188cc2019-01-07 10:46:33 -0700265 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600266 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700267 struct task_struct *sqo_thread; /* if using sq thread polling */
268 struct mm_struct *sqo_mm;
269 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270
Jens Axboe6b063142019-01-10 22:13:58 -0700271 /*
272 * If used, fixed file set. Writers must ensure that ->refs is dead,
273 * readers must ensure that ->refs is alive as long as the file* is
274 * used. Only updated through io_uring_register(2).
275 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700276 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700277 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300278 int ring_fd;
279 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700280
Jens Axboeedafcce2019-01-09 09:16:05 -0700281 /* if used, fixed mapped user buffers */
282 unsigned nr_user_bufs;
283 struct io_mapped_ubuf *user_bufs;
284
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285 struct user_struct *user;
286
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700287 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700288
Jens Axboe0f158b42020-05-14 17:18:39 -0600289 struct completion ref_comp;
290 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700291
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700292 /* if all else fails... */
293 struct io_kiocb *fallback_req;
294
Jens Axboe206aefd2019-11-07 18:27:42 -0700295#if defined(CONFIG_UNIX)
296 struct socket *ring_sock;
297#endif
298
Jens Axboe5a2e7452020-02-23 16:23:11 -0700299 struct idr io_buffer_idr;
300
Jens Axboe071698e2020-01-28 10:04:42 -0700301 struct idr personality_idr;
302
Jens Axboe206aefd2019-11-07 18:27:42 -0700303 struct {
304 unsigned cached_cq_tail;
305 unsigned cq_entries;
306 unsigned cq_mask;
307 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700308 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700309 struct wait_queue_head cq_wait;
310 struct fasync_struct *cq_fasync;
311 struct eventfd_ctx *cq_ev_fd;
312 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
314 struct {
315 struct mutex uring_lock;
316 wait_queue_head_t wait;
317 } ____cacheline_aligned_in_smp;
318
319 struct {
320 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700321
Jens Axboedef596e2019-01-09 08:59:42 -0700322 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300323 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700324 * io_uring instances that don't use IORING_SETUP_SQPOLL.
325 * For SQPOLL, only the single threaded io_sq_thread() will
326 * manipulate the list, hence no extra locking is needed there.
327 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300328 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700329 struct hlist_head *cancel_hash;
330 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700331 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600332
333 spinlock_t inflight_lock;
334 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600336
Jens Axboe4a38aed22020-05-14 17:21:15 -0600337 struct delayed_work file_put_work;
338 struct llist_head file_put_llist;
339
Jens Axboe85faa7b2020-04-09 18:14:00 -0600340 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341};
342
Jens Axboe09bb8392019-03-13 12:39:28 -0600343/*
344 * First field must be the file pointer in all the
345 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
346 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700347struct io_poll_iocb {
348 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700349 union {
350 struct wait_queue_head *head;
351 u64 addr;
352 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700353 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600354 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700355 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700356 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700357};
358
Jens Axboeb5dba592019-12-11 14:02:38 -0700359struct io_close {
360 struct file *file;
361 struct file *put_file;
362 int fd;
363};
364
Jens Axboead8a48a2019-11-15 08:49:11 -0700365struct io_timeout_data {
366 struct io_kiocb *req;
367 struct hrtimer timer;
368 struct timespec64 ts;
369 enum hrtimer_mode mode;
370};
371
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700372struct io_accept {
373 struct file *file;
374 struct sockaddr __user *addr;
375 int __user *addr_len;
376 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600377 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700378};
379
380struct io_sync {
381 struct file *file;
382 loff_t len;
383 loff_t off;
384 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700385 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700386};
387
Jens Axboefbf23842019-12-17 18:45:56 -0700388struct io_cancel {
389 struct file *file;
390 u64 addr;
391};
392
Jens Axboeb29472e2019-12-17 18:50:29 -0700393struct io_timeout {
394 struct file *file;
395 u64 addr;
396 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300397 u32 off;
398 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300399 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700400};
401
Jens Axboe9adbd452019-12-20 08:45:55 -0700402struct io_rw {
403 /* NOTE: kiocb has the file as the first member, so don't do it here */
404 struct kiocb kiocb;
405 u64 addr;
406 u64 len;
407};
408
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700409struct io_connect {
410 struct file *file;
411 struct sockaddr __user *addr;
412 int addr_len;
413};
414
Jens Axboee47293f2019-12-20 08:58:21 -0700415struct io_sr_msg {
416 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700417 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300418 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700419 void __user *buf;
420 };
Jens Axboee47293f2019-12-20 08:58:21 -0700421 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700422 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700423 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700424 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700425};
426
Jens Axboe15b71ab2019-12-11 11:20:36 -0700427struct io_open {
428 struct file *file;
429 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700430 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700431 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600432 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700433};
434
Jens Axboe05f3fb32019-12-09 11:22:50 -0700435struct io_files_update {
436 struct file *file;
437 u64 arg;
438 u32 nr_args;
439 u32 offset;
440};
441
Jens Axboe4840e412019-12-25 22:03:45 -0700442struct io_fadvise {
443 struct file *file;
444 u64 offset;
445 u32 len;
446 u32 advice;
447};
448
Jens Axboec1ca7572019-12-25 22:18:28 -0700449struct io_madvise {
450 struct file *file;
451 u64 addr;
452 u32 len;
453 u32 advice;
454};
455
Jens Axboe3e4827b2020-01-08 15:18:09 -0700456struct io_epoll {
457 struct file *file;
458 int epfd;
459 int op;
460 int fd;
461 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700462};
463
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300464struct io_splice {
465 struct file *file_out;
466 struct file *file_in;
467 loff_t off_out;
468 loff_t off_in;
469 u64 len;
470 unsigned int flags;
471};
472
Jens Axboeddf0322d2020-02-23 16:41:33 -0700473struct io_provide_buf {
474 struct file *file;
475 __u64 addr;
476 __s32 len;
477 __u32 bgid;
478 __u16 nbufs;
479 __u16 bid;
480};
481
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700482struct io_statx {
483 struct file *file;
484 int dfd;
485 unsigned int mask;
486 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700487 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700488 struct statx __user *buffer;
489};
490
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300491struct io_completion {
492 struct file *file;
493 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300494 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300495};
496
Jens Axboef499a022019-12-02 16:28:46 -0700497struct io_async_connect {
498 struct sockaddr_storage address;
499};
500
Jens Axboe03b12302019-12-02 18:50:25 -0700501struct io_async_msghdr {
502 struct iovec fast_iov[UIO_FASTIOV];
503 struct iovec *iov;
504 struct sockaddr __user *uaddr;
505 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700506 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700507};
508
Jens Axboef67676d2019-12-02 11:03:47 -0700509struct io_async_rw {
510 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600511 const struct iovec *free_iovec;
512 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600513 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600514 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700515};
516
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700517struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700518 union {
519 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700520 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700521 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700522 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700523 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700524};
525
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300526enum {
527 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
528 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
529 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
530 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
531 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700532 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300533
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300534 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300535 REQ_F_FAIL_LINK_BIT,
536 REQ_F_INFLIGHT_BIT,
537 REQ_F_CUR_POS_BIT,
538 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300539 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300540 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300541 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300542 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700543 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700544 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600545 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800546 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300547 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700548
549 /* not a real bit, just to check we're not overflowing the space */
550 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300551};
552
553enum {
554 /* ctx owns file */
555 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
556 /* drain existing IO first */
557 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
558 /* linked sqes */
559 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
560 /* doesn't sever on completion < 0 */
561 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
562 /* IOSQE_ASYNC */
563 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700564 /* IOSQE_BUFFER_SELECT */
565 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300566
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300567 /* head of a link */
568 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300569 /* fail rest of links */
570 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
571 /* on inflight list */
572 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
573 /* read/write uses file position */
574 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
575 /* must not punt to workers */
576 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577 /* has linked timeout */
578 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300579 /* regular file */
580 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300581 /* completion under lock */
582 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300583 /* needs cleanup */
584 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700585 /* already went through poll handler */
586 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700587 /* buffer already selected */
588 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600589 /* doesn't need file table for this request */
590 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800591 /* io_wq_work is initialized */
592 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300593 /* req->task is refcounted */
594 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700595};
596
597struct async_poll {
598 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600599 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300600};
601
Jens Axboe09bb8392019-03-13 12:39:28 -0600602/*
603 * NOTE! Each of the iocb union members has the file pointer
604 * as the first entry in their struct definition. So you can
605 * access the file pointer through any of the sub-structs,
606 * or directly as just 'ki_filp' in this struct.
607 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700609 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600610 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700611 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700612 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700613 struct io_accept accept;
614 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700615 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700616 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700617 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700618 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700619 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700620 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700621 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700622 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700623 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700624 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300625 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700626 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700627 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300628 /* use only after cleaning per-op data, see io_clean_op() */
629 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700630 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700632 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700633 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800634 /* polled IO has completed */
635 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700637 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300638 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700639
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300640 struct io_ring_ctx *ctx;
641 unsigned int flags;
642 refcount_t refs;
643 struct task_struct *task;
644 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300646 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700647
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300648 /*
649 * 1. used with ctx->iopoll_list with reads/writes
650 * 2. to track reqs with ->files (see io_op_def::file_table)
651 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300652 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600653
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300654 struct percpu_ref *fixed_file_refs;
655 struct callback_head task_work;
656 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
657 struct hlist_node hash_node;
658 struct async_poll *apoll;
659 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700660};
661
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300662struct io_defer_entry {
663 struct list_head list;
664 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300665 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300666};
667
Jens Axboedef596e2019-01-09 08:59:42 -0700668#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700669
Jens Axboe013538b2020-06-22 09:29:15 -0600670struct io_comp_state {
671 unsigned int nr;
672 struct list_head list;
673 struct io_ring_ctx *ctx;
674};
675
Jens Axboe9a56a232019-01-09 09:06:50 -0700676struct io_submit_state {
677 struct blk_plug plug;
678
679 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700680 * io_kiocb alloc cache
681 */
682 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300683 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700684
685 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600686 * Batch completion logic
687 */
688 struct io_comp_state comp;
689
690 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700691 * File reference cache
692 */
693 struct file *file;
694 unsigned int fd;
695 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700696 unsigned int ios_left;
697};
698
Jens Axboed3656342019-12-18 09:50:26 -0700699struct io_op_def {
700 /* needs req->io allocated for deferral/async */
701 unsigned async_ctx : 1;
702 /* needs current->mm setup, does mm access */
703 unsigned needs_mm : 1;
704 /* needs req->file assigned */
705 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600706 /* don't fail if file grab fails */
707 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700708 /* hash wq insertion if file is a regular file */
709 unsigned hash_reg_file : 1;
710 /* unbound wq insertion if file is a non-regular file */
711 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700712 /* opcode is not supported by this kernel */
713 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700714 /* needs file table */
715 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700716 /* needs ->fs */
717 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700718 /* set if opcode supports polled "wait" */
719 unsigned pollin : 1;
720 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700721 /* op supports buffer selection */
722 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300723 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700724};
725
726static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_NOP] = {},
728 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700729 .async_ctx = 1,
730 .needs_mm = 1,
731 .needs_file = 1,
732 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700733 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700734 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700735 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300736 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700737 .async_ctx = 1,
738 .needs_mm = 1,
739 .needs_file = 1,
740 .hash_reg_file = 1,
741 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700742 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300743 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700744 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300745 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700746 .needs_file = 1,
747 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300748 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700749 .needs_file = 1,
750 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700751 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700752 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300753 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700754 .needs_file = 1,
755 .hash_reg_file = 1,
756 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700757 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300758 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700759 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700761 .needs_file = 1,
762 .unbound_nonreg_file = 1,
763 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300764 [IORING_OP_POLL_REMOVE] = {},
765 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700766 .needs_file = 1,
767 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300768 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700769 .async_ctx = 1,
770 .needs_mm = 1,
771 .needs_file = 1,
772 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700773 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700774 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700777 .async_ctx = 1,
778 .needs_mm = 1,
779 .needs_file = 1,
780 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700781 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700782 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700783 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .async_ctx = 1,
787 .needs_mm = 1,
788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_TIMEOUT_REMOVE] = {},
790 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700791 .needs_mm = 1,
792 .needs_file = 1,
793 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700794 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700795 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_ASYNC_CANCEL] = {},
798 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700799 .async_ctx = 1,
800 .needs_mm = 1,
801 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300802 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700803 .async_ctx = 1,
804 .needs_mm = 1,
805 .needs_file = 1,
806 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700807 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300811 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700812 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300813 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700814 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700815 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700816 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300817 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600818 .needs_file = 1,
819 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700820 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700823 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700824 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700825 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300826 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700827 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700828 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600829 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700830 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300831 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700832 .needs_mm = 1,
833 .needs_file = 1,
834 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700835 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700836 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700837 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300838 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700839 .needs_mm = 1,
840 .needs_file = 1,
841 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700842 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300843 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700846 .needs_file = 1,
847 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700849 .needs_mm = 1,
850 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300851 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700852 .needs_mm = 1,
853 .needs_file = 1,
854 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700855 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700856 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300857 [IORING_OP_RECV] = {
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 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700862 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700865 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700866 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700867 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700868 [IORING_OP_EPOLL_CTL] = {
869 .unbound_nonreg_file = 1,
870 .file_table = 1,
871 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300872 [IORING_OP_SPLICE] = {
873 .needs_file = 1,
874 .hash_reg_file = 1,
875 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700876 },
877 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700878 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300879 [IORING_OP_TEE] = {
880 .needs_file = 1,
881 .hash_reg_file = 1,
882 .unbound_nonreg_file = 1,
883 },
Jens Axboed3656342019-12-18 09:50:26 -0700884};
885
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700886enum io_mem_account {
887 ACCT_LOCKED,
888 ACCT_PINNED,
889};
890
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300891static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
892 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700893static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800894static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600895static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700896static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700897static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600898static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700899static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700900static int __io_sqe_files_update(struct io_ring_ctx *ctx,
901 struct io_uring_files_update *ip,
902 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300903static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300904static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700905static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
906 int fd, struct file **out_file, bool fixed);
907static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600908 const struct io_uring_sqe *sqe,
909 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600910static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600911
Jens Axboeb63534c2020-06-04 11:28:00 -0600912static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
913 struct iovec **iovec, struct iov_iter *iter,
914 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600915static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
916 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600917 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700918
919static struct kmem_cache *req_cachep;
920
921static const struct file_operations io_uring_fops;
922
923struct sock *io_uring_get_socket(struct file *file)
924{
925#if defined(CONFIG_UNIX)
926 if (file->f_op == &io_uring_fops) {
927 struct io_ring_ctx *ctx = file->private_data;
928
929 return ctx->ring_sock->sk;
930 }
931#endif
932 return NULL;
933}
934EXPORT_SYMBOL(io_uring_get_socket);
935
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300936static void io_get_req_task(struct io_kiocb *req)
937{
938 if (req->flags & REQ_F_TASK_PINNED)
939 return;
940 get_task_struct(req->task);
941 req->flags |= REQ_F_TASK_PINNED;
942}
943
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300944static inline void io_clean_op(struct io_kiocb *req)
945{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300946 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
947 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300948 __io_clean_op(req);
949}
950
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300951/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
952static void __io_put_req_task(struct io_kiocb *req)
953{
954 if (req->flags & REQ_F_TASK_PINNED)
955 put_task_struct(req->task);
956}
957
Jens Axboe4349f302020-07-09 15:07:01 -0600958static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600959{
960 struct mm_struct *mm = current->mm;
961
962 if (mm) {
963 kthread_unuse_mm(mm);
964 mmput(mm);
965 }
966}
967
968static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
969{
970 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300971 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
972 !mmget_not_zero(ctx->sqo_mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600973 return -EFAULT;
974 kthread_use_mm(ctx->sqo_mm);
975 }
976
977 return 0;
978}
979
980static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
981 struct io_kiocb *req)
982{
983 if (!io_op_defs[req->opcode].needs_mm)
984 return 0;
985 return __io_sq_thread_acquire_mm(ctx);
986}
987
988static inline void req_set_fail_links(struct io_kiocb *req)
989{
990 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
991 req->flags |= REQ_F_FAIL_LINK;
992}
Jens Axboe4a38aed22020-05-14 17:21:15 -0600993
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800994/*
995 * Note: must call io_req_init_async() for the first time you
996 * touch any members of io_wq_work.
997 */
998static inline void io_req_init_async(struct io_kiocb *req)
999{
1000 if (req->flags & REQ_F_WORK_INITIALIZED)
1001 return;
1002
1003 memset(&req->work, 0, sizeof(req->work));
1004 req->flags |= REQ_F_WORK_INITIALIZED;
1005}
1006
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001007static inline bool io_async_submit(struct io_ring_ctx *ctx)
1008{
1009 return ctx->flags & IORING_SETUP_SQPOLL;
1010}
1011
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1013{
1014 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1015
Jens Axboe0f158b42020-05-14 17:18:39 -06001016 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001017}
1018
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001019static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1020{
1021 return !req->timeout.off;
1022}
1023
Jens Axboe2b188cc2019-01-07 10:46:33 -07001024static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1025{
1026 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001027 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001028
1029 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1030 if (!ctx)
1031 return NULL;
1032
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001033 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1034 if (!ctx->fallback_req)
1035 goto err;
1036
Jens Axboe78076bb2019-12-04 19:56:40 -07001037 /*
1038 * Use 5 bits less than the max cq entries, that should give us around
1039 * 32 entries per hash list if totally full and uniformly spread.
1040 */
1041 hash_bits = ilog2(p->cq_entries);
1042 hash_bits -= 5;
1043 if (hash_bits <= 0)
1044 hash_bits = 1;
1045 ctx->cancel_hash_bits = hash_bits;
1046 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1047 GFP_KERNEL);
1048 if (!ctx->cancel_hash)
1049 goto err;
1050 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1051
Roman Gushchin21482892019-05-07 10:01:48 -07001052 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001053 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1054 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001055
1056 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001057 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001058 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001059 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001060 init_completion(&ctx->ref_comp);
1061 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001062 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001063 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001064 mutex_init(&ctx->uring_lock);
1065 init_waitqueue_head(&ctx->wait);
1066 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001067 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001068 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001069 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001070 init_waitqueue_head(&ctx->inflight_wait);
1071 spin_lock_init(&ctx->inflight_lock);
1072 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001073 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1074 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001076err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001077 if (ctx->fallback_req)
1078 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001079 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001080 kfree(ctx);
1081 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082}
1083
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001084static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001085{
Jens Axboe2bc99302020-07-09 09:43:27 -06001086 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1087 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001088
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001089 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001090 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001091 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001092
Bob Liu9d858b22019-11-13 18:06:25 +08001093 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001094}
1095
Jens Axboede0617e2019-04-06 21:51:27 -06001096static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001097{
Hristo Venev75b28af2019-08-26 17:23:46 +00001098 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001099
Pavel Begunkov07910152020-01-17 03:52:46 +03001100 /* order cqe stores with ring update */
1101 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102
Pavel Begunkov07910152020-01-17 03:52:46 +03001103 if (wq_has_sleeper(&ctx->cq_wait)) {
1104 wake_up_interruptible(&ctx->cq_wait);
1105 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106 }
1107}
1108
Jens Axboe51a4cc12020-08-10 10:55:56 -06001109/*
1110 * Returns true if we need to defer file table putting. This can only happen
1111 * from the error path with REQ_F_COMP_LOCKED set.
1112 */
1113static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001114{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001115 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001116 return false;
1117
1118 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001119
Jens Axboecccf0ee2020-01-27 16:34:48 -07001120 if (req->work.mm) {
1121 mmdrop(req->work.mm);
1122 req->work.mm = NULL;
1123 }
1124 if (req->work.creds) {
1125 put_cred(req->work.creds);
1126 req->work.creds = NULL;
1127 }
Jens Axboeff002b32020-02-07 16:05:21 -07001128 if (req->work.fs) {
1129 struct fs_struct *fs = req->work.fs;
1130
Jens Axboe51a4cc12020-08-10 10:55:56 -06001131 if (req->flags & REQ_F_COMP_LOCKED)
1132 return true;
1133
Jens Axboeff002b32020-02-07 16:05:21 -07001134 spin_lock(&req->work.fs->lock);
1135 if (--fs->users)
1136 fs = NULL;
1137 spin_unlock(&req->work.fs->lock);
1138 if (fs)
1139 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001140 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001141 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001142
1143 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001144}
1145
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001146static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001147{
Jens Axboed3656342019-12-18 09:50:26 -07001148 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001149
Pavel Begunkov16d59802020-07-12 16:16:47 +03001150 io_req_init_async(req);
1151
Jens Axboed3656342019-12-18 09:50:26 -07001152 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001153 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001154 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001155 } else {
1156 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001157 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001158 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001159 if (!req->work.mm && def->needs_mm) {
1160 mmgrab(current->mm);
1161 req->work.mm = current->mm;
1162 }
1163 if (!req->work.creds)
1164 req->work.creds = get_current_cred();
1165 if (!req->work.fs && def->needs_fs) {
1166 spin_lock(&current->fs->lock);
1167 if (!current->fs->in_exec) {
1168 req->work.fs = current->fs;
1169 req->work.fs->users++;
1170 } else {
1171 req->work.flags |= IO_WQ_WORK_CANCEL;
1172 }
1173 spin_unlock(&current->fs->lock);
1174 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001175 if (def->needs_fsize)
1176 req->work.fsize = rlimit(RLIMIT_FSIZE);
1177 else
1178 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001179}
1180
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001181static void io_prep_async_link(struct io_kiocb *req)
1182{
1183 struct io_kiocb *cur;
1184
1185 io_prep_async_work(req);
1186 if (req->flags & REQ_F_LINK_HEAD)
1187 list_for_each_entry(cur, &req->link_list, link_list)
1188 io_prep_async_work(cur);
1189}
1190
Jens Axboe7271ef32020-08-10 09:55:22 -06001191static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001192{
Jackie Liua197f662019-11-08 08:09:12 -07001193 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001194 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001195
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001196 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1197 &req->work, req->flags);
1198 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001199 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001200}
1201
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001202static void io_queue_async_work(struct io_kiocb *req)
1203{
Jens Axboe7271ef32020-08-10 09:55:22 -06001204 struct io_kiocb *link;
1205
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001206 /* init ->work of the whole link before punting */
1207 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001208 link = __io_queue_async_work(req);
1209
1210 if (link)
1211 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001212}
1213
Jens Axboe5262f562019-09-17 12:26:57 -06001214static void io_kill_timeout(struct io_kiocb *req)
1215{
1216 int ret;
1217
Jens Axboe2d283902019-12-04 11:08:05 -07001218 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001219 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001220 atomic_set(&req->ctx->cq_timeouts,
1221 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001222 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001223 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001224 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001225 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001226 }
1227}
1228
Jens Axboef3606e32020-09-22 08:18:24 -06001229static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1230{
1231 struct io_ring_ctx *ctx = req->ctx;
1232
1233 if (!tsk || req->task == tsk)
1234 return true;
1235 if ((ctx->flags & IORING_SETUP_SQPOLL) && req->task == ctx->sqo_thread)
1236 return true;
1237 return false;
1238}
1239
1240static void io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001241{
1242 struct io_kiocb *req, *tmp;
1243
1244 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001245 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
1246 if (io_task_match(req, tsk))
1247 io_kill_timeout(req);
1248 }
Jens Axboe5262f562019-09-17 12:26:57 -06001249 spin_unlock_irq(&ctx->completion_lock);
1250}
1251
Pavel Begunkov04518942020-05-26 20:34:05 +03001252static void __io_queue_deferred(struct io_ring_ctx *ctx)
1253{
1254 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001255 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1256 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001257 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001258
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001259 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001260 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001261 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001262 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001263 link = __io_queue_async_work(de->req);
1264 if (link) {
1265 __io_queue_linked_timeout(link);
1266 /* drop submission reference */
1267 link->flags |= REQ_F_COMP_LOCKED;
1268 io_put_req(link);
1269 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001270 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001271 } while (!list_empty(&ctx->defer_list));
1272}
1273
Pavel Begunkov360428f2020-05-30 14:54:17 +03001274static void io_flush_timeouts(struct io_ring_ctx *ctx)
1275{
1276 while (!list_empty(&ctx->timeout_list)) {
1277 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001278 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001279
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001280 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001281 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001282 if (req->timeout.target_seq != ctx->cached_cq_tail
1283 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001284 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001285
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001286 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001287 io_kill_timeout(req);
1288 }
1289}
1290
Jens Axboede0617e2019-04-06 21:51:27 -06001291static void io_commit_cqring(struct io_ring_ctx *ctx)
1292{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001293 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001294 __io_commit_cqring(ctx);
1295
Pavel Begunkov04518942020-05-26 20:34:05 +03001296 if (unlikely(!list_empty(&ctx->defer_list)))
1297 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001298}
1299
Jens Axboe2b188cc2019-01-07 10:46:33 -07001300static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1301{
Hristo Venev75b28af2019-08-26 17:23:46 +00001302 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001303 unsigned tail;
1304
1305 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001306 /*
1307 * writes to the cq entry need to come after reading head; the
1308 * control dependency is enough as we're using WRITE_ONCE to
1309 * fill the cq entry
1310 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001311 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001312 return NULL;
1313
1314 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001315 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001316}
1317
Jens Axboef2842ab2020-01-08 11:04:00 -07001318static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1319{
Jens Axboef0b493e2020-02-01 21:30:11 -07001320 if (!ctx->cq_ev_fd)
1321 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001322 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1323 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001324 if (!ctx->eventfd_async)
1325 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001326 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001327}
1328
Jens Axboeb41e9852020-02-17 09:52:41 -07001329static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001330{
1331 if (waitqueue_active(&ctx->wait))
1332 wake_up(&ctx->wait);
1333 if (waitqueue_active(&ctx->sqo_wait))
1334 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001335 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001336 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001337}
1338
Pavel Begunkov46930142020-07-30 18:43:49 +03001339static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1340{
1341 if (list_empty(&ctx->cq_overflow_list)) {
1342 clear_bit(0, &ctx->sq_check_overflow);
1343 clear_bit(0, &ctx->cq_check_overflow);
1344 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1345 }
1346}
1347
Jens Axboec4a2ed72019-11-21 21:01:26 -07001348/* Returns true if there are no backlogged entries after the flush */
1349static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001351 struct io_rings *rings = ctx->rings;
1352 struct io_uring_cqe *cqe;
1353 struct io_kiocb *req;
1354 unsigned long flags;
1355 LIST_HEAD(list);
1356
1357 if (!force) {
1358 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001359 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001360 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1361 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001362 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001363 }
1364
1365 spin_lock_irqsave(&ctx->completion_lock, flags);
1366
1367 /* if force is set, the ring is going away. always drop after that */
1368 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001369 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001370
Jens Axboec4a2ed72019-11-21 21:01:26 -07001371 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001372 while (!list_empty(&ctx->cq_overflow_list)) {
1373 cqe = io_get_cqring(ctx);
1374 if (!cqe && !force)
1375 break;
1376
1377 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001378 compl.list);
1379 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001380 if (cqe) {
1381 WRITE_ONCE(cqe->user_data, req->user_data);
1382 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001383 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001384 } else {
1385 WRITE_ONCE(ctx->rings->cq_overflow,
1386 atomic_inc_return(&ctx->cached_cq_overflow));
1387 }
1388 }
1389
1390 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001391 io_cqring_mark_overflow(ctx);
1392
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001393 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1394 io_cqring_ev_posted(ctx);
1395
1396 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001397 req = list_first_entry(&list, struct io_kiocb, compl.list);
1398 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001399 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001400 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001401
1402 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001403}
1404
Jens Axboebcda7ba2020-02-23 16:42:51 -07001405static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001407 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001408 struct io_uring_cqe *cqe;
1409
Jens Axboe78e19bb2019-11-06 15:21:34 -07001410 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001411
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412 /*
1413 * If we can't get a cq entry, userspace overflowed the
1414 * submission (by quite a lot). Increment the overflow count in
1415 * the ring.
1416 */
1417 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001418 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001419 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001421 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001422 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001423 WRITE_ONCE(ctx->rings->cq_overflow,
1424 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001425 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001426 if (list_empty(&ctx->cq_overflow_list)) {
1427 set_bit(0, &ctx->sq_check_overflow);
1428 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001429 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001430 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001431 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001432 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001433 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001434 refcount_inc(&req->refs);
1435 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436 }
1437}
1438
Jens Axboebcda7ba2020-02-23 16:42:51 -07001439static void io_cqring_fill_event(struct io_kiocb *req, long res)
1440{
1441 __io_cqring_fill_event(req, res, 0);
1442}
1443
Jens Axboee1e16092020-06-22 09:17:17 -06001444static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001445{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001446 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001447 unsigned long flags;
1448
1449 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001450 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001451 io_commit_cqring(ctx);
1452 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1453
Jens Axboe8c838782019-03-12 15:48:16 -06001454 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001455}
1456
Jens Axboe229a7b62020-06-22 10:13:11 -06001457static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001458{
Jens Axboe229a7b62020-06-22 10:13:11 -06001459 struct io_ring_ctx *ctx = cs->ctx;
1460
1461 spin_lock_irq(&ctx->completion_lock);
1462 while (!list_empty(&cs->list)) {
1463 struct io_kiocb *req;
1464
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001465 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1466 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001467 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001468 if (!(req->flags & REQ_F_LINK_HEAD)) {
1469 req->flags |= REQ_F_COMP_LOCKED;
1470 io_put_req(req);
1471 } else {
1472 spin_unlock_irq(&ctx->completion_lock);
1473 io_put_req(req);
1474 spin_lock_irq(&ctx->completion_lock);
1475 }
1476 }
1477 io_commit_cqring(ctx);
1478 spin_unlock_irq(&ctx->completion_lock);
1479
1480 io_cqring_ev_posted(ctx);
1481 cs->nr = 0;
1482}
1483
1484static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1485 struct io_comp_state *cs)
1486{
1487 if (!cs) {
1488 io_cqring_add_event(req, res, cflags);
1489 io_put_req(req);
1490 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001491 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001492 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001493 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001494 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001495 if (++cs->nr >= 32)
1496 io_submit_flush_completions(cs);
1497 }
Jens Axboee1e16092020-06-22 09:17:17 -06001498}
1499
1500static void io_req_complete(struct io_kiocb *req, long res)
1501{
Jens Axboe229a7b62020-06-22 10:13:11 -06001502 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001503}
1504
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001505static inline bool io_is_fallback_req(struct io_kiocb *req)
1506{
1507 return req == (struct io_kiocb *)
1508 ((unsigned long) req->ctx->fallback_req & ~1UL);
1509}
1510
1511static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1512{
1513 struct io_kiocb *req;
1514
1515 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001516 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001517 return req;
1518
1519 return NULL;
1520}
1521
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001522static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1523 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001524{
Jens Axboefd6fab22019-03-14 16:30:06 -06001525 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001526 struct io_kiocb *req;
1527
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001528 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001529 size_t sz;
1530 int ret;
1531
1532 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001533 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1534
1535 /*
1536 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1537 * retry single alloc to be on the safe side.
1538 */
1539 if (unlikely(ret <= 0)) {
1540 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1541 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001542 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001543 ret = 1;
1544 }
Jens Axboe2579f912019-01-09 09:10:43 -07001545 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001546 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001547 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001548 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001549 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001550 }
1551
Jens Axboe2579f912019-01-09 09:10:43 -07001552 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001553fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001554 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001555}
1556
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001557static inline void io_put_file(struct io_kiocb *req, struct file *file,
1558 bool fixed)
1559{
1560 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001561 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001562 else
1563 fput(file);
1564}
1565
Jens Axboe51a4cc12020-08-10 10:55:56 -06001566static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001567{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001568 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001569
Jens Axboe5acbbc82020-07-08 15:15:26 -06001570 if (req->io)
1571 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001572 if (req->file)
1573 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001574
Jens Axboe51a4cc12020-08-10 10:55:56 -06001575 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001576}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001577
Jens Axboe51a4cc12020-08-10 10:55:56 -06001578static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001579{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001580 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001581
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001582 __io_put_req_task(req);
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
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001909 if (req->flags & REQ_F_TASK_PINNED) {
1910 if (req->task != rb->task) {
1911 if (rb->task)
1912 put_task_struct_many(rb->task, rb->task_refs);
1913 rb->task = req->task;
1914 rb->task_refs = 0;
1915 }
1916 rb->task_refs++;
1917 req->flags &= ~REQ_F_TASK_PINNED;
1918 }
1919
Jens Axboe51a4cc12020-08-10 10:55:56 -06001920 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001921 rb->reqs[rb->to_free++] = req;
1922 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1923 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001924}
1925
Jens Axboeba816ad2019-09-28 11:36:45 -06001926/*
1927 * Drop reference to request, return next in chain (if there is one) if this
1928 * was the last reference to this request.
1929 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001930static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001931{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001932 struct io_kiocb *nxt = NULL;
1933
Jens Axboe2a44f462020-02-25 13:25:41 -07001934 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001935 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001936 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001937 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001938 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001939}
1940
Jens Axboe2b188cc2019-01-07 10:46:33 -07001941static void io_put_req(struct io_kiocb *req)
1942{
Jens Axboedef596e2019-01-09 08:59:42 -07001943 if (refcount_dec_and_test(&req->refs))
1944 io_free_req(req);
1945}
1946
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001947static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001948{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001949 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001950
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001951 /*
1952 * A ref is owned by io-wq in which context we're. So, if that's the
1953 * last one, it's safe to steal next work. False negatives are Ok,
1954 * it just will be re-punted async in io_put_work()
1955 */
1956 if (refcount_read(&req->refs) != 1)
1957 return NULL;
1958
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001959 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001960 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001961}
1962
Jens Axboe978db572019-11-14 22:39:04 -07001963/*
1964 * Must only be used if we don't need to care about links, usually from
1965 * within the completion handling itself.
1966 */
1967static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001968{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001969 /* drop both submit and complete references */
1970 if (refcount_sub_and_test(2, &req->refs))
1971 __io_free_req(req);
1972}
1973
Jens Axboe978db572019-11-14 22:39:04 -07001974static void io_double_put_req(struct io_kiocb *req)
1975{
1976 /* drop both submit and complete references */
1977 if (refcount_sub_and_test(2, &req->refs))
1978 io_free_req(req);
1979}
1980
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001981static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001982{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001983 struct io_rings *rings = ctx->rings;
1984
Jens Axboead3eb2c2019-12-18 17:12:20 -07001985 if (test_bit(0, &ctx->cq_check_overflow)) {
1986 /*
1987 * noflush == true is from the waitqueue handler, just ensure
1988 * we wake up the task, and the next invocation will flush the
1989 * entries. We cannot safely to it from here.
1990 */
1991 if (noflush && !list_empty(&ctx->cq_overflow_list))
1992 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001993
Jens Axboead3eb2c2019-12-18 17:12:20 -07001994 io_cqring_overflow_flush(ctx, false);
1995 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001996
Jens Axboea3a0e432019-08-20 11:03:11 -06001997 /* See comment at the top of this file */
1998 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001999 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002000}
2001
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002002static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2003{
2004 struct io_rings *rings = ctx->rings;
2005
2006 /* make sure SQ entry isn't read before tail */
2007 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2008}
2009
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002010static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002011{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002012 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002013
Jens Axboebcda7ba2020-02-23 16:42:51 -07002014 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2015 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002016 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002017 kfree(kbuf);
2018 return cflags;
2019}
2020
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002021static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2022{
2023 struct io_buffer *kbuf;
2024
2025 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2026 return io_put_kbuf(req, kbuf);
2027}
2028
Jens Axboe4c6e2772020-07-01 11:29:10 -06002029static inline bool io_run_task_work(void)
2030{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002031 /*
2032 * Not safe to run on exiting task, and the task_work handling will
2033 * not add work to such a task.
2034 */
2035 if (unlikely(current->flags & PF_EXITING))
2036 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002037 if (current->task_works) {
2038 __set_current_state(TASK_RUNNING);
2039 task_work_run();
2040 return true;
2041 }
2042
2043 return false;
2044}
2045
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002046static void io_iopoll_queue(struct list_head *again)
2047{
2048 struct io_kiocb *req;
2049
2050 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002051 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2052 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002053 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002054 } while (!list_empty(again));
2055}
2056
Jens Axboedef596e2019-01-09 08:59:42 -07002057/*
2058 * Find and free completed poll iocbs
2059 */
2060static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2061 struct list_head *done)
2062{
Jens Axboe8237e042019-12-28 10:48:22 -07002063 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002064 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002065 LIST_HEAD(again);
2066
2067 /* order with ->result store in io_complete_rw_iopoll() */
2068 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002069
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002070 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002071 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002072 int cflags = 0;
2073
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002074 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002075 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002076 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002077 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002078 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002079 continue;
2080 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002081 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002082
Jens Axboebcda7ba2020-02-23 16:42:51 -07002083 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002084 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002085
2086 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002087 (*nr_events)++;
2088
Pavel Begunkovc3524382020-06-28 12:52:32 +03002089 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002090 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002091 }
Jens Axboedef596e2019-01-09 08:59:42 -07002092
Jens Axboe09bb8392019-03-13 12:39:28 -06002093 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002094 if (ctx->flags & IORING_SETUP_SQPOLL)
2095 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002096 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002097
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002098 if (!list_empty(&again))
2099 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002100}
2101
Jens Axboedef596e2019-01-09 08:59:42 -07002102static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2103 long min)
2104{
2105 struct io_kiocb *req, *tmp;
2106 LIST_HEAD(done);
2107 bool spin;
2108 int ret;
2109
2110 /*
2111 * Only spin for completions if we don't have multiple devices hanging
2112 * off our complete list, and we're under the requested amount.
2113 */
2114 spin = !ctx->poll_multi_file && *nr_events < min;
2115
2116 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002117 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002118 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002119
2120 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002121 * Move completed and retryable entries to our local lists.
2122 * If we find a request that requires polling, break out
2123 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002124 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002125 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002126 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002127 continue;
2128 }
2129 if (!list_empty(&done))
2130 break;
2131
2132 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2133 if (ret < 0)
2134 break;
2135
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002136 /* iopoll may have completed current req */
2137 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002138 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002139
Jens Axboedef596e2019-01-09 08:59:42 -07002140 if (ret && spin)
2141 spin = false;
2142 ret = 0;
2143 }
2144
2145 if (!list_empty(&done))
2146 io_iopoll_complete(ctx, nr_events, &done);
2147
2148 return ret;
2149}
2150
2151/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002152 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002153 * non-spinning poll check - we'll still enter the driver poll loop, but only
2154 * as a non-spinning completion check.
2155 */
2156static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2157 long min)
2158{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002159 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002160 int ret;
2161
2162 ret = io_do_iopoll(ctx, nr_events, min);
2163 if (ret < 0)
2164 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002165 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002166 return 0;
2167 }
2168
2169 return 1;
2170}
2171
2172/*
2173 * We can't just wait for polled events to come to us, we have to actively
2174 * find and complete them.
2175 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002176static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002177{
2178 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2179 return;
2180
2181 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002182 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002183 unsigned int nr_events = 0;
2184
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002185 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002186
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002187 /* let it sleep and repeat later if can't complete a request */
2188 if (nr_events == 0)
2189 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002190 /*
2191 * Ensure we allow local-to-the-cpu processing to take place,
2192 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002193 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002194 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002195 if (need_resched()) {
2196 mutex_unlock(&ctx->uring_lock);
2197 cond_resched();
2198 mutex_lock(&ctx->uring_lock);
2199 }
Jens Axboedef596e2019-01-09 08:59:42 -07002200 }
2201 mutex_unlock(&ctx->uring_lock);
2202}
2203
Pavel Begunkov7668b922020-07-07 16:36:21 +03002204static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002205{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002206 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002207 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002208
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002209 /*
2210 * We disallow the app entering submit/complete with polling, but we
2211 * still need to lock the ring to prevent racing with polled issue
2212 * that got punted to a workqueue.
2213 */
2214 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002215 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002216 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002217 * Don't enter poll loop if we already have events pending.
2218 * If we do, we can potentially be spinning for commands that
2219 * already triggered a CQE (eg in error).
2220 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002221 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002222 break;
2223
2224 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002225 * If a submit got punted to a workqueue, we can have the
2226 * application entering polling for a command before it gets
2227 * issued. That app will hold the uring_lock for the duration
2228 * of the poll right here, so we need to take a breather every
2229 * now and then to ensure that the issue has a chance to add
2230 * the poll to the issued list. Otherwise we can spin here
2231 * forever, while the workqueue is stuck trying to acquire the
2232 * very same mutex.
2233 */
2234 if (!(++iters & 7)) {
2235 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002236 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002237 mutex_lock(&ctx->uring_lock);
2238 }
2239
Pavel Begunkov7668b922020-07-07 16:36:21 +03002240 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002241 if (ret <= 0)
2242 break;
2243 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002244 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002245
Jens Axboe500f9fb2019-08-19 12:15:59 -06002246 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002247 return ret;
2248}
2249
Jens Axboe491381ce2019-10-17 09:20:46 -06002250static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002251{
Jens Axboe491381ce2019-10-17 09:20:46 -06002252 /*
2253 * Tell lockdep we inherited freeze protection from submission
2254 * thread.
2255 */
2256 if (req->flags & REQ_F_ISREG) {
2257 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002258
Jens Axboe491381ce2019-10-17 09:20:46 -06002259 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002260 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002261 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002262}
2263
Jens Axboea1d7c392020-06-22 11:09:46 -06002264static void io_complete_rw_common(struct kiocb *kiocb, long res,
2265 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002266{
Jens Axboe9adbd452019-12-20 08:45:55 -07002267 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002268 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002269
Jens Axboe491381ce2019-10-17 09:20:46 -06002270 if (kiocb->ki_flags & IOCB_WRITE)
2271 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002272
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002273 if (res != req->result)
2274 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002275 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002276 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002277 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002278}
2279
Jens Axboeb63534c2020-06-04 11:28:00 -06002280#ifdef CONFIG_BLOCK
2281static bool io_resubmit_prep(struct io_kiocb *req, int error)
2282{
2283 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2284 ssize_t ret = -ECANCELED;
2285 struct iov_iter iter;
2286 int rw;
2287
2288 if (error) {
2289 ret = error;
2290 goto end_req;
2291 }
2292
2293 switch (req->opcode) {
2294 case IORING_OP_READV:
2295 case IORING_OP_READ_FIXED:
2296 case IORING_OP_READ:
2297 rw = READ;
2298 break;
2299 case IORING_OP_WRITEV:
2300 case IORING_OP_WRITE_FIXED:
2301 case IORING_OP_WRITE:
2302 rw = WRITE;
2303 break;
2304 default:
2305 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2306 req->opcode);
2307 goto end_req;
2308 }
2309
Jens Axboe8f3d7492020-09-14 09:28:14 -06002310 if (!req->io) {
2311 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2312 if (ret < 0)
2313 goto end_req;
2314 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2315 if (!ret)
2316 return true;
2317 kfree(iovec);
2318 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002319 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002320 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002321end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002322 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002323 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002324 return false;
2325}
Jens Axboeb63534c2020-06-04 11:28:00 -06002326#endif
2327
2328static bool io_rw_reissue(struct io_kiocb *req, long res)
2329{
2330#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002331 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002332 int ret;
2333
Jens Axboe355afae2020-09-02 09:30:31 -06002334 if (!S_ISBLK(mode) && !S_ISREG(mode))
2335 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002336 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2337 return false;
2338
Jens Axboefdee9462020-08-27 16:46:24 -06002339 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002340
Jens Axboefdee9462020-08-27 16:46:24 -06002341 if (io_resubmit_prep(req, ret)) {
2342 refcount_inc(&req->refs);
2343 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002344 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002345 }
2346
Jens Axboeb63534c2020-06-04 11:28:00 -06002347#endif
2348 return false;
2349}
2350
Jens Axboea1d7c392020-06-22 11:09:46 -06002351static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2352 struct io_comp_state *cs)
2353{
2354 if (!io_rw_reissue(req, res))
2355 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002356}
2357
2358static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2359{
Jens Axboe9adbd452019-12-20 08:45:55 -07002360 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002361
Jens Axboea1d7c392020-06-22 11:09:46 -06002362 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002363}
2364
Jens Axboedef596e2019-01-09 08:59:42 -07002365static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2366{
Jens Axboe9adbd452019-12-20 08:45:55 -07002367 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002368
Jens Axboe491381ce2019-10-17 09:20:46 -06002369 if (kiocb->ki_flags & IOCB_WRITE)
2370 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002371
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002372 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002373 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002374
2375 WRITE_ONCE(req->result, res);
2376 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002377 smp_wmb();
2378 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002379}
2380
2381/*
2382 * After the iocb has been issued, it's safe to be found on the poll list.
2383 * Adding the kiocb to the list AFTER submission ensures that we don't
2384 * find it from a io_iopoll_getevents() thread before the issuer is done
2385 * accessing the kiocb cookie.
2386 */
2387static void io_iopoll_req_issued(struct io_kiocb *req)
2388{
2389 struct io_ring_ctx *ctx = req->ctx;
2390
2391 /*
2392 * Track whether we have multiple files in our lists. This will impact
2393 * how we do polling eventually, not spinning if we're on potentially
2394 * different devices.
2395 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002396 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002397 ctx->poll_multi_file = false;
2398 } else if (!ctx->poll_multi_file) {
2399 struct io_kiocb *list_req;
2400
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002401 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002402 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002403 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002404 ctx->poll_multi_file = true;
2405 }
2406
2407 /*
2408 * For fast devices, IO may have already completed. If it has, add
2409 * it to the front so we find it first.
2410 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002411 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002412 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002413 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002414 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002415
2416 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2417 wq_has_sleeper(&ctx->sqo_wait))
2418 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002419}
2420
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002421static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002422{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002423 if (state->has_refs)
2424 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002425 state->file = NULL;
2426}
2427
2428static inline void io_state_file_put(struct io_submit_state *state)
2429{
2430 if (state->file)
2431 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002432}
2433
2434/*
2435 * Get as many references to a file as we have IOs left in this submission,
2436 * assuming most submissions are for one file, or at least that each file
2437 * has more than one submission.
2438 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002439static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002440{
2441 if (!state)
2442 return fget(fd);
2443
2444 if (state->file) {
2445 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002446 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002447 state->ios_left--;
2448 return state->file;
2449 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002450 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002451 }
2452 state->file = fget_many(fd, state->ios_left);
2453 if (!state->file)
2454 return NULL;
2455
2456 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002457 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002458 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002459 return state->file;
2460}
2461
Jens Axboe4503b762020-06-01 10:00:27 -06002462static bool io_bdev_nowait(struct block_device *bdev)
2463{
2464#ifdef CONFIG_BLOCK
2465 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2466#else
2467 return true;
2468#endif
2469}
2470
Jens Axboe2b188cc2019-01-07 10:46:33 -07002471/*
2472 * If we tracked the file through the SCM inflight mechanism, we could support
2473 * any file. For now, just ensure that anything potentially problematic is done
2474 * inline.
2475 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002476static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002477{
2478 umode_t mode = file_inode(file)->i_mode;
2479
Jens Axboe4503b762020-06-01 10:00:27 -06002480 if (S_ISBLK(mode)) {
2481 if (io_bdev_nowait(file->f_inode->i_bdev))
2482 return true;
2483 return false;
2484 }
2485 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002486 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002487 if (S_ISREG(mode)) {
2488 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2489 file->f_op != &io_uring_fops)
2490 return true;
2491 return false;
2492 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002493
Jens Axboec5b85622020-06-09 19:23:05 -06002494 /* any ->read/write should understand O_NONBLOCK */
2495 if (file->f_flags & O_NONBLOCK)
2496 return true;
2497
Jens Axboeaf197f52020-04-28 13:15:06 -06002498 if (!(file->f_mode & FMODE_NOWAIT))
2499 return false;
2500
2501 if (rw == READ)
2502 return file->f_op->read_iter != NULL;
2503
2504 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002505}
2506
Jens Axboe3529d8c2019-12-19 18:24:38 -07002507static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2508 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002509{
Jens Axboedef596e2019-01-09 08:59:42 -07002510 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002511 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002512 unsigned ioprio;
2513 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002514
Jens Axboe491381ce2019-10-17 09:20:46 -06002515 if (S_ISREG(file_inode(req->file)->i_mode))
2516 req->flags |= REQ_F_ISREG;
2517
Jens Axboe2b188cc2019-01-07 10:46:33 -07002518 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002519 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2520 req->flags |= REQ_F_CUR_POS;
2521 kiocb->ki_pos = req->file->f_pos;
2522 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002524 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2525 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2526 if (unlikely(ret))
2527 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002528
2529 ioprio = READ_ONCE(sqe->ioprio);
2530 if (ioprio) {
2531 ret = ioprio_check_cap(ioprio);
2532 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002533 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002534
2535 kiocb->ki_ioprio = ioprio;
2536 } else
2537 kiocb->ki_ioprio = get_current_ioprio();
2538
Stefan Bühler8449eed2019-04-27 20:34:19 +02002539 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002540 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002541 req->flags |= REQ_F_NOWAIT;
2542
Jens Axboeb63534c2020-06-04 11:28:00 -06002543 if (kiocb->ki_flags & IOCB_DIRECT)
2544 io_get_req_task(req);
2545
Stefan Bühler8449eed2019-04-27 20:34:19 +02002546 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002548
Jens Axboedef596e2019-01-09 08:59:42 -07002549 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002550 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2551 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002552 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002553
Jens Axboedef596e2019-01-09 08:59:42 -07002554 kiocb->ki_flags |= IOCB_HIPRI;
2555 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002556 req->iopoll_completed = 0;
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +03002557 io_get_req_task(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002558 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002559 if (kiocb->ki_flags & IOCB_HIPRI)
2560 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002561 kiocb->ki_complete = io_complete_rw;
2562 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002563
Jens Axboe3529d8c2019-12-19 18:24:38 -07002564 req->rw.addr = READ_ONCE(sqe->addr);
2565 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002566 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002567 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002568}
2569
2570static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2571{
2572 switch (ret) {
2573 case -EIOCBQUEUED:
2574 break;
2575 case -ERESTARTSYS:
2576 case -ERESTARTNOINTR:
2577 case -ERESTARTNOHAND:
2578 case -ERESTART_RESTARTBLOCK:
2579 /*
2580 * We can't just restart the syscall, since previously
2581 * submitted sqes may already be in progress. Just fail this
2582 * IO with EINTR.
2583 */
2584 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002585 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002586 default:
2587 kiocb->ki_complete(kiocb, ret, 0);
2588 }
2589}
2590
Jens Axboea1d7c392020-06-22 11:09:46 -06002591static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2592 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002593{
Jens Axboeba042912019-12-25 16:33:42 -07002594 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2595
Jens Axboe227c0c92020-08-13 11:51:40 -06002596 /* add previously done IO, if any */
2597 if (req->io && req->io->rw.bytes_done > 0) {
2598 if (ret < 0)
2599 ret = req->io->rw.bytes_done;
2600 else
2601 ret += req->io->rw.bytes_done;
2602 }
2603
Jens Axboeba042912019-12-25 16:33:42 -07002604 if (req->flags & REQ_F_CUR_POS)
2605 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002606 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002607 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002608 else
2609 io_rw_done(kiocb, ret);
2610}
2611
Jens Axboe9adbd452019-12-20 08:45:55 -07002612static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002613 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002614{
Jens Axboe9adbd452019-12-20 08:45:55 -07002615 struct io_ring_ctx *ctx = req->ctx;
2616 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002617 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002618 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002619 size_t offset;
2620 u64 buf_addr;
2621
2622 /* attempt to use fixed buffers without having provided iovecs */
2623 if (unlikely(!ctx->user_bufs))
2624 return -EFAULT;
2625
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002626 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002627 if (unlikely(buf_index >= ctx->nr_user_bufs))
2628 return -EFAULT;
2629
2630 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2631 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002632 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002633
2634 /* overflow */
2635 if (buf_addr + len < buf_addr)
2636 return -EFAULT;
2637 /* not inside the mapped region */
2638 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2639 return -EFAULT;
2640
2641 /*
2642 * May not be a start of buffer, set size appropriately
2643 * and advance us to the beginning.
2644 */
2645 offset = buf_addr - imu->ubuf;
2646 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002647
2648 if (offset) {
2649 /*
2650 * Don't use iov_iter_advance() here, as it's really slow for
2651 * using the latter parts of a big fixed buffer - it iterates
2652 * over each segment manually. We can cheat a bit here, because
2653 * we know that:
2654 *
2655 * 1) it's a BVEC iter, we set it up
2656 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2657 * first and last bvec
2658 *
2659 * So just find our index, and adjust the iterator afterwards.
2660 * If the offset is within the first bvec (or the whole first
2661 * bvec, just use iov_iter_advance(). This makes it easier
2662 * since we can just skip the first segment, which may not
2663 * be PAGE_SIZE aligned.
2664 */
2665 const struct bio_vec *bvec = imu->bvec;
2666
2667 if (offset <= bvec->bv_len) {
2668 iov_iter_advance(iter, offset);
2669 } else {
2670 unsigned long seg_skip;
2671
2672 /* skip first vec */
2673 offset -= bvec->bv_len;
2674 seg_skip = 1 + (offset >> PAGE_SHIFT);
2675
2676 iter->bvec = bvec + seg_skip;
2677 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002678 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002679 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002680 }
2681 }
2682
Jens Axboe5e559562019-11-13 16:12:46 -07002683 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002684}
2685
Jens Axboebcda7ba2020-02-23 16:42:51 -07002686static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2687{
2688 if (needs_lock)
2689 mutex_unlock(&ctx->uring_lock);
2690}
2691
2692static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2693{
2694 /*
2695 * "Normal" inline submissions always hold the uring_lock, since we
2696 * grab it from the system call. Same is true for the SQPOLL offload.
2697 * The only exception is when we've detached the request and issue it
2698 * from an async worker thread, grab the lock for that case.
2699 */
2700 if (needs_lock)
2701 mutex_lock(&ctx->uring_lock);
2702}
2703
2704static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2705 int bgid, struct io_buffer *kbuf,
2706 bool needs_lock)
2707{
2708 struct io_buffer *head;
2709
2710 if (req->flags & REQ_F_BUFFER_SELECTED)
2711 return kbuf;
2712
2713 io_ring_submit_lock(req->ctx, needs_lock);
2714
2715 lockdep_assert_held(&req->ctx->uring_lock);
2716
2717 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2718 if (head) {
2719 if (!list_empty(&head->list)) {
2720 kbuf = list_last_entry(&head->list, struct io_buffer,
2721 list);
2722 list_del(&kbuf->list);
2723 } else {
2724 kbuf = head;
2725 idr_remove(&req->ctx->io_buffer_idr, bgid);
2726 }
2727 if (*len > kbuf->len)
2728 *len = kbuf->len;
2729 } else {
2730 kbuf = ERR_PTR(-ENOBUFS);
2731 }
2732
2733 io_ring_submit_unlock(req->ctx, needs_lock);
2734
2735 return kbuf;
2736}
2737
Jens Axboe4d954c22020-02-27 07:31:19 -07002738static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2739 bool needs_lock)
2740{
2741 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002742 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002743
2744 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002745 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002746 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2747 if (IS_ERR(kbuf))
2748 return kbuf;
2749 req->rw.addr = (u64) (unsigned long) kbuf;
2750 req->flags |= REQ_F_BUFFER_SELECTED;
2751 return u64_to_user_ptr(kbuf->addr);
2752}
2753
2754#ifdef CONFIG_COMPAT
2755static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2756 bool needs_lock)
2757{
2758 struct compat_iovec __user *uiov;
2759 compat_ssize_t clen;
2760 void __user *buf;
2761 ssize_t len;
2762
2763 uiov = u64_to_user_ptr(req->rw.addr);
2764 if (!access_ok(uiov, sizeof(*uiov)))
2765 return -EFAULT;
2766 if (__get_user(clen, &uiov->iov_len))
2767 return -EFAULT;
2768 if (clen < 0)
2769 return -EINVAL;
2770
2771 len = clen;
2772 buf = io_rw_buffer_select(req, &len, needs_lock);
2773 if (IS_ERR(buf))
2774 return PTR_ERR(buf);
2775 iov[0].iov_base = buf;
2776 iov[0].iov_len = (compat_size_t) len;
2777 return 0;
2778}
2779#endif
2780
2781static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2782 bool needs_lock)
2783{
2784 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2785 void __user *buf;
2786 ssize_t len;
2787
2788 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2789 return -EFAULT;
2790
2791 len = iov[0].iov_len;
2792 if (len < 0)
2793 return -EINVAL;
2794 buf = io_rw_buffer_select(req, &len, needs_lock);
2795 if (IS_ERR(buf))
2796 return PTR_ERR(buf);
2797 iov[0].iov_base = buf;
2798 iov[0].iov_len = len;
2799 return 0;
2800}
2801
2802static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2803 bool needs_lock)
2804{
Jens Axboedddb3e22020-06-04 11:27:01 -06002805 if (req->flags & REQ_F_BUFFER_SELECTED) {
2806 struct io_buffer *kbuf;
2807
2808 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2809 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2810 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002811 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002812 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002813 if (!req->rw.len)
2814 return 0;
2815 else if (req->rw.len > 1)
2816 return -EINVAL;
2817
2818#ifdef CONFIG_COMPAT
2819 if (req->ctx->compat)
2820 return io_compat_import(req, iov, needs_lock);
2821#endif
2822
2823 return __io_iov_buffer_select(req, iov, needs_lock);
2824}
2825
Jens Axboe8452fd02020-08-18 13:58:33 -07002826static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2827 struct iovec **iovec, struct iov_iter *iter,
2828 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002829{
Jens Axboe9adbd452019-12-20 08:45:55 -07002830 void __user *buf = u64_to_user_ptr(req->rw.addr);
2831 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002832 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002833 u8 opcode;
2834
Jens Axboed625c6e2019-12-17 19:53:05 -07002835 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002836 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002837 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002838 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002839 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002840
Jens Axboebcda7ba2020-02-23 16:42:51 -07002841 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002842 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002843 return -EINVAL;
2844
Jens Axboe3a6820f2019-12-22 15:19:35 -07002845 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002846 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002847 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002848 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002849 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002850 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002851 }
2852
Jens Axboe3a6820f2019-12-22 15:19:35 -07002853 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2854 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002855 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002856 }
2857
Jens Axboe4d954c22020-02-27 07:31:19 -07002858 if (req->flags & REQ_F_BUFFER_SELECT) {
2859 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002860 if (!ret) {
2861 ret = (*iovec)->iov_len;
2862 iov_iter_init(iter, rw, *iovec, 1, ret);
2863 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002864 *iovec = NULL;
2865 return ret;
2866 }
2867
Jens Axboe2b188cc2019-01-07 10:46:33 -07002868#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002869 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002870 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2871 iovec, iter);
2872#endif
2873
2874 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2875}
2876
Jens Axboe8452fd02020-08-18 13:58:33 -07002877static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2878 struct iovec **iovec, struct iov_iter *iter,
2879 bool needs_lock)
2880{
2881 if (!req->io)
2882 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2883 *iovec = NULL;
2884 return iov_iter_count(&req->io->rw.iter);
2885}
2886
Jens Axboe0fef9482020-08-26 10:36:20 -06002887static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2888{
2889 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2890}
2891
Jens Axboe32960612019-09-23 11:05:34 -06002892/*
2893 * For files that don't have ->read_iter() and ->write_iter(), handle them
2894 * by looping over ->read() or ->write() manually.
2895 */
2896static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2897 struct iov_iter *iter)
2898{
2899 ssize_t ret = 0;
2900
2901 /*
2902 * Don't support polled IO through this interface, and we can't
2903 * support non-blocking either. For the latter, this just causes
2904 * the kiocb to be handled from an async context.
2905 */
2906 if (kiocb->ki_flags & IOCB_HIPRI)
2907 return -EOPNOTSUPP;
2908 if (kiocb->ki_flags & IOCB_NOWAIT)
2909 return -EAGAIN;
2910
2911 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002912 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002913 ssize_t nr;
2914
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002915 if (!iov_iter_is_bvec(iter)) {
2916 iovec = iov_iter_iovec(iter);
2917 } else {
2918 /* fixed buffers import bvec */
2919 iovec.iov_base = kmap(iter->bvec->bv_page)
2920 + iter->iov_offset;
2921 iovec.iov_len = min(iter->count,
2922 iter->bvec->bv_len - iter->iov_offset);
2923 }
2924
Jens Axboe32960612019-09-23 11:05:34 -06002925 if (rw == READ) {
2926 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002927 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002928 } else {
2929 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002930 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002931 }
2932
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002933 if (iov_iter_is_bvec(iter))
2934 kunmap(iter->bvec->bv_page);
2935
Jens Axboe32960612019-09-23 11:05:34 -06002936 if (nr < 0) {
2937 if (!ret)
2938 ret = nr;
2939 break;
2940 }
2941 ret += nr;
2942 if (nr != iovec.iov_len)
2943 break;
2944 iov_iter_advance(iter, nr);
2945 }
2946
2947 return ret;
2948}
2949
Jens Axboeff6165b2020-08-13 09:47:43 -06002950static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2951 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07002952{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002953 struct io_async_rw *rw = &req->io->rw;
2954
Jens Axboeff6165b2020-08-13 09:47:43 -06002955 memcpy(&rw->iter, iter, sizeof(*iter));
2956 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06002957 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06002958 /* can only be fixed buffers, no need to do anything */
2959 if (iter->type == ITER_BVEC)
2960 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002961 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06002962 unsigned iov_off = 0;
2963
2964 rw->iter.iov = rw->fast_iov;
2965 if (iter->iov != fast_iov) {
2966 iov_off = iter->iov - fast_iov;
2967 rw->iter.iov += iov_off;
2968 }
2969 if (rw->fast_iov != fast_iov)
2970 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002971 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002972 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06002973 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002974 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002975 }
2976}
2977
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002978static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2979{
2980 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2981 return req->io == NULL;
2982}
2983
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002984static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002985{
Jens Axboed3656342019-12-18 09:50:26 -07002986 if (!io_op_defs[req->opcode].async_ctx)
2987 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002988
2989 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002990}
2991
Jens Axboeff6165b2020-08-13 09:47:43 -06002992static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
2993 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06002994 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002995{
Jens Axboe227c0c92020-08-13 11:51:40 -06002996 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002997 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002998 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002999 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003000 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003001
Jens Axboeff6165b2020-08-13 09:47:43 -06003002 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003003 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003004 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003005}
3006
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003007static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3008 bool force_nonblock)
3009{
Jens Axboeff6165b2020-08-13 09:47:43 -06003010 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06003011 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003012 ssize_t ret;
3013
Jens Axboec183edf2020-09-04 22:36:52 -06003014 iorw->iter.iov = iov = iorw->fast_iov;
3015 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003016 if (unlikely(ret < 0))
3017 return ret;
3018
Jens Axboec183edf2020-09-04 22:36:52 -06003019 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003020 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003021 return 0;
3022}
3023
Jens Axboe3529d8c2019-12-19 18:24:38 -07003024static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3025 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003026{
3027 ssize_t ret;
3028
Jens Axboe3529d8c2019-12-19 18:24:38 -07003029 ret = io_prep_rw(req, sqe, force_nonblock);
3030 if (ret)
3031 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003032
Jens Axboe3529d8c2019-12-19 18:24:38 -07003033 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3034 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003035
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003036 /* either don't need iovec imported or already have it */
3037 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003038 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003039 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003040}
3041
Jens Axboec1dd91d2020-08-03 16:43:59 -06003042/*
3043 * This is our waitqueue callback handler, registered through lock_page_async()
3044 * when we initially tried to do the IO with the iocb armed our waitqueue.
3045 * This gets called when the page is unlocked, and we generally expect that to
3046 * happen when the page IO is completed and the page is now uptodate. This will
3047 * queue a task_work based retry of the operation, attempting to copy the data
3048 * again. If the latter fails because the page was NOT uptodate, then we will
3049 * do a thread based blocking retry of the operation. That's the unexpected
3050 * slow path.
3051 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003052static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3053 int sync, void *arg)
3054{
3055 struct wait_page_queue *wpq;
3056 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003057 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003058 int ret;
3059
3060 wpq = container_of(wait, struct wait_page_queue, wait);
3061
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003062 if (!wake_page_match(wpq, key))
3063 return 0;
3064
Hao Xuc8d317a2020-09-29 20:00:45 +08003065 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003066 list_del_init(&wait->entry);
3067
Pavel Begunkove7375122020-07-12 20:42:04 +03003068 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003069 percpu_ref_get(&req->ctx->refs);
3070
Jens Axboebcf5a062020-05-22 09:24:42 -06003071 /* submit ref gets dropped, acquire a new one */
3072 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003073 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003074 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003075 struct task_struct *tsk;
3076
Jens Axboebcf5a062020-05-22 09:24:42 -06003077 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003078 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003079 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003080 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003081 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003082 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003083 return 1;
3084}
3085
Jens Axboec1dd91d2020-08-03 16:43:59 -06003086/*
3087 * This controls whether a given IO request should be armed for async page
3088 * based retry. If we return false here, the request is handed to the async
3089 * worker threads for retry. If we're doing buffered reads on a regular file,
3090 * we prepare a private wait_page_queue entry and retry the operation. This
3091 * will either succeed because the page is now uptodate and unlocked, or it
3092 * will register a callback when the page is unlocked at IO completion. Through
3093 * that callback, io_uring uses task_work to setup a retry of the operation.
3094 * That retry will attempt the buffered read again. The retry will generally
3095 * succeed, or in rare cases where it fails, we then fall back to using the
3096 * async worker threads for a blocking retry.
3097 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003098static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003099{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003100 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003101 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003102
3103 /* never retry for NOWAIT, we just complete with -EAGAIN */
3104 if (req->flags & REQ_F_NOWAIT)
3105 return false;
3106
Jens Axboe227c0c92020-08-13 11:51:40 -06003107 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003108 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003109 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003110
Jens Axboebcf5a062020-05-22 09:24:42 -06003111 /*
3112 * just use poll if we can, and don't attempt if the fs doesn't
3113 * support callback based unlocks
3114 */
3115 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3116 return false;
3117
Jens Axboe3b2a4432020-08-16 10:58:43 -07003118 wait->wait.func = io_async_buf_func;
3119 wait->wait.private = req;
3120 wait->wait.flags = 0;
3121 INIT_LIST_HEAD(&wait->wait.entry);
3122 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003123 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003124 kiocb->ki_waitq = wait;
Jens Axboebcf5a062020-05-22 09:24:42 -06003125
Jens Axboe3b2a4432020-08-16 10:58:43 -07003126 io_get_req_task(req);
3127 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003128}
3129
3130static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3131{
3132 if (req->file->f_op->read_iter)
3133 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003134 else if (req->file->f_op->read)
3135 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3136 else
3137 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003138}
3139
Jens Axboea1d7c392020-06-22 11:09:46 -06003140static int io_read(struct io_kiocb *req, bool force_nonblock,
3141 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003142{
3143 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003144 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003145 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003146 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003147 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003148 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003149
Jens Axboeff6165b2020-08-13 09:47:43 -06003150 if (req->io)
3151 iter = &req->io->rw.iter;
3152
3153 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003154 if (ret < 0)
3155 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003156 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003157 io_size = ret;
3158 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003159 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003160
Jens Axboefd6c2e42019-12-18 12:19:41 -07003161 /* Ensure we clear previously set non-block flag */
3162 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003163 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003164
Pavel Begunkov24c74672020-06-21 13:09:51 +03003165 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003166 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3167 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003168 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003169
Jens Axboe0fef9482020-08-26 10:36:20 -06003170 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003171 if (unlikely(ret))
3172 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003173
Jens Axboe227c0c92020-08-13 11:51:40 -06003174 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003175
Jens Axboe227c0c92020-08-13 11:51:40 -06003176 if (!ret) {
3177 goto done;
3178 } else if (ret == -EIOCBQUEUED) {
3179 ret = 0;
3180 goto out_free;
3181 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003182 /* IOPOLL retry should happen for io-wq threads */
3183 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003184 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003185 /* no retry on NONBLOCK marked file */
3186 if (req->file->f_flags & O_NONBLOCK)
3187 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003188 /* some cases will consume bytes even on error returns */
3189 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003190 ret = 0;
3191 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003192 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003193 /* make sure -ERESTARTSYS -> -EINTR is done */
3194 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003195 }
3196
3197 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003198 if (!iov_iter_count(iter) || !force_nonblock ||
3199 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003200 goto done;
3201
3202 io_size -= ret;
3203copy_iov:
3204 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3205 if (ret2) {
3206 ret = ret2;
3207 goto out_free;
3208 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003209 if (no_async)
3210 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003211 /* it's copied and will be cleaned with ->io */
3212 iovec = NULL;
3213 /* now use our persistent iterator, if we aren't already */
3214 iter = &req->io->rw.iter;
3215retry:
3216 req->io->rw.bytes_done += ret;
3217 /* if we can retry, do so with the callbacks armed */
3218 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003219 kiocb->ki_flags &= ~IOCB_WAITQ;
3220 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003221 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003222
3223 /*
3224 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3225 * get -EIOCBQUEUED, then we'll get a notification when the desired
3226 * page gets unlocked. We can also get a partial read here, and if we
3227 * do, then just retry at the new offset.
3228 */
3229 ret = io_iter_do_read(req, iter);
3230 if (ret == -EIOCBQUEUED) {
3231 ret = 0;
3232 goto out_free;
3233 } else if (ret > 0 && ret < io_size) {
3234 /* we got some bytes, but not all. retry. */
3235 goto retry;
3236 }
3237done:
3238 kiocb_done(kiocb, ret, cs);
3239 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003240out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003241 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003242 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003243 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003244 return ret;
3245}
3246
Jens Axboe3529d8c2019-12-19 18:24:38 -07003247static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3248 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003249{
3250 ssize_t ret;
3251
Jens Axboe3529d8c2019-12-19 18:24:38 -07003252 ret = io_prep_rw(req, sqe, force_nonblock);
3253 if (ret)
3254 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003255
Jens Axboe3529d8c2019-12-19 18:24:38 -07003256 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3257 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003258
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003259 /* either don't need iovec imported or already have it */
3260 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003261 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003262 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003263}
3264
Jens Axboea1d7c392020-06-22 11:09:46 -06003265static int io_write(struct io_kiocb *req, bool force_nonblock,
3266 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003267{
3268 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003269 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003270 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003271 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003272 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003273
Jens Axboeff6165b2020-08-13 09:47:43 -06003274 if (req->io)
3275 iter = &req->io->rw.iter;
3276
3277 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003278 if (ret < 0)
3279 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003280 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003281 io_size = ret;
3282 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003283
Jens Axboefd6c2e42019-12-18 12:19:41 -07003284 /* Ensure we clear previously set non-block flag */
3285 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003286 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003287
Pavel Begunkov24c74672020-06-21 13:09:51 +03003288 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003289 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003290 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003291
Jens Axboe10d59342019-12-09 20:16:22 -07003292 /* file path doesn't support NOWAIT for non-direct_IO */
3293 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3294 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003295 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003296
Jens Axboe0fef9482020-08-26 10:36:20 -06003297 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003298 if (unlikely(ret))
3299 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003300
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003301 /*
3302 * Open-code file_start_write here to grab freeze protection,
3303 * which will be released by another thread in
3304 * io_complete_rw(). Fool lockdep by telling it the lock got
3305 * released so that it doesn't complain about the held lock when
3306 * we return to userspace.
3307 */
3308 if (req->flags & REQ_F_ISREG) {
3309 __sb_start_write(file_inode(req->file)->i_sb,
3310 SB_FREEZE_WRITE, true);
3311 __sb_writers_release(file_inode(req->file)->i_sb,
3312 SB_FREEZE_WRITE);
3313 }
3314 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003315
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003316 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003317 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003318 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003319 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003320 else
3321 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003322
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003323 /*
3324 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3325 * retry them without IOCB_NOWAIT.
3326 */
3327 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3328 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003329 /* no retry on NONBLOCK marked file */
3330 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3331 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003332 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003333 /* IOPOLL retry should happen for io-wq threads */
3334 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3335 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003336done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003337 kiocb_done(kiocb, ret2, cs);
3338 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003339copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003340 /* some cases will consume bytes even on error returns */
3341 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003342 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003343 if (!ret)
3344 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003345 }
Jens Axboe31b51512019-01-18 22:56:34 -07003346out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003347 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003348 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003349 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003350 return ret;
3351}
3352
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003353static int __io_splice_prep(struct io_kiocb *req,
3354 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003355{
3356 struct io_splice* sp = &req->splice;
3357 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3358 int ret;
3359
3360 if (req->flags & REQ_F_NEED_CLEANUP)
3361 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003362 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3363 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003364
3365 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003366 sp->len = READ_ONCE(sqe->len);
3367 sp->flags = READ_ONCE(sqe->splice_flags);
3368
3369 if (unlikely(sp->flags & ~valid_flags))
3370 return -EINVAL;
3371
3372 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3373 (sp->flags & SPLICE_F_FD_IN_FIXED));
3374 if (ret)
3375 return ret;
3376 req->flags |= REQ_F_NEED_CLEANUP;
3377
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003378 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3379 /*
3380 * Splice operation will be punted aync, and here need to
3381 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3382 */
3383 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003384 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003385 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003386
3387 return 0;
3388}
3389
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003390static int io_tee_prep(struct io_kiocb *req,
3391 const struct io_uring_sqe *sqe)
3392{
3393 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3394 return -EINVAL;
3395 return __io_splice_prep(req, sqe);
3396}
3397
3398static int io_tee(struct io_kiocb *req, bool force_nonblock)
3399{
3400 struct io_splice *sp = &req->splice;
3401 struct file *in = sp->file_in;
3402 struct file *out = sp->file_out;
3403 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3404 long ret = 0;
3405
3406 if (force_nonblock)
3407 return -EAGAIN;
3408 if (sp->len)
3409 ret = do_tee(in, out, sp->len, flags);
3410
3411 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3412 req->flags &= ~REQ_F_NEED_CLEANUP;
3413
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003414 if (ret != sp->len)
3415 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003416 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003417 return 0;
3418}
3419
3420static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3421{
3422 struct io_splice* sp = &req->splice;
3423
3424 sp->off_in = READ_ONCE(sqe->splice_off_in);
3425 sp->off_out = READ_ONCE(sqe->off);
3426 return __io_splice_prep(req, sqe);
3427}
3428
Pavel Begunkov014db002020-03-03 21:33:12 +03003429static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003430{
3431 struct io_splice *sp = &req->splice;
3432 struct file *in = sp->file_in;
3433 struct file *out = sp->file_out;
3434 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3435 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003436 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003437
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003438 if (force_nonblock)
3439 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003440
3441 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3442 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003443
Jens Axboe948a7742020-05-17 14:21:38 -06003444 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003445 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003446
3447 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3448 req->flags &= ~REQ_F_NEED_CLEANUP;
3449
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003450 if (ret != sp->len)
3451 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003452 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003453 return 0;
3454}
3455
Jens Axboe2b188cc2019-01-07 10:46:33 -07003456/*
3457 * IORING_OP_NOP just posts a completion event, nothing else.
3458 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003459static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003460{
3461 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003462
Jens Axboedef596e2019-01-09 08:59:42 -07003463 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3464 return -EINVAL;
3465
Jens Axboe229a7b62020-06-22 10:13:11 -06003466 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003467 return 0;
3468}
3469
Jens Axboe3529d8c2019-12-19 18:24:38 -07003470static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003471{
Jens Axboe6b063142019-01-10 22:13:58 -07003472 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003473
Jens Axboe09bb8392019-03-13 12:39:28 -06003474 if (!req->file)
3475 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003476
Jens Axboe6b063142019-01-10 22:13:58 -07003477 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003478 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003479 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003480 return -EINVAL;
3481
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003482 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3483 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3484 return -EINVAL;
3485
3486 req->sync.off = READ_ONCE(sqe->off);
3487 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003488 return 0;
3489}
3490
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003491static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003492{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003493 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003494 int ret;
3495
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003496 /* fsync always requires a blocking context */
3497 if (force_nonblock)
3498 return -EAGAIN;
3499
Jens Axboe9adbd452019-12-20 08:45:55 -07003500 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003501 end > 0 ? end : LLONG_MAX,
3502 req->sync.flags & IORING_FSYNC_DATASYNC);
3503 if (ret < 0)
3504 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003505 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003506 return 0;
3507}
3508
Jens Axboed63d1b52019-12-10 10:38:56 -07003509static int io_fallocate_prep(struct io_kiocb *req,
3510 const struct io_uring_sqe *sqe)
3511{
3512 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3513 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003514 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3515 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003516
3517 req->sync.off = READ_ONCE(sqe->off);
3518 req->sync.len = READ_ONCE(sqe->addr);
3519 req->sync.mode = READ_ONCE(sqe->len);
3520 return 0;
3521}
3522
Pavel Begunkov014db002020-03-03 21:33:12 +03003523static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003524{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003525 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003526
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003527 /* fallocate always requiring blocking context */
3528 if (force_nonblock)
3529 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003530 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3531 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003532 if (ret < 0)
3533 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003534 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003535 return 0;
3536}
3537
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003538static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003539{
Jens Axboef8748882020-01-08 17:47:02 -07003540 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003541 int ret;
3542
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003543 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003544 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003545 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003546 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003547
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003548 /* open.how should be already initialised */
3549 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003550 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003551
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003552 req->open.dfd = READ_ONCE(sqe->fd);
3553 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003554 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003555 if (IS_ERR(req->open.filename)) {
3556 ret = PTR_ERR(req->open.filename);
3557 req->open.filename = NULL;
3558 return ret;
3559 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003560 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003561 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003562 return 0;
3563}
3564
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003565static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3566{
3567 u64 flags, mode;
3568
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003569 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3570 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003571 if (req->flags & REQ_F_NEED_CLEANUP)
3572 return 0;
3573 mode = READ_ONCE(sqe->len);
3574 flags = READ_ONCE(sqe->open_flags);
3575 req->open.how = build_open_how(flags, mode);
3576 return __io_openat_prep(req, sqe);
3577}
3578
Jens Axboecebdb982020-01-08 17:59:24 -07003579static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3580{
3581 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003582 size_t len;
3583 int ret;
3584
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003585 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3586 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003587 if (req->flags & REQ_F_NEED_CLEANUP)
3588 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003589 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3590 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003591 if (len < OPEN_HOW_SIZE_VER0)
3592 return -EINVAL;
3593
3594 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3595 len);
3596 if (ret)
3597 return ret;
3598
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003599 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003600}
3601
Pavel Begunkov014db002020-03-03 21:33:12 +03003602static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003603{
3604 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003605 struct file *file;
3606 int ret;
3607
Jens Axboef86cd202020-01-29 13:46:44 -07003608 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003609 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003610
Jens Axboecebdb982020-01-08 17:59:24 -07003611 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003612 if (ret)
3613 goto err;
3614
Jens Axboe4022e7a2020-03-19 19:23:18 -06003615 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003616 if (ret < 0)
3617 goto err;
3618
3619 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3620 if (IS_ERR(file)) {
3621 put_unused_fd(ret);
3622 ret = PTR_ERR(file);
3623 } else {
3624 fsnotify_open(file);
3625 fd_install(ret, file);
3626 }
3627err:
3628 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003629 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003630 if (ret < 0)
3631 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003632 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003633 return 0;
3634}
3635
Pavel Begunkov014db002020-03-03 21:33:12 +03003636static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003637{
Pavel Begunkov014db002020-03-03 21:33:12 +03003638 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003639}
3640
Jens Axboe067524e2020-03-02 16:32:28 -07003641static int io_remove_buffers_prep(struct io_kiocb *req,
3642 const struct io_uring_sqe *sqe)
3643{
3644 struct io_provide_buf *p = &req->pbuf;
3645 u64 tmp;
3646
3647 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3648 return -EINVAL;
3649
3650 tmp = READ_ONCE(sqe->fd);
3651 if (!tmp || tmp > USHRT_MAX)
3652 return -EINVAL;
3653
3654 memset(p, 0, sizeof(*p));
3655 p->nbufs = tmp;
3656 p->bgid = READ_ONCE(sqe->buf_group);
3657 return 0;
3658}
3659
3660static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3661 int bgid, unsigned nbufs)
3662{
3663 unsigned i = 0;
3664
3665 /* shouldn't happen */
3666 if (!nbufs)
3667 return 0;
3668
3669 /* the head kbuf is the list itself */
3670 while (!list_empty(&buf->list)) {
3671 struct io_buffer *nxt;
3672
3673 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3674 list_del(&nxt->list);
3675 kfree(nxt);
3676 if (++i == nbufs)
3677 return i;
3678 }
3679 i++;
3680 kfree(buf);
3681 idr_remove(&ctx->io_buffer_idr, bgid);
3682
3683 return i;
3684}
3685
Jens Axboe229a7b62020-06-22 10:13:11 -06003686static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3687 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003688{
3689 struct io_provide_buf *p = &req->pbuf;
3690 struct io_ring_ctx *ctx = req->ctx;
3691 struct io_buffer *head;
3692 int ret = 0;
3693
3694 io_ring_submit_lock(ctx, !force_nonblock);
3695
3696 lockdep_assert_held(&ctx->uring_lock);
3697
3698 ret = -ENOENT;
3699 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3700 if (head)
3701 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3702
3703 io_ring_submit_lock(ctx, !force_nonblock);
3704 if (ret < 0)
3705 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003706 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003707 return 0;
3708}
3709
Jens Axboeddf0322d2020-02-23 16:41:33 -07003710static int io_provide_buffers_prep(struct io_kiocb *req,
3711 const struct io_uring_sqe *sqe)
3712{
3713 struct io_provide_buf *p = &req->pbuf;
3714 u64 tmp;
3715
3716 if (sqe->ioprio || sqe->rw_flags)
3717 return -EINVAL;
3718
3719 tmp = READ_ONCE(sqe->fd);
3720 if (!tmp || tmp > USHRT_MAX)
3721 return -E2BIG;
3722 p->nbufs = tmp;
3723 p->addr = READ_ONCE(sqe->addr);
3724 p->len = READ_ONCE(sqe->len);
3725
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003726 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003727 return -EFAULT;
3728
3729 p->bgid = READ_ONCE(sqe->buf_group);
3730 tmp = READ_ONCE(sqe->off);
3731 if (tmp > USHRT_MAX)
3732 return -E2BIG;
3733 p->bid = tmp;
3734 return 0;
3735}
3736
3737static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3738{
3739 struct io_buffer *buf;
3740 u64 addr = pbuf->addr;
3741 int i, bid = pbuf->bid;
3742
3743 for (i = 0; i < pbuf->nbufs; i++) {
3744 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3745 if (!buf)
3746 break;
3747
3748 buf->addr = addr;
3749 buf->len = pbuf->len;
3750 buf->bid = bid;
3751 addr += pbuf->len;
3752 bid++;
3753 if (!*head) {
3754 INIT_LIST_HEAD(&buf->list);
3755 *head = buf;
3756 } else {
3757 list_add_tail(&buf->list, &(*head)->list);
3758 }
3759 }
3760
3761 return i ? i : -ENOMEM;
3762}
3763
Jens Axboe229a7b62020-06-22 10:13:11 -06003764static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3765 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003766{
3767 struct io_provide_buf *p = &req->pbuf;
3768 struct io_ring_ctx *ctx = req->ctx;
3769 struct io_buffer *head, *list;
3770 int ret = 0;
3771
3772 io_ring_submit_lock(ctx, !force_nonblock);
3773
3774 lockdep_assert_held(&ctx->uring_lock);
3775
3776 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3777
3778 ret = io_add_buffers(p, &head);
3779 if (ret < 0)
3780 goto out;
3781
3782 if (!list) {
3783 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3784 GFP_KERNEL);
3785 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003786 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003787 goto out;
3788 }
3789 }
3790out:
3791 io_ring_submit_unlock(ctx, !force_nonblock);
3792 if (ret < 0)
3793 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003794 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003795 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003796}
3797
Jens Axboe3e4827b2020-01-08 15:18:09 -07003798static int io_epoll_ctl_prep(struct io_kiocb *req,
3799 const struct io_uring_sqe *sqe)
3800{
3801#if defined(CONFIG_EPOLL)
3802 if (sqe->ioprio || sqe->buf_index)
3803 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003804 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003805 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003806
3807 req->epoll.epfd = READ_ONCE(sqe->fd);
3808 req->epoll.op = READ_ONCE(sqe->len);
3809 req->epoll.fd = READ_ONCE(sqe->off);
3810
3811 if (ep_op_has_event(req->epoll.op)) {
3812 struct epoll_event __user *ev;
3813
3814 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3815 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3816 return -EFAULT;
3817 }
3818
3819 return 0;
3820#else
3821 return -EOPNOTSUPP;
3822#endif
3823}
3824
Jens Axboe229a7b62020-06-22 10:13:11 -06003825static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3826 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003827{
3828#if defined(CONFIG_EPOLL)
3829 struct io_epoll *ie = &req->epoll;
3830 int ret;
3831
3832 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3833 if (force_nonblock && ret == -EAGAIN)
3834 return -EAGAIN;
3835
3836 if (ret < 0)
3837 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003838 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003839 return 0;
3840#else
3841 return -EOPNOTSUPP;
3842#endif
3843}
3844
Jens Axboec1ca7572019-12-25 22:18:28 -07003845static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3846{
3847#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3848 if (sqe->ioprio || sqe->buf_index || sqe->off)
3849 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003850 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3851 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003852
3853 req->madvise.addr = READ_ONCE(sqe->addr);
3854 req->madvise.len = READ_ONCE(sqe->len);
3855 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3856 return 0;
3857#else
3858 return -EOPNOTSUPP;
3859#endif
3860}
3861
Pavel Begunkov014db002020-03-03 21:33:12 +03003862static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003863{
3864#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3865 struct io_madvise *ma = &req->madvise;
3866 int ret;
3867
3868 if (force_nonblock)
3869 return -EAGAIN;
3870
3871 ret = do_madvise(ma->addr, ma->len, ma->advice);
3872 if (ret < 0)
3873 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003874 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003875 return 0;
3876#else
3877 return -EOPNOTSUPP;
3878#endif
3879}
3880
Jens Axboe4840e412019-12-25 22:03:45 -07003881static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3882{
3883 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3884 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003885 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3886 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003887
3888 req->fadvise.offset = READ_ONCE(sqe->off);
3889 req->fadvise.len = READ_ONCE(sqe->len);
3890 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3891 return 0;
3892}
3893
Pavel Begunkov014db002020-03-03 21:33:12 +03003894static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003895{
3896 struct io_fadvise *fa = &req->fadvise;
3897 int ret;
3898
Jens Axboe3e694262020-02-01 09:22:49 -07003899 if (force_nonblock) {
3900 switch (fa->advice) {
3901 case POSIX_FADV_NORMAL:
3902 case POSIX_FADV_RANDOM:
3903 case POSIX_FADV_SEQUENTIAL:
3904 break;
3905 default:
3906 return -EAGAIN;
3907 }
3908 }
Jens Axboe4840e412019-12-25 22:03:45 -07003909
3910 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3911 if (ret < 0)
3912 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003913 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003914 return 0;
3915}
3916
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003917static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3918{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003919 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003920 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003921 if (sqe->ioprio || sqe->buf_index)
3922 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003923 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003924 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003925
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003926 req->statx.dfd = READ_ONCE(sqe->fd);
3927 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003928 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003929 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3930 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003931
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003932 return 0;
3933}
3934
Pavel Begunkov014db002020-03-03 21:33:12 +03003935static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003936{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003937 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003938 int ret;
3939
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003940 if (force_nonblock) {
3941 /* only need file table for an actual valid fd */
3942 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3943 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003944 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003945 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003946
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003947 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3948 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003949
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003950 if (ret < 0)
3951 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003952 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003953 return 0;
3954}
3955
Jens Axboeb5dba592019-12-11 14:02:38 -07003956static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3957{
3958 /*
3959 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003960 * leave the 'file' in an undeterminate state, and here need to modify
3961 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003962 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003963 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003964 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3965
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003966 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3967 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003968 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3969 sqe->rw_flags || sqe->buf_index)
3970 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003971 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003972 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003973
3974 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003975 if ((req->file && req->file->f_op == &io_uring_fops) ||
3976 req->close.fd == req->ctx->ring_fd)
3977 return -EBADF;
3978
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003979 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003980 return 0;
3981}
3982
Jens Axboe229a7b62020-06-22 10:13:11 -06003983static int io_close(struct io_kiocb *req, bool force_nonblock,
3984 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003985{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003986 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003987 int ret;
3988
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003989 /* might be already done during nonblock submission */
3990 if (!close->put_file) {
3991 ret = __close_fd_get_file(close->fd, &close->put_file);
3992 if (ret < 0)
3993 return (ret == -ENOENT) ? -EBADF : ret;
3994 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003995
3996 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003997 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003998 /* was never set, but play safe */
3999 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004000 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004001 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004002 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004003 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004004
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004005 /* No ->flush() or already async, safely close from here */
4006 ret = filp_close(close->put_file, req->work.files);
4007 if (ret < 0)
4008 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004009 fput(close->put_file);
4010 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004011 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004012 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004013}
4014
Jens Axboe3529d8c2019-12-19 18:24:38 -07004015static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004016{
4017 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004018
4019 if (!req->file)
4020 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004021
4022 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4023 return -EINVAL;
4024 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4025 return -EINVAL;
4026
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004027 req->sync.off = READ_ONCE(sqe->off);
4028 req->sync.len = READ_ONCE(sqe->len);
4029 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004030 return 0;
4031}
4032
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004033static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004034{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004035 int ret;
4036
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004037 /* sync_file_range always requires a blocking context */
4038 if (force_nonblock)
4039 return -EAGAIN;
4040
Jens Axboe9adbd452019-12-20 08:45:55 -07004041 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004042 req->sync.flags);
4043 if (ret < 0)
4044 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004045 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004046 return 0;
4047}
4048
YueHaibing469956e2020-03-04 15:53:52 +08004049#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004050static int io_setup_async_msg(struct io_kiocb *req,
4051 struct io_async_msghdr *kmsg)
4052{
4053 if (req->io)
4054 return -EAGAIN;
4055 if (io_alloc_async_ctx(req)) {
4056 if (kmsg->iov != kmsg->fast_iov)
4057 kfree(kmsg->iov);
4058 return -ENOMEM;
4059 }
4060 req->flags |= REQ_F_NEED_CLEANUP;
4061 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4062 return -EAGAIN;
4063}
4064
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004065static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4066 struct io_async_msghdr *iomsg)
4067{
4068 iomsg->iov = iomsg->fast_iov;
4069 iomsg->msg.msg_name = &iomsg->addr;
4070 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4071 req->sr_msg.msg_flags, &iomsg->iov);
4072}
4073
Jens Axboe3529d8c2019-12-19 18:24:38 -07004074static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004075{
Jens Axboee47293f2019-12-20 08:58:21 -07004076 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004077 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004078 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004079
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004080 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4081 return -EINVAL;
4082
Jens Axboee47293f2019-12-20 08:58:21 -07004083 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004084 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004085 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004086
Jens Axboed8768362020-02-27 14:17:49 -07004087#ifdef CONFIG_COMPAT
4088 if (req->ctx->compat)
4089 sr->msg_flags |= MSG_CMSG_COMPAT;
4090#endif
4091
Jens Axboefddafac2020-01-04 20:19:44 -07004092 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004093 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004094 /* iovec is already imported */
4095 if (req->flags & REQ_F_NEED_CLEANUP)
4096 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004097
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004098 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004099 if (!ret)
4100 req->flags |= REQ_F_NEED_CLEANUP;
4101 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004102}
4103
Jens Axboe229a7b62020-06-22 10:13:11 -06004104static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4105 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004106{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004107 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004108 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004109 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004110 int ret;
4111
Jens Axboe03b12302019-12-02 18:50:25 -07004112 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004113 if (unlikely(!sock))
4114 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004115
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004116 if (req->io) {
4117 kmsg = &req->io->msg;
4118 kmsg->msg.msg_name = &req->io->msg.addr;
4119 /* if iov is set, it's allocated already */
4120 if (!kmsg->iov)
4121 kmsg->iov = kmsg->fast_iov;
4122 kmsg->msg.msg_iter.iov = kmsg->iov;
4123 } else {
4124 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004125 if (ret)
4126 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004127 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004128 }
4129
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004130 flags = req->sr_msg.msg_flags;
4131 if (flags & MSG_DONTWAIT)
4132 req->flags |= REQ_F_NOWAIT;
4133 else if (force_nonblock)
4134 flags |= MSG_DONTWAIT;
4135
4136 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4137 if (force_nonblock && ret == -EAGAIN)
4138 return io_setup_async_msg(req, kmsg);
4139 if (ret == -ERESTARTSYS)
4140 ret = -EINTR;
4141
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004142 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004143 kfree(kmsg->iov);
4144 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004145 if (ret < 0)
4146 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004147 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004148 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004149}
4150
Jens Axboe229a7b62020-06-22 10:13:11 -06004151static int io_send(struct io_kiocb *req, bool force_nonblock,
4152 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004153{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004154 struct io_sr_msg *sr = &req->sr_msg;
4155 struct msghdr msg;
4156 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004157 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004158 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004159 int ret;
4160
4161 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004162 if (unlikely(!sock))
4163 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004164
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004165 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4166 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004167 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004168
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004169 msg.msg_name = NULL;
4170 msg.msg_control = NULL;
4171 msg.msg_controllen = 0;
4172 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004173
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004174 flags = req->sr_msg.msg_flags;
4175 if (flags & MSG_DONTWAIT)
4176 req->flags |= REQ_F_NOWAIT;
4177 else if (force_nonblock)
4178 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004179
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004180 msg.msg_flags = flags;
4181 ret = sock_sendmsg(sock, &msg);
4182 if (force_nonblock && ret == -EAGAIN)
4183 return -EAGAIN;
4184 if (ret == -ERESTARTSYS)
4185 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004186
Jens Axboe03b12302019-12-02 18:50:25 -07004187 if (ret < 0)
4188 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004189 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004190 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004191}
4192
Pavel Begunkov1400e692020-07-12 20:41:05 +03004193static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4194 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004195{
4196 struct io_sr_msg *sr = &req->sr_msg;
4197 struct iovec __user *uiov;
4198 size_t iov_len;
4199 int ret;
4200
Pavel Begunkov1400e692020-07-12 20:41:05 +03004201 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4202 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004203 if (ret)
4204 return ret;
4205
4206 if (req->flags & REQ_F_BUFFER_SELECT) {
4207 if (iov_len > 1)
4208 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004209 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004210 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004211 sr->len = iomsg->iov[0].iov_len;
4212 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004213 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004214 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004215 } else {
4216 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004217 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004218 if (ret > 0)
4219 ret = 0;
4220 }
4221
4222 return ret;
4223}
4224
4225#ifdef CONFIG_COMPAT
4226static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004227 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004228{
4229 struct compat_msghdr __user *msg_compat;
4230 struct io_sr_msg *sr = &req->sr_msg;
4231 struct compat_iovec __user *uiov;
4232 compat_uptr_t ptr;
4233 compat_size_t len;
4234 int ret;
4235
Pavel Begunkov270a5942020-07-12 20:41:04 +03004236 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004237 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004238 &ptr, &len);
4239 if (ret)
4240 return ret;
4241
4242 uiov = compat_ptr(ptr);
4243 if (req->flags & REQ_F_BUFFER_SELECT) {
4244 compat_ssize_t clen;
4245
4246 if (len > 1)
4247 return -EINVAL;
4248 if (!access_ok(uiov, sizeof(*uiov)))
4249 return -EFAULT;
4250 if (__get_user(clen, &uiov->iov_len))
4251 return -EFAULT;
4252 if (clen < 0)
4253 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004254 sr->len = iomsg->iov[0].iov_len;
4255 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004256 } else {
4257 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004258 &iomsg->iov,
4259 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004260 if (ret < 0)
4261 return ret;
4262 }
4263
4264 return 0;
4265}
Jens Axboe03b12302019-12-02 18:50:25 -07004266#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004267
Pavel Begunkov1400e692020-07-12 20:41:05 +03004268static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4269 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004270{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004271 iomsg->msg.msg_name = &iomsg->addr;
4272 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004273
4274#ifdef CONFIG_COMPAT
4275 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004276 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004277#endif
4278
Pavel Begunkov1400e692020-07-12 20:41:05 +03004279 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004280}
4281
Jens Axboebcda7ba2020-02-23 16:42:51 -07004282static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004283 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004284{
4285 struct io_sr_msg *sr = &req->sr_msg;
4286 struct io_buffer *kbuf;
4287
Jens Axboebcda7ba2020-02-23 16:42:51 -07004288 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4289 if (IS_ERR(kbuf))
4290 return kbuf;
4291
4292 sr->kbuf = kbuf;
4293 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004294 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004295}
4296
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004297static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4298{
4299 return io_put_kbuf(req, req->sr_msg.kbuf);
4300}
4301
Jens Axboe3529d8c2019-12-19 18:24:38 -07004302static int io_recvmsg_prep(struct io_kiocb *req,
4303 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004304{
Jens Axboee47293f2019-12-20 08:58:21 -07004305 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004306 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004307 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004308
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004309 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4310 return -EINVAL;
4311
Jens Axboe3529d8c2019-12-19 18:24:38 -07004312 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004313 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004314 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004315 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004316
Jens Axboed8768362020-02-27 14:17:49 -07004317#ifdef CONFIG_COMPAT
4318 if (req->ctx->compat)
4319 sr->msg_flags |= MSG_CMSG_COMPAT;
4320#endif
4321
Jens Axboefddafac2020-01-04 20:19:44 -07004322 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004323 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004324 /* iovec is already imported */
4325 if (req->flags & REQ_F_NEED_CLEANUP)
4326 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004327
Pavel Begunkov1400e692020-07-12 20:41:05 +03004328 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004329 if (!ret)
4330 req->flags |= REQ_F_NEED_CLEANUP;
4331 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004332}
4333
Jens Axboe229a7b62020-06-22 10:13:11 -06004334static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4335 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004336{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004337 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004338 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004339 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004340 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004341 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004342
Jens Axboe0fa03c62019-04-19 13:34:07 -06004343 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004344 if (unlikely(!sock))
4345 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004346
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004347 if (req->io) {
4348 kmsg = &req->io->msg;
4349 kmsg->msg.msg_name = &req->io->msg.addr;
4350 /* if iov is set, it's allocated already */
4351 if (!kmsg->iov)
4352 kmsg->iov = kmsg->fast_iov;
4353 kmsg->msg.msg_iter.iov = kmsg->iov;
4354 } else {
4355 ret = io_recvmsg_copy_hdr(req, &iomsg);
4356 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004357 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004358 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004359 }
4360
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004361 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004362 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004363 if (IS_ERR(kbuf))
4364 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004365 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4366 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4367 1, req->sr_msg.len);
4368 }
4369
4370 flags = req->sr_msg.msg_flags;
4371 if (flags & MSG_DONTWAIT)
4372 req->flags |= REQ_F_NOWAIT;
4373 else if (force_nonblock)
4374 flags |= MSG_DONTWAIT;
4375
4376 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4377 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004378 if (force_nonblock && ret == -EAGAIN)
4379 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004380 if (ret == -ERESTARTSYS)
4381 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004382
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004383 if (req->flags & REQ_F_BUFFER_SELECTED)
4384 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004385 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004386 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004387 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004388 if (ret < 0)
4389 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004390 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004391 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004392}
4393
Jens Axboe229a7b62020-06-22 10:13:11 -06004394static int io_recv(struct io_kiocb *req, bool force_nonblock,
4395 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004396{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004397 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004398 struct io_sr_msg *sr = &req->sr_msg;
4399 struct msghdr msg;
4400 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004401 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004402 struct iovec iov;
4403 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004404 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004405
Jens Axboefddafac2020-01-04 20:19:44 -07004406 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004407 if (unlikely(!sock))
4408 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004409
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004410 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004411 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004412 if (IS_ERR(kbuf))
4413 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004414 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004415 }
4416
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004417 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004418 if (unlikely(ret))
4419 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004420
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004421 msg.msg_name = NULL;
4422 msg.msg_control = NULL;
4423 msg.msg_controllen = 0;
4424 msg.msg_namelen = 0;
4425 msg.msg_iocb = NULL;
4426 msg.msg_flags = 0;
4427
4428 flags = req->sr_msg.msg_flags;
4429 if (flags & MSG_DONTWAIT)
4430 req->flags |= REQ_F_NOWAIT;
4431 else if (force_nonblock)
4432 flags |= MSG_DONTWAIT;
4433
4434 ret = sock_recvmsg(sock, &msg, flags);
4435 if (force_nonblock && ret == -EAGAIN)
4436 return -EAGAIN;
4437 if (ret == -ERESTARTSYS)
4438 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004439out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004440 if (req->flags & REQ_F_BUFFER_SELECTED)
4441 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004442 if (ret < 0)
4443 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004444 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004445 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004446}
4447
Jens Axboe3529d8c2019-12-19 18:24:38 -07004448static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004449{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004450 struct io_accept *accept = &req->accept;
4451
Jens Axboe17f2fe32019-10-17 14:42:58 -06004452 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4453 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004454 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004455 return -EINVAL;
4456
Jens Axboed55e5f52019-12-11 16:12:15 -07004457 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4458 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004459 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004460 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004461 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004462}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004463
Jens Axboe229a7b62020-06-22 10:13:11 -06004464static int io_accept(struct io_kiocb *req, bool force_nonblock,
4465 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004466{
4467 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004468 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004469 int ret;
4470
Jiufei Xuee697dee2020-06-10 13:41:59 +08004471 if (req->file->f_flags & O_NONBLOCK)
4472 req->flags |= REQ_F_NOWAIT;
4473
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004474 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004475 accept->addr_len, accept->flags,
4476 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004477 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004478 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004479 if (ret < 0) {
4480 if (ret == -ERESTARTSYS)
4481 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004482 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004483 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004484 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004485 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004486}
4487
Jens Axboe3529d8c2019-12-19 18:24:38 -07004488static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004489{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004490 struct io_connect *conn = &req->connect;
4491 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004492
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004493 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4494 return -EINVAL;
4495 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4496 return -EINVAL;
4497
Jens Axboe3529d8c2019-12-19 18:24:38 -07004498 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4499 conn->addr_len = READ_ONCE(sqe->addr2);
4500
4501 if (!io)
4502 return 0;
4503
4504 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004505 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004506}
4507
Jens Axboe229a7b62020-06-22 10:13:11 -06004508static int io_connect(struct io_kiocb *req, bool force_nonblock,
4509 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004510{
Jens Axboef499a022019-12-02 16:28:46 -07004511 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004512 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004513 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004514
Jens Axboef499a022019-12-02 16:28:46 -07004515 if (req->io) {
4516 io = req->io;
4517 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004518 ret = move_addr_to_kernel(req->connect.addr,
4519 req->connect.addr_len,
4520 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004521 if (ret)
4522 goto out;
4523 io = &__io;
4524 }
4525
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004526 file_flags = force_nonblock ? O_NONBLOCK : 0;
4527
4528 ret = __sys_connect_file(req->file, &io->connect.address,
4529 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004530 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004531 if (req->io)
4532 return -EAGAIN;
4533 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004534 ret = -ENOMEM;
4535 goto out;
4536 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004537 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004538 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004539 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004540 if (ret == -ERESTARTSYS)
4541 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004542out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004543 if (ret < 0)
4544 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004545 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004546 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004547}
YueHaibing469956e2020-03-04 15:53:52 +08004548#else /* !CONFIG_NET */
4549static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4550{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004551 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004552}
4553
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004554static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4555 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004556{
YueHaibing469956e2020-03-04 15:53:52 +08004557 return -EOPNOTSUPP;
4558}
4559
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004560static int io_send(struct io_kiocb *req, bool force_nonblock,
4561 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004562{
4563 return -EOPNOTSUPP;
4564}
4565
4566static int io_recvmsg_prep(struct io_kiocb *req,
4567 const struct io_uring_sqe *sqe)
4568{
4569 return -EOPNOTSUPP;
4570}
4571
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004572static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4573 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004574{
4575 return -EOPNOTSUPP;
4576}
4577
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004578static int io_recv(struct io_kiocb *req, bool force_nonblock,
4579 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004580{
4581 return -EOPNOTSUPP;
4582}
4583
4584static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4585{
4586 return -EOPNOTSUPP;
4587}
4588
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004589static int io_accept(struct io_kiocb *req, bool force_nonblock,
4590 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004591{
4592 return -EOPNOTSUPP;
4593}
4594
4595static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4596{
4597 return -EOPNOTSUPP;
4598}
4599
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004600static int io_connect(struct io_kiocb *req, bool force_nonblock,
4601 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004602{
4603 return -EOPNOTSUPP;
4604}
4605#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004606
Jens Axboed7718a92020-02-14 22:23:12 -07004607struct io_poll_table {
4608 struct poll_table_struct pt;
4609 struct io_kiocb *req;
4610 int error;
4611};
4612
Jens Axboed7718a92020-02-14 22:23:12 -07004613static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4614 __poll_t mask, task_work_func_t func)
4615{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004616 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004617 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004618
4619 /* for instances that support it check for an event match first: */
4620 if (mask && !(mask & poll->events))
4621 return 0;
4622
4623 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4624
4625 list_del_init(&poll->wait.entry);
4626
Jens Axboed7718a92020-02-14 22:23:12 -07004627 req->result = mask;
4628 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004629 percpu_ref_get(&req->ctx->refs);
4630
Jens Axboed7718a92020-02-14 22:23:12 -07004631 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004632 * If we using the signalfd wait_queue_head for this wakeup, then
4633 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4634 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4635 * either, as the normal wakeup will suffice.
4636 */
4637 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4638
4639 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004640 * If this fails, then the task is exiting. When a task exits, the
4641 * work gets canceled, so just cancel this request as well instead
4642 * of executing it. We can't safely execute it anyway, as we may not
4643 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004644 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004645 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004646 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004647 struct task_struct *tsk;
4648
Jens Axboee3aabf92020-05-18 11:04:17 -06004649 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004650 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004651 task_work_add(tsk, &req->task_work, 0);
4652 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004653 }
Jens Axboed7718a92020-02-14 22:23:12 -07004654 return 1;
4655}
4656
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004657static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4658 __acquires(&req->ctx->completion_lock)
4659{
4660 struct io_ring_ctx *ctx = req->ctx;
4661
4662 if (!req->result && !READ_ONCE(poll->canceled)) {
4663 struct poll_table_struct pt = { ._key = poll->events };
4664
4665 req->result = vfs_poll(req->file, &pt) & poll->events;
4666 }
4667
4668 spin_lock_irq(&ctx->completion_lock);
4669 if (!req->result && !READ_ONCE(poll->canceled)) {
4670 add_wait_queue(poll->head, &poll->wait);
4671 return true;
4672 }
4673
4674 return false;
4675}
4676
Jens Axboed4e7cd32020-08-15 11:44:50 -07004677static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004678{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004679 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4680 if (req->opcode == IORING_OP_POLL_ADD)
4681 return (struct io_poll_iocb *) req->io;
4682 return req->apoll->double_poll;
4683}
4684
4685static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4686{
4687 if (req->opcode == IORING_OP_POLL_ADD)
4688 return &req->poll;
4689 return &req->apoll->poll;
4690}
4691
4692static void io_poll_remove_double(struct io_kiocb *req)
4693{
4694 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004695
4696 lockdep_assert_held(&req->ctx->completion_lock);
4697
4698 if (poll && poll->head) {
4699 struct wait_queue_head *head = poll->head;
4700
4701 spin_lock(&head->lock);
4702 list_del_init(&poll->wait.entry);
4703 if (poll->wait.private)
4704 refcount_dec(&req->refs);
4705 poll->head = NULL;
4706 spin_unlock(&head->lock);
4707 }
4708}
4709
4710static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4711{
4712 struct io_ring_ctx *ctx = req->ctx;
4713
Jens Axboed4e7cd32020-08-15 11:44:50 -07004714 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004715 req->poll.done = true;
4716 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4717 io_commit_cqring(ctx);
4718}
4719
4720static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4721{
4722 struct io_ring_ctx *ctx = req->ctx;
4723
4724 if (io_poll_rewait(req, &req->poll)) {
4725 spin_unlock_irq(&ctx->completion_lock);
4726 return;
4727 }
4728
4729 hash_del(&req->hash_node);
4730 io_poll_complete(req, req->result, 0);
4731 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004732 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004733 spin_unlock_irq(&ctx->completion_lock);
4734
4735 io_cqring_ev_posted(ctx);
4736}
4737
4738static void io_poll_task_func(struct callback_head *cb)
4739{
4740 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004741 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004742 struct io_kiocb *nxt = NULL;
4743
4744 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004745 if (nxt)
4746 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004747 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004748}
4749
4750static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4751 int sync, void *key)
4752{
4753 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004754 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004755 __poll_t mask = key_to_poll(key);
4756
4757 /* for instances that support it check for an event match first: */
4758 if (mask && !(mask & poll->events))
4759 return 0;
4760
Jens Axboe8706e042020-09-28 08:38:54 -06004761 list_del_init(&wait->entry);
4762
Jens Axboe807abcb2020-07-17 17:09:27 -06004763 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004764 bool done;
4765
Jens Axboe807abcb2020-07-17 17:09:27 -06004766 spin_lock(&poll->head->lock);
4767 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004768 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004769 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004770 /* make sure double remove sees this as being gone */
4771 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004772 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004773 if (!done)
4774 __io_async_wake(req, poll, mask, io_poll_task_func);
4775 }
4776 refcount_dec(&req->refs);
4777 return 1;
4778}
4779
4780static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4781 wait_queue_func_t wake_func)
4782{
4783 poll->head = NULL;
4784 poll->done = false;
4785 poll->canceled = false;
4786 poll->events = events;
4787 INIT_LIST_HEAD(&poll->wait.entry);
4788 init_waitqueue_func_entry(&poll->wait, wake_func);
4789}
4790
4791static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004792 struct wait_queue_head *head,
4793 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004794{
4795 struct io_kiocb *req = pt->req;
4796
4797 /*
4798 * If poll->head is already set, it's because the file being polled
4799 * uses multiple waitqueues for poll handling (eg one for read, one
4800 * for write). Setup a separate io_poll_iocb if this happens.
4801 */
4802 if (unlikely(poll->head)) {
4803 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004804 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004805 pt->error = -EINVAL;
4806 return;
4807 }
4808 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4809 if (!poll) {
4810 pt->error = -ENOMEM;
4811 return;
4812 }
4813 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4814 refcount_inc(&req->refs);
4815 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004816 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004817 }
4818
4819 pt->error = 0;
4820 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004821
4822 if (poll->events & EPOLLEXCLUSIVE)
4823 add_wait_queue_exclusive(head, &poll->wait);
4824 else
4825 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004826}
4827
4828static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4829 struct poll_table_struct *p)
4830{
4831 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004832 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004833
Jens Axboe807abcb2020-07-17 17:09:27 -06004834 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004835}
4836
Jens Axboed7718a92020-02-14 22:23:12 -07004837static void io_async_task_func(struct callback_head *cb)
4838{
4839 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4840 struct async_poll *apoll = req->apoll;
4841 struct io_ring_ctx *ctx = req->ctx;
4842
4843 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4844
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004845 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004846 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004847 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004848 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004849 }
4850
Jens Axboe31067252020-05-17 17:43:31 -06004851 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004852 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004853 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004854
Jens Axboed4e7cd32020-08-15 11:44:50 -07004855 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004856 spin_unlock_irq(&ctx->completion_lock);
4857
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004858 if (!READ_ONCE(apoll->poll.canceled))
4859 __io_req_task_submit(req);
4860 else
4861 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004862
Jens Axboe6d816e02020-08-11 08:04:14 -06004863 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004864 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004865 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004866}
4867
4868static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4869 void *key)
4870{
4871 struct io_kiocb *req = wait->private;
4872 struct io_poll_iocb *poll = &req->apoll->poll;
4873
4874 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4875 key_to_poll(key));
4876
4877 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4878}
4879
4880static void io_poll_req_insert(struct io_kiocb *req)
4881{
4882 struct io_ring_ctx *ctx = req->ctx;
4883 struct hlist_head *list;
4884
4885 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4886 hlist_add_head(&req->hash_node, list);
4887}
4888
4889static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4890 struct io_poll_iocb *poll,
4891 struct io_poll_table *ipt, __poll_t mask,
4892 wait_queue_func_t wake_func)
4893 __acquires(&ctx->completion_lock)
4894{
4895 struct io_ring_ctx *ctx = req->ctx;
4896 bool cancel = false;
4897
Jens Axboe18bceab2020-05-15 11:56:54 -06004898 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004899 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004900 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004901
4902 ipt->pt._key = mask;
4903 ipt->req = req;
4904 ipt->error = -EINVAL;
4905
Jens Axboed7718a92020-02-14 22:23:12 -07004906 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4907
4908 spin_lock_irq(&ctx->completion_lock);
4909 if (likely(poll->head)) {
4910 spin_lock(&poll->head->lock);
4911 if (unlikely(list_empty(&poll->wait.entry))) {
4912 if (ipt->error)
4913 cancel = true;
4914 ipt->error = 0;
4915 mask = 0;
4916 }
4917 if (mask || ipt->error)
4918 list_del_init(&poll->wait.entry);
4919 else if (cancel)
4920 WRITE_ONCE(poll->canceled, true);
4921 else if (!poll->done) /* actually waiting for an event */
4922 io_poll_req_insert(req);
4923 spin_unlock(&poll->head->lock);
4924 }
4925
4926 return mask;
4927}
4928
4929static bool io_arm_poll_handler(struct io_kiocb *req)
4930{
4931 const struct io_op_def *def = &io_op_defs[req->opcode];
4932 struct io_ring_ctx *ctx = req->ctx;
4933 struct async_poll *apoll;
4934 struct io_poll_table ipt;
4935 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004936 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004937
4938 if (!req->file || !file_can_poll(req->file))
4939 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004940 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004941 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004942 if (def->pollin)
4943 rw = READ;
4944 else if (def->pollout)
4945 rw = WRITE;
4946 else
4947 return false;
4948 /* if we can't nonblock try, then no point in arming a poll handler */
4949 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004950 return false;
4951
4952 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4953 if (unlikely(!apoll))
4954 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004955 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004956
4957 req->flags |= REQ_F_POLLED;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004958 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004959 req->apoll = apoll;
4960 INIT_HLIST_NODE(&req->hash_node);
4961
Nathan Chancellor8755d972020-03-02 16:01:19 -07004962 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004963 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004964 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004965 if (def->pollout)
4966 mask |= POLLOUT | POLLWRNORM;
4967 mask |= POLLERR | POLLPRI;
4968
4969 ipt.pt._qproc = io_async_queue_proc;
4970
4971 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4972 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06004973 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07004974 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004975 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06004976 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004977 kfree(apoll);
4978 return false;
4979 }
4980 spin_unlock_irq(&ctx->completion_lock);
4981 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4982 apoll->poll.events);
4983 return true;
4984}
4985
4986static bool __io_poll_remove_one(struct io_kiocb *req,
4987 struct io_poll_iocb *poll)
4988{
Jens Axboeb41e9852020-02-17 09:52:41 -07004989 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004990
4991 spin_lock(&poll->head->lock);
4992 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004993 if (!list_empty(&poll->wait.entry)) {
4994 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004995 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004996 }
4997 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004998 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004999 return do_complete;
5000}
5001
5002static bool io_poll_remove_one(struct io_kiocb *req)
5003{
5004 bool do_complete;
5005
Jens Axboed4e7cd32020-08-15 11:44:50 -07005006 io_poll_remove_double(req);
5007
Jens Axboed7718a92020-02-14 22:23:12 -07005008 if (req->opcode == IORING_OP_POLL_ADD) {
5009 do_complete = __io_poll_remove_one(req, &req->poll);
5010 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005011 struct async_poll *apoll = req->apoll;
5012
Jens Axboed7718a92020-02-14 22:23:12 -07005013 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005014 do_complete = __io_poll_remove_one(req, &apoll->poll);
5015 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005016 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005017 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005018 kfree(apoll);
5019 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005020 }
5021
Jens Axboeb41e9852020-02-17 09:52:41 -07005022 if (do_complete) {
5023 io_cqring_fill_event(req, -ECANCELED);
5024 io_commit_cqring(req->ctx);
5025 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005026 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005027 io_put_req(req);
5028 }
5029
5030 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005031}
5032
Jens Axboef3606e32020-09-22 08:18:24 -06005033static void io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005034{
Jens Axboe78076bb2019-12-04 19:56:40 -07005035 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005036 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005037 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005038
5039 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005040 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5041 struct hlist_head *list;
5042
5043 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005044 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5045 if (io_task_match(req, tsk))
5046 posted += io_poll_remove_one(req);
5047 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005048 }
5049 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005050
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005051 if (posted)
5052 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005053}
5054
Jens Axboe47f46762019-11-09 17:43:02 -07005055static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5056{
Jens Axboe78076bb2019-12-04 19:56:40 -07005057 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005058 struct io_kiocb *req;
5059
Jens Axboe78076bb2019-12-04 19:56:40 -07005060 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5061 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005062 if (sqe_addr != req->user_data)
5063 continue;
5064 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005065 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005066 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005067 }
5068
5069 return -ENOENT;
5070}
5071
Jens Axboe3529d8c2019-12-19 18:24:38 -07005072static int io_poll_remove_prep(struct io_kiocb *req,
5073 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005074{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005075 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5076 return -EINVAL;
5077 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5078 sqe->poll_events)
5079 return -EINVAL;
5080
Jens Axboe0969e782019-12-17 18:40:57 -07005081 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005082 return 0;
5083}
5084
5085/*
5086 * Find a running poll command that matches one specified in sqe->addr,
5087 * and remove it if found.
5088 */
5089static int io_poll_remove(struct io_kiocb *req)
5090{
5091 struct io_ring_ctx *ctx = req->ctx;
5092 u64 addr;
5093 int ret;
5094
Jens Axboe0969e782019-12-17 18:40:57 -07005095 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005096 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005097 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005098 spin_unlock_irq(&ctx->completion_lock);
5099
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005100 if (ret < 0)
5101 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005102 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005103 return 0;
5104}
5105
Jens Axboe221c5eb2019-01-17 09:41:58 -07005106static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5107 void *key)
5108{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005109 struct io_kiocb *req = wait->private;
5110 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005111
Jens Axboed7718a92020-02-14 22:23:12 -07005112 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005113}
5114
Jens Axboe221c5eb2019-01-17 09:41:58 -07005115static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5116 struct poll_table_struct *p)
5117{
5118 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5119
Jens Axboe807abcb2020-07-17 17:09:27 -06005120 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005121}
5122
Jens Axboe3529d8c2019-12-19 18:24:38 -07005123static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005124{
5125 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005126 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005127
5128 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5129 return -EINVAL;
5130 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5131 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005132 if (!poll->file)
5133 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005134
Jiufei Xue5769a352020-06-17 17:53:55 +08005135 events = READ_ONCE(sqe->poll32_events);
5136#ifdef __BIG_ENDIAN
5137 events = swahw32(events);
5138#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005139 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5140 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005141
Pavel Begunkov4dd28242020-06-15 10:33:13 +03005142 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07005143 return 0;
5144}
5145
Pavel Begunkov014db002020-03-03 21:33:12 +03005146static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005147{
5148 struct io_poll_iocb *poll = &req->poll;
5149 struct io_ring_ctx *ctx = req->ctx;
5150 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005151 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005152
Jens Axboe78076bb2019-12-04 19:56:40 -07005153 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005154 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005155
Jens Axboed7718a92020-02-14 22:23:12 -07005156 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5157 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005158
Jens Axboe8c838782019-03-12 15:48:16 -06005159 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005160 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005161 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005162 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005163 spin_unlock_irq(&ctx->completion_lock);
5164
Jens Axboe8c838782019-03-12 15:48:16 -06005165 if (mask) {
5166 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005167 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005168 }
Jens Axboe8c838782019-03-12 15:48:16 -06005169 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005170}
5171
Jens Axboe5262f562019-09-17 12:26:57 -06005172static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5173{
Jens Axboead8a48a2019-11-15 08:49:11 -07005174 struct io_timeout_data *data = container_of(timer,
5175 struct io_timeout_data, timer);
5176 struct io_kiocb *req = data->req;
5177 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005178 unsigned long flags;
5179
Jens Axboe5262f562019-09-17 12:26:57 -06005180 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005181 atomic_set(&req->ctx->cq_timeouts,
5182 atomic_read(&req->ctx->cq_timeouts) + 1);
5183
zhangyi (F)ef036812019-10-23 15:10:08 +08005184 /*
Jens Axboe11365042019-10-16 09:08:32 -06005185 * We could be racing with timeout deletion. If the list is empty,
5186 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005187 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005188 if (!list_empty(&req->timeout.list))
5189 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005190
Jens Axboe78e19bb2019-11-06 15:21:34 -07005191 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005192 io_commit_cqring(ctx);
5193 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5194
5195 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005196 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005197 io_put_req(req);
5198 return HRTIMER_NORESTART;
5199}
5200
Jens Axboef254ac02020-08-12 17:33:30 -06005201static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005202{
Jens Axboef254ac02020-08-12 17:33:30 -06005203 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005204
Jens Axboef254ac02020-08-12 17:33:30 -06005205 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005206
Jens Axboe2d283902019-12-04 11:08:05 -07005207 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005208 if (ret == -1)
5209 return -EALREADY;
5210
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005211 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005212 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005213 io_cqring_fill_event(req, -ECANCELED);
5214 io_put_req(req);
5215 return 0;
5216}
5217
Jens Axboef254ac02020-08-12 17:33:30 -06005218static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5219{
5220 struct io_kiocb *req;
5221 int ret = -ENOENT;
5222
5223 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5224 if (user_data == req->user_data) {
5225 ret = 0;
5226 break;
5227 }
5228 }
5229
5230 if (ret == -ENOENT)
5231 return ret;
5232
5233 return __io_timeout_cancel(req);
5234}
5235
Jens Axboe3529d8c2019-12-19 18:24:38 -07005236static int io_timeout_remove_prep(struct io_kiocb *req,
5237 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005238{
Jens Axboeb29472e2019-12-17 18:50:29 -07005239 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5240 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005241 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5242 return -EINVAL;
5243 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005244 return -EINVAL;
5245
5246 req->timeout.addr = READ_ONCE(sqe->addr);
5247 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5248 if (req->timeout.flags)
5249 return -EINVAL;
5250
Jens Axboeb29472e2019-12-17 18:50:29 -07005251 return 0;
5252}
5253
Jens Axboe11365042019-10-16 09:08:32 -06005254/*
5255 * Remove or update an existing timeout command
5256 */
Jens Axboefc4df992019-12-10 14:38:45 -07005257static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005258{
5259 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005260 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005261
Jens Axboe11365042019-10-16 09:08:32 -06005262 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005263 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005264
Jens Axboe47f46762019-11-09 17:43:02 -07005265 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005266 io_commit_cqring(ctx);
5267 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005268 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005269 if (ret < 0)
5270 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005271 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005272 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005273}
5274
Jens Axboe3529d8c2019-12-19 18:24:38 -07005275static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005276 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005277{
Jens Axboead8a48a2019-11-15 08:49:11 -07005278 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005279 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005280 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005281
Jens Axboead8a48a2019-11-15 08:49:11 -07005282 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005283 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005284 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005285 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005286 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005287 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005288 flags = READ_ONCE(sqe->timeout_flags);
5289 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005290 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005291
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005292 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005293
Jens Axboe3529d8c2019-12-19 18:24:38 -07005294 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005295 return -ENOMEM;
5296
5297 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005298 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005299
5300 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005301 return -EFAULT;
5302
Jens Axboe11365042019-10-16 09:08:32 -06005303 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005304 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005305 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005306 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005307
Jens Axboead8a48a2019-11-15 08:49:11 -07005308 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5309 return 0;
5310}
5311
Jens Axboefc4df992019-12-10 14:38:45 -07005312static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005313{
Jens Axboead8a48a2019-11-15 08:49:11 -07005314 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005315 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005316 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005317 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005318
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005319 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005320
Jens Axboe5262f562019-09-17 12:26:57 -06005321 /*
5322 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005323 * timeout event to be satisfied. If it isn't set, then this is
5324 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005325 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005326 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005327 entry = ctx->timeout_list.prev;
5328 goto add;
5329 }
Jens Axboe5262f562019-09-17 12:26:57 -06005330
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005331 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5332 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005333
5334 /*
5335 * Insertion sort, ensuring the first entry in the list is always
5336 * the one we need first.
5337 */
Jens Axboe5262f562019-09-17 12:26:57 -06005338 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005339 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5340 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005341
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005342 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005343 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005344 /* nxt.seq is behind @tail, otherwise would've been completed */
5345 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005346 break;
5347 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005348add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005349 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005350 data->timer.function = io_timeout_fn;
5351 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005352 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005353 return 0;
5354}
5355
Jens Axboe62755e32019-10-28 21:49:21 -06005356static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005357{
Jens Axboe62755e32019-10-28 21:49:21 -06005358 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005359
Jens Axboe62755e32019-10-28 21:49:21 -06005360 return req->user_data == (unsigned long) data;
5361}
5362
Jens Axboee977d6d2019-11-05 12:39:45 -07005363static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005364{
Jens Axboe62755e32019-10-28 21:49:21 -06005365 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005366 int ret = 0;
5367
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005368 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005369 switch (cancel_ret) {
5370 case IO_WQ_CANCEL_OK:
5371 ret = 0;
5372 break;
5373 case IO_WQ_CANCEL_RUNNING:
5374 ret = -EALREADY;
5375 break;
5376 case IO_WQ_CANCEL_NOTFOUND:
5377 ret = -ENOENT;
5378 break;
5379 }
5380
Jens Axboee977d6d2019-11-05 12:39:45 -07005381 return ret;
5382}
5383
Jens Axboe47f46762019-11-09 17:43:02 -07005384static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5385 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005386 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005387{
5388 unsigned long flags;
5389 int ret;
5390
5391 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5392 if (ret != -ENOENT) {
5393 spin_lock_irqsave(&ctx->completion_lock, flags);
5394 goto done;
5395 }
5396
5397 spin_lock_irqsave(&ctx->completion_lock, flags);
5398 ret = io_timeout_cancel(ctx, sqe_addr);
5399 if (ret != -ENOENT)
5400 goto done;
5401 ret = io_poll_cancel(ctx, sqe_addr);
5402done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005403 if (!ret)
5404 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005405 io_cqring_fill_event(req, ret);
5406 io_commit_cqring(ctx);
5407 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5408 io_cqring_ev_posted(ctx);
5409
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005410 if (ret < 0)
5411 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005412 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005413}
5414
Jens Axboe3529d8c2019-12-19 18:24:38 -07005415static int io_async_cancel_prep(struct io_kiocb *req,
5416 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005417{
Jens Axboefbf23842019-12-17 18:45:56 -07005418 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005419 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005420 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5421 return -EINVAL;
5422 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005423 return -EINVAL;
5424
Jens Axboefbf23842019-12-17 18:45:56 -07005425 req->cancel.addr = READ_ONCE(sqe->addr);
5426 return 0;
5427}
5428
Pavel Begunkov014db002020-03-03 21:33:12 +03005429static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005430{
5431 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005432
Pavel Begunkov014db002020-03-03 21:33:12 +03005433 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005434 return 0;
5435}
5436
Jens Axboe05f3fb32019-12-09 11:22:50 -07005437static int io_files_update_prep(struct io_kiocb *req,
5438 const struct io_uring_sqe *sqe)
5439{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005440 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5441 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005442 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5443 return -EINVAL;
5444 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005445 return -EINVAL;
5446
5447 req->files_update.offset = READ_ONCE(sqe->off);
5448 req->files_update.nr_args = READ_ONCE(sqe->len);
5449 if (!req->files_update.nr_args)
5450 return -EINVAL;
5451 req->files_update.arg = READ_ONCE(sqe->addr);
5452 return 0;
5453}
5454
Jens Axboe229a7b62020-06-22 10:13:11 -06005455static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5456 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005457{
5458 struct io_ring_ctx *ctx = req->ctx;
5459 struct io_uring_files_update up;
5460 int ret;
5461
Jens Axboef86cd202020-01-29 13:46:44 -07005462 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005463 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005464
5465 up.offset = req->files_update.offset;
5466 up.fds = req->files_update.arg;
5467
5468 mutex_lock(&ctx->uring_lock);
5469 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5470 mutex_unlock(&ctx->uring_lock);
5471
5472 if (ret < 0)
5473 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005474 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005475 return 0;
5476}
5477
Jens Axboe3529d8c2019-12-19 18:24:38 -07005478static int io_req_defer_prep(struct io_kiocb *req,
5479 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005480{
Jens Axboee7815732019-12-17 19:45:06 -07005481 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005482
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005483 if (!sqe)
5484 return 0;
5485
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005486 if (io_alloc_async_ctx(req))
5487 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005488 ret = io_prep_work_files(req);
5489 if (unlikely(ret))
5490 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005491
Jens Axboe202700e12020-09-12 13:18:10 -06005492 io_prep_async_work(req);
5493
Jens Axboed625c6e2019-12-17 19:53:05 -07005494 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005495 case IORING_OP_NOP:
5496 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005497 case IORING_OP_READV:
5498 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005499 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005500 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005501 break;
5502 case IORING_OP_WRITEV:
5503 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005504 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005505 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005506 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005507 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005508 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005509 break;
5510 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005511 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005512 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005513 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005514 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005515 break;
5516 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005517 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005518 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005519 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005520 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005521 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005522 break;
5523 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005524 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005525 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005526 break;
Jens Axboef499a022019-12-02 16:28:46 -07005527 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005528 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005529 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005530 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005531 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005532 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005533 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005534 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005535 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005536 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005537 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005538 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005539 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005540 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005541 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005542 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005543 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005544 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005545 case IORING_OP_FALLOCATE:
5546 ret = io_fallocate_prep(req, sqe);
5547 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005548 case IORING_OP_OPENAT:
5549 ret = io_openat_prep(req, sqe);
5550 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005551 case IORING_OP_CLOSE:
5552 ret = io_close_prep(req, sqe);
5553 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005554 case IORING_OP_FILES_UPDATE:
5555 ret = io_files_update_prep(req, sqe);
5556 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005557 case IORING_OP_STATX:
5558 ret = io_statx_prep(req, sqe);
5559 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005560 case IORING_OP_FADVISE:
5561 ret = io_fadvise_prep(req, sqe);
5562 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005563 case IORING_OP_MADVISE:
5564 ret = io_madvise_prep(req, sqe);
5565 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005566 case IORING_OP_OPENAT2:
5567 ret = io_openat2_prep(req, sqe);
5568 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005569 case IORING_OP_EPOLL_CTL:
5570 ret = io_epoll_ctl_prep(req, sqe);
5571 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005572 case IORING_OP_SPLICE:
5573 ret = io_splice_prep(req, sqe);
5574 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005575 case IORING_OP_PROVIDE_BUFFERS:
5576 ret = io_provide_buffers_prep(req, sqe);
5577 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005578 case IORING_OP_REMOVE_BUFFERS:
5579 ret = io_remove_buffers_prep(req, sqe);
5580 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005581 case IORING_OP_TEE:
5582 ret = io_tee_prep(req, sqe);
5583 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005584 default:
Jens Axboee7815732019-12-17 19:45:06 -07005585 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5586 req->opcode);
5587 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005588 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005589 }
5590
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005591 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005592}
5593
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005594static u32 io_get_sequence(struct io_kiocb *req)
5595{
5596 struct io_kiocb *pos;
5597 struct io_ring_ctx *ctx = req->ctx;
5598 u32 total_submitted, nr_reqs = 1;
5599
5600 if (req->flags & REQ_F_LINK_HEAD)
5601 list_for_each_entry(pos, &req->link_list, link_list)
5602 nr_reqs++;
5603
5604 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5605 return total_submitted - nr_reqs;
5606}
5607
Jens Axboe3529d8c2019-12-19 18:24:38 -07005608static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005609{
Jackie Liua197f662019-11-08 08:09:12 -07005610 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005611 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005612 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005613 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005614
Bob Liu9d858b22019-11-13 18:06:25 +08005615 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005616 if (likely(list_empty_careful(&ctx->defer_list) &&
5617 !(req->flags & REQ_F_IO_DRAIN)))
5618 return 0;
5619
5620 seq = io_get_sequence(req);
5621 /* Still a chance to pass the sequence check */
5622 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005623 return 0;
5624
Pavel Begunkov650b5482020-05-17 14:02:11 +03005625 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005626 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005627 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005628 return ret;
5629 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005630 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005631 de = kmalloc(sizeof(*de), GFP_KERNEL);
5632 if (!de)
5633 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005634
Jens Axboede0617e2019-04-06 21:51:27 -06005635 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005636 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005637 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005638 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005639 io_queue_async_work(req);
5640 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005641 }
5642
Jens Axboe915967f2019-11-21 09:01:20 -07005643 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005644 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005645 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005646 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005647 spin_unlock_irq(&ctx->completion_lock);
5648 return -EIOCBQUEUED;
5649}
5650
Jens Axboef573d382020-09-22 10:19:24 -06005651static void io_req_drop_files(struct io_kiocb *req)
5652{
5653 struct io_ring_ctx *ctx = req->ctx;
5654 unsigned long flags;
5655
5656 spin_lock_irqsave(&ctx->inflight_lock, flags);
5657 list_del(&req->inflight_entry);
5658 if (waitqueue_active(&ctx->inflight_wait))
5659 wake_up(&ctx->inflight_wait);
5660 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5661 req->flags &= ~REQ_F_INFLIGHT;
5662 req->work.files = NULL;
5663}
5664
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005665static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005666{
5667 struct io_async_ctx *io = req->io;
5668
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005669 if (req->flags & REQ_F_BUFFER_SELECTED) {
5670 switch (req->opcode) {
5671 case IORING_OP_READV:
5672 case IORING_OP_READ_FIXED:
5673 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005674 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005675 break;
5676 case IORING_OP_RECVMSG:
5677 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005678 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005679 break;
5680 }
5681 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005682 }
5683
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005684 if (req->flags & REQ_F_NEED_CLEANUP) {
5685 switch (req->opcode) {
5686 case IORING_OP_READV:
5687 case IORING_OP_READ_FIXED:
5688 case IORING_OP_READ:
5689 case IORING_OP_WRITEV:
5690 case IORING_OP_WRITE_FIXED:
5691 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005692 if (io->rw.free_iovec)
5693 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005694 break;
5695 case IORING_OP_RECVMSG:
5696 case IORING_OP_SENDMSG:
5697 if (io->msg.iov != io->msg.fast_iov)
5698 kfree(io->msg.iov);
5699 break;
5700 case IORING_OP_SPLICE:
5701 case IORING_OP_TEE:
5702 io_put_file(req, req->splice.file_in,
5703 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5704 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005705 case IORING_OP_OPENAT:
5706 case IORING_OP_OPENAT2:
5707 if (req->open.filename)
5708 putname(req->open.filename);
5709 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005710 }
5711 req->flags &= ~REQ_F_NEED_CLEANUP;
5712 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005713
Jens Axboef573d382020-09-22 10:19:24 -06005714 if (req->flags & REQ_F_INFLIGHT)
5715 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005716}
5717
Jens Axboe3529d8c2019-12-19 18:24:38 -07005718static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005719 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005720{
Jackie Liua197f662019-11-08 08:09:12 -07005721 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005722 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005723
Jens Axboed625c6e2019-12-17 19:53:05 -07005724 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005725 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005726 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005727 break;
5728 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005729 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005730 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005731 if (sqe) {
5732 ret = io_read_prep(req, sqe, force_nonblock);
5733 if (ret < 0)
5734 break;
5735 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005736 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005737 break;
5738 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005739 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005740 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005741 if (sqe) {
5742 ret = io_write_prep(req, sqe, force_nonblock);
5743 if (ret < 0)
5744 break;
5745 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005746 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005747 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005748 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005749 if (sqe) {
5750 ret = io_prep_fsync(req, sqe);
5751 if (ret < 0)
5752 break;
5753 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005754 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005755 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005756 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005757 if (sqe) {
5758 ret = io_poll_add_prep(req, sqe);
5759 if (ret)
5760 break;
5761 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005762 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005763 break;
5764 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005765 if (sqe) {
5766 ret = io_poll_remove_prep(req, sqe);
5767 if (ret < 0)
5768 break;
5769 }
Jens Axboefc4df992019-12-10 14:38:45 -07005770 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005771 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005772 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005773 if (sqe) {
5774 ret = io_prep_sfr(req, sqe);
5775 if (ret < 0)
5776 break;
5777 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005778 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005779 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005780 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005781 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005782 if (sqe) {
5783 ret = io_sendmsg_prep(req, sqe);
5784 if (ret < 0)
5785 break;
5786 }
Jens Axboefddafac2020-01-04 20:19:44 -07005787 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005788 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005789 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005790 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005791 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005792 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005793 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005794 if (sqe) {
5795 ret = io_recvmsg_prep(req, sqe);
5796 if (ret)
5797 break;
5798 }
Jens Axboefddafac2020-01-04 20:19:44 -07005799 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005800 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005801 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005802 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005803 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005804 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005805 if (sqe) {
5806 ret = io_timeout_prep(req, sqe, false);
5807 if (ret)
5808 break;
5809 }
Jens Axboefc4df992019-12-10 14:38:45 -07005810 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005811 break;
Jens Axboe11365042019-10-16 09:08:32 -06005812 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005813 if (sqe) {
5814 ret = io_timeout_remove_prep(req, sqe);
5815 if (ret)
5816 break;
5817 }
Jens Axboefc4df992019-12-10 14:38:45 -07005818 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005819 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005820 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005821 if (sqe) {
5822 ret = io_accept_prep(req, sqe);
5823 if (ret)
5824 break;
5825 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005826 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005827 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005828 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005829 if (sqe) {
5830 ret = io_connect_prep(req, sqe);
5831 if (ret)
5832 break;
5833 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005834 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005835 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005836 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005837 if (sqe) {
5838 ret = io_async_cancel_prep(req, sqe);
5839 if (ret)
5840 break;
5841 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005842 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005843 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005844 case IORING_OP_FALLOCATE:
5845 if (sqe) {
5846 ret = io_fallocate_prep(req, sqe);
5847 if (ret)
5848 break;
5849 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005850 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005851 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005852 case IORING_OP_OPENAT:
5853 if (sqe) {
5854 ret = io_openat_prep(req, sqe);
5855 if (ret)
5856 break;
5857 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005858 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005859 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005860 case IORING_OP_CLOSE:
5861 if (sqe) {
5862 ret = io_close_prep(req, sqe);
5863 if (ret)
5864 break;
5865 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005866 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005867 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005868 case IORING_OP_FILES_UPDATE:
5869 if (sqe) {
5870 ret = io_files_update_prep(req, sqe);
5871 if (ret)
5872 break;
5873 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005874 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005875 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005876 case IORING_OP_STATX:
5877 if (sqe) {
5878 ret = io_statx_prep(req, sqe);
5879 if (ret)
5880 break;
5881 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005882 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005883 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005884 case IORING_OP_FADVISE:
5885 if (sqe) {
5886 ret = io_fadvise_prep(req, sqe);
5887 if (ret)
5888 break;
5889 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005890 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005891 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005892 case IORING_OP_MADVISE:
5893 if (sqe) {
5894 ret = io_madvise_prep(req, sqe);
5895 if (ret)
5896 break;
5897 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005898 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005899 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005900 case IORING_OP_OPENAT2:
5901 if (sqe) {
5902 ret = io_openat2_prep(req, sqe);
5903 if (ret)
5904 break;
5905 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005906 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005907 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005908 case IORING_OP_EPOLL_CTL:
5909 if (sqe) {
5910 ret = io_epoll_ctl_prep(req, sqe);
5911 if (ret)
5912 break;
5913 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005914 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005915 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005916 case IORING_OP_SPLICE:
5917 if (sqe) {
5918 ret = io_splice_prep(req, sqe);
5919 if (ret < 0)
5920 break;
5921 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005922 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005923 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005924 case IORING_OP_PROVIDE_BUFFERS:
5925 if (sqe) {
5926 ret = io_provide_buffers_prep(req, sqe);
5927 if (ret)
5928 break;
5929 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005930 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005931 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005932 case IORING_OP_REMOVE_BUFFERS:
5933 if (sqe) {
5934 ret = io_remove_buffers_prep(req, sqe);
5935 if (ret)
5936 break;
5937 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005938 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005939 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005940 case IORING_OP_TEE:
5941 if (sqe) {
5942 ret = io_tee_prep(req, sqe);
5943 if (ret < 0)
5944 break;
5945 }
5946 ret = io_tee(req, force_nonblock);
5947 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005948 default:
5949 ret = -EINVAL;
5950 break;
5951 }
5952
5953 if (ret)
5954 return ret;
5955
Jens Axboeb5325762020-05-19 21:20:27 -06005956 /* If the op doesn't have a file, we're not polling for it */
5957 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005958 const bool in_async = io_wq_current_is_worker();
5959
Jens Axboe11ba8202020-01-15 21:51:17 -07005960 /* workqueue context doesn't hold uring_lock, grab it now */
5961 if (in_async)
5962 mutex_lock(&ctx->uring_lock);
5963
Jens Axboe2b188cc2019-01-07 10:46:33 -07005964 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005965
5966 if (in_async)
5967 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005968 }
5969
5970 return 0;
5971}
5972
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005973static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005974{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005975 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005976 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005977 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005978
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005979 timeout = io_prep_linked_timeout(req);
5980 if (timeout)
5981 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005982
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005983 /* if NO_CANCEL is set, we must still run the work */
5984 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5985 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005986 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005987 }
Jens Axboe31b51512019-01-18 22:56:34 -07005988
Jens Axboe561fb042019-10-24 07:25:42 -06005989 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005990 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005991 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005992 /*
5993 * We can get EAGAIN for polled IO even though we're
5994 * forcing a sync submission from here, since we can't
5995 * wait for request slots on the block side.
5996 */
5997 if (ret != -EAGAIN)
5998 break;
5999 cond_resched();
6000 } while (1);
6001 }
Jens Axboe31b51512019-01-18 22:56:34 -07006002
Jens Axboe561fb042019-10-24 07:25:42 -06006003 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006004 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006005 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006006 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006007
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006008 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006009}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006010
Jens Axboe65e19f52019-10-26 07:20:21 -06006011static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6012 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006013{
Jens Axboe65e19f52019-10-26 07:20:21 -06006014 struct fixed_file_table *table;
6015
Jens Axboe05f3fb32019-12-09 11:22:50 -07006016 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006017 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006018}
6019
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006020static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6021 int fd, struct file **out_file, bool fixed)
6022{
6023 struct io_ring_ctx *ctx = req->ctx;
6024 struct file *file;
6025
6026 if (fixed) {
6027 if (unlikely(!ctx->file_data ||
6028 (unsigned) fd >= ctx->nr_user_files))
6029 return -EBADF;
6030 fd = array_index_nospec(fd, ctx->nr_user_files);
6031 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006032 if (file) {
6033 req->fixed_file_refs = ctx->file_data->cur_refs;
6034 percpu_ref_get(req->fixed_file_refs);
6035 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006036 } else {
6037 trace_io_uring_file_get(ctx, fd);
6038 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006039 }
6040
Jens Axboefd2206e2020-06-02 16:40:47 -06006041 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6042 *out_file = file;
6043 return 0;
6044 }
6045 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006046}
6047
Jens Axboe3529d8c2019-12-19 18:24:38 -07006048static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006049 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006050{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006051 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006052
Jens Axboe63ff8222020-05-07 14:56:15 -06006053 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006054 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006055 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006056
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006057 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006058}
6059
Jackie Liua197f662019-11-08 08:09:12 -07006060static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006061{
Jens Axboefcb323c2019-10-24 12:39:47 -06006062 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07006063 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006064
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006065 io_req_init_async(req);
6066
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006067 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006068 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006069 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07006070 return -EBADF;
6071
Jens Axboefcb323c2019-10-24 12:39:47 -06006072 rcu_read_lock();
6073 spin_lock_irq(&ctx->inflight_lock);
6074 /*
6075 * We use the f_ops->flush() handler to ensure that we can flush
6076 * out work accessing these files if the fd is closed. Check if
6077 * the fd has changed since we started down this path, and disallow
6078 * this operation if it has.
6079 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006080 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006081 list_add(&req->inflight_entry, &ctx->inflight_list);
6082 req->flags |= REQ_F_INFLIGHT;
6083 req->work.files = current->files;
6084 ret = 0;
6085 }
6086 spin_unlock_irq(&ctx->inflight_lock);
6087 rcu_read_unlock();
6088
6089 return ret;
6090}
6091
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006092static inline int io_prep_work_files(struct io_kiocb *req)
6093{
6094 if (!io_op_defs[req->opcode].file_table)
6095 return 0;
6096 return io_grab_files(req);
6097}
6098
Jens Axboe2665abf2019-11-05 12:40:47 -07006099static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6100{
Jens Axboead8a48a2019-11-15 08:49:11 -07006101 struct io_timeout_data *data = container_of(timer,
6102 struct io_timeout_data, timer);
6103 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006104 struct io_ring_ctx *ctx = req->ctx;
6105 struct io_kiocb *prev = NULL;
6106 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006107
6108 spin_lock_irqsave(&ctx->completion_lock, flags);
6109
6110 /*
6111 * We don't expect the list to be empty, that will only happen if we
6112 * race with the completion of the linked work.
6113 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006114 if (!list_empty(&req->link_list)) {
6115 prev = list_entry(req->link_list.prev, struct io_kiocb,
6116 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006117 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006118 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006119 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6120 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006121 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006122 }
6123
6124 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6125
6126 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006127 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006128 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006129 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006130 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006131 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006132 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006133 return HRTIMER_NORESTART;
6134}
6135
Jens Axboe7271ef32020-08-10 09:55:22 -06006136static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006137{
Jens Axboe76a46e02019-11-10 23:34:16 -07006138 /*
6139 * If the list is now empty, then our linked request finished before
6140 * we got a chance to setup the timer
6141 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006142 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006143 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006144
Jens Axboead8a48a2019-11-15 08:49:11 -07006145 data->timer.function = io_link_timeout_fn;
6146 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6147 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006148 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006149}
6150
6151static void io_queue_linked_timeout(struct io_kiocb *req)
6152{
6153 struct io_ring_ctx *ctx = req->ctx;
6154
6155 spin_lock_irq(&ctx->completion_lock);
6156 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006157 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006158
Jens Axboe2665abf2019-11-05 12:40:47 -07006159 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006160 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006161}
6162
Jens Axboead8a48a2019-11-15 08:49:11 -07006163static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006164{
6165 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006166
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006167 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006168 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006169 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006170 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006171
Pavel Begunkov44932332019-12-05 16:16:35 +03006172 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6173 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006174 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006175 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006176
Jens Axboe76a46e02019-11-10 23:34:16 -07006177 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006178 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006179}
6180
Jens Axboef13fad72020-06-22 09:34:30 -06006181static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6182 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006183{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006184 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006185 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006186 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006187 int ret;
6188
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006189again:
6190 linked_timeout = io_prep_linked_timeout(req);
6191
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006192 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6193 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006194 if (old_creds)
6195 revert_creds(old_creds);
6196 if (old_creds == req->work.creds)
6197 old_creds = NULL; /* restored original creds */
6198 else
6199 old_creds = override_creds(req->work.creds);
6200 }
6201
Jens Axboef13fad72020-06-22 09:34:30 -06006202 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006203
6204 /*
6205 * We async punt it if the file wasn't marked NOWAIT, or if the file
6206 * doesn't support non-blocking read/write attempts
6207 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006208 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006209 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006210punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006211 ret = io_prep_work_files(req);
6212 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006213 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006214 /*
6215 * Queued up for async execution, worker will release
6216 * submit reference when the iocb is actually submitted.
6217 */
6218 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006219 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006220
Pavel Begunkovf063c542020-07-25 14:41:59 +03006221 if (linked_timeout)
6222 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006223 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006224 }
Jens Axboee65ef562019-03-12 10:16:44 -06006225
Pavel Begunkov652532a2020-07-03 22:15:07 +03006226 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006227err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006228 /* un-prep timeout, so it'll be killed as any other linked */
6229 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006230 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006231 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006232 io_req_complete(req, ret);
6233 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006234 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006235
Jens Axboe6c271ce2019-01-10 11:22:30 -07006236 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006237 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006238 if (linked_timeout)
6239 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006240
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006241 if (nxt) {
6242 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006243
6244 if (req->flags & REQ_F_FORCE_ASYNC)
6245 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006246 goto again;
6247 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006248exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006249 if (old_creds)
6250 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006251}
6252
Jens Axboef13fad72020-06-22 09:34:30 -06006253static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6254 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006255{
6256 int ret;
6257
Jens Axboe3529d8c2019-12-19 18:24:38 -07006258 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006259 if (ret) {
6260 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006261fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006262 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006263 io_put_req(req);
6264 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006265 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006266 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006267 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006268 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006269 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006270 goto fail_req;
6271 }
6272
Jens Axboece35a472019-12-17 08:04:44 -07006273 /*
6274 * Never try inline submit of IOSQE_ASYNC is set, go straight
6275 * to async execution.
6276 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006277 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006278 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6279 io_queue_async_work(req);
6280 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006281 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006282 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006283}
6284
Jens Axboef13fad72020-06-22 09:34:30 -06006285static inline void io_queue_link_head(struct io_kiocb *req,
6286 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006287{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006288 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006289 io_put_req(req);
6290 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006291 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006292 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006293}
6294
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006295static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006296 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006297{
Jackie Liua197f662019-11-08 08:09:12 -07006298 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006299 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006300
Jens Axboe9e645e112019-05-10 16:07:28 -06006301 /*
6302 * If we already have a head request, queue this one for async
6303 * submittal once the head completes. If we don't have a head but
6304 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6305 * submitted sync once the chain is complete. If none of those
6306 * conditions are true (normal request), then just queue it.
6307 */
6308 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006309 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006310
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006311 /*
6312 * Taking sequential execution of a link, draining both sides
6313 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6314 * requests in the link. So, it drains the head and the
6315 * next after the link request. The last one is done via
6316 * drain_next flag to persist the effect across calls.
6317 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006318 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006319 head->flags |= REQ_F_IO_DRAIN;
6320 ctx->drain_next = 1;
6321 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006322 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006323 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006324 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006325 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006326 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006327 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006328 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006329 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006330 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006331
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006332 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006333 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006334 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006335 *link = NULL;
6336 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006337 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006338 if (unlikely(ctx->drain_next)) {
6339 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006340 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006341 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006342 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006343 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006344 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006345
Pavel Begunkov711be032020-01-17 03:57:59 +03006346 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006347 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006348 req->flags |= REQ_F_FAIL_LINK;
6349 *link = req;
6350 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006351 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006352 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006353 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006354
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006355 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006356}
6357
Jens Axboe9a56a232019-01-09 09:06:50 -07006358/*
6359 * Batched submission is done, ensure local IO is flushed out.
6360 */
6361static void io_submit_state_end(struct io_submit_state *state)
6362{
Jens Axboef13fad72020-06-22 09:34:30 -06006363 if (!list_empty(&state->comp.list))
6364 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006365 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006366 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006367 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006368 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006369}
6370
6371/*
6372 * Start submission side cache.
6373 */
6374static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006375 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006376{
6377 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006378 state->comp.nr = 0;
6379 INIT_LIST_HEAD(&state->comp.list);
6380 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006381 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006382 state->file = NULL;
6383 state->ios_left = max_ios;
6384}
6385
Jens Axboe2b188cc2019-01-07 10:46:33 -07006386static void io_commit_sqring(struct io_ring_ctx *ctx)
6387{
Hristo Venev75b28af2019-08-26 17:23:46 +00006388 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006389
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006390 /*
6391 * Ensure any loads from the SQEs are done at this point,
6392 * since once we write the new head, the application could
6393 * write new data to them.
6394 */
6395 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006396}
6397
6398/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006399 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006400 * that is mapped by userspace. This means that care needs to be taken to
6401 * ensure that reads are stable, as we cannot rely on userspace always
6402 * being a good citizen. If members of the sqe are validated and then later
6403 * used, it's important that those reads are done through READ_ONCE() to
6404 * prevent a re-load down the line.
6405 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006406static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006407{
Hristo Venev75b28af2019-08-26 17:23:46 +00006408 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006409 unsigned head;
6410
6411 /*
6412 * The cached sq head (or cq tail) serves two purposes:
6413 *
6414 * 1) allows us to batch the cost of updating the user visible
6415 * head updates.
6416 * 2) allows the kernel side to track the head on its own, even
6417 * though the application is the one updating it.
6418 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006419 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006420 if (likely(head < ctx->sq_entries))
6421 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006422
6423 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006424 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006425 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006426 return NULL;
6427}
6428
6429static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6430{
6431 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006432}
6433
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006434#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6435 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6436 IOSQE_BUFFER_SELECT)
6437
6438static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6439 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006440 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006441{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006442 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006443 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006444
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006445 req->opcode = READ_ONCE(sqe->opcode);
6446 req->user_data = READ_ONCE(sqe->user_data);
6447 req->io = NULL;
6448 req->file = NULL;
6449 req->ctx = ctx;
6450 req->flags = 0;
6451 /* one is dropped after submission, the other at completion */
6452 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006453 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006454 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006455
6456 if (unlikely(req->opcode >= IORING_OP_LAST))
6457 return -EINVAL;
6458
Jens Axboe9d8426a2020-06-16 18:42:49 -06006459 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6460 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006461
6462 sqe_flags = READ_ONCE(sqe->flags);
6463 /* enforce forwards compatibility on users */
6464 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6465 return -EINVAL;
6466
6467 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6468 !io_op_defs[req->opcode].buffer_select)
6469 return -EOPNOTSUPP;
6470
6471 id = READ_ONCE(sqe->personality);
6472 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006473 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006474 req->work.creds = idr_find(&ctx->personality_idr, id);
6475 if (unlikely(!req->work.creds))
6476 return -EINVAL;
6477 get_cred(req->work.creds);
6478 }
6479
6480 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006481 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006482
Jens Axboe63ff8222020-05-07 14:56:15 -06006483 if (!io_op_defs[req->opcode].needs_file)
6484 return 0;
6485
6486 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006487}
6488
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006489static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006490 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006491{
Jens Axboeac8691c2020-06-01 08:30:41 -06006492 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006493 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006494 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006495
Jens Axboec4a2ed72019-11-21 21:01:26 -07006496 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006497 if (test_bit(0, &ctx->sq_check_overflow)) {
6498 if (!list_empty(&ctx->cq_overflow_list) &&
6499 !io_cqring_overflow_flush(ctx, false))
6500 return -EBUSY;
6501 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006502
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006503 /* make sure SQ entry isn't read before tail */
6504 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006505
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006506 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6507 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006508
Jens Axboe013538b2020-06-22 09:29:15 -06006509 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006510
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006511 ctx->ring_fd = ring_fd;
6512 ctx->ring_file = ring_file;
6513
Jens Axboe6c271ce2019-01-10 11:22:30 -07006514 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006515 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006516 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006517 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006518
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006519 sqe = io_get_sqe(ctx);
6520 if (unlikely(!sqe)) {
6521 io_consume_sqe(ctx);
6522 break;
6523 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006524 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006525 if (unlikely(!req)) {
6526 if (!submitted)
6527 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006528 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006529 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006530
Jens Axboeac8691c2020-06-01 08:30:41 -06006531 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006532 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006533 /* will complete beyond this point, count as submitted */
6534 submitted++;
6535
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006536 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006537fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006538 io_put_req(req);
6539 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006540 break;
6541 }
6542
Jens Axboe354420f2020-01-08 18:55:15 -07006543 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006544 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006545 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006546 if (err)
6547 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006548 }
6549
Pavel Begunkov9466f432020-01-25 22:34:01 +03006550 if (unlikely(submitted != nr)) {
6551 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6552
6553 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6554 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006555 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006556 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006557 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006558
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006559 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6560 io_commit_sqring(ctx);
6561
Jens Axboe6c271ce2019-01-10 11:22:30 -07006562 return submitted;
6563}
6564
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006565static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6566{
6567 /* Tell userspace we may need a wakeup call */
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
6573static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6574{
6575 spin_lock_irq(&ctx->completion_lock);
6576 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6577 spin_unlock_irq(&ctx->completion_lock);
6578}
6579
Jens Axboe6c271ce2019-01-10 11:22:30 -07006580static int io_sq_thread(void *data)
6581{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006582 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006583 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006584 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006585 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006586 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006587
Jens Axboe0f158b42020-05-14 17:18:39 -06006588 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006589
Jens Axboe181e4482019-11-25 08:52:30 -07006590 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006591
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006592 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006593 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006594 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006595
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006596 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006597 unsigned nr_events = 0;
6598
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006599 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006600 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006601 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006602 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006603 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006604 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006605 }
6606
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006607 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006608
6609 /*
6610 * If submit got -EBUSY, flag us as needing the application
6611 * to enter the kernel to reap and flush events.
6612 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006613 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006614 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006615 * Drop cur_mm before scheduling, we can't hold it for
6616 * long periods (or over schedule()). Do this before
6617 * adding ourselves to the waitqueue, as the unuse/drop
6618 * may sleep.
6619 */
Jens Axboe4349f302020-07-09 15:07:01 -06006620 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006621
6622 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006623 * We're polling. If we're within the defined idle
6624 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006625 * to sleep. The exception is if we got EBUSY doing
6626 * more IO, we should wait for the application to
6627 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006628 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006629 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006630 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6631 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006632 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006633 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006634 continue;
6635 }
6636
Jens Axboe6c271ce2019-01-10 11:22:30 -07006637 prepare_to_wait(&ctx->sqo_wait, &wait,
6638 TASK_INTERRUPTIBLE);
6639
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006640 /*
6641 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006642 * to check if there are new reqs added to iopoll_list,
6643 * it is because reqs may have been punted to io worker
6644 * and will be added to iopoll_list later, hence check
6645 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006646 */
6647 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006648 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006649 finish_wait(&ctx->sqo_wait, &wait);
6650 continue;
6651 }
6652
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006653 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006654
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006655 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006656 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006657 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006658 finish_wait(&ctx->sqo_wait, &wait);
6659 break;
6660 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006661 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006662 finish_wait(&ctx->sqo_wait, &wait);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006663 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006664 continue;
6665 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006666 if (signal_pending(current))
6667 flush_signals(current);
6668 schedule();
6669 finish_wait(&ctx->sqo_wait, &wait);
6670
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006671 io_ring_clear_wakeup_flag(ctx);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006672 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006673 continue;
6674 }
6675 finish_wait(&ctx->sqo_wait, &wait);
6676
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006677 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006678 }
6679
Jens Axboe8a4955f2019-12-09 14:52:35 -07006680 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006681 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6682 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006683 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006684 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006685 }
6686
Jens Axboe4c6e2772020-07-01 11:29:10 -06006687 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006688
Jens Axboe4349f302020-07-09 15:07:01 -06006689 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006690 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006691
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006692 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006693
Jens Axboe6c271ce2019-01-10 11:22:30 -07006694 return 0;
6695}
6696
Jens Axboebda52162019-09-24 13:47:15 -06006697struct io_wait_queue {
6698 struct wait_queue_entry wq;
6699 struct io_ring_ctx *ctx;
6700 unsigned to_wait;
6701 unsigned nr_timeouts;
6702};
6703
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006704static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006705{
6706 struct io_ring_ctx *ctx = iowq->ctx;
6707
6708 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006709 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006710 * started waiting. For timeouts, we always want to return to userspace,
6711 * regardless of event count.
6712 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006713 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006714 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6715}
6716
6717static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6718 int wake_flags, void *key)
6719{
6720 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6721 wq);
6722
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006723 /* use noflush == true, as we can't safely rely on locking context */
6724 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006725 return -1;
6726
6727 return autoremove_wake_function(curr, mode, wake_flags, key);
6728}
6729
Jens Axboe2b188cc2019-01-07 10:46:33 -07006730/*
6731 * Wait until events become available, if we don't already have some. The
6732 * application must reap them itself, as they reside on the shared cq ring.
6733 */
6734static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6735 const sigset_t __user *sig, size_t sigsz)
6736{
Jens Axboebda52162019-09-24 13:47:15 -06006737 struct io_wait_queue iowq = {
6738 .wq = {
6739 .private = current,
6740 .func = io_wake_function,
6741 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6742 },
6743 .ctx = ctx,
6744 .to_wait = min_events,
6745 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006746 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006747 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006748
Jens Axboeb41e9852020-02-17 09:52:41 -07006749 do {
6750 if (io_cqring_events(ctx, false) >= min_events)
6751 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006752 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006753 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006754 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006755
6756 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006757#ifdef CONFIG_COMPAT
6758 if (in_compat_syscall())
6759 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006760 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006761 else
6762#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006763 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006764
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765 if (ret)
6766 return ret;
6767 }
6768
Jens Axboebda52162019-09-24 13:47:15 -06006769 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006770 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006771 do {
6772 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6773 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006774 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006775 if (io_run_task_work())
6776 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006777 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006778 if (current->jobctl & JOBCTL_TASK_WORK) {
6779 spin_lock_irq(&current->sighand->siglock);
6780 current->jobctl &= ~JOBCTL_TASK_WORK;
6781 recalc_sigpending();
6782 spin_unlock_irq(&current->sighand->siglock);
6783 continue;
6784 }
6785 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006786 break;
6787 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006788 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006789 break;
6790 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006791 } while (1);
6792 finish_wait(&ctx->wait, &iowq.wq);
6793
Jens Axboeb7db41c2020-07-04 08:55:50 -06006794 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006795
Hristo Venev75b28af2019-08-26 17:23:46 +00006796 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797}
6798
Jens Axboe6b063142019-01-10 22:13:58 -07006799static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6800{
6801#if defined(CONFIG_UNIX)
6802 if (ctx->ring_sock) {
6803 struct sock *sock = ctx->ring_sock->sk;
6804 struct sk_buff *skb;
6805
6806 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6807 kfree_skb(skb);
6808 }
6809#else
6810 int i;
6811
Jens Axboe65e19f52019-10-26 07:20:21 -06006812 for (i = 0; i < ctx->nr_user_files; i++) {
6813 struct file *file;
6814
6815 file = io_file_from_index(ctx, i);
6816 if (file)
6817 fput(file);
6818 }
Jens Axboe6b063142019-01-10 22:13:58 -07006819#endif
6820}
6821
Jens Axboe05f3fb32019-12-09 11:22:50 -07006822static void io_file_ref_kill(struct percpu_ref *ref)
6823{
6824 struct fixed_file_data *data;
6825
6826 data = container_of(ref, struct fixed_file_data, refs);
6827 complete(&data->done);
6828}
6829
Jens Axboe6b063142019-01-10 22:13:58 -07006830static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6831{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006832 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006833 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006834 unsigned nr_tables, i;
6835
Jens Axboe05f3fb32019-12-09 11:22:50 -07006836 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006837 return -ENXIO;
6838
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006839 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006840 if (!list_empty(&data->ref_list))
6841 ref_node = list_first_entry(&data->ref_list,
6842 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006843 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006844 if (ref_node)
6845 percpu_ref_kill(&ref_node->refs);
6846
6847 percpu_ref_kill(&data->refs);
6848
6849 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006850 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006851 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006852
Jens Axboe6b063142019-01-10 22:13:58 -07006853 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006854 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6855 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006856 kfree(data->table[i].files);
6857 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006858 percpu_ref_exit(&data->refs);
6859 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006860 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006861 ctx->nr_user_files = 0;
6862 return 0;
6863}
6864
Jens Axboe6c271ce2019-01-10 11:22:30 -07006865static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6866{
6867 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006868 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006869 /*
6870 * The park is a bit of a work-around, without it we get
6871 * warning spews on shutdown with SQPOLL set and affinity
6872 * set to a single CPU.
6873 */
Jens Axboe06058632019-04-13 09:26:03 -06006874 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006875 kthread_stop(ctx->sqo_thread);
6876 ctx->sqo_thread = NULL;
6877 }
6878}
6879
Jens Axboe6b063142019-01-10 22:13:58 -07006880static void io_finish_async(struct io_ring_ctx *ctx)
6881{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006882 io_sq_thread_stop(ctx);
6883
Jens Axboe561fb042019-10-24 07:25:42 -06006884 if (ctx->io_wq) {
6885 io_wq_destroy(ctx->io_wq);
6886 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006887 }
6888}
6889
6890#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006891/*
6892 * Ensure the UNIX gc is aware of our file set, so we are certain that
6893 * the io_uring can be safely unregistered on process exit, even if we have
6894 * loops in the file referencing.
6895 */
6896static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6897{
6898 struct sock *sk = ctx->ring_sock->sk;
6899 struct scm_fp_list *fpl;
6900 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006901 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006902
Jens Axboe6b063142019-01-10 22:13:58 -07006903 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6904 if (!fpl)
6905 return -ENOMEM;
6906
6907 skb = alloc_skb(0, GFP_KERNEL);
6908 if (!skb) {
6909 kfree(fpl);
6910 return -ENOMEM;
6911 }
6912
6913 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006914
Jens Axboe08a45172019-10-03 08:11:03 -06006915 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006916 fpl->user = get_uid(ctx->user);
6917 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006918 struct file *file = io_file_from_index(ctx, i + offset);
6919
6920 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006921 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006922 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006923 unix_inflight(fpl->user, fpl->fp[nr_files]);
6924 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006925 }
6926
Jens Axboe08a45172019-10-03 08:11:03 -06006927 if (nr_files) {
6928 fpl->max = SCM_MAX_FD;
6929 fpl->count = nr_files;
6930 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006931 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006932 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6933 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006934
Jens Axboe08a45172019-10-03 08:11:03 -06006935 for (i = 0; i < nr_files; i++)
6936 fput(fpl->fp[i]);
6937 } else {
6938 kfree_skb(skb);
6939 kfree(fpl);
6940 }
Jens Axboe6b063142019-01-10 22:13:58 -07006941
6942 return 0;
6943}
6944
6945/*
6946 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6947 * causes regular reference counting to break down. We rely on the UNIX
6948 * garbage collection to take care of this problem for us.
6949 */
6950static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6951{
6952 unsigned left, total;
6953 int ret = 0;
6954
6955 total = 0;
6956 left = ctx->nr_user_files;
6957 while (left) {
6958 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006959
6960 ret = __io_sqe_files_scm(ctx, this_files, total);
6961 if (ret)
6962 break;
6963 left -= this_files;
6964 total += this_files;
6965 }
6966
6967 if (!ret)
6968 return 0;
6969
6970 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006971 struct file *file = io_file_from_index(ctx, total);
6972
6973 if (file)
6974 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006975 total++;
6976 }
6977
6978 return ret;
6979}
6980#else
6981static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6982{
6983 return 0;
6984}
6985#endif
6986
Jens Axboe65e19f52019-10-26 07:20:21 -06006987static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6988 unsigned nr_files)
6989{
6990 int i;
6991
6992 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006993 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006994 unsigned this_files;
6995
6996 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6997 table->files = kcalloc(this_files, sizeof(struct file *),
6998 GFP_KERNEL);
6999 if (!table->files)
7000 break;
7001 nr_files -= this_files;
7002 }
7003
7004 if (i == nr_tables)
7005 return 0;
7006
7007 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007008 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007009 kfree(table->files);
7010 }
7011 return 1;
7012}
7013
Jens Axboe05f3fb32019-12-09 11:22:50 -07007014static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007015{
7016#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007017 struct sock *sock = ctx->ring_sock->sk;
7018 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7019 struct sk_buff *skb;
7020 int i;
7021
7022 __skb_queue_head_init(&list);
7023
7024 /*
7025 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7026 * remove this entry and rearrange the file array.
7027 */
7028 skb = skb_dequeue(head);
7029 while (skb) {
7030 struct scm_fp_list *fp;
7031
7032 fp = UNIXCB(skb).fp;
7033 for (i = 0; i < fp->count; i++) {
7034 int left;
7035
7036 if (fp->fp[i] != file)
7037 continue;
7038
7039 unix_notinflight(fp->user, fp->fp[i]);
7040 left = fp->count - 1 - i;
7041 if (left) {
7042 memmove(&fp->fp[i], &fp->fp[i + 1],
7043 left * sizeof(struct file *));
7044 }
7045 fp->count--;
7046 if (!fp->count) {
7047 kfree_skb(skb);
7048 skb = NULL;
7049 } else {
7050 __skb_queue_tail(&list, skb);
7051 }
7052 fput(file);
7053 file = NULL;
7054 break;
7055 }
7056
7057 if (!file)
7058 break;
7059
7060 __skb_queue_tail(&list, skb);
7061
7062 skb = skb_dequeue(head);
7063 }
7064
7065 if (skb_peek(&list)) {
7066 spin_lock_irq(&head->lock);
7067 while ((skb = __skb_dequeue(&list)) != NULL)
7068 __skb_queue_tail(head, skb);
7069 spin_unlock_irq(&head->lock);
7070 }
7071#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007072 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007073#endif
7074}
7075
Jens Axboe05f3fb32019-12-09 11:22:50 -07007076struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007077 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007078 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007079};
7080
Jens Axboe4a38aed22020-05-14 17:21:15 -06007081static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007082{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007083 struct fixed_file_data *file_data = ref_node->file_data;
7084 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007085 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007086
7087 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007088 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007089 io_ring_file_put(ctx, pfile->file);
7090 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007091 }
7092
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007093 spin_lock(&file_data->lock);
7094 list_del(&ref_node->node);
7095 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007096
Xiaoguang Wang05589552020-03-31 14:05:18 +08007097 percpu_ref_exit(&ref_node->refs);
7098 kfree(ref_node);
7099 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007100}
7101
Jens Axboe4a38aed22020-05-14 17:21:15 -06007102static void io_file_put_work(struct work_struct *work)
7103{
7104 struct io_ring_ctx *ctx;
7105 struct llist_node *node;
7106
7107 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7108 node = llist_del_all(&ctx->file_put_llist);
7109
7110 while (node) {
7111 struct fixed_file_ref_node *ref_node;
7112 struct llist_node *next = node->next;
7113
7114 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7115 __io_file_put_work(ref_node);
7116 node = next;
7117 }
7118}
7119
Jens Axboe05f3fb32019-12-09 11:22:50 -07007120static void io_file_data_ref_zero(struct percpu_ref *ref)
7121{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007122 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007123 struct io_ring_ctx *ctx;
7124 bool first_add;
7125 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007126
Xiaoguang Wang05589552020-03-31 14:05:18 +08007127 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007128 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007129
Jens Axboe4a38aed22020-05-14 17:21:15 -06007130 if (percpu_ref_is_dying(&ctx->file_data->refs))
7131 delay = 0;
7132
7133 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7134 if (!delay)
7135 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7136 else if (first_add)
7137 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007138}
7139
7140static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7141 struct io_ring_ctx *ctx)
7142{
7143 struct fixed_file_ref_node *ref_node;
7144
7145 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7146 if (!ref_node)
7147 return ERR_PTR(-ENOMEM);
7148
7149 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7150 0, GFP_KERNEL)) {
7151 kfree(ref_node);
7152 return ERR_PTR(-ENOMEM);
7153 }
7154 INIT_LIST_HEAD(&ref_node->node);
7155 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007156 ref_node->file_data = ctx->file_data;
7157 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007158}
7159
7160static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7161{
7162 percpu_ref_exit(&ref_node->refs);
7163 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007164}
7165
7166static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7167 unsigned nr_args)
7168{
7169 __s32 __user *fds = (__s32 __user *) arg;
7170 unsigned nr_tables;
7171 struct file *file;
7172 int fd, ret = 0;
7173 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007174 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007175
7176 if (ctx->file_data)
7177 return -EBUSY;
7178 if (!nr_args)
7179 return -EINVAL;
7180 if (nr_args > IORING_MAX_FIXED_FILES)
7181 return -EMFILE;
7182
7183 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7184 if (!ctx->file_data)
7185 return -ENOMEM;
7186 ctx->file_data->ctx = ctx;
7187 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007188 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007189 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007190
7191 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7192 ctx->file_data->table = kcalloc(nr_tables,
7193 sizeof(struct fixed_file_table),
7194 GFP_KERNEL);
7195 if (!ctx->file_data->table) {
7196 kfree(ctx->file_data);
7197 ctx->file_data = NULL;
7198 return -ENOMEM;
7199 }
7200
Xiaoguang Wang05589552020-03-31 14:05:18 +08007201 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007202 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7203 kfree(ctx->file_data->table);
7204 kfree(ctx->file_data);
7205 ctx->file_data = NULL;
7206 return -ENOMEM;
7207 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007208
7209 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7210 percpu_ref_exit(&ctx->file_data->refs);
7211 kfree(ctx->file_data->table);
7212 kfree(ctx->file_data);
7213 ctx->file_data = NULL;
7214 return -ENOMEM;
7215 }
7216
7217 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7218 struct fixed_file_table *table;
7219 unsigned index;
7220
7221 ret = -EFAULT;
7222 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7223 break;
7224 /* allow sparse sets */
7225 if (fd == -1) {
7226 ret = 0;
7227 continue;
7228 }
7229
7230 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7231 index = i & IORING_FILE_TABLE_MASK;
7232 file = fget(fd);
7233
7234 ret = -EBADF;
7235 if (!file)
7236 break;
7237
7238 /*
7239 * Don't allow io_uring instances to be registered. If UNIX
7240 * isn't enabled, then this causes a reference cycle and this
7241 * instance can never get freed. If UNIX is enabled we'll
7242 * handle it just fine, but there's still no point in allowing
7243 * a ring fd as it doesn't support regular read/write anyway.
7244 */
7245 if (file->f_op == &io_uring_fops) {
7246 fput(file);
7247 break;
7248 }
7249 ret = 0;
7250 table->files[index] = file;
7251 }
7252
7253 if (ret) {
7254 for (i = 0; i < ctx->nr_user_files; i++) {
7255 file = io_file_from_index(ctx, i);
7256 if (file)
7257 fput(file);
7258 }
7259 for (i = 0; i < nr_tables; i++)
7260 kfree(ctx->file_data->table[i].files);
7261
Yang Yingliang667e57d2020-07-10 14:14:20 +00007262 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007263 kfree(ctx->file_data->table);
7264 kfree(ctx->file_data);
7265 ctx->file_data = NULL;
7266 ctx->nr_user_files = 0;
7267 return ret;
7268 }
7269
7270 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007271 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007272 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007273 return ret;
7274 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007275
Xiaoguang Wang05589552020-03-31 14:05:18 +08007276 ref_node = alloc_fixed_file_ref_node(ctx);
7277 if (IS_ERR(ref_node)) {
7278 io_sqe_files_unregister(ctx);
7279 return PTR_ERR(ref_node);
7280 }
7281
7282 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007283 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007284 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007285 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007286 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007287 return ret;
7288}
7289
Jens Axboec3a31e62019-10-03 13:59:56 -06007290static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7291 int index)
7292{
7293#if defined(CONFIG_UNIX)
7294 struct sock *sock = ctx->ring_sock->sk;
7295 struct sk_buff_head *head = &sock->sk_receive_queue;
7296 struct sk_buff *skb;
7297
7298 /*
7299 * See if we can merge this file into an existing skb SCM_RIGHTS
7300 * file set. If there's no room, fall back to allocating a new skb
7301 * and filling it in.
7302 */
7303 spin_lock_irq(&head->lock);
7304 skb = skb_peek(head);
7305 if (skb) {
7306 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7307
7308 if (fpl->count < SCM_MAX_FD) {
7309 __skb_unlink(skb, head);
7310 spin_unlock_irq(&head->lock);
7311 fpl->fp[fpl->count] = get_file(file);
7312 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7313 fpl->count++;
7314 spin_lock_irq(&head->lock);
7315 __skb_queue_head(head, skb);
7316 } else {
7317 skb = NULL;
7318 }
7319 }
7320 spin_unlock_irq(&head->lock);
7321
7322 if (skb) {
7323 fput(file);
7324 return 0;
7325 }
7326
7327 return __io_sqe_files_scm(ctx, 1, index);
7328#else
7329 return 0;
7330#endif
7331}
7332
Hillf Dantona5318d32020-03-23 17:47:15 +08007333static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007334 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007335{
Hillf Dantona5318d32020-03-23 17:47:15 +08007336 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007337 struct percpu_ref *refs = data->cur_refs;
7338 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007339
Jens Axboe05f3fb32019-12-09 11:22:50 -07007340 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007341 if (!pfile)
7342 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007343
Xiaoguang Wang05589552020-03-31 14:05:18 +08007344 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007345 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007346 list_add(&pfile->list, &ref_node->file_list);
7347
Hillf Dantona5318d32020-03-23 17:47:15 +08007348 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007349}
7350
7351static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7352 struct io_uring_files_update *up,
7353 unsigned nr_args)
7354{
7355 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007356 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007357 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007358 __s32 __user *fds;
7359 int fd, i, err;
7360 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007361 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007362
Jens Axboe05f3fb32019-12-09 11:22:50 -07007363 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007364 return -EOVERFLOW;
7365 if (done > ctx->nr_user_files)
7366 return -EINVAL;
7367
Xiaoguang Wang05589552020-03-31 14:05:18 +08007368 ref_node = alloc_fixed_file_ref_node(ctx);
7369 if (IS_ERR(ref_node))
7370 return PTR_ERR(ref_node);
7371
Jens Axboec3a31e62019-10-03 13:59:56 -06007372 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007373 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007374 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007375 struct fixed_file_table *table;
7376 unsigned index;
7377
Jens Axboec3a31e62019-10-03 13:59:56 -06007378 err = 0;
7379 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7380 err = -EFAULT;
7381 break;
7382 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007383 i = array_index_nospec(up->offset, ctx->nr_user_files);
7384 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007385 index = i & IORING_FILE_TABLE_MASK;
7386 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007387 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007388 err = io_queue_file_removal(data, file);
7389 if (err)
7390 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007391 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007392 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007393 }
7394 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007395 file = fget(fd);
7396 if (!file) {
7397 err = -EBADF;
7398 break;
7399 }
7400 /*
7401 * Don't allow io_uring instances to be registered. If
7402 * UNIX isn't enabled, then this causes a reference
7403 * cycle and this instance can never get freed. If UNIX
7404 * is enabled we'll handle it just fine, but there's
7405 * still no point in allowing a ring fd as it doesn't
7406 * support regular read/write anyway.
7407 */
7408 if (file->f_op == &io_uring_fops) {
7409 fput(file);
7410 err = -EBADF;
7411 break;
7412 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007413 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007414 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007415 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007416 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007417 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007418 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007419 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007420 }
7421 nr_args--;
7422 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007423 up->offset++;
7424 }
7425
Xiaoguang Wang05589552020-03-31 14:05:18 +08007426 if (needs_switch) {
7427 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007428 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007429 list_add(&ref_node->node, &data->ref_list);
7430 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007431 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007432 percpu_ref_get(&ctx->file_data->refs);
7433 } else
7434 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007435
7436 return done ? done : err;
7437}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007438
Jens Axboe05f3fb32019-12-09 11:22:50 -07007439static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7440 unsigned nr_args)
7441{
7442 struct io_uring_files_update up;
7443
7444 if (!ctx->file_data)
7445 return -ENXIO;
7446 if (!nr_args)
7447 return -EINVAL;
7448 if (copy_from_user(&up, arg, sizeof(up)))
7449 return -EFAULT;
7450 if (up.resv)
7451 return -EINVAL;
7452
7453 return __io_sqe_files_update(ctx, &up, nr_args);
7454}
Jens Axboec3a31e62019-10-03 13:59:56 -06007455
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007456static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007457{
7458 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7459
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007460 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007461 io_put_req(req);
7462}
7463
Pavel Begunkov24369c22020-01-28 03:15:48 +03007464static int io_init_wq_offload(struct io_ring_ctx *ctx,
7465 struct io_uring_params *p)
7466{
7467 struct io_wq_data data;
7468 struct fd f;
7469 struct io_ring_ctx *ctx_attach;
7470 unsigned int concurrency;
7471 int ret = 0;
7472
7473 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007474 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007475 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007476
7477 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7478 /* Do QD, or 4 * CPUS, whatever is smallest */
7479 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7480
7481 ctx->io_wq = io_wq_create(concurrency, &data);
7482 if (IS_ERR(ctx->io_wq)) {
7483 ret = PTR_ERR(ctx->io_wq);
7484 ctx->io_wq = NULL;
7485 }
7486 return ret;
7487 }
7488
7489 f = fdget(p->wq_fd);
7490 if (!f.file)
7491 return -EBADF;
7492
7493 if (f.file->f_op != &io_uring_fops) {
7494 ret = -EINVAL;
7495 goto out_fput;
7496 }
7497
7498 ctx_attach = f.file->private_data;
7499 /* @io_wq is protected by holding the fd */
7500 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7501 ret = -EINVAL;
7502 goto out_fput;
7503 }
7504
7505 ctx->io_wq = ctx_attach->io_wq;
7506out_fput:
7507 fdput(f);
7508 return ret;
7509}
7510
Jens Axboe6c271ce2019-01-10 11:22:30 -07007511static int io_sq_offload_start(struct io_ring_ctx *ctx,
7512 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007513{
7514 int ret;
7515
Jens Axboe6c271ce2019-01-10 11:22:30 -07007516 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007517 ret = -EPERM;
7518 if (!capable(CAP_SYS_ADMIN))
7519 goto err;
7520
Jens Axboe917257d2019-04-13 09:28:55 -06007521 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7522 if (!ctx->sq_thread_idle)
7523 ctx->sq_thread_idle = HZ;
7524
Jens Axboe6c271ce2019-01-10 11:22:30 -07007525 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007526 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007527
Jens Axboe917257d2019-04-13 09:28:55 -06007528 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007529 if (cpu >= nr_cpu_ids)
7530 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007531 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007532 goto err;
7533
Jens Axboe6c271ce2019-01-10 11:22:30 -07007534 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7535 ctx, cpu,
7536 "io_uring-sq");
7537 } else {
7538 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7539 "io_uring-sq");
7540 }
7541 if (IS_ERR(ctx->sqo_thread)) {
7542 ret = PTR_ERR(ctx->sqo_thread);
7543 ctx->sqo_thread = NULL;
7544 goto err;
7545 }
7546 wake_up_process(ctx->sqo_thread);
7547 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7548 /* Can't have SQ_AFF without SQPOLL */
7549 ret = -EINVAL;
7550 goto err;
7551 }
7552
Pavel Begunkov24369c22020-01-28 03:15:48 +03007553 ret = io_init_wq_offload(ctx, p);
7554 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007555 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007556
7557 return 0;
7558err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007559 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007560 return ret;
7561}
7562
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007563static inline void __io_unaccount_mem(struct user_struct *user,
7564 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007565{
7566 atomic_long_sub(nr_pages, &user->locked_vm);
7567}
7568
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007569static inline int __io_account_mem(struct user_struct *user,
7570 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007571{
7572 unsigned long page_limit, cur_pages, new_pages;
7573
7574 /* Don't allow more pages than we can safely lock */
7575 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7576
7577 do {
7578 cur_pages = atomic_long_read(&user->locked_vm);
7579 new_pages = cur_pages + nr_pages;
7580 if (new_pages > page_limit)
7581 return -ENOMEM;
7582 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7583 new_pages) != cur_pages);
7584
7585 return 0;
7586}
7587
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007588static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7589 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007590{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007591 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007592 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007593
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007594 if (ctx->sqo_mm) {
7595 if (acct == ACCT_LOCKED)
7596 ctx->sqo_mm->locked_vm -= nr_pages;
7597 else if (acct == ACCT_PINNED)
7598 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7599 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007600}
7601
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007602static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7603 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007604{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007605 int ret;
7606
7607 if (ctx->limit_mem) {
7608 ret = __io_account_mem(ctx->user, nr_pages);
7609 if (ret)
7610 return ret;
7611 }
7612
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007613 if (ctx->sqo_mm) {
7614 if (acct == ACCT_LOCKED)
7615 ctx->sqo_mm->locked_vm += nr_pages;
7616 else if (acct == ACCT_PINNED)
7617 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7618 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007619
7620 return 0;
7621}
7622
Jens Axboe2b188cc2019-01-07 10:46:33 -07007623static void io_mem_free(void *ptr)
7624{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007625 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007626
Mark Rutland52e04ef2019-04-30 17:30:21 +01007627 if (!ptr)
7628 return;
7629
7630 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007631 if (put_page_testzero(page))
7632 free_compound_page(page);
7633}
7634
7635static void *io_mem_alloc(size_t size)
7636{
7637 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7638 __GFP_NORETRY;
7639
7640 return (void *) __get_free_pages(gfp_flags, get_order(size));
7641}
7642
Hristo Venev75b28af2019-08-26 17:23:46 +00007643static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7644 size_t *sq_offset)
7645{
7646 struct io_rings *rings;
7647 size_t off, sq_array_size;
7648
7649 off = struct_size(rings, cqes, cq_entries);
7650 if (off == SIZE_MAX)
7651 return SIZE_MAX;
7652
7653#ifdef CONFIG_SMP
7654 off = ALIGN(off, SMP_CACHE_BYTES);
7655 if (off == 0)
7656 return SIZE_MAX;
7657#endif
7658
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007659 if (sq_offset)
7660 *sq_offset = off;
7661
Hristo Venev75b28af2019-08-26 17:23:46 +00007662 sq_array_size = array_size(sizeof(u32), sq_entries);
7663 if (sq_array_size == SIZE_MAX)
7664 return SIZE_MAX;
7665
7666 if (check_add_overflow(off, sq_array_size, &off))
7667 return SIZE_MAX;
7668
Hristo Venev75b28af2019-08-26 17:23:46 +00007669 return off;
7670}
7671
Jens Axboe2b188cc2019-01-07 10:46:33 -07007672static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7673{
Hristo Venev75b28af2019-08-26 17:23:46 +00007674 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007675
Hristo Venev75b28af2019-08-26 17:23:46 +00007676 pages = (size_t)1 << get_order(
7677 rings_size(sq_entries, cq_entries, NULL));
7678 pages += (size_t)1 << get_order(
7679 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007680
Hristo Venev75b28af2019-08-26 17:23:46 +00007681 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007682}
7683
Jens Axboeedafcce2019-01-09 09:16:05 -07007684static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7685{
7686 int i, j;
7687
7688 if (!ctx->user_bufs)
7689 return -ENXIO;
7690
7691 for (i = 0; i < ctx->nr_user_bufs; i++) {
7692 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7693
7694 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007695 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007696
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007697 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007698 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007699 imu->nr_bvecs = 0;
7700 }
7701
7702 kfree(ctx->user_bufs);
7703 ctx->user_bufs = NULL;
7704 ctx->nr_user_bufs = 0;
7705 return 0;
7706}
7707
7708static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7709 void __user *arg, unsigned index)
7710{
7711 struct iovec __user *src;
7712
7713#ifdef CONFIG_COMPAT
7714 if (ctx->compat) {
7715 struct compat_iovec __user *ciovs;
7716 struct compat_iovec ciov;
7717
7718 ciovs = (struct compat_iovec __user *) arg;
7719 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7720 return -EFAULT;
7721
Jens Axboed55e5f52019-12-11 16:12:15 -07007722 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007723 dst->iov_len = ciov.iov_len;
7724 return 0;
7725 }
7726#endif
7727 src = (struct iovec __user *) arg;
7728 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7729 return -EFAULT;
7730 return 0;
7731}
7732
7733static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7734 unsigned nr_args)
7735{
7736 struct vm_area_struct **vmas = NULL;
7737 struct page **pages = NULL;
7738 int i, j, got_pages = 0;
7739 int ret = -EINVAL;
7740
7741 if (ctx->user_bufs)
7742 return -EBUSY;
7743 if (!nr_args || nr_args > UIO_MAXIOV)
7744 return -EINVAL;
7745
7746 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7747 GFP_KERNEL);
7748 if (!ctx->user_bufs)
7749 return -ENOMEM;
7750
7751 for (i = 0; i < nr_args; i++) {
7752 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7753 unsigned long off, start, end, ubuf;
7754 int pret, nr_pages;
7755 struct iovec iov;
7756 size_t size;
7757
7758 ret = io_copy_iov(ctx, &iov, arg, i);
7759 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007760 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007761
7762 /*
7763 * Don't impose further limits on the size and buffer
7764 * constraints here, we'll -EINVAL later when IO is
7765 * submitted if they are wrong.
7766 */
7767 ret = -EFAULT;
7768 if (!iov.iov_base || !iov.iov_len)
7769 goto err;
7770
7771 /* arbitrary limit, but we need something */
7772 if (iov.iov_len > SZ_1G)
7773 goto err;
7774
7775 ubuf = (unsigned long) iov.iov_base;
7776 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7777 start = ubuf >> PAGE_SHIFT;
7778 nr_pages = end - start;
7779
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007780 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007781 if (ret)
7782 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007783
7784 ret = 0;
7785 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007786 kvfree(vmas);
7787 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007788 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007789 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007790 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007791 sizeof(struct vm_area_struct *),
7792 GFP_KERNEL);
7793 if (!pages || !vmas) {
7794 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007795 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007796 goto err;
7797 }
7798 got_pages = nr_pages;
7799 }
7800
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007801 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007802 GFP_KERNEL);
7803 ret = -ENOMEM;
7804 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007805 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007806 goto err;
7807 }
7808
7809 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007810 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007811 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007812 FOLL_WRITE | FOLL_LONGTERM,
7813 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007814 if (pret == nr_pages) {
7815 /* don't support file backed memory */
7816 for (j = 0; j < nr_pages; j++) {
7817 struct vm_area_struct *vma = vmas[j];
7818
7819 if (vma->vm_file &&
7820 !is_file_hugepages(vma->vm_file)) {
7821 ret = -EOPNOTSUPP;
7822 break;
7823 }
7824 }
7825 } else {
7826 ret = pret < 0 ? pret : -EFAULT;
7827 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007828 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007829 if (ret) {
7830 /*
7831 * if we did partial map, or found file backed vmas,
7832 * release any pages we did get
7833 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007834 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007835 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007836 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007837 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007838 goto err;
7839 }
7840
7841 off = ubuf & ~PAGE_MASK;
7842 size = iov.iov_len;
7843 for (j = 0; j < nr_pages; j++) {
7844 size_t vec_len;
7845
7846 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7847 imu->bvec[j].bv_page = pages[j];
7848 imu->bvec[j].bv_len = vec_len;
7849 imu->bvec[j].bv_offset = off;
7850 off = 0;
7851 size -= vec_len;
7852 }
7853 /* store original address for later verification */
7854 imu->ubuf = ubuf;
7855 imu->len = iov.iov_len;
7856 imu->nr_bvecs = nr_pages;
7857
7858 ctx->nr_user_bufs++;
7859 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007860 kvfree(pages);
7861 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007862 return 0;
7863err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007864 kvfree(pages);
7865 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007866 io_sqe_buffer_unregister(ctx);
7867 return ret;
7868}
7869
Jens Axboe9b402842019-04-11 11:45:41 -06007870static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7871{
7872 __s32 __user *fds = arg;
7873 int fd;
7874
7875 if (ctx->cq_ev_fd)
7876 return -EBUSY;
7877
7878 if (copy_from_user(&fd, fds, sizeof(*fds)))
7879 return -EFAULT;
7880
7881 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7882 if (IS_ERR(ctx->cq_ev_fd)) {
7883 int ret = PTR_ERR(ctx->cq_ev_fd);
7884 ctx->cq_ev_fd = NULL;
7885 return ret;
7886 }
7887
7888 return 0;
7889}
7890
7891static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7892{
7893 if (ctx->cq_ev_fd) {
7894 eventfd_ctx_put(ctx->cq_ev_fd);
7895 ctx->cq_ev_fd = NULL;
7896 return 0;
7897 }
7898
7899 return -ENXIO;
7900}
7901
Jens Axboe5a2e7452020-02-23 16:23:11 -07007902static int __io_destroy_buffers(int id, void *p, void *data)
7903{
7904 struct io_ring_ctx *ctx = data;
7905 struct io_buffer *buf = p;
7906
Jens Axboe067524e2020-03-02 16:32:28 -07007907 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007908 return 0;
7909}
7910
7911static void io_destroy_buffers(struct io_ring_ctx *ctx)
7912{
7913 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7914 idr_destroy(&ctx->io_buffer_idr);
7915}
7916
Jens Axboe2b188cc2019-01-07 10:46:33 -07007917static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7918{
Jens Axboe6b063142019-01-10 22:13:58 -07007919 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007920 io_sqe_buffer_unregister(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007921 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007922 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007923 ctx->sqo_mm = NULL;
7924 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007925
Jens Axboe6b063142019-01-10 22:13:58 -07007926 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007927 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007928 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007929 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007930
Jens Axboe2b188cc2019-01-07 10:46:33 -07007931#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007932 if (ctx->ring_sock) {
7933 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007934 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007935 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007936#endif
7937
Hristo Venev75b28af2019-08-26 17:23:46 +00007938 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007939 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007940
7941 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007942 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007943 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007944 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007945 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007946 kfree(ctx);
7947}
7948
7949static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7950{
7951 struct io_ring_ctx *ctx = file->private_data;
7952 __poll_t mask = 0;
7953
7954 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007955 /*
7956 * synchronizes with barrier from wq_has_sleeper call in
7957 * io_commit_cqring
7958 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007959 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007960 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7961 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007962 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007963 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007964 mask |= EPOLLIN | EPOLLRDNORM;
7965
7966 return mask;
7967}
7968
7969static int io_uring_fasync(int fd, struct file *file, int on)
7970{
7971 struct io_ring_ctx *ctx = file->private_data;
7972
7973 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7974}
7975
Jens Axboe071698e2020-01-28 10:04:42 -07007976static int io_remove_personalities(int id, void *p, void *data)
7977{
7978 struct io_ring_ctx *ctx = data;
7979 const struct cred *cred;
7980
7981 cred = idr_remove(&ctx->personality_idr, id);
7982 if (cred)
7983 put_cred(cred);
7984 return 0;
7985}
7986
Jens Axboe85faa7b2020-04-09 18:14:00 -06007987static void io_ring_exit_work(struct work_struct *work)
7988{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007989 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
7990 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007991
Jens Axboe56952e92020-06-17 15:00:04 -06007992 /*
7993 * If we're doing polled IO and end up having requests being
7994 * submitted async (out-of-line), then completions can come in while
7995 * we're waiting for refs to drop. We need to reap these manually,
7996 * as nobody else will be looking for them.
7997 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007998 do {
Jens Axboe56952e92020-06-17 15:00:04 -06007999 if (ctx->rings)
8000 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008001 io_iopoll_try_reap_events(ctx);
8002 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008003 io_ring_ctx_free(ctx);
8004}
8005
Jens Axboe2b188cc2019-01-07 10:46:33 -07008006static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8007{
8008 mutex_lock(&ctx->uring_lock);
8009 percpu_ref_kill(&ctx->refs);
8010 mutex_unlock(&ctx->uring_lock);
8011
Jens Axboef3606e32020-09-22 08:18:24 -06008012 io_kill_timeouts(ctx, NULL);
8013 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008014
8015 if (ctx->io_wq)
8016 io_wq_cancel_all(ctx->io_wq);
8017
Jens Axboe15dff282019-11-13 09:09:23 -07008018 /* if we failed setting up the ctx, we might not have any rings */
8019 if (ctx->rings)
8020 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008021 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008022 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008023
8024 /*
8025 * Do this upfront, so we won't have a grace period where the ring
8026 * is closed but resources aren't reaped yet. This can cause
8027 * spurious failure in setting up a new ring.
8028 */
Jens Axboe760618f2020-07-24 12:53:31 -06008029 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8030 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008031
Jens Axboe85faa7b2020-04-09 18:14:00 -06008032 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008033 /*
8034 * Use system_unbound_wq to avoid spawning tons of event kworkers
8035 * if we're exiting a ton of rings at the same time. It just adds
8036 * noise and overhead, there's no discernable change in runtime
8037 * over using system_wq.
8038 */
8039 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008040}
8041
8042static int io_uring_release(struct inode *inode, struct file *file)
8043{
8044 struct io_ring_ctx *ctx = file->private_data;
8045
8046 file->private_data = NULL;
8047 io_ring_ctx_wait_and_kill(ctx);
8048 return 0;
8049}
8050
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008051static bool io_wq_files_match(struct io_wq_work *work, void *data)
8052{
8053 struct files_struct *files = data;
8054
8055 return work->files == files;
8056}
8057
Jens Axboef254ac02020-08-12 17:33:30 -06008058/*
8059 * Returns true if 'preq' is the link parent of 'req'
8060 */
8061static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8062{
8063 struct io_kiocb *link;
8064
8065 if (!(preq->flags & REQ_F_LINK_HEAD))
8066 return false;
8067
8068 list_for_each_entry(link, &preq->link_list, link_list) {
8069 if (link == req)
8070 return true;
8071 }
8072
8073 return false;
8074}
8075
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008076static inline bool io_match_files(struct io_kiocb *req,
8077 struct files_struct *files)
8078{
8079 return (req->flags & REQ_F_WORK_INITIALIZED) && req->work.files == files;
8080}
8081
8082static bool io_match_link_files(struct io_kiocb *req,
8083 struct files_struct *files)
8084{
8085 struct io_kiocb *link;
8086
8087 if (io_match_files(req, files))
8088 return true;
8089 if (req->flags & REQ_F_LINK_HEAD) {
8090 list_for_each_entry(link, &req->link_list, link_list) {
8091 if (io_match_files(link, files))
8092 return true;
8093 }
8094 }
8095 return false;
8096}
8097
Jens Axboef254ac02020-08-12 17:33:30 -06008098/*
8099 * We're looking to cancel 'req' because it's holding on to our files, but
8100 * 'req' could be a link to another request. See if it is, and cancel that
8101 * parent request if so.
8102 */
8103static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8104{
8105 struct hlist_node *tmp;
8106 struct io_kiocb *preq;
8107 bool found = false;
8108 int i;
8109
8110 spin_lock_irq(&ctx->completion_lock);
8111 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8112 struct hlist_head *list;
8113
8114 list = &ctx->cancel_hash[i];
8115 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8116 found = io_match_link(preq, req);
8117 if (found) {
8118 io_poll_remove_one(preq);
8119 break;
8120 }
8121 }
8122 }
8123 spin_unlock_irq(&ctx->completion_lock);
8124 return found;
8125}
8126
8127static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8128 struct io_kiocb *req)
8129{
8130 struct io_kiocb *preq;
8131 bool found = false;
8132
8133 spin_lock_irq(&ctx->completion_lock);
8134 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8135 found = io_match_link(preq, req);
8136 if (found) {
8137 __io_timeout_cancel(preq);
8138 break;
8139 }
8140 }
8141 spin_unlock_irq(&ctx->completion_lock);
8142 return found;
8143}
8144
Jens Axboeb711d4e2020-08-16 08:23:05 -07008145static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8146{
8147 return io_match_link(container_of(work, struct io_kiocb, work), data);
8148}
8149
8150static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8151{
8152 enum io_wq_cancel cret;
8153
8154 /* cancel this particular work, if it's running */
8155 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8156 if (cret != IO_WQ_CANCEL_NOTFOUND)
8157 return;
8158
8159 /* find links that hold this pending, cancel those */
8160 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8161 if (cret != IO_WQ_CANCEL_NOTFOUND)
8162 return;
8163
8164 /* if we have a poll link holding this pending, cancel that */
8165 if (io_poll_remove_link(ctx, req))
8166 return;
8167
8168 /* final option, timeout link is holding this req pending */
8169 io_timeout_remove_link(ctx, req);
8170}
8171
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008172static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8173 struct files_struct *files)
8174{
8175 struct io_defer_entry *de = NULL;
8176 LIST_HEAD(list);
8177
8178 spin_lock_irq(&ctx->completion_lock);
8179 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008180 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008181 list_cut_position(&list, &ctx->defer_list, &de->list);
8182 break;
8183 }
8184 }
8185 spin_unlock_irq(&ctx->completion_lock);
8186
8187 while (!list_empty(&list)) {
8188 de = list_first_entry(&list, struct io_defer_entry, list);
8189 list_del_init(&de->list);
8190 req_set_fail_links(de->req);
8191 io_put_req(de->req);
8192 io_req_complete(de->req, -ECANCELED);
8193 kfree(de);
8194 }
8195}
8196
Jens Axboefcb323c2019-10-24 12:39:47 -06008197static void io_uring_cancel_files(struct io_ring_ctx *ctx,
8198 struct files_struct *files)
8199{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008200 if (list_empty_careful(&ctx->inflight_list))
8201 return;
8202
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008203 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008204 /* cancel all at once, should be faster than doing it one by one*/
8205 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8206
Jens Axboefcb323c2019-10-24 12:39:47 -06008207 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008208 struct io_kiocb *cancel_req = NULL, *req;
8209 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008210
8211 spin_lock_irq(&ctx->inflight_lock);
8212 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07008213 if (req->work.files != files)
8214 continue;
8215 /* req is being completed, ignore */
8216 if (!refcount_inc_not_zero(&req->refs))
8217 continue;
8218 cancel_req = req;
8219 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008220 }
Jens Axboe768134d2019-11-10 20:30:53 -07008221 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008222 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008223 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008224 spin_unlock_irq(&ctx->inflight_lock);
8225
Jens Axboe768134d2019-11-10 20:30:53 -07008226 /* We need to keep going until we don't find a matching req */
8227 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008228 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008229 /* cancel this request, or head link requests */
8230 io_attempt_cancel(ctx, cancel_req);
8231 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008232 /* cancellations _may_ trigger task work */
8233 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008234 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008235 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008236 }
8237}
8238
Pavel Begunkov801dd572020-06-15 10:33:14 +03008239static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008240{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008241 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8242 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008243
Jens Axboef3606e32020-09-22 08:18:24 -06008244 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008245}
8246
Jens Axboefcb323c2019-10-24 12:39:47 -06008247static int io_uring_flush(struct file *file, void *data)
8248{
8249 struct io_ring_ctx *ctx = file->private_data;
8250
8251 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07008252
8253 /*
8254 * If the task is going away, cancel work it may have pending
8255 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008256 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
8257 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07008258
Jens Axboefcb323c2019-10-24 12:39:47 -06008259 return 0;
8260}
8261
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008262static void *io_uring_validate_mmap_request(struct file *file,
8263 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008264{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008265 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008266 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008267 struct page *page;
8268 void *ptr;
8269
8270 switch (offset) {
8271 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008272 case IORING_OFF_CQ_RING:
8273 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008274 break;
8275 case IORING_OFF_SQES:
8276 ptr = ctx->sq_sqes;
8277 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008278 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008279 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008280 }
8281
8282 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008283 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008284 return ERR_PTR(-EINVAL);
8285
8286 return ptr;
8287}
8288
8289#ifdef CONFIG_MMU
8290
8291static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8292{
8293 size_t sz = vma->vm_end - vma->vm_start;
8294 unsigned long pfn;
8295 void *ptr;
8296
8297 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8298 if (IS_ERR(ptr))
8299 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008300
8301 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8302 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8303}
8304
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008305#else /* !CONFIG_MMU */
8306
8307static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8308{
8309 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8310}
8311
8312static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8313{
8314 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8315}
8316
8317static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8318 unsigned long addr, unsigned long len,
8319 unsigned long pgoff, unsigned long flags)
8320{
8321 void *ptr;
8322
8323 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8324 if (IS_ERR(ptr))
8325 return PTR_ERR(ptr);
8326
8327 return (unsigned long) ptr;
8328}
8329
8330#endif /* !CONFIG_MMU */
8331
Jens Axboe2b188cc2019-01-07 10:46:33 -07008332SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8333 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8334 size_t, sigsz)
8335{
8336 struct io_ring_ctx *ctx;
8337 long ret = -EBADF;
8338 int submitted = 0;
8339 struct fd f;
8340
Jens Axboe4c6e2772020-07-01 11:29:10 -06008341 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008342
Jens Axboe6c271ce2019-01-10 11:22:30 -07008343 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008344 return -EINVAL;
8345
8346 f = fdget(fd);
8347 if (!f.file)
8348 return -EBADF;
8349
8350 ret = -EOPNOTSUPP;
8351 if (f.file->f_op != &io_uring_fops)
8352 goto out_fput;
8353
8354 ret = -ENXIO;
8355 ctx = f.file->private_data;
8356 if (!percpu_ref_tryget(&ctx->refs))
8357 goto out_fput;
8358
Jens Axboe6c271ce2019-01-10 11:22:30 -07008359 /*
8360 * For SQ polling, the thread will do all submissions and completions.
8361 * Just return the requested submit count, and wake the thread if
8362 * we were asked to.
8363 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008364 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008365 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008366 if (!list_empty_careful(&ctx->cq_overflow_list))
8367 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008368 if (flags & IORING_ENTER_SQ_WAKEUP)
8369 wake_up(&ctx->sqo_wait);
8370 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008371 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07008372 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03008373 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008374 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008375
8376 if (submitted != to_submit)
8377 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008378 }
8379 if (flags & IORING_ENTER_GETEVENTS) {
8380 min_complete = min(min_complete, ctx->cq_entries);
8381
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008382 /*
8383 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8384 * space applications don't need to do io completion events
8385 * polling again, they can rely on io_sq_thread to do polling
8386 * work, which can reduce cpu usage and uring_lock contention.
8387 */
8388 if (ctx->flags & IORING_SETUP_IOPOLL &&
8389 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008390 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008391 } else {
8392 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8393 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008394 }
8395
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008396out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008397 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008398out_fput:
8399 fdput(f);
8400 return submitted ? submitted : ret;
8401}
8402
Tobias Klauserbebdb652020-02-26 18:38:32 +01008403#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008404static int io_uring_show_cred(int id, void *p, void *data)
8405{
8406 const struct cred *cred = p;
8407 struct seq_file *m = data;
8408 struct user_namespace *uns = seq_user_ns(m);
8409 struct group_info *gi;
8410 kernel_cap_t cap;
8411 unsigned __capi;
8412 int g;
8413
8414 seq_printf(m, "%5d\n", id);
8415 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8416 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8417 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8418 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8419 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8420 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8421 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8422 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8423 seq_puts(m, "\n\tGroups:\t");
8424 gi = cred->group_info;
8425 for (g = 0; g < gi->ngroups; g++) {
8426 seq_put_decimal_ull(m, g ? " " : "",
8427 from_kgid_munged(uns, gi->gid[g]));
8428 }
8429 seq_puts(m, "\n\tCapEff:\t");
8430 cap = cred->cap_effective;
8431 CAP_FOR_EACH_U32(__capi)
8432 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8433 seq_putc(m, '\n');
8434 return 0;
8435}
8436
8437static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8438{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008439 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008440 int i;
8441
Jens Axboefad8e0d2020-09-28 08:57:48 -06008442 /*
8443 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8444 * since fdinfo case grabs it in the opposite direction of normal use
8445 * cases. If we fail to get the lock, we just don't iterate any
8446 * structures that could be going away outside the io_uring mutex.
8447 */
8448 has_lock = mutex_trylock(&ctx->uring_lock);
8449
Jens Axboe87ce9552020-01-30 08:25:34 -07008450 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008451 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008452 struct fixed_file_table *table;
8453 struct file *f;
8454
8455 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8456 f = table->files[i & IORING_FILE_TABLE_MASK];
8457 if (f)
8458 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8459 else
8460 seq_printf(m, "%5u: <none>\n", i);
8461 }
8462 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008463 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008464 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8465
8466 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8467 (unsigned int) buf->len);
8468 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008469 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008470 seq_printf(m, "Personalities:\n");
8471 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8472 }
Jens Axboed7718a92020-02-14 22:23:12 -07008473 seq_printf(m, "PollList:\n");
8474 spin_lock_irq(&ctx->completion_lock);
8475 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8476 struct hlist_head *list = &ctx->cancel_hash[i];
8477 struct io_kiocb *req;
8478
8479 hlist_for_each_entry(req, list, hash_node)
8480 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8481 req->task->task_works != NULL);
8482 }
8483 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008484 if (has_lock)
8485 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008486}
8487
8488static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8489{
8490 struct io_ring_ctx *ctx = f->private_data;
8491
8492 if (percpu_ref_tryget(&ctx->refs)) {
8493 __io_uring_show_fdinfo(ctx, m);
8494 percpu_ref_put(&ctx->refs);
8495 }
8496}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008497#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008498
Jens Axboe2b188cc2019-01-07 10:46:33 -07008499static const struct file_operations io_uring_fops = {
8500 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008501 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008502 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008503#ifndef CONFIG_MMU
8504 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8505 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8506#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008507 .poll = io_uring_poll,
8508 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008509#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008510 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008511#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008512};
8513
8514static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8515 struct io_uring_params *p)
8516{
Hristo Venev75b28af2019-08-26 17:23:46 +00008517 struct io_rings *rings;
8518 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008519
Jens Axboebd740482020-08-05 12:58:23 -06008520 /* make sure these are sane, as we already accounted them */
8521 ctx->sq_entries = p->sq_entries;
8522 ctx->cq_entries = p->cq_entries;
8523
Hristo Venev75b28af2019-08-26 17:23:46 +00008524 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8525 if (size == SIZE_MAX)
8526 return -EOVERFLOW;
8527
8528 rings = io_mem_alloc(size);
8529 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008530 return -ENOMEM;
8531
Hristo Venev75b28af2019-08-26 17:23:46 +00008532 ctx->rings = rings;
8533 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8534 rings->sq_ring_mask = p->sq_entries - 1;
8535 rings->cq_ring_mask = p->cq_entries - 1;
8536 rings->sq_ring_entries = p->sq_entries;
8537 rings->cq_ring_entries = p->cq_entries;
8538 ctx->sq_mask = rings->sq_ring_mask;
8539 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008540
8541 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008542 if (size == SIZE_MAX) {
8543 io_mem_free(ctx->rings);
8544 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008545 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008546 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008547
8548 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008549 if (!ctx->sq_sqes) {
8550 io_mem_free(ctx->rings);
8551 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008552 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008554
Jens Axboe2b188cc2019-01-07 10:46:33 -07008555 return 0;
8556}
8557
8558/*
8559 * Allocate an anonymous fd, this is what constitutes the application
8560 * visible backing of an io_uring instance. The application mmaps this
8561 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8562 * we have to tie this fd to a socket for file garbage collection purposes.
8563 */
8564static int io_uring_get_fd(struct io_ring_ctx *ctx)
8565{
8566 struct file *file;
8567 int ret;
8568
8569#if defined(CONFIG_UNIX)
8570 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8571 &ctx->ring_sock);
8572 if (ret)
8573 return ret;
8574#endif
8575
8576 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8577 if (ret < 0)
8578 goto err;
8579
8580 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8581 O_RDWR | O_CLOEXEC);
8582 if (IS_ERR(file)) {
8583 put_unused_fd(ret);
8584 ret = PTR_ERR(file);
8585 goto err;
8586 }
8587
8588#if defined(CONFIG_UNIX)
8589 ctx->ring_sock->file = file;
8590#endif
8591 fd_install(ret, file);
8592 return ret;
8593err:
8594#if defined(CONFIG_UNIX)
8595 sock_release(ctx->ring_sock);
8596 ctx->ring_sock = NULL;
8597#endif
8598 return ret;
8599}
8600
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008601static int io_uring_create(unsigned entries, struct io_uring_params *p,
8602 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008603{
8604 struct user_struct *user = NULL;
8605 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008606 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008607 int ret;
8608
Jens Axboe8110c1a2019-12-28 15:39:54 -07008609 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008610 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008611 if (entries > IORING_MAX_ENTRIES) {
8612 if (!(p->flags & IORING_SETUP_CLAMP))
8613 return -EINVAL;
8614 entries = IORING_MAX_ENTRIES;
8615 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008616
8617 /*
8618 * Use twice as many entries for the CQ ring. It's possible for the
8619 * application to drive a higher depth than the size of the SQ ring,
8620 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008621 * some flexibility in overcommitting a bit. If the application has
8622 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8623 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008624 */
8625 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008626 if (p->flags & IORING_SETUP_CQSIZE) {
8627 /*
8628 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8629 * to a power-of-two, if it isn't already. We do NOT impose
8630 * any cq vs sq ring sizing.
8631 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008632 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008633 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008634 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8635 if (!(p->flags & IORING_SETUP_CLAMP))
8636 return -EINVAL;
8637 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8638 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008639 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8640 } else {
8641 p->cq_entries = 2 * p->sq_entries;
8642 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008643
8644 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008645 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008646
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008647 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008648 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008649 ring_pages(p->sq_entries, p->cq_entries));
8650 if (ret) {
8651 free_uid(user);
8652 return ret;
8653 }
8654 }
8655
8656 ctx = io_ring_ctx_alloc(p);
8657 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008658 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008659 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008660 p->cq_entries));
8661 free_uid(user);
8662 return -ENOMEM;
8663 }
8664 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008665 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008666 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008667
Jens Axboe6b7898e2020-08-25 07:58:00 -06008668 mmgrab(current->mm);
8669 ctx->sqo_mm = current->mm;
8670
Jens Axboef74441e2020-08-05 13:00:44 -06008671 /*
8672 * Account memory _before_ installing the file descriptor. Once
8673 * the descriptor is installed, it can get closed at any time. Also
8674 * do this before hitting the general error path, as ring freeing
8675 * will un-account as well.
8676 */
8677 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8678 ACCT_LOCKED);
8679 ctx->limit_mem = limit_mem;
8680
Jens Axboe2b188cc2019-01-07 10:46:33 -07008681 ret = io_allocate_scq_urings(ctx, p);
8682 if (ret)
8683 goto err;
8684
Jens Axboe6c271ce2019-01-10 11:22:30 -07008685 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008686 if (ret)
8687 goto err;
8688
Jens Axboe2b188cc2019-01-07 10:46:33 -07008689 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008690 p->sq_off.head = offsetof(struct io_rings, sq.head);
8691 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8692 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8693 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8694 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8695 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8696 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008697
8698 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008699 p->cq_off.head = offsetof(struct io_rings, cq.head);
8700 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8701 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8702 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8703 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8704 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008705 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008706
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008707 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8708 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008709 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8710 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008711
8712 if (copy_to_user(params, p, sizeof(*p))) {
8713 ret = -EFAULT;
8714 goto err;
8715 }
Jens Axboed1719f72020-07-30 13:43:53 -06008716
8717 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06008718 * Install ring fd as the very last thing, so we don't risk someone
8719 * having closed it before we finish setup
8720 */
8721 ret = io_uring_get_fd(ctx);
8722 if (ret < 0)
8723 goto err;
8724
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008725 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008726 return ret;
8727err:
8728 io_ring_ctx_wait_and_kill(ctx);
8729 return ret;
8730}
8731
8732/*
8733 * Sets up an aio uring context, and returns the fd. Applications asks for a
8734 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8735 * params structure passed in.
8736 */
8737static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8738{
8739 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008740 int i;
8741
8742 if (copy_from_user(&p, params, sizeof(p)))
8743 return -EFAULT;
8744 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8745 if (p.resv[i])
8746 return -EINVAL;
8747 }
8748
Jens Axboe6c271ce2019-01-10 11:22:30 -07008749 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008750 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008751 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008752 return -EINVAL;
8753
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008754 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008755}
8756
8757SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8758 struct io_uring_params __user *, params)
8759{
8760 return io_uring_setup(entries, params);
8761}
8762
Jens Axboe66f4af92020-01-16 15:36:52 -07008763static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8764{
8765 struct io_uring_probe *p;
8766 size_t size;
8767 int i, ret;
8768
8769 size = struct_size(p, ops, nr_args);
8770 if (size == SIZE_MAX)
8771 return -EOVERFLOW;
8772 p = kzalloc(size, GFP_KERNEL);
8773 if (!p)
8774 return -ENOMEM;
8775
8776 ret = -EFAULT;
8777 if (copy_from_user(p, arg, size))
8778 goto out;
8779 ret = -EINVAL;
8780 if (memchr_inv(p, 0, size))
8781 goto out;
8782
8783 p->last_op = IORING_OP_LAST - 1;
8784 if (nr_args > IORING_OP_LAST)
8785 nr_args = IORING_OP_LAST;
8786
8787 for (i = 0; i < nr_args; i++) {
8788 p->ops[i].op = i;
8789 if (!io_op_defs[i].not_supported)
8790 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8791 }
8792 p->ops_len = i;
8793
8794 ret = 0;
8795 if (copy_to_user(arg, p, size))
8796 ret = -EFAULT;
8797out:
8798 kfree(p);
8799 return ret;
8800}
8801
Jens Axboe071698e2020-01-28 10:04:42 -07008802static int io_register_personality(struct io_ring_ctx *ctx)
8803{
8804 const struct cred *creds = get_current_cred();
8805 int id;
8806
8807 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8808 USHRT_MAX, GFP_KERNEL);
8809 if (id < 0)
8810 put_cred(creds);
8811 return id;
8812}
8813
8814static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8815{
8816 const struct cred *old_creds;
8817
8818 old_creds = idr_remove(&ctx->personality_idr, id);
8819 if (old_creds) {
8820 put_cred(old_creds);
8821 return 0;
8822 }
8823
8824 return -EINVAL;
8825}
8826
8827static bool io_register_op_must_quiesce(int op)
8828{
8829 switch (op) {
8830 case IORING_UNREGISTER_FILES:
8831 case IORING_REGISTER_FILES_UPDATE:
8832 case IORING_REGISTER_PROBE:
8833 case IORING_REGISTER_PERSONALITY:
8834 case IORING_UNREGISTER_PERSONALITY:
8835 return false;
8836 default:
8837 return true;
8838 }
8839}
8840
Jens Axboeedafcce2019-01-09 09:16:05 -07008841static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8842 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008843 __releases(ctx->uring_lock)
8844 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008845{
8846 int ret;
8847
Jens Axboe35fa71a2019-04-22 10:23:23 -06008848 /*
8849 * We're inside the ring mutex, if the ref is already dying, then
8850 * someone else killed the ctx or is already going through
8851 * io_uring_register().
8852 */
8853 if (percpu_ref_is_dying(&ctx->refs))
8854 return -ENXIO;
8855
Jens Axboe071698e2020-01-28 10:04:42 -07008856 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008857 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008858
Jens Axboe05f3fb32019-12-09 11:22:50 -07008859 /*
8860 * Drop uring mutex before waiting for references to exit. If
8861 * another thread is currently inside io_uring_enter() it might
8862 * need to grab the uring_lock to make progress. If we hold it
8863 * here across the drain wait, then we can deadlock. It's safe
8864 * to drop the mutex here, since no new references will come in
8865 * after we've killed the percpu ref.
8866 */
8867 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008868 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008869 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008870 if (ret) {
8871 percpu_ref_resurrect(&ctx->refs);
8872 ret = -EINTR;
8873 goto out;
8874 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008875 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008876
8877 switch (opcode) {
8878 case IORING_REGISTER_BUFFERS:
8879 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8880 break;
8881 case IORING_UNREGISTER_BUFFERS:
8882 ret = -EINVAL;
8883 if (arg || nr_args)
8884 break;
8885 ret = io_sqe_buffer_unregister(ctx);
8886 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008887 case IORING_REGISTER_FILES:
8888 ret = io_sqe_files_register(ctx, arg, nr_args);
8889 break;
8890 case IORING_UNREGISTER_FILES:
8891 ret = -EINVAL;
8892 if (arg || nr_args)
8893 break;
8894 ret = io_sqe_files_unregister(ctx);
8895 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008896 case IORING_REGISTER_FILES_UPDATE:
8897 ret = io_sqe_files_update(ctx, arg, nr_args);
8898 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008899 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008900 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008901 ret = -EINVAL;
8902 if (nr_args != 1)
8903 break;
8904 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008905 if (ret)
8906 break;
8907 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8908 ctx->eventfd_async = 1;
8909 else
8910 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008911 break;
8912 case IORING_UNREGISTER_EVENTFD:
8913 ret = -EINVAL;
8914 if (arg || nr_args)
8915 break;
8916 ret = io_eventfd_unregister(ctx);
8917 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008918 case IORING_REGISTER_PROBE:
8919 ret = -EINVAL;
8920 if (!arg || nr_args > 256)
8921 break;
8922 ret = io_probe(ctx, arg, nr_args);
8923 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008924 case IORING_REGISTER_PERSONALITY:
8925 ret = -EINVAL;
8926 if (arg || nr_args)
8927 break;
8928 ret = io_register_personality(ctx);
8929 break;
8930 case IORING_UNREGISTER_PERSONALITY:
8931 ret = -EINVAL;
8932 if (arg)
8933 break;
8934 ret = io_unregister_personality(ctx, nr_args);
8935 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008936 default:
8937 ret = -EINVAL;
8938 break;
8939 }
8940
Jens Axboe071698e2020-01-28 10:04:42 -07008941 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008942 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008943 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008944out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008945 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008946 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008947 return ret;
8948}
8949
8950SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8951 void __user *, arg, unsigned int, nr_args)
8952{
8953 struct io_ring_ctx *ctx;
8954 long ret = -EBADF;
8955 struct fd f;
8956
8957 f = fdget(fd);
8958 if (!f.file)
8959 return -EBADF;
8960
8961 ret = -EOPNOTSUPP;
8962 if (f.file->f_op != &io_uring_fops)
8963 goto out_fput;
8964
8965 ctx = f.file->private_data;
8966
8967 mutex_lock(&ctx->uring_lock);
8968 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8969 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008970 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8971 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008972out_fput:
8973 fdput(f);
8974 return ret;
8975}
8976
Jens Axboe2b188cc2019-01-07 10:46:33 -07008977static int __init io_uring_init(void)
8978{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008979#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8980 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8981 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8982} while (0)
8983
8984#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8985 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8986 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8987 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8988 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8989 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8990 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8991 BUILD_BUG_SQE_ELEM(8, __u64, off);
8992 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8993 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008994 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008995 BUILD_BUG_SQE_ELEM(24, __u32, len);
8996 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8997 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8998 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8999 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009000 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9001 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009002 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9003 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9004 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9005 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9006 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9007 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9008 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9009 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009010 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009011 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9012 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9013 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009014 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009015
Jens Axboed3656342019-12-18 09:50:26 -07009016 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009017 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009018 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9019 return 0;
9020};
9021__initcall(io_uring_init);