blob: 1e4ac48b15572a4c9c008016ca450943c1b4f7ec [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;
494};
495
Jens Axboef499a022019-12-02 16:28:46 -0700496struct io_async_connect {
497 struct sockaddr_storage address;
498};
499
Jens Axboe03b12302019-12-02 18:50:25 -0700500struct io_async_msghdr {
501 struct iovec fast_iov[UIO_FASTIOV];
502 struct iovec *iov;
503 struct sockaddr __user *uaddr;
504 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700505 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700506};
507
Jens Axboef67676d2019-12-02 11:03:47 -0700508struct io_async_rw {
509 struct iovec fast_iov[UIO_FASTIOV];
510 struct iovec *iov;
511 ssize_t nr_segs;
512 ssize_t size;
Jens Axboebcf5a062020-05-22 09:24:42 -0600513 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700514};
515
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700516struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700517 union {
518 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700519 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700520 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700521 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700522 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700523};
524
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300525enum {
526 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
527 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
528 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
529 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
530 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700531 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300532
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300533 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300534 REQ_F_FAIL_LINK_BIT,
535 REQ_F_INFLIGHT_BIT,
536 REQ_F_CUR_POS_BIT,
537 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300538 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300539 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300540 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300541 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700542 REQ_F_OVERFLOW_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 Axboe2ca10252020-02-13 17:17:35 -0700585 /* in overflow list */
586 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700587 /* already went through poll handler */
588 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700589 /* buffer already selected */
590 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600591 /* doesn't need file table for this request */
592 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800593 /* io_wq_work is initialized */
594 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300595 /* req->task is refcounted */
596 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700597};
598
599struct async_poll {
600 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600601 struct io_poll_iocb *double_poll;
Jens Axboed7718a92020-02-14 22:23:12 -0700602 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300603};
604
Jens Axboe09bb8392019-03-13 12:39:28 -0600605/*
606 * NOTE! Each of the iocb union members has the file pointer
607 * as the first entry in their struct definition. So you can
608 * access the file pointer through any of the sub-structs,
609 * or directly as just 'ki_filp' in this struct.
610 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700611struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700612 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600613 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700614 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700615 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700616 struct io_accept accept;
617 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700618 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700619 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700620 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700621 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700622 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700623 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700624 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700625 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700626 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700627 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300628 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700629 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700630 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300631 /* use only after cleaning per-op data, see io_clean_op() */
632 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700633 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700634
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700635 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300636 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700637 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800638 /* polled IO has completed */
639 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700641 u16 buf_index;
642
Jens Axboe2b188cc2019-01-07 10:46:33 -0700643 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700644 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700646 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600647 struct task_struct *task;
648 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600650 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600651 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700652
Jens Axboed7718a92020-02-14 22:23:12 -0700653 struct list_head link_list;
654
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300655 /*
656 * 1. used with ctx->iopoll_list with reads/writes
657 * 2. to track reqs with ->files (see io_op_def::file_table)
658 */
Jens Axboefcb323c2019-10-24 12:39:47 -0600659 struct list_head inflight_entry;
660
Xiaoguang Wang05589552020-03-31 14:05:18 +0800661 struct percpu_ref *fixed_file_refs;
662
Jens Axboeb41e9852020-02-17 09:52:41 -0700663 union {
664 /*
665 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700666 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
667 * async armed poll handlers for regular commands. The latter
668 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700669 */
670 struct {
Jens Axboed7718a92020-02-14 22:23:12 -0700671 struct hlist_node hash_node;
672 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700673 };
674 struct io_wq_work work;
675 };
Pavel Begunkov8ef77762020-06-27 14:04:59 +0300676 struct callback_head task_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700677};
678
Jens Axboedef596e2019-01-09 08:59:42 -0700679#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700680
Jens Axboe013538b2020-06-22 09:29:15 -0600681struct io_comp_state {
682 unsigned int nr;
683 struct list_head list;
684 struct io_ring_ctx *ctx;
685};
686
Jens Axboe9a56a232019-01-09 09:06:50 -0700687struct io_submit_state {
688 struct blk_plug plug;
689
690 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700691 * io_kiocb alloc cache
692 */
693 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300694 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700695
696 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600697 * Batch completion logic
698 */
699 struct io_comp_state comp;
700
701 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700702 * File reference cache
703 */
704 struct file *file;
705 unsigned int fd;
706 unsigned int has_refs;
707 unsigned int used_refs;
708 unsigned int ios_left;
709};
710
Jens Axboed3656342019-12-18 09:50:26 -0700711struct io_op_def {
712 /* needs req->io allocated for deferral/async */
713 unsigned async_ctx : 1;
714 /* needs current->mm setup, does mm access */
715 unsigned needs_mm : 1;
716 /* needs req->file assigned */
717 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600718 /* don't fail if file grab fails */
719 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700720 /* hash wq insertion if file is a regular file */
721 unsigned hash_reg_file : 1;
722 /* unbound wq insertion if file is a non-regular file */
723 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700724 /* opcode is not supported by this kernel */
725 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700726 /* needs file table */
727 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700728 /* needs ->fs */
729 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700730 /* set if opcode supports polled "wait" */
731 unsigned pollin : 1;
732 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700733 /* op supports buffer selection */
734 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700735};
736
737static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_NOP] = {},
739 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700740 .async_ctx = 1,
741 .needs_mm = 1,
742 .needs_file = 1,
743 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700744 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700745 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .async_ctx = 1,
749 .needs_mm = 1,
750 .needs_file = 1,
751 .hash_reg_file = 1,
752 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700753 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700756 .needs_file = 1,
757 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300758 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700759 .needs_file = 1,
760 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700761 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700762 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700764 .needs_file = 1,
765 .hash_reg_file = 1,
766 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700767 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .needs_file = 1,
771 .unbound_nonreg_file = 1,
772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_POLL_REMOVE] = {},
774 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_file = 1,
776 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300777 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .async_ctx = 1,
779 .needs_mm = 1,
780 .needs_file = 1,
781 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700782 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700783 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .async_ctx = 1,
787 .needs_mm = 1,
788 .needs_file = 1,
789 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700790 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700791 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700792 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700793 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300794 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700795 .async_ctx = 1,
796 .needs_mm = 1,
797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_TIMEOUT_REMOVE] = {},
799 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700800 .needs_mm = 1,
801 .needs_file = 1,
802 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700803 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700804 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700805 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300806 [IORING_OP_ASYNC_CANCEL] = {},
807 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700808 .async_ctx = 1,
809 .needs_mm = 1,
810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700812 .async_ctx = 1,
813 .needs_mm = 1,
814 .needs_file = 1,
815 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700816 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .needs_file = 1,
820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700822 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700823 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600826 .needs_file = 1,
827 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700828 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700831 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700832 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700833 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700835 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700836 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600837 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700838 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300839 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700840 .needs_mm = 1,
841 .needs_file = 1,
842 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700843 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700844 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700847 .needs_mm = 1,
848 .needs_file = 1,
849 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700850 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700853 .needs_file = 1,
854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700856 .needs_mm = 1,
857 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300858 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700859 .needs_mm = 1,
860 .needs_file = 1,
861 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700862 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700865 .needs_mm = 1,
866 .needs_file = 1,
867 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700868 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700869 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700872 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700873 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700874 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700875 [IORING_OP_EPOLL_CTL] = {
876 .unbound_nonreg_file = 1,
877 .file_table = 1,
878 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300879 [IORING_OP_SPLICE] = {
880 .needs_file = 1,
881 .hash_reg_file = 1,
882 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700883 },
884 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700885 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300886 [IORING_OP_TEE] = {
887 .needs_file = 1,
888 .hash_reg_file = 1,
889 .unbound_nonreg_file = 1,
890 },
Jens Axboed3656342019-12-18 09:50:26 -0700891};
892
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700893enum io_mem_account {
894 ACCT_LOCKED,
895 ACCT_PINNED,
896};
897
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +0300898static bool io_rw_reissue(struct io_kiocb *req, long res);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700899static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800900static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600901static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700902static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700903static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
904static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700905static int __io_sqe_files_update(struct io_ring_ctx *ctx,
906 struct io_uring_files_update *ip,
907 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700908static int io_grab_files(struct io_kiocb *req);
Jens Axboe2237d762020-06-26 13:44:16 -0600909static void io_complete_rw_common(struct kiocb *kiocb, long res,
910 struct io_comp_state *cs);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300911static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700912static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
913 int fd, struct file **out_file, bool fixed);
914static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600915 const struct io_uring_sqe *sqe,
916 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600917static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600918
Jens Axboeb63534c2020-06-04 11:28:00 -0600919static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
920 struct iovec **iovec, struct iov_iter *iter,
921 bool needs_lock);
922static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
923 struct iovec *iovec, struct iovec *fast_iov,
924 struct iov_iter *iter);
925
Jens Axboe2b188cc2019-01-07 10:46:33 -0700926static struct kmem_cache *req_cachep;
927
928static const struct file_operations io_uring_fops;
929
930struct sock *io_uring_get_socket(struct file *file)
931{
932#if defined(CONFIG_UNIX)
933 if (file->f_op == &io_uring_fops) {
934 struct io_ring_ctx *ctx = file->private_data;
935
936 return ctx->ring_sock->sk;
937 }
938#endif
939 return NULL;
940}
941EXPORT_SYMBOL(io_uring_get_socket);
942
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300943static void io_get_req_task(struct io_kiocb *req)
944{
945 if (req->flags & REQ_F_TASK_PINNED)
946 return;
947 get_task_struct(req->task);
948 req->flags |= REQ_F_TASK_PINNED;
949}
950
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300951static inline void io_clean_op(struct io_kiocb *req)
952{
953 if (req->flags & REQ_F_NEED_CLEANUP)
954 __io_clean_op(req);
955}
956
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300957/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
958static void __io_put_req_task(struct io_kiocb *req)
959{
960 if (req->flags & REQ_F_TASK_PINNED)
961 put_task_struct(req->task);
962}
963
Jens Axboe4349f302020-07-09 15:07:01 -0600964static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600965{
966 struct mm_struct *mm = current->mm;
967
968 if (mm) {
969 kthread_unuse_mm(mm);
970 mmput(mm);
971 }
972}
973
974static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
975{
976 if (!current->mm) {
Pavel Begunkov8eb06d72020-06-30 15:20:39 +0300977 if (unlikely(!ctx->sqo_mm || !mmget_not_zero(ctx->sqo_mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600978 return -EFAULT;
979 kthread_use_mm(ctx->sqo_mm);
980 }
981
982 return 0;
983}
984
985static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
986 struct io_kiocb *req)
987{
988 if (!io_op_defs[req->opcode].needs_mm)
989 return 0;
990 return __io_sq_thread_acquire_mm(ctx);
991}
992
993static inline void req_set_fail_links(struct io_kiocb *req)
994{
995 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
996 req->flags |= REQ_F_FAIL_LINK;
997}
998
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800999/*
1000 * Note: must call io_req_init_async() for the first time you
1001 * touch any members of io_wq_work.
1002 */
1003static inline void io_req_init_async(struct io_kiocb *req)
1004{
1005 if (req->flags & REQ_F_WORK_INITIALIZED)
1006 return;
1007
1008 memset(&req->work, 0, sizeof(req->work));
1009 req->flags |= REQ_F_WORK_INITIALIZED;
1010}
1011
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001012static inline bool io_async_submit(struct io_ring_ctx *ctx)
1013{
1014 return ctx->flags & IORING_SETUP_SQPOLL;
1015}
1016
Jens Axboe2b188cc2019-01-07 10:46:33 -07001017static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1018{
1019 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1020
Jens Axboe0f158b42020-05-14 17:18:39 -06001021 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022}
1023
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001024static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1025{
1026 return !req->timeout.off;
1027}
1028
Jens Axboe2b188cc2019-01-07 10:46:33 -07001029static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1030{
1031 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001032 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001033
1034 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1035 if (!ctx)
1036 return NULL;
1037
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001038 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1039 if (!ctx->fallback_req)
1040 goto err;
1041
Jens Axboe78076bb2019-12-04 19:56:40 -07001042 /*
1043 * Use 5 bits less than the max cq entries, that should give us around
1044 * 32 entries per hash list if totally full and uniformly spread.
1045 */
1046 hash_bits = ilog2(p->cq_entries);
1047 hash_bits -= 5;
1048 if (hash_bits <= 0)
1049 hash_bits = 1;
1050 ctx->cancel_hash_bits = hash_bits;
1051 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1052 GFP_KERNEL);
1053 if (!ctx->cancel_hash)
1054 goto err;
1055 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1056
Roman Gushchin21482892019-05-07 10:01:48 -07001057 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001058 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1059 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060
1061 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001062 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001063 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001064 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001065 init_completion(&ctx->ref_comp);
1066 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001067 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001068 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001069 mutex_init(&ctx->uring_lock);
1070 init_waitqueue_head(&ctx->wait);
1071 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001072 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001073 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001074 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001075 init_waitqueue_head(&ctx->inflight_wait);
1076 spin_lock_init(&ctx->inflight_lock);
1077 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001078 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1079 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001080 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001081err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001082 if (ctx->fallback_req)
1083 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001084 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001085 kfree(ctx);
1086 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087}
1088
Bob Liu9d858b22019-11-13 18:06:25 +08001089static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001090{
Jens Axboe2bc99302020-07-09 09:43:27 -06001091 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1092 struct io_ring_ctx *ctx = req->ctx;
1093
1094 return req->sequence != ctx->cached_cq_tail
1095 + atomic_read(&ctx->cached_cq_overflow);
1096 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001097
Bob Liu9d858b22019-11-13 18:06:25 +08001098 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001099}
1100
Jens Axboede0617e2019-04-06 21:51:27 -06001101static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102{
Hristo Venev75b28af2019-08-26 17:23:46 +00001103 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104
Pavel Begunkov07910152020-01-17 03:52:46 +03001105 /* order cqe stores with ring update */
1106 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001107
Pavel Begunkov07910152020-01-17 03:52:46 +03001108 if (wq_has_sleeper(&ctx->cq_wait)) {
1109 wake_up_interruptible(&ctx->cq_wait);
1110 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111 }
1112}
1113
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001114static void io_req_work_grab_env(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -06001115{
Pavel Begunkov351fd532020-06-29 19:18:40 +03001116 const struct io_op_def *def = &io_op_defs[req->opcode];
1117
Pavel Begunkovedcdfcc2020-06-29 19:18:41 +03001118 io_req_init_async(req);
1119
Jens Axboecccf0ee2020-01-27 16:34:48 -07001120 if (!req->work.mm && def->needs_mm) {
1121 mmgrab(current->mm);
1122 req->work.mm = current->mm;
1123 }
1124 if (!req->work.creds)
1125 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001126 if (!req->work.fs && def->needs_fs) {
1127 spin_lock(&current->fs->lock);
1128 if (!current->fs->in_exec) {
1129 req->work.fs = current->fs;
1130 req->work.fs->users++;
1131 } else {
1132 req->work.flags |= IO_WQ_WORK_CANCEL;
1133 }
1134 spin_unlock(&current->fs->lock);
1135 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001136}
1137
1138static inline void io_req_work_drop_env(struct io_kiocb *req)
1139{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001140 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1141 return;
1142
Jens Axboecccf0ee2020-01-27 16:34:48 -07001143 if (req->work.mm) {
1144 mmdrop(req->work.mm);
1145 req->work.mm = NULL;
1146 }
1147 if (req->work.creds) {
1148 put_cred(req->work.creds);
1149 req->work.creds = NULL;
1150 }
Jens Axboeff002b32020-02-07 16:05:21 -07001151 if (req->work.fs) {
1152 struct fs_struct *fs = req->work.fs;
1153
1154 spin_lock(&req->work.fs->lock);
1155 if (--fs->users)
1156 fs = NULL;
1157 spin_unlock(&req->work.fs->lock);
1158 if (fs)
1159 free_fs_struct(fs);
1160 }
Jens Axboe561fb042019-10-24 07:25:42 -06001161}
1162
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001163static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001164{
Jens Axboed3656342019-12-18 09:50:26 -07001165 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001166
Pavel Begunkov16d59802020-07-12 16:16:47 +03001167 io_req_init_async(req);
1168
Jens Axboed3656342019-12-18 09:50:26 -07001169 if (req->flags & REQ_F_ISREG) {
1170 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001171 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001172 } else {
1173 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001174 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001175 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001176
Pavel Begunkov351fd532020-06-29 19:18:40 +03001177 io_req_work_grab_env(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001178}
1179
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001180static void io_prep_async_link(struct io_kiocb *req)
1181{
1182 struct io_kiocb *cur;
1183
1184 io_prep_async_work(req);
1185 if (req->flags & REQ_F_LINK_HEAD)
1186 list_for_each_entry(cur, &req->link_list, link_list)
1187 io_prep_async_work(cur);
1188}
1189
1190static void __io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001191{
Jackie Liua197f662019-11-08 08:09:12 -07001192 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001193 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001194
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001195 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1196 &req->work, req->flags);
1197 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001198
1199 if (link)
1200 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001201}
1202
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001203static void io_queue_async_work(struct io_kiocb *req)
1204{
1205 /* init ->work of the whole link before punting */
1206 io_prep_async_link(req);
1207 __io_queue_async_work(req);
1208}
1209
Jens Axboe5262f562019-09-17 12:26:57 -06001210static void io_kill_timeout(struct io_kiocb *req)
1211{
1212 int ret;
1213
Jens Axboe2d283902019-12-04 11:08:05 -07001214 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001215 if (ret != -1) {
1216 atomic_inc(&req->ctx->cq_timeouts);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001217 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001218 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001219 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001220 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001221 }
1222}
1223
1224static void io_kill_timeouts(struct io_ring_ctx *ctx)
1225{
1226 struct io_kiocb *req, *tmp;
1227
1228 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001229 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list)
Jens Axboe5262f562019-09-17 12:26:57 -06001230 io_kill_timeout(req);
1231 spin_unlock_irq(&ctx->completion_lock);
1232}
1233
Pavel Begunkov04518942020-05-26 20:34:05 +03001234static void __io_queue_deferred(struct io_ring_ctx *ctx)
1235{
1236 do {
1237 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1238 struct io_kiocb, list);
1239
1240 if (req_need_defer(req))
1241 break;
1242 list_del_init(&req->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001243 /* punt-init is done before queueing for defer */
1244 __io_queue_async_work(req);
Pavel Begunkov04518942020-05-26 20:34:05 +03001245 } while (!list_empty(&ctx->defer_list));
1246}
1247
Pavel Begunkov360428f2020-05-30 14:54:17 +03001248static void io_flush_timeouts(struct io_ring_ctx *ctx)
1249{
1250 while (!list_empty(&ctx->timeout_list)) {
1251 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001252 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001253
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001254 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001255 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001256 if (req->timeout.target_seq != ctx->cached_cq_tail
1257 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001258 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001259
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001260 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001261 io_kill_timeout(req);
1262 }
1263}
1264
Jens Axboede0617e2019-04-06 21:51:27 -06001265static void io_commit_cqring(struct io_ring_ctx *ctx)
1266{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001267 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001268 __io_commit_cqring(ctx);
1269
Pavel Begunkov04518942020-05-26 20:34:05 +03001270 if (unlikely(!list_empty(&ctx->defer_list)))
1271 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001272}
1273
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1275{
Hristo Venev75b28af2019-08-26 17:23:46 +00001276 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001277 unsigned tail;
1278
1279 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001280 /*
1281 * writes to the cq entry need to come after reading head; the
1282 * control dependency is enough as we're using WRITE_ONCE to
1283 * fill the cq entry
1284 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001285 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286 return NULL;
1287
1288 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001289 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290}
1291
Jens Axboef2842ab2020-01-08 11:04:00 -07001292static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1293{
Jens Axboef0b493e2020-02-01 21:30:11 -07001294 if (!ctx->cq_ev_fd)
1295 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001296 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1297 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001298 if (!ctx->eventfd_async)
1299 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001300 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001301}
1302
Jens Axboeb41e9852020-02-17 09:52:41 -07001303static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001304{
1305 if (waitqueue_active(&ctx->wait))
1306 wake_up(&ctx->wait);
1307 if (waitqueue_active(&ctx->sqo_wait))
1308 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001309 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001310 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001311}
1312
Jens Axboec4a2ed72019-11-21 21:01:26 -07001313/* Returns true if there are no backlogged entries after the flush */
1314static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001316 struct io_rings *rings = ctx->rings;
1317 struct io_uring_cqe *cqe;
1318 struct io_kiocb *req;
1319 unsigned long flags;
1320 LIST_HEAD(list);
1321
1322 if (!force) {
1323 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001324 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001325 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1326 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001327 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001328 }
1329
1330 spin_lock_irqsave(&ctx->completion_lock, flags);
1331
1332 /* if force is set, the ring is going away. always drop after that */
1333 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001334 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001335
Jens Axboec4a2ed72019-11-21 21:01:26 -07001336 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001337 while (!list_empty(&ctx->cq_overflow_list)) {
1338 cqe = io_get_cqring(ctx);
1339 if (!cqe && !force)
1340 break;
1341
1342 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001343 compl.list);
1344 list_move(&req->compl.list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001345 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001346 if (cqe) {
1347 WRITE_ONCE(cqe->user_data, req->user_data);
1348 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001349 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001350 } else {
1351 WRITE_ONCE(ctx->rings->cq_overflow,
1352 atomic_inc_return(&ctx->cached_cq_overflow));
1353 }
1354 }
1355
1356 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001357 if (cqe) {
1358 clear_bit(0, &ctx->sq_check_overflow);
1359 clear_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001360 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001361 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001362 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1363 io_cqring_ev_posted(ctx);
1364
1365 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001366 req = list_first_entry(&list, struct io_kiocb, compl.list);
1367 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001368 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001369 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001370
1371 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001372}
1373
Jens Axboebcda7ba2020-02-23 16:42:51 -07001374static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001375{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001376 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001377 struct io_uring_cqe *cqe;
1378
Jens Axboe78e19bb2019-11-06 15:21:34 -07001379 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001380
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381 /*
1382 * If we can't get a cq entry, userspace overflowed the
1383 * submission (by quite a lot). Increment the overflow count in
1384 * the ring.
1385 */
1386 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001387 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001388 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001390 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001391 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001392 WRITE_ONCE(ctx->rings->cq_overflow,
1393 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001394 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001395 if (list_empty(&ctx->cq_overflow_list)) {
1396 set_bit(0, &ctx->sq_check_overflow);
1397 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001398 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001399 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001400 io_clean_op(req);
Jens Axboe2ca10252020-02-13 17:17:35 -07001401 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001402 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001403 req->cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001404 refcount_inc(&req->refs);
1405 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406 }
1407}
1408
Jens Axboebcda7ba2020-02-23 16:42:51 -07001409static void io_cqring_fill_event(struct io_kiocb *req, long res)
1410{
1411 __io_cqring_fill_event(req, res, 0);
1412}
1413
Jens Axboee1e16092020-06-22 09:17:17 -06001414static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001416 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001417 unsigned long flags;
1418
1419 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001420 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001421 io_commit_cqring(ctx);
1422 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1423
Jens Axboe8c838782019-03-12 15:48:16 -06001424 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001425}
1426
Jens Axboe229a7b62020-06-22 10:13:11 -06001427static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001428{
Jens Axboe229a7b62020-06-22 10:13:11 -06001429 struct io_ring_ctx *ctx = cs->ctx;
1430
1431 spin_lock_irq(&ctx->completion_lock);
1432 while (!list_empty(&cs->list)) {
1433 struct io_kiocb *req;
1434
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001435 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1436 list_del(&req->compl.list);
Pavel Begunkov8b3656a2020-07-03 22:15:08 +03001437 __io_cqring_fill_event(req, req->result, req->cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001438 if (!(req->flags & REQ_F_LINK_HEAD)) {
1439 req->flags |= REQ_F_COMP_LOCKED;
1440 io_put_req(req);
1441 } else {
1442 spin_unlock_irq(&ctx->completion_lock);
1443 io_put_req(req);
1444 spin_lock_irq(&ctx->completion_lock);
1445 }
1446 }
1447 io_commit_cqring(ctx);
1448 spin_unlock_irq(&ctx->completion_lock);
1449
1450 io_cqring_ev_posted(ctx);
1451 cs->nr = 0;
1452}
1453
1454static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1455 struct io_comp_state *cs)
1456{
1457 if (!cs) {
1458 io_cqring_add_event(req, res, cflags);
1459 io_put_req(req);
1460 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001461 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001462 req->result = res;
Pavel Begunkov8b3656a2020-07-03 22:15:08 +03001463 req->cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001464 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001465 if (++cs->nr >= 32)
1466 io_submit_flush_completions(cs);
1467 }
Jens Axboee1e16092020-06-22 09:17:17 -06001468}
1469
1470static void io_req_complete(struct io_kiocb *req, long res)
1471{
Jens Axboe229a7b62020-06-22 10:13:11 -06001472 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001473}
1474
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001475static inline bool io_is_fallback_req(struct io_kiocb *req)
1476{
1477 return req == (struct io_kiocb *)
1478 ((unsigned long) req->ctx->fallback_req & ~1UL);
1479}
1480
1481static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1482{
1483 struct io_kiocb *req;
1484
1485 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001486 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001487 return req;
1488
1489 return NULL;
1490}
1491
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001492static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1493 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001494{
Jens Axboefd6fab22019-03-14 16:30:06 -06001495 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001496 struct io_kiocb *req;
1497
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001498 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001499 size_t sz;
1500 int ret;
1501
1502 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001503 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1504
1505 /*
1506 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1507 * retry single alloc to be on the safe side.
1508 */
1509 if (unlikely(ret <= 0)) {
1510 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1511 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001512 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001513 ret = 1;
1514 }
Jens Axboe2579f912019-01-09 09:10:43 -07001515 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001516 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001517 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001518 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001519 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001520 }
1521
Jens Axboe2579f912019-01-09 09:10:43 -07001522 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001523fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001524 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001525}
1526
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001527static inline void io_put_file(struct io_kiocb *req, struct file *file,
1528 bool fixed)
1529{
1530 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001531 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001532 else
1533 fput(file);
1534}
1535
Pavel Begunkove6543a82020-06-28 12:52:30 +03001536static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001537{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001538 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001539
Jens Axboe5acbbc82020-07-08 15:15:26 -06001540 if (req->io)
1541 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001542 if (req->file)
1543 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001544 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001545 io_req_work_drop_env(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001546
Jens Axboefcb323c2019-10-24 12:39:47 -06001547 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001548 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001549 unsigned long flags;
1550
1551 spin_lock_irqsave(&ctx->inflight_lock, flags);
1552 list_del(&req->inflight_entry);
1553 if (waitqueue_active(&ctx->inflight_wait))
1554 wake_up(&ctx->inflight_wait);
1555 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1556 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001557}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001558
Pavel Begunkove6543a82020-06-28 12:52:30 +03001559static void __io_free_req(struct io_kiocb *req)
1560{
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001561 struct io_ring_ctx *ctx;
1562
Pavel Begunkove6543a82020-06-28 12:52:30 +03001563 io_dismantle_req(req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001564 ctx = req->ctx;
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001565 if (likely(!io_is_fallback_req(req)))
1566 kmem_cache_free(req_cachep, req);
1567 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001568 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1569 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001570}
1571
Jackie Liua197f662019-11-08 08:09:12 -07001572static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001573{
Jackie Liua197f662019-11-08 08:09:12 -07001574 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001575 int ret;
1576
Jens Axboe2d283902019-12-04 11:08:05 -07001577 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001578 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001579 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001580 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001581 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001582 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001583 return true;
1584 }
1585
1586 return false;
1587}
1588
Jens Axboeab0b6452020-06-30 08:43:15 -06001589static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001590{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001591 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001592 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001593
1594 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001595 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001596 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1597 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001598 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001599
1600 list_del_init(&link->link_list);
1601 wake_ev = io_link_cancel_timeout(link);
1602 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001603 return wake_ev;
1604}
1605
1606static void io_kill_linked_timeout(struct io_kiocb *req)
1607{
1608 struct io_ring_ctx *ctx = req->ctx;
1609 bool wake_ev;
1610
1611 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1612 unsigned long flags;
1613
1614 spin_lock_irqsave(&ctx->completion_lock, flags);
1615 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001616 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001617 } else {
1618 wake_ev = __io_kill_linked_timeout(req);
1619 }
1620
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001621 if (wake_ev)
1622 io_cqring_ev_posted(ctx);
1623}
1624
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001625static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001626{
1627 struct io_kiocb *nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001628
1629 /*
1630 * The list should never be empty when we are called here. But could
1631 * potentially happen if the chain is messed up, check to be on the
1632 * safe side.
1633 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001634 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001635 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001636
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001637 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1638 list_del_init(&req->link_list);
1639 if (!list_empty(&nxt->link_list))
1640 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001641 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001642}
1643
1644/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001645 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001646 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001647static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001648{
Jens Axboe2665abf2019-11-05 12:40:47 -07001649 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001650
1651 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001652 struct io_kiocb *link = list_first_entry(&req->link_list,
1653 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001654
Pavel Begunkov44932332019-12-05 16:16:35 +03001655 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001656 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001657
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001658 io_cqring_fill_event(link, -ECANCELED);
1659 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001660 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001661 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001662
1663 io_commit_cqring(ctx);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001664 io_cqring_ev_posted(ctx);
1665}
1666
1667static void io_fail_links(struct io_kiocb *req)
1668{
1669 struct io_ring_ctx *ctx = req->ctx;
1670
1671 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1672 unsigned long flags;
1673
1674 spin_lock_irqsave(&ctx->completion_lock, flags);
1675 __io_fail_links(req);
1676 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1677 } else {
1678 __io_fail_links(req);
1679 }
1680
Jens Axboe2665abf2019-11-05 12:40:47 -07001681 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001682}
1683
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001684static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001685{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001686 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001687 if (req->flags & REQ_F_LINK_TIMEOUT)
1688 io_kill_linked_timeout(req);
1689
Jens Axboe9e645e112019-05-10 16:07:28 -06001690 /*
1691 * If LINK is set, we have dependent requests in this chain. If we
1692 * didn't fail this request, queue the first one up, moving any other
1693 * dependencies to the next request. In case of failure, fail the rest
1694 * of the chain.
1695 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001696 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1697 return io_req_link_next(req);
1698 io_fail_links(req);
1699 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001700}
Jens Axboe9e645e112019-05-10 16:07:28 -06001701
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001702static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1703{
1704 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1705 return NULL;
1706 return __io_req_find_next(req);
1707}
1708
Jens Axboec2c4c832020-07-01 15:37:11 -06001709static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb)
1710{
1711 struct task_struct *tsk = req->task;
1712 struct io_ring_ctx *ctx = req->ctx;
1713 int ret, notify = TWA_RESUME;
1714
1715 /*
1716 * SQPOLL kernel thread doesn't need notification, just a wakeup.
1717 * If we're not using an eventfd, then TWA_RESUME is always fine,
1718 * as we won't have dependencies between request completions for
1719 * other kernel wait conditions.
1720 */
1721 if (ctx->flags & IORING_SETUP_SQPOLL)
1722 notify = 0;
1723 else if (ctx->cq_ev_fd)
1724 notify = TWA_SIGNAL;
1725
1726 ret = task_work_add(tsk, cb, notify);
1727 if (!ret)
1728 wake_up_process(tsk);
1729 return ret;
1730}
1731
Jens Axboec40f6372020-06-25 15:39:59 -06001732static void __io_req_task_cancel(struct io_kiocb *req, int error)
1733{
1734 struct io_ring_ctx *ctx = req->ctx;
1735
1736 spin_lock_irq(&ctx->completion_lock);
1737 io_cqring_fill_event(req, error);
1738 io_commit_cqring(ctx);
1739 spin_unlock_irq(&ctx->completion_lock);
1740
1741 io_cqring_ev_posted(ctx);
1742 req_set_fail_links(req);
1743 io_double_put_req(req);
1744}
1745
1746static void io_req_task_cancel(struct callback_head *cb)
1747{
1748 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1749
1750 __io_req_task_cancel(req, -ECANCELED);
1751}
1752
1753static void __io_req_task_submit(struct io_kiocb *req)
1754{
1755 struct io_ring_ctx *ctx = req->ctx;
1756
Jens Axboec40f6372020-06-25 15:39:59 -06001757 if (!__io_sq_thread_acquire_mm(ctx)) {
1758 mutex_lock(&ctx->uring_lock);
1759 __io_queue_sqe(req, NULL, NULL);
1760 mutex_unlock(&ctx->uring_lock);
1761 } else {
1762 __io_req_task_cancel(req, -EFAULT);
1763 }
1764}
1765
1766static void io_req_task_submit(struct callback_head *cb)
1767{
1768 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1769
1770 __io_req_task_submit(req);
1771}
1772
1773static void io_req_task_queue(struct io_kiocb *req)
1774{
Jens Axboec40f6372020-06-25 15:39:59 -06001775 int ret;
1776
1777 init_task_work(&req->task_work, io_req_task_submit);
1778
Jens Axboec2c4c832020-07-01 15:37:11 -06001779 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboec40f6372020-06-25 15:39:59 -06001780 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001781 struct task_struct *tsk;
1782
Jens Axboec40f6372020-06-25 15:39:59 -06001783 init_task_work(&req->task_work, io_req_task_cancel);
1784 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001785 task_work_add(tsk, &req->task_work, 0);
1786 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001787 }
Jens Axboec40f6372020-06-25 15:39:59 -06001788}
1789
Pavel Begunkovc3524382020-06-28 12:52:32 +03001790static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001791{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001792 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001793
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001794 if (nxt)
1795 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001796}
1797
Pavel Begunkovc3524382020-06-28 12:52:32 +03001798static void io_free_req(struct io_kiocb *req)
1799{
1800 io_queue_next(req);
1801 __io_free_req(req);
1802}
1803
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001804struct req_batch {
1805 void *reqs[IO_IOPOLL_BATCH];
1806 int to_free;
1807};
1808
1809static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1810 struct req_batch *rb)
1811{
1812 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1813 percpu_ref_put_many(&ctx->refs, rb->to_free);
1814 rb->to_free = 0;
1815}
1816
1817static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1818 struct req_batch *rb)
1819{
1820 if (rb->to_free)
1821 __io_req_free_batch_flush(ctx, rb);
1822}
1823
1824static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1825{
1826 if (unlikely(io_is_fallback_req(req))) {
1827 io_free_req(req);
1828 return;
1829 }
1830 if (req->flags & REQ_F_LINK_HEAD)
1831 io_queue_next(req);
1832
1833 io_dismantle_req(req);
1834 rb->reqs[rb->to_free++] = req;
1835 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1836 __io_req_free_batch_flush(req->ctx, rb);
1837}
1838
Jens Axboeba816ad2019-09-28 11:36:45 -06001839/*
1840 * Drop reference to request, return next in chain (if there is one) if this
1841 * was the last reference to this request.
1842 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001843static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001844{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001845 struct io_kiocb *nxt = NULL;
1846
Jens Axboe2a44f462020-02-25 13:25:41 -07001847 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001848 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001849 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001850 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001851 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001852}
1853
Jens Axboe2b188cc2019-01-07 10:46:33 -07001854static void io_put_req(struct io_kiocb *req)
1855{
Jens Axboedef596e2019-01-09 08:59:42 -07001856 if (refcount_dec_and_test(&req->refs))
1857 io_free_req(req);
1858}
1859
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001860static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001861{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001862 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001863
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001864 /*
1865 * A ref is owned by io-wq in which context we're. So, if that's the
1866 * last one, it's safe to steal next work. False negatives are Ok,
1867 * it just will be re-punted async in io_put_work()
1868 */
1869 if (refcount_read(&req->refs) != 1)
1870 return NULL;
1871
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001872 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001873 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001874}
1875
Jens Axboe978db572019-11-14 22:39:04 -07001876/*
1877 * Must only be used if we don't need to care about links, usually from
1878 * within the completion handling itself.
1879 */
1880static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001881{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001882 /* drop both submit and complete references */
1883 if (refcount_sub_and_test(2, &req->refs))
1884 __io_free_req(req);
1885}
1886
Jens Axboe978db572019-11-14 22:39:04 -07001887static void io_double_put_req(struct io_kiocb *req)
1888{
1889 /* drop both submit and complete references */
1890 if (refcount_sub_and_test(2, &req->refs))
1891 io_free_req(req);
1892}
1893
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001894static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001895{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001896 struct io_rings *rings = ctx->rings;
1897
Jens Axboead3eb2c2019-12-18 17:12:20 -07001898 if (test_bit(0, &ctx->cq_check_overflow)) {
1899 /*
1900 * noflush == true is from the waitqueue handler, just ensure
1901 * we wake up the task, and the next invocation will flush the
1902 * entries. We cannot safely to it from here.
1903 */
1904 if (noflush && !list_empty(&ctx->cq_overflow_list))
1905 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001906
Jens Axboead3eb2c2019-12-18 17:12:20 -07001907 io_cqring_overflow_flush(ctx, false);
1908 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001909
Jens Axboea3a0e432019-08-20 11:03:11 -06001910 /* See comment at the top of this file */
1911 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001912 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001913}
1914
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001915static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1916{
1917 struct io_rings *rings = ctx->rings;
1918
1919 /* make sure SQ entry isn't read before tail */
1920 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1921}
1922
Jens Axboebcda7ba2020-02-23 16:42:51 -07001923static int io_put_kbuf(struct io_kiocb *req)
1924{
Jens Axboe4d954c22020-02-27 07:31:19 -07001925 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001926 int cflags;
1927
Jens Axboe4d954c22020-02-27 07:31:19 -07001928 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001929 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1930 cflags |= IORING_CQE_F_BUFFER;
1931 req->rw.addr = 0;
1932 kfree(kbuf);
1933 return cflags;
1934}
1935
Jens Axboe4c6e2772020-07-01 11:29:10 -06001936static inline bool io_run_task_work(void)
1937{
1938 if (current->task_works) {
1939 __set_current_state(TASK_RUNNING);
1940 task_work_run();
1941 return true;
1942 }
1943
1944 return false;
1945}
1946
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001947static void io_iopoll_queue(struct list_head *again)
1948{
1949 struct io_kiocb *req;
1950
1951 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03001952 req = list_first_entry(again, struct io_kiocb, inflight_entry);
1953 list_del(&req->inflight_entry);
Pavel Begunkovcf2f5422020-06-30 15:20:40 +03001954 if (!io_rw_reissue(req, -EAGAIN))
Jens Axboe2237d762020-06-26 13:44:16 -06001955 io_complete_rw_common(&req->rw.kiocb, -EAGAIN, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001956 } while (!list_empty(again));
1957}
1958
Jens Axboedef596e2019-01-09 08:59:42 -07001959/*
1960 * Find and free completed poll iocbs
1961 */
1962static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1963 struct list_head *done)
1964{
Jens Axboe8237e042019-12-28 10:48:22 -07001965 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001966 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001967 LIST_HEAD(again);
1968
1969 /* order with ->result store in io_complete_rw_iopoll() */
1970 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001971
Pavel Begunkov2757a232020-06-28 12:52:31 +03001972 rb.to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001973 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001974 int cflags = 0;
1975
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03001976 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001977 if (READ_ONCE(req->result) == -EAGAIN) {
1978 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03001979 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001980 continue;
1981 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03001982 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07001983
Jens Axboebcda7ba2020-02-23 16:42:51 -07001984 if (req->flags & REQ_F_BUFFER_SELECTED)
1985 cflags = io_put_kbuf(req);
1986
1987 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001988 (*nr_events)++;
1989
Pavel Begunkovc3524382020-06-28 12:52:32 +03001990 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001991 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07001992 }
Jens Axboedef596e2019-01-09 08:59:42 -07001993
Jens Axboe09bb8392019-03-13 12:39:28 -06001994 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001995 if (ctx->flags & IORING_SETUP_SQPOLL)
1996 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001997 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001998
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001999 if (!list_empty(&again))
2000 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002001}
2002
Jens Axboedef596e2019-01-09 08:59:42 -07002003static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2004 long min)
2005{
2006 struct io_kiocb *req, *tmp;
2007 LIST_HEAD(done);
2008 bool spin;
2009 int ret;
2010
2011 /*
2012 * Only spin for completions if we don't have multiple devices hanging
2013 * off our complete list, and we're under the requested amount.
2014 */
2015 spin = !ctx->poll_multi_file && *nr_events < min;
2016
2017 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002018 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002019 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002020
2021 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002022 * Move completed and retryable entries to our local lists.
2023 * If we find a request that requires polling, break out
2024 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002025 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002026 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002027 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002028 continue;
2029 }
2030 if (!list_empty(&done))
2031 break;
2032
2033 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2034 if (ret < 0)
2035 break;
2036
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002037 /* iopoll may have completed current req */
2038 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002039 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002040
Jens Axboedef596e2019-01-09 08:59:42 -07002041 if (ret && spin)
2042 spin = false;
2043 ret = 0;
2044 }
2045
2046 if (!list_empty(&done))
2047 io_iopoll_complete(ctx, nr_events, &done);
2048
2049 return ret;
2050}
2051
2052/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002053 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002054 * non-spinning poll check - we'll still enter the driver poll loop, but only
2055 * as a non-spinning completion check.
2056 */
2057static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2058 long min)
2059{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002060 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002061 int ret;
2062
2063 ret = io_do_iopoll(ctx, nr_events, min);
2064 if (ret < 0)
2065 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002066 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002067 return 0;
2068 }
2069
2070 return 1;
2071}
2072
2073/*
2074 * We can't just wait for polled events to come to us, we have to actively
2075 * find and complete them.
2076 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002077static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002078{
2079 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2080 return;
2081
2082 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002083 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002084 unsigned int nr_events = 0;
2085
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002086 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002087
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002088 /* let it sleep and repeat later if can't complete a request */
2089 if (nr_events == 0)
2090 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002091 /*
2092 * Ensure we allow local-to-the-cpu processing to take place,
2093 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002094 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002095 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002096 if (need_resched()) {
2097 mutex_unlock(&ctx->uring_lock);
2098 cond_resched();
2099 mutex_lock(&ctx->uring_lock);
2100 }
Jens Axboedef596e2019-01-09 08:59:42 -07002101 }
2102 mutex_unlock(&ctx->uring_lock);
2103}
2104
Pavel Begunkov7668b922020-07-07 16:36:21 +03002105static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002106{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002107 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002108 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002109
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002110 /*
2111 * We disallow the app entering submit/complete with polling, but we
2112 * still need to lock the ring to prevent racing with polled issue
2113 * that got punted to a workqueue.
2114 */
2115 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002116 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002117 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002118 * Don't enter poll loop if we already have events pending.
2119 * If we do, we can potentially be spinning for commands that
2120 * already triggered a CQE (eg in error).
2121 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002122 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002123 break;
2124
2125 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002126 * If a submit got punted to a workqueue, we can have the
2127 * application entering polling for a command before it gets
2128 * issued. That app will hold the uring_lock for the duration
2129 * of the poll right here, so we need to take a breather every
2130 * now and then to ensure that the issue has a chance to add
2131 * the poll to the issued list. Otherwise we can spin here
2132 * forever, while the workqueue is stuck trying to acquire the
2133 * very same mutex.
2134 */
2135 if (!(++iters & 7)) {
2136 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002137 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002138 mutex_lock(&ctx->uring_lock);
2139 }
2140
Pavel Begunkov7668b922020-07-07 16:36:21 +03002141 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002142 if (ret <= 0)
2143 break;
2144 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002145 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002146
Jens Axboe500f9fb2019-08-19 12:15:59 -06002147 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002148 return ret;
2149}
2150
Jens Axboe491381ce2019-10-17 09:20:46 -06002151static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002152{
Jens Axboe491381ce2019-10-17 09:20:46 -06002153 /*
2154 * Tell lockdep we inherited freeze protection from submission
2155 * thread.
2156 */
2157 if (req->flags & REQ_F_ISREG) {
2158 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002159
Jens Axboe491381ce2019-10-17 09:20:46 -06002160 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002161 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002162 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002163}
2164
Jens Axboea1d7c392020-06-22 11:09:46 -06002165static void io_complete_rw_common(struct kiocb *kiocb, long res,
2166 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002167{
Jens Axboe9adbd452019-12-20 08:45:55 -07002168 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002169 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002170
Jens Axboe491381ce2019-10-17 09:20:46 -06002171 if (kiocb->ki_flags & IOCB_WRITE)
2172 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002173
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002174 if (res != req->result)
2175 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002176 if (req->flags & REQ_F_BUFFER_SELECTED)
2177 cflags = io_put_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002178 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002179}
2180
Jens Axboeb63534c2020-06-04 11:28:00 -06002181#ifdef CONFIG_BLOCK
2182static bool io_resubmit_prep(struct io_kiocb *req, int error)
2183{
2184 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2185 ssize_t ret = -ECANCELED;
2186 struct iov_iter iter;
2187 int rw;
2188
2189 if (error) {
2190 ret = error;
2191 goto end_req;
2192 }
2193
2194 switch (req->opcode) {
2195 case IORING_OP_READV:
2196 case IORING_OP_READ_FIXED:
2197 case IORING_OP_READ:
2198 rw = READ;
2199 break;
2200 case IORING_OP_WRITEV:
2201 case IORING_OP_WRITE_FIXED:
2202 case IORING_OP_WRITE:
2203 rw = WRITE;
2204 break;
2205 default:
2206 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2207 req->opcode);
2208 goto end_req;
2209 }
2210
2211 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2212 if (ret < 0)
2213 goto end_req;
2214 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2215 if (!ret)
2216 return true;
2217 kfree(iovec);
2218end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002219 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002220 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002221 return false;
2222}
2223
2224static void io_rw_resubmit(struct callback_head *cb)
2225{
2226 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2227 struct io_ring_ctx *ctx = req->ctx;
2228 int err;
2229
Jens Axboeb63534c2020-06-04 11:28:00 -06002230 err = io_sq_thread_acquire_mm(ctx, req);
2231
2232 if (io_resubmit_prep(req, err)) {
2233 refcount_inc(&req->refs);
2234 io_queue_async_work(req);
2235 }
2236}
2237#endif
2238
2239static bool io_rw_reissue(struct io_kiocb *req, long res)
2240{
2241#ifdef CONFIG_BLOCK
Jens Axboeb63534c2020-06-04 11:28:00 -06002242 int ret;
2243
2244 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2245 return false;
2246
Jens Axboeb63534c2020-06-04 11:28:00 -06002247 init_task_work(&req->task_work, io_rw_resubmit);
Jens Axboec2c4c832020-07-01 15:37:11 -06002248 ret = io_req_task_work_add(req, &req->task_work);
2249 if (!ret)
Jens Axboeb63534c2020-06-04 11:28:00 -06002250 return true;
2251#endif
2252 return false;
2253}
2254
Jens Axboea1d7c392020-06-22 11:09:46 -06002255static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2256 struct io_comp_state *cs)
2257{
2258 if (!io_rw_reissue(req, res))
2259 io_complete_rw_common(&req->rw.kiocb, res, cs);
2260}
2261
Jens Axboeba816ad2019-09-28 11:36:45 -06002262static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2263{
Jens Axboe9adbd452019-12-20 08:45:55 -07002264 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002265
Jens Axboea1d7c392020-06-22 11:09:46 -06002266 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002267}
2268
Jens Axboedef596e2019-01-09 08:59:42 -07002269static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2270{
Jens Axboe9adbd452019-12-20 08:45:55 -07002271 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002272
Jens Axboe491381ce2019-10-17 09:20:46 -06002273 if (kiocb->ki_flags & IOCB_WRITE)
2274 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002275
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002276 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002277 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002278
2279 WRITE_ONCE(req->result, res);
2280 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002281 smp_wmb();
2282 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002283}
2284
2285/*
2286 * After the iocb has been issued, it's safe to be found on the poll list.
2287 * Adding the kiocb to the list AFTER submission ensures that we don't
2288 * find it from a io_iopoll_getevents() thread before the issuer is done
2289 * accessing the kiocb cookie.
2290 */
2291static void io_iopoll_req_issued(struct io_kiocb *req)
2292{
2293 struct io_ring_ctx *ctx = req->ctx;
2294
2295 /*
2296 * Track whether we have multiple files in our lists. This will impact
2297 * how we do polling eventually, not spinning if we're on potentially
2298 * different devices.
2299 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002300 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002301 ctx->poll_multi_file = false;
2302 } else if (!ctx->poll_multi_file) {
2303 struct io_kiocb *list_req;
2304
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002305 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002306 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002307 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002308 ctx->poll_multi_file = true;
2309 }
2310
2311 /*
2312 * For fast devices, IO may have already completed. If it has, add
2313 * it to the front so we find it first.
2314 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002315 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002316 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002317 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002318 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002319
2320 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2321 wq_has_sleeper(&ctx->sqo_wait))
2322 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002323}
2324
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002325static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002326{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002327 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002328
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002329 if (diff)
2330 fput_many(state->file, diff);
2331 state->file = NULL;
2332}
2333
2334static inline void io_state_file_put(struct io_submit_state *state)
2335{
2336 if (state->file)
2337 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002338}
2339
2340/*
2341 * Get as many references to a file as we have IOs left in this submission,
2342 * assuming most submissions are for one file, or at least that each file
2343 * has more than one submission.
2344 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002345static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002346{
2347 if (!state)
2348 return fget(fd);
2349
2350 if (state->file) {
2351 if (state->fd == fd) {
2352 state->used_refs++;
2353 state->ios_left--;
2354 return state->file;
2355 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002356 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002357 }
2358 state->file = fget_many(fd, state->ios_left);
2359 if (!state->file)
2360 return NULL;
2361
2362 state->fd = fd;
2363 state->has_refs = state->ios_left;
2364 state->used_refs = 1;
2365 state->ios_left--;
2366 return state->file;
2367}
2368
Jens Axboe4503b762020-06-01 10:00:27 -06002369static bool io_bdev_nowait(struct block_device *bdev)
2370{
2371#ifdef CONFIG_BLOCK
2372 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2373#else
2374 return true;
2375#endif
2376}
2377
Jens Axboe2b188cc2019-01-07 10:46:33 -07002378/*
2379 * If we tracked the file through the SCM inflight mechanism, we could support
2380 * any file. For now, just ensure that anything potentially problematic is done
2381 * inline.
2382 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002383static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002384{
2385 umode_t mode = file_inode(file)->i_mode;
2386
Jens Axboe4503b762020-06-01 10:00:27 -06002387 if (S_ISBLK(mode)) {
2388 if (io_bdev_nowait(file->f_inode->i_bdev))
2389 return true;
2390 return false;
2391 }
2392 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002393 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002394 if (S_ISREG(mode)) {
2395 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2396 file->f_op != &io_uring_fops)
2397 return true;
2398 return false;
2399 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400
Jens Axboec5b85622020-06-09 19:23:05 -06002401 /* any ->read/write should understand O_NONBLOCK */
2402 if (file->f_flags & O_NONBLOCK)
2403 return true;
2404
Jens Axboeaf197f52020-04-28 13:15:06 -06002405 if (!(file->f_mode & FMODE_NOWAIT))
2406 return false;
2407
2408 if (rw == READ)
2409 return file->f_op->read_iter != NULL;
2410
2411 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002412}
2413
Jens Axboe3529d8c2019-12-19 18:24:38 -07002414static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2415 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002416{
Jens Axboedef596e2019-01-09 08:59:42 -07002417 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002418 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002419 unsigned ioprio;
2420 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002421
Jens Axboe491381ce2019-10-17 09:20:46 -06002422 if (S_ISREG(file_inode(req->file)->i_mode))
2423 req->flags |= REQ_F_ISREG;
2424
Jens Axboe2b188cc2019-01-07 10:46:33 -07002425 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002426 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2427 req->flags |= REQ_F_CUR_POS;
2428 kiocb->ki_pos = req->file->f_pos;
2429 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002430 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002431 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2432 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2433 if (unlikely(ret))
2434 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002435
2436 ioprio = READ_ONCE(sqe->ioprio);
2437 if (ioprio) {
2438 ret = ioprio_check_cap(ioprio);
2439 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002440 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002441
2442 kiocb->ki_ioprio = ioprio;
2443 } else
2444 kiocb->ki_ioprio = get_current_ioprio();
2445
Stefan Bühler8449eed2019-04-27 20:34:19 +02002446 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002447 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002448 req->flags |= REQ_F_NOWAIT;
2449
Jens Axboeb63534c2020-06-04 11:28:00 -06002450 if (kiocb->ki_flags & IOCB_DIRECT)
2451 io_get_req_task(req);
2452
Stefan Bühler8449eed2019-04-27 20:34:19 +02002453 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002454 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002455
Jens Axboedef596e2019-01-09 08:59:42 -07002456 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002457 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2458 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002459 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002460
Jens Axboedef596e2019-01-09 08:59:42 -07002461 kiocb->ki_flags |= IOCB_HIPRI;
2462 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002463 req->iopoll_completed = 0;
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +03002464 io_get_req_task(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002465 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002466 if (kiocb->ki_flags & IOCB_HIPRI)
2467 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002468 kiocb->ki_complete = io_complete_rw;
2469 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002470
Jens Axboe3529d8c2019-12-19 18:24:38 -07002471 req->rw.addr = READ_ONCE(sqe->addr);
2472 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002473 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002474 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002475}
2476
2477static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2478{
2479 switch (ret) {
2480 case -EIOCBQUEUED:
2481 break;
2482 case -ERESTARTSYS:
2483 case -ERESTARTNOINTR:
2484 case -ERESTARTNOHAND:
2485 case -ERESTART_RESTARTBLOCK:
2486 /*
2487 * We can't just restart the syscall, since previously
2488 * submitted sqes may already be in progress. Just fail this
2489 * IO with EINTR.
2490 */
2491 ret = -EINTR;
2492 /* fall through */
2493 default:
2494 kiocb->ki_complete(kiocb, ret, 0);
2495 }
2496}
2497
Jens Axboea1d7c392020-06-22 11:09:46 -06002498static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2499 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002500{
Jens Axboeba042912019-12-25 16:33:42 -07002501 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2502
2503 if (req->flags & REQ_F_CUR_POS)
2504 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002505 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002506 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002507 else
2508 io_rw_done(kiocb, ret);
2509}
2510
Jens Axboe9adbd452019-12-20 08:45:55 -07002511static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002512 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002513{
Jens Axboe9adbd452019-12-20 08:45:55 -07002514 struct io_ring_ctx *ctx = req->ctx;
2515 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002516 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002517 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002518 size_t offset;
2519 u64 buf_addr;
2520
2521 /* attempt to use fixed buffers without having provided iovecs */
2522 if (unlikely(!ctx->user_bufs))
2523 return -EFAULT;
2524
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002525 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002526 if (unlikely(buf_index >= ctx->nr_user_bufs))
2527 return -EFAULT;
2528
2529 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2530 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002531 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002532
2533 /* overflow */
2534 if (buf_addr + len < buf_addr)
2535 return -EFAULT;
2536 /* not inside the mapped region */
2537 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2538 return -EFAULT;
2539
2540 /*
2541 * May not be a start of buffer, set size appropriately
2542 * and advance us to the beginning.
2543 */
2544 offset = buf_addr - imu->ubuf;
2545 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002546
2547 if (offset) {
2548 /*
2549 * Don't use iov_iter_advance() here, as it's really slow for
2550 * using the latter parts of a big fixed buffer - it iterates
2551 * over each segment manually. We can cheat a bit here, because
2552 * we know that:
2553 *
2554 * 1) it's a BVEC iter, we set it up
2555 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2556 * first and last bvec
2557 *
2558 * So just find our index, and adjust the iterator afterwards.
2559 * If the offset is within the first bvec (or the whole first
2560 * bvec, just use iov_iter_advance(). This makes it easier
2561 * since we can just skip the first segment, which may not
2562 * be PAGE_SIZE aligned.
2563 */
2564 const struct bio_vec *bvec = imu->bvec;
2565
2566 if (offset <= bvec->bv_len) {
2567 iov_iter_advance(iter, offset);
2568 } else {
2569 unsigned long seg_skip;
2570
2571 /* skip first vec */
2572 offset -= bvec->bv_len;
2573 seg_skip = 1 + (offset >> PAGE_SHIFT);
2574
2575 iter->bvec = bvec + seg_skip;
2576 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002577 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002578 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002579 }
2580 }
2581
Jens Axboe5e559562019-11-13 16:12:46 -07002582 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002583}
2584
Jens Axboebcda7ba2020-02-23 16:42:51 -07002585static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2586{
2587 if (needs_lock)
2588 mutex_unlock(&ctx->uring_lock);
2589}
2590
2591static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2592{
2593 /*
2594 * "Normal" inline submissions always hold the uring_lock, since we
2595 * grab it from the system call. Same is true for the SQPOLL offload.
2596 * The only exception is when we've detached the request and issue it
2597 * from an async worker thread, grab the lock for that case.
2598 */
2599 if (needs_lock)
2600 mutex_lock(&ctx->uring_lock);
2601}
2602
2603static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2604 int bgid, struct io_buffer *kbuf,
2605 bool needs_lock)
2606{
2607 struct io_buffer *head;
2608
2609 if (req->flags & REQ_F_BUFFER_SELECTED)
2610 return kbuf;
2611
2612 io_ring_submit_lock(req->ctx, needs_lock);
2613
2614 lockdep_assert_held(&req->ctx->uring_lock);
2615
2616 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2617 if (head) {
2618 if (!list_empty(&head->list)) {
2619 kbuf = list_last_entry(&head->list, struct io_buffer,
2620 list);
2621 list_del(&kbuf->list);
2622 } else {
2623 kbuf = head;
2624 idr_remove(&req->ctx->io_buffer_idr, bgid);
2625 }
2626 if (*len > kbuf->len)
2627 *len = kbuf->len;
2628 } else {
2629 kbuf = ERR_PTR(-ENOBUFS);
2630 }
2631
2632 io_ring_submit_unlock(req->ctx, needs_lock);
2633
2634 return kbuf;
2635}
2636
Jens Axboe4d954c22020-02-27 07:31:19 -07002637static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2638 bool needs_lock)
2639{
2640 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002641 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002642
2643 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002644 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002645 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2646 if (IS_ERR(kbuf))
2647 return kbuf;
2648 req->rw.addr = (u64) (unsigned long) kbuf;
2649 req->flags |= REQ_F_BUFFER_SELECTED;
2650 return u64_to_user_ptr(kbuf->addr);
2651}
2652
2653#ifdef CONFIG_COMPAT
2654static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2655 bool needs_lock)
2656{
2657 struct compat_iovec __user *uiov;
2658 compat_ssize_t clen;
2659 void __user *buf;
2660 ssize_t len;
2661
2662 uiov = u64_to_user_ptr(req->rw.addr);
2663 if (!access_ok(uiov, sizeof(*uiov)))
2664 return -EFAULT;
2665 if (__get_user(clen, &uiov->iov_len))
2666 return -EFAULT;
2667 if (clen < 0)
2668 return -EINVAL;
2669
2670 len = clen;
2671 buf = io_rw_buffer_select(req, &len, needs_lock);
2672 if (IS_ERR(buf))
2673 return PTR_ERR(buf);
2674 iov[0].iov_base = buf;
2675 iov[0].iov_len = (compat_size_t) len;
2676 return 0;
2677}
2678#endif
2679
2680static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2681 bool needs_lock)
2682{
2683 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2684 void __user *buf;
2685 ssize_t len;
2686
2687 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2688 return -EFAULT;
2689
2690 len = iov[0].iov_len;
2691 if (len < 0)
2692 return -EINVAL;
2693 buf = io_rw_buffer_select(req, &len, needs_lock);
2694 if (IS_ERR(buf))
2695 return PTR_ERR(buf);
2696 iov[0].iov_base = buf;
2697 iov[0].iov_len = len;
2698 return 0;
2699}
2700
2701static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2702 bool needs_lock)
2703{
Jens Axboedddb3e22020-06-04 11:27:01 -06002704 if (req->flags & REQ_F_BUFFER_SELECTED) {
2705 struct io_buffer *kbuf;
2706
2707 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2708 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2709 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002710 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002711 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002712 if (!req->rw.len)
2713 return 0;
2714 else if (req->rw.len > 1)
2715 return -EINVAL;
2716
2717#ifdef CONFIG_COMPAT
2718 if (req->ctx->compat)
2719 return io_compat_import(req, iov, needs_lock);
2720#endif
2721
2722 return __io_iov_buffer_select(req, iov, needs_lock);
2723}
2724
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002725static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002726 struct iovec **iovec, struct iov_iter *iter,
2727 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002728{
Jens Axboe9adbd452019-12-20 08:45:55 -07002729 void __user *buf = u64_to_user_ptr(req->rw.addr);
2730 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002731 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002732 u8 opcode;
2733
Jens Axboed625c6e2019-12-17 19:53:05 -07002734 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002735 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002736 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002737 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002738 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002739
Jens Axboebcda7ba2020-02-23 16:42:51 -07002740 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002741 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002742 return -EINVAL;
2743
Jens Axboe3a6820f2019-12-22 15:19:35 -07002744 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002745 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002746 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2747 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002748 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002749 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002750 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002751 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002752 }
2753
Jens Axboe3a6820f2019-12-22 15:19:35 -07002754 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2755 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002756 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002757 }
2758
Jens Axboef67676d2019-12-02 11:03:47 -07002759 if (req->io) {
2760 struct io_async_rw *iorw = &req->io->rw;
2761
Pavel Begunkov252917c2020-07-13 22:59:20 +03002762 iov_iter_init(iter, rw, iorw->iov, iorw->nr_segs, iorw->size);
2763 *iovec = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07002764 return iorw->size;
2765 }
2766
Jens Axboe4d954c22020-02-27 07:31:19 -07002767 if (req->flags & REQ_F_BUFFER_SELECT) {
2768 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002769 if (!ret) {
2770 ret = (*iovec)->iov_len;
2771 iov_iter_init(iter, rw, *iovec, 1, ret);
2772 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002773 *iovec = NULL;
2774 return ret;
2775 }
2776
Jens Axboe2b188cc2019-01-07 10:46:33 -07002777#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002778 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002779 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2780 iovec, iter);
2781#endif
2782
2783 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2784}
2785
Jens Axboe32960612019-09-23 11:05:34 -06002786/*
2787 * For files that don't have ->read_iter() and ->write_iter(), handle them
2788 * by looping over ->read() or ->write() manually.
2789 */
2790static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2791 struct iov_iter *iter)
2792{
2793 ssize_t ret = 0;
2794
2795 /*
2796 * Don't support polled IO through this interface, and we can't
2797 * support non-blocking either. For the latter, this just causes
2798 * the kiocb to be handled from an async context.
2799 */
2800 if (kiocb->ki_flags & IOCB_HIPRI)
2801 return -EOPNOTSUPP;
2802 if (kiocb->ki_flags & IOCB_NOWAIT)
2803 return -EAGAIN;
2804
2805 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002806 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002807 ssize_t nr;
2808
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002809 if (!iov_iter_is_bvec(iter)) {
2810 iovec = iov_iter_iovec(iter);
2811 } else {
2812 /* fixed buffers import bvec */
2813 iovec.iov_base = kmap(iter->bvec->bv_page)
2814 + iter->iov_offset;
2815 iovec.iov_len = min(iter->count,
2816 iter->bvec->bv_len - iter->iov_offset);
2817 }
2818
Jens Axboe32960612019-09-23 11:05:34 -06002819 if (rw == READ) {
2820 nr = file->f_op->read(file, iovec.iov_base,
2821 iovec.iov_len, &kiocb->ki_pos);
2822 } else {
2823 nr = file->f_op->write(file, iovec.iov_base,
2824 iovec.iov_len, &kiocb->ki_pos);
2825 }
2826
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002827 if (iov_iter_is_bvec(iter))
2828 kunmap(iter->bvec->bv_page);
2829
Jens Axboe32960612019-09-23 11:05:34 -06002830 if (nr < 0) {
2831 if (!ret)
2832 ret = nr;
2833 break;
2834 }
2835 ret += nr;
2836 if (nr != iovec.iov_len)
2837 break;
2838 iov_iter_advance(iter, nr);
2839 }
2840
2841 return ret;
2842}
2843
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002844static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002845 struct iovec *iovec, struct iovec *fast_iov,
2846 struct iov_iter *iter)
2847{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002848 struct io_async_rw *rw = &req->io->rw;
2849
2850 rw->nr_segs = iter->nr_segs;
2851 rw->size = io_size;
2852 if (!iovec) {
2853 rw->iov = rw->fast_iov;
2854 if (rw->iov != fast_iov)
2855 memcpy(rw->iov, fast_iov,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002856 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002857 } else {
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002858 rw->iov = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002859 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002860 }
2861}
2862
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002863static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2864{
2865 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2866 return req->io == NULL;
2867}
2868
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002869static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002870{
Jens Axboed3656342019-12-18 09:50:26 -07002871 if (!io_op_defs[req->opcode].async_ctx)
2872 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002873
2874 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002875}
2876
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002877static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2878 struct iovec *iovec, struct iovec *fast_iov,
2879 struct iov_iter *iter)
2880{
Jens Axboe980ad262020-01-24 23:08:54 -07002881 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002882 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002883 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002884 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002885 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002886
Jens Axboe5d204bc2020-01-31 12:06:52 -07002887 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2888 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002889 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002890}
2891
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03002892static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
2893 bool force_nonblock)
2894{
2895 struct io_async_ctx *io = req->io;
2896 struct iov_iter iter;
2897 ssize_t ret;
2898
2899 io->rw.iov = io->rw.fast_iov;
2900 req->io = NULL;
2901 ret = io_import_iovec(rw, req, &io->rw.iov, &iter, !force_nonblock);
2902 req->io = io;
2903 if (unlikely(ret < 0))
2904 return ret;
2905
2906 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2907 return 0;
2908}
2909
Jens Axboe3529d8c2019-12-19 18:24:38 -07002910static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2911 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002912{
2913 ssize_t ret;
2914
Jens Axboe3529d8c2019-12-19 18:24:38 -07002915 ret = io_prep_rw(req, sqe, force_nonblock);
2916 if (ret)
2917 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002918
Jens Axboe3529d8c2019-12-19 18:24:38 -07002919 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2920 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002921
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002922 /* either don't need iovec imported or already have it */
2923 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002924 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03002925 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07002926}
2927
Jens Axboebcf5a062020-05-22 09:24:42 -06002928static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2929 int sync, void *arg)
2930{
2931 struct wait_page_queue *wpq;
2932 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06002933 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06002934 int ret;
2935
2936 wpq = container_of(wait, struct wait_page_queue, wait);
2937
2938 ret = wake_page_match(wpq, key);
2939 if (ret != 1)
2940 return ret;
2941
2942 list_del_init(&wait->entry);
2943
Pavel Begunkove7375122020-07-12 20:42:04 +03002944 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboebcf5a062020-05-22 09:24:42 -06002945 /* submit ref gets dropped, acquire a new one */
2946 refcount_inc(&req->refs);
Pavel Begunkove7375122020-07-12 20:42:04 +03002947 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboebcf5a062020-05-22 09:24:42 -06002948 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002949 struct task_struct *tsk;
2950
Jens Axboebcf5a062020-05-22 09:24:42 -06002951 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03002952 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06002953 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03002954 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06002955 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06002956 }
Jens Axboebcf5a062020-05-22 09:24:42 -06002957 return 1;
2958}
2959
2960static bool io_rw_should_retry(struct io_kiocb *req)
2961{
2962 struct kiocb *kiocb = &req->rw.kiocb;
2963 int ret;
2964
2965 /* never retry for NOWAIT, we just complete with -EAGAIN */
2966 if (req->flags & REQ_F_NOWAIT)
2967 return false;
2968
2969 /* already tried, or we're doing O_DIRECT */
2970 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
2971 return false;
2972 /*
2973 * just use poll if we can, and don't attempt if the fs doesn't
2974 * support callback based unlocks
2975 */
2976 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
2977 return false;
2978
2979 /*
2980 * If request type doesn't require req->io to defer in general,
2981 * we need to allocate it here
2982 */
2983 if (!req->io && __io_alloc_async_ctx(req))
2984 return false;
2985
2986 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
2987 io_async_buf_func, req);
2988 if (!ret) {
2989 io_get_req_task(req);
2990 return true;
2991 }
2992
2993 return false;
2994}
2995
2996static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
2997{
2998 if (req->file->f_op->read_iter)
2999 return call_read_iter(req->file, &req->rw.kiocb, iter);
3000 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3001}
3002
Jens Axboea1d7c392020-06-22 11:09:46 -06003003static int io_read(struct io_kiocb *req, bool force_nonblock,
3004 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003005{
3006 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003007 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003008 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003009 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003010 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003011
Jens Axboebcda7ba2020-02-23 16:42:51 -07003012 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003013 if (ret < 0)
3014 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003015
Jens Axboefd6c2e42019-12-18 12:19:41 -07003016 /* Ensure we clear previously set non-block flag */
3017 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003018 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003019
Jens Axboef67676d2019-12-02 11:03:47 -07003020 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03003021 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07003022
Pavel Begunkov24c74672020-06-21 13:09:51 +03003023 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003024 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07003025 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003026
Jens Axboe31b51512019-01-18 22:56:34 -07003027 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003028 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003029 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003030 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06003031 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003032
Jens Axboebcf5a062020-05-22 09:24:42 -06003033 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003034
Jens Axboe9d93a3f2019-05-15 13:53:07 -06003035 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06003036 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003037 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003038 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003039 iter.count = iov_count;
3040 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003041copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003042 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003043 inline_vecs, &iter);
3044 if (ret)
3045 goto out_free;
Pavel Begunkov252917c2020-07-13 22:59:20 +03003046 /* it's copied and will be cleaned with ->io */
3047 iovec = NULL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003048 /* if we can retry, do so with the callbacks armed */
3049 if (io_rw_should_retry(req)) {
3050 ret2 = io_iter_do_read(req, &iter);
3051 if (ret2 == -EIOCBQUEUED) {
3052 goto out_free;
3053 } else if (ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003054 kiocb_done(kiocb, ret2, cs);
Jens Axboebcf5a062020-05-22 09:24:42 -06003055 goto out_free;
3056 }
3057 }
3058 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07003059 return -EAGAIN;
3060 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003061 }
Jens Axboef67676d2019-12-02 11:03:47 -07003062out_free:
Pavel Begunkov252917c2020-07-13 22:59:20 +03003063 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003064 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003065 return ret;
3066}
3067
Jens Axboe3529d8c2019-12-19 18:24:38 -07003068static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3069 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003070{
3071 ssize_t ret;
3072
Jens Axboe3529d8c2019-12-19 18:24:38 -07003073 ret = io_prep_rw(req, sqe, force_nonblock);
3074 if (ret)
3075 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003076
Jens Axboe3529d8c2019-12-19 18:24:38 -07003077 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3078 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003079
Jens Axboe4ed734b2020-03-20 11:23:41 -06003080 req->fsize = rlimit(RLIMIT_FSIZE);
3081
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003082 /* either don't need iovec imported or already have it */
3083 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003084 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003085 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003086}
3087
Jens Axboea1d7c392020-06-22 11:09:46 -06003088static int io_write(struct io_kiocb *req, bool force_nonblock,
3089 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003090{
3091 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003092 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003093 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003094 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003095 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003096
Jens Axboebcda7ba2020-02-23 16:42:51 -07003097 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003098 if (ret < 0)
3099 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003100
Jens Axboefd6c2e42019-12-18 12:19:41 -07003101 /* Ensure we clear previously set non-block flag */
3102 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003103 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003104
Jens Axboef67676d2019-12-02 11:03:47 -07003105 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03003106 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07003107
Pavel Begunkov24c74672020-06-21 13:09:51 +03003108 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003109 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003110 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003111
Jens Axboe10d59342019-12-09 20:16:22 -07003112 /* file path doesn't support NOWAIT for non-direct_IO */
3113 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3114 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003115 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003116
Jens Axboe31b51512019-01-18 22:56:34 -07003117 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003118 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003119 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003120 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003121 ssize_t ret2;
3122
Jens Axboe2b188cc2019-01-07 10:46:33 -07003123 /*
3124 * Open-code file_start_write here to grab freeze protection,
3125 * which will be released by another thread in
3126 * io_complete_rw(). Fool lockdep by telling it the lock got
3127 * released so that it doesn't complain about the held lock when
3128 * we return to userspace.
3129 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003130 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003131 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003132 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003133 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003134 SB_FREEZE_WRITE);
3135 }
3136 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003137
Jens Axboe4ed734b2020-03-20 11:23:41 -06003138 if (!force_nonblock)
3139 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3140
Jens Axboe9adbd452019-12-20 08:45:55 -07003141 if (req->file->f_op->write_iter)
3142 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003143 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003144 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003145
3146 if (!force_nonblock)
3147 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3148
Jens Axboefaac9962020-02-07 15:45:22 -07003149 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003150 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003151 * retry them without IOCB_NOWAIT.
3152 */
3153 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3154 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003155 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003156 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003157 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003158 iter.count = iov_count;
3159 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003160copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003161 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003162 inline_vecs, &iter);
3163 if (ret)
3164 goto out_free;
Pavel Begunkov252917c2020-07-13 22:59:20 +03003165 /* it's copied and will be cleaned with ->io */
3166 iovec = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003167 return -EAGAIN;
3168 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003169 }
Jens Axboe31b51512019-01-18 22:56:34 -07003170out_free:
Pavel Begunkov252917c2020-07-13 22:59:20 +03003171 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003172 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003173 return ret;
3174}
3175
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003176static int __io_splice_prep(struct io_kiocb *req,
3177 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003178{
3179 struct io_splice* sp = &req->splice;
3180 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3181 int ret;
3182
3183 if (req->flags & REQ_F_NEED_CLEANUP)
3184 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003185 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3186 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003187
3188 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003189 sp->len = READ_ONCE(sqe->len);
3190 sp->flags = READ_ONCE(sqe->splice_flags);
3191
3192 if (unlikely(sp->flags & ~valid_flags))
3193 return -EINVAL;
3194
3195 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3196 (sp->flags & SPLICE_F_FD_IN_FIXED));
3197 if (ret)
3198 return ret;
3199 req->flags |= REQ_F_NEED_CLEANUP;
3200
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003201 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3202 /*
3203 * Splice operation will be punted aync, and here need to
3204 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3205 */
3206 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003207 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003208 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003209
3210 return 0;
3211}
3212
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003213static int io_tee_prep(struct io_kiocb *req,
3214 const struct io_uring_sqe *sqe)
3215{
3216 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3217 return -EINVAL;
3218 return __io_splice_prep(req, sqe);
3219}
3220
3221static int io_tee(struct io_kiocb *req, bool force_nonblock)
3222{
3223 struct io_splice *sp = &req->splice;
3224 struct file *in = sp->file_in;
3225 struct file *out = sp->file_out;
3226 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3227 long ret = 0;
3228
3229 if (force_nonblock)
3230 return -EAGAIN;
3231 if (sp->len)
3232 ret = do_tee(in, out, sp->len, flags);
3233
3234 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3235 req->flags &= ~REQ_F_NEED_CLEANUP;
3236
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003237 if (ret != sp->len)
3238 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003239 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003240 return 0;
3241}
3242
3243static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3244{
3245 struct io_splice* sp = &req->splice;
3246
3247 sp->off_in = READ_ONCE(sqe->splice_off_in);
3248 sp->off_out = READ_ONCE(sqe->off);
3249 return __io_splice_prep(req, sqe);
3250}
3251
Pavel Begunkov014db002020-03-03 21:33:12 +03003252static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003253{
3254 struct io_splice *sp = &req->splice;
3255 struct file *in = sp->file_in;
3256 struct file *out = sp->file_out;
3257 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3258 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003259 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003260
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003261 if (force_nonblock)
3262 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003263
3264 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3265 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003266
Jens Axboe948a7742020-05-17 14:21:38 -06003267 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003268 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003269
3270 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3271 req->flags &= ~REQ_F_NEED_CLEANUP;
3272
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003273 if (ret != sp->len)
3274 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003275 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003276 return 0;
3277}
3278
Jens Axboe2b188cc2019-01-07 10:46:33 -07003279/*
3280 * IORING_OP_NOP just posts a completion event, nothing else.
3281 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003282static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003283{
3284 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003285
Jens Axboedef596e2019-01-09 08:59:42 -07003286 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3287 return -EINVAL;
3288
Jens Axboe229a7b62020-06-22 10:13:11 -06003289 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003290 return 0;
3291}
3292
Jens Axboe3529d8c2019-12-19 18:24:38 -07003293static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003294{
Jens Axboe6b063142019-01-10 22:13:58 -07003295 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003296
Jens Axboe09bb8392019-03-13 12:39:28 -06003297 if (!req->file)
3298 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003299
Jens Axboe6b063142019-01-10 22:13:58 -07003300 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003301 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003302 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003303 return -EINVAL;
3304
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003305 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3306 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3307 return -EINVAL;
3308
3309 req->sync.off = READ_ONCE(sqe->off);
3310 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003311 return 0;
3312}
3313
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003314static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003315{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003316 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003317 int ret;
3318
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003319 /* fsync always requires a blocking context */
3320 if (force_nonblock)
3321 return -EAGAIN;
3322
Jens Axboe9adbd452019-12-20 08:45:55 -07003323 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003324 end > 0 ? end : LLONG_MAX,
3325 req->sync.flags & IORING_FSYNC_DATASYNC);
3326 if (ret < 0)
3327 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003328 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003329 return 0;
3330}
3331
Jens Axboed63d1b52019-12-10 10:38:56 -07003332static int io_fallocate_prep(struct io_kiocb *req,
3333 const struct io_uring_sqe *sqe)
3334{
3335 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3336 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003337 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3338 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003339
3340 req->sync.off = READ_ONCE(sqe->off);
3341 req->sync.len = READ_ONCE(sqe->addr);
3342 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003343 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07003344 return 0;
3345}
3346
Pavel Begunkov014db002020-03-03 21:33:12 +03003347static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003348{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003349 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003350
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003351 /* fallocate always requiring blocking context */
3352 if (force_nonblock)
3353 return -EAGAIN;
3354
3355 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3356 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3357 req->sync.len);
3358 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3359 if (ret < 0)
3360 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003361 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003362 return 0;
3363}
3364
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003365static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003366{
Jens Axboef8748882020-01-08 17:47:02 -07003367 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003368 int ret;
3369
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003370 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003371 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003372 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003373 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003374 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003375 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003376
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003377 /* open.how should be already initialised */
3378 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003379 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003380
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003381 req->open.dfd = READ_ONCE(sqe->fd);
3382 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003383 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003384 if (IS_ERR(req->open.filename)) {
3385 ret = PTR_ERR(req->open.filename);
3386 req->open.filename = NULL;
3387 return ret;
3388 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003389 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003390 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003391 return 0;
3392}
3393
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003394static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3395{
3396 u64 flags, mode;
3397
3398 if (req->flags & REQ_F_NEED_CLEANUP)
3399 return 0;
3400 mode = READ_ONCE(sqe->len);
3401 flags = READ_ONCE(sqe->open_flags);
3402 req->open.how = build_open_how(flags, mode);
3403 return __io_openat_prep(req, sqe);
3404}
3405
Jens Axboecebdb982020-01-08 17:59:24 -07003406static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3407{
3408 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003409 size_t len;
3410 int ret;
3411
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003412 if (req->flags & REQ_F_NEED_CLEANUP)
3413 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003414 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3415 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003416 if (len < OPEN_HOW_SIZE_VER0)
3417 return -EINVAL;
3418
3419 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3420 len);
3421 if (ret)
3422 return ret;
3423
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003424 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003425}
3426
Pavel Begunkov014db002020-03-03 21:33:12 +03003427static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003428{
3429 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003430 struct file *file;
3431 int ret;
3432
Jens Axboef86cd202020-01-29 13:46:44 -07003433 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003434 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003435
Jens Axboecebdb982020-01-08 17:59:24 -07003436 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003437 if (ret)
3438 goto err;
3439
Jens Axboe4022e7a2020-03-19 19:23:18 -06003440 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003441 if (ret < 0)
3442 goto err;
3443
3444 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3445 if (IS_ERR(file)) {
3446 put_unused_fd(ret);
3447 ret = PTR_ERR(file);
3448 } else {
3449 fsnotify_open(file);
3450 fd_install(ret, file);
3451 }
3452err:
3453 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003454 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003455 if (ret < 0)
3456 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003457 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003458 return 0;
3459}
3460
Pavel Begunkov014db002020-03-03 21:33:12 +03003461static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003462{
Pavel Begunkov014db002020-03-03 21:33:12 +03003463 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003464}
3465
Jens Axboe067524e2020-03-02 16:32:28 -07003466static int io_remove_buffers_prep(struct io_kiocb *req,
3467 const struct io_uring_sqe *sqe)
3468{
3469 struct io_provide_buf *p = &req->pbuf;
3470 u64 tmp;
3471
3472 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3473 return -EINVAL;
3474
3475 tmp = READ_ONCE(sqe->fd);
3476 if (!tmp || tmp > USHRT_MAX)
3477 return -EINVAL;
3478
3479 memset(p, 0, sizeof(*p));
3480 p->nbufs = tmp;
3481 p->bgid = READ_ONCE(sqe->buf_group);
3482 return 0;
3483}
3484
3485static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3486 int bgid, unsigned nbufs)
3487{
3488 unsigned i = 0;
3489
3490 /* shouldn't happen */
3491 if (!nbufs)
3492 return 0;
3493
3494 /* the head kbuf is the list itself */
3495 while (!list_empty(&buf->list)) {
3496 struct io_buffer *nxt;
3497
3498 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3499 list_del(&nxt->list);
3500 kfree(nxt);
3501 if (++i == nbufs)
3502 return i;
3503 }
3504 i++;
3505 kfree(buf);
3506 idr_remove(&ctx->io_buffer_idr, bgid);
3507
3508 return i;
3509}
3510
Jens Axboe229a7b62020-06-22 10:13:11 -06003511static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3512 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003513{
3514 struct io_provide_buf *p = &req->pbuf;
3515 struct io_ring_ctx *ctx = req->ctx;
3516 struct io_buffer *head;
3517 int ret = 0;
3518
3519 io_ring_submit_lock(ctx, !force_nonblock);
3520
3521 lockdep_assert_held(&ctx->uring_lock);
3522
3523 ret = -ENOENT;
3524 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3525 if (head)
3526 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3527
3528 io_ring_submit_lock(ctx, !force_nonblock);
3529 if (ret < 0)
3530 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003531 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003532 return 0;
3533}
3534
Jens Axboeddf0322d2020-02-23 16:41:33 -07003535static int io_provide_buffers_prep(struct io_kiocb *req,
3536 const struct io_uring_sqe *sqe)
3537{
3538 struct io_provide_buf *p = &req->pbuf;
3539 u64 tmp;
3540
3541 if (sqe->ioprio || sqe->rw_flags)
3542 return -EINVAL;
3543
3544 tmp = READ_ONCE(sqe->fd);
3545 if (!tmp || tmp > USHRT_MAX)
3546 return -E2BIG;
3547 p->nbufs = tmp;
3548 p->addr = READ_ONCE(sqe->addr);
3549 p->len = READ_ONCE(sqe->len);
3550
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003551 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003552 return -EFAULT;
3553
3554 p->bgid = READ_ONCE(sqe->buf_group);
3555 tmp = READ_ONCE(sqe->off);
3556 if (tmp > USHRT_MAX)
3557 return -E2BIG;
3558 p->bid = tmp;
3559 return 0;
3560}
3561
3562static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3563{
3564 struct io_buffer *buf;
3565 u64 addr = pbuf->addr;
3566 int i, bid = pbuf->bid;
3567
3568 for (i = 0; i < pbuf->nbufs; i++) {
3569 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3570 if (!buf)
3571 break;
3572
3573 buf->addr = addr;
3574 buf->len = pbuf->len;
3575 buf->bid = bid;
3576 addr += pbuf->len;
3577 bid++;
3578 if (!*head) {
3579 INIT_LIST_HEAD(&buf->list);
3580 *head = buf;
3581 } else {
3582 list_add_tail(&buf->list, &(*head)->list);
3583 }
3584 }
3585
3586 return i ? i : -ENOMEM;
3587}
3588
Jens Axboe229a7b62020-06-22 10:13:11 -06003589static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3590 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003591{
3592 struct io_provide_buf *p = &req->pbuf;
3593 struct io_ring_ctx *ctx = req->ctx;
3594 struct io_buffer *head, *list;
3595 int ret = 0;
3596
3597 io_ring_submit_lock(ctx, !force_nonblock);
3598
3599 lockdep_assert_held(&ctx->uring_lock);
3600
3601 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3602
3603 ret = io_add_buffers(p, &head);
3604 if (ret < 0)
3605 goto out;
3606
3607 if (!list) {
3608 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3609 GFP_KERNEL);
3610 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003611 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003612 goto out;
3613 }
3614 }
3615out:
3616 io_ring_submit_unlock(ctx, !force_nonblock);
3617 if (ret < 0)
3618 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003619 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003620 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003621}
3622
Jens Axboe3e4827b2020-01-08 15:18:09 -07003623static int io_epoll_ctl_prep(struct io_kiocb *req,
3624 const struct io_uring_sqe *sqe)
3625{
3626#if defined(CONFIG_EPOLL)
3627 if (sqe->ioprio || sqe->buf_index)
3628 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003629 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3630 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003631
3632 req->epoll.epfd = READ_ONCE(sqe->fd);
3633 req->epoll.op = READ_ONCE(sqe->len);
3634 req->epoll.fd = READ_ONCE(sqe->off);
3635
3636 if (ep_op_has_event(req->epoll.op)) {
3637 struct epoll_event __user *ev;
3638
3639 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3640 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3641 return -EFAULT;
3642 }
3643
3644 return 0;
3645#else
3646 return -EOPNOTSUPP;
3647#endif
3648}
3649
Jens Axboe229a7b62020-06-22 10:13:11 -06003650static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3651 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003652{
3653#if defined(CONFIG_EPOLL)
3654 struct io_epoll *ie = &req->epoll;
3655 int ret;
3656
3657 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3658 if (force_nonblock && ret == -EAGAIN)
3659 return -EAGAIN;
3660
3661 if (ret < 0)
3662 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003663 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003664 return 0;
3665#else
3666 return -EOPNOTSUPP;
3667#endif
3668}
3669
Jens Axboec1ca7572019-12-25 22:18:28 -07003670static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3671{
3672#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3673 if (sqe->ioprio || sqe->buf_index || sqe->off)
3674 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003675 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3676 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003677
3678 req->madvise.addr = READ_ONCE(sqe->addr);
3679 req->madvise.len = READ_ONCE(sqe->len);
3680 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3681 return 0;
3682#else
3683 return -EOPNOTSUPP;
3684#endif
3685}
3686
Pavel Begunkov014db002020-03-03 21:33:12 +03003687static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003688{
3689#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3690 struct io_madvise *ma = &req->madvise;
3691 int ret;
3692
3693 if (force_nonblock)
3694 return -EAGAIN;
3695
3696 ret = do_madvise(ma->addr, ma->len, ma->advice);
3697 if (ret < 0)
3698 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003699 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003700 return 0;
3701#else
3702 return -EOPNOTSUPP;
3703#endif
3704}
3705
Jens Axboe4840e412019-12-25 22:03:45 -07003706static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3707{
3708 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3709 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003710 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3711 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003712
3713 req->fadvise.offset = READ_ONCE(sqe->off);
3714 req->fadvise.len = READ_ONCE(sqe->len);
3715 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3716 return 0;
3717}
3718
Pavel Begunkov014db002020-03-03 21:33:12 +03003719static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003720{
3721 struct io_fadvise *fa = &req->fadvise;
3722 int ret;
3723
Jens Axboe3e694262020-02-01 09:22:49 -07003724 if (force_nonblock) {
3725 switch (fa->advice) {
3726 case POSIX_FADV_NORMAL:
3727 case POSIX_FADV_RANDOM:
3728 case POSIX_FADV_SEQUENTIAL:
3729 break;
3730 default:
3731 return -EAGAIN;
3732 }
3733 }
Jens Axboe4840e412019-12-25 22:03:45 -07003734
3735 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3736 if (ret < 0)
3737 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003738 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003739 return 0;
3740}
3741
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003742static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3743{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003744 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3745 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003746 if (sqe->ioprio || sqe->buf_index)
3747 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003748 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003749 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003750
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003751 req->statx.dfd = READ_ONCE(sqe->fd);
3752 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003753 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003754 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3755 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003756
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003757 return 0;
3758}
3759
Pavel Begunkov014db002020-03-03 21:33:12 +03003760static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003761{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003762 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003763 int ret;
3764
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003765 if (force_nonblock) {
3766 /* only need file table for an actual valid fd */
3767 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3768 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003769 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003770 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003771
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003772 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3773 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003774
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003775 if (ret < 0)
3776 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003777 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003778 return 0;
3779}
3780
Jens Axboeb5dba592019-12-11 14:02:38 -07003781static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3782{
3783 /*
3784 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003785 * leave the 'file' in an undeterminate state, and here need to modify
3786 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003787 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003788 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003789 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3790
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003791 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3792 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003793 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3794 sqe->rw_flags || sqe->buf_index)
3795 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003796 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003797 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003798
3799 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003800 if ((req->file && req->file->f_op == &io_uring_fops) ||
3801 req->close.fd == req->ctx->ring_fd)
3802 return -EBADF;
3803
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003804 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003805 return 0;
3806}
3807
Jens Axboe229a7b62020-06-22 10:13:11 -06003808static int io_close(struct io_kiocb *req, bool force_nonblock,
3809 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003810{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003811 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003812 int ret;
3813
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003814 /* might be already done during nonblock submission */
3815 if (!close->put_file) {
3816 ret = __close_fd_get_file(close->fd, &close->put_file);
3817 if (ret < 0)
3818 return (ret == -ENOENT) ? -EBADF : ret;
3819 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003820
3821 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003822 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003823 /* was never set, but play safe */
3824 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003825 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003826 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003827 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003828 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003829
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003830 /* No ->flush() or already async, safely close from here */
3831 ret = filp_close(close->put_file, req->work.files);
3832 if (ret < 0)
3833 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003834 fput(close->put_file);
3835 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003836 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003837 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003838}
3839
Jens Axboe3529d8c2019-12-19 18:24:38 -07003840static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003841{
3842 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003843
3844 if (!req->file)
3845 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003846
3847 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3848 return -EINVAL;
3849 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3850 return -EINVAL;
3851
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003852 req->sync.off = READ_ONCE(sqe->off);
3853 req->sync.len = READ_ONCE(sqe->len);
3854 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003855 return 0;
3856}
3857
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003858static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003859{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003860 int ret;
3861
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003862 /* sync_file_range always requires a blocking context */
3863 if (force_nonblock)
3864 return -EAGAIN;
3865
Jens Axboe9adbd452019-12-20 08:45:55 -07003866 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003867 req->sync.flags);
3868 if (ret < 0)
3869 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003870 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003871 return 0;
3872}
3873
YueHaibing469956e2020-03-04 15:53:52 +08003874#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003875static int io_setup_async_msg(struct io_kiocb *req,
3876 struct io_async_msghdr *kmsg)
3877{
3878 if (req->io)
3879 return -EAGAIN;
3880 if (io_alloc_async_ctx(req)) {
3881 if (kmsg->iov != kmsg->fast_iov)
3882 kfree(kmsg->iov);
3883 return -ENOMEM;
3884 }
3885 req->flags |= REQ_F_NEED_CLEANUP;
3886 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3887 return -EAGAIN;
3888}
3889
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03003890static int io_sendmsg_copy_hdr(struct io_kiocb *req,
3891 struct io_async_msghdr *iomsg)
3892{
3893 iomsg->iov = iomsg->fast_iov;
3894 iomsg->msg.msg_name = &iomsg->addr;
3895 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
3896 req->sr_msg.msg_flags, &iomsg->iov);
3897}
3898
Jens Axboe3529d8c2019-12-19 18:24:38 -07003899static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003900{
Jens Axboee47293f2019-12-20 08:58:21 -07003901 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003902 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003903 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003904
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003905 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3906 return -EINVAL;
3907
Jens Axboee47293f2019-12-20 08:58:21 -07003908 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03003909 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003910 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003911
Jens Axboed8768362020-02-27 14:17:49 -07003912#ifdef CONFIG_COMPAT
3913 if (req->ctx->compat)
3914 sr->msg_flags |= MSG_CMSG_COMPAT;
3915#endif
3916
Jens Axboefddafac2020-01-04 20:19:44 -07003917 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003918 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003919 /* iovec is already imported */
3920 if (req->flags & REQ_F_NEED_CLEANUP)
3921 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003922
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03003923 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003924 if (!ret)
3925 req->flags |= REQ_F_NEED_CLEANUP;
3926 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003927}
3928
Jens Axboe229a7b62020-06-22 10:13:11 -06003929static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3930 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07003931{
Jens Axboe0b416c32019-12-15 10:57:46 -07003932 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003933 struct socket *sock;
3934 int ret;
3935
Jens Axboe03b12302019-12-02 18:50:25 -07003936 sock = sock_from_file(req->file, &ret);
3937 if (sock) {
Pavel Begunkov1400e692020-07-12 20:41:05 +03003938 struct io_async_msghdr iomsg;
Jens Axboe03b12302019-12-02 18:50:25 -07003939 unsigned flags;
3940
Jens Axboe03b12302019-12-02 18:50:25 -07003941 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003942 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003943 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003944 /* if iov is set, it's allocated already */
3945 if (!kmsg->iov)
3946 kmsg->iov = kmsg->fast_iov;
3947 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003948 } else {
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03003949 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003950 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003951 return ret;
Pavel Begunkov1400e692020-07-12 20:41:05 +03003952 kmsg = &iomsg;
Jens Axboe03b12302019-12-02 18:50:25 -07003953 }
3954
Jens Axboee47293f2019-12-20 08:58:21 -07003955 flags = req->sr_msg.msg_flags;
3956 if (flags & MSG_DONTWAIT)
3957 req->flags |= REQ_F_NOWAIT;
3958 else if (force_nonblock)
3959 flags |= MSG_DONTWAIT;
3960
Jens Axboe0b416c32019-12-15 10:57:46 -07003961 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003962 if (force_nonblock && ret == -EAGAIN)
3963 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003964 if (ret == -ERESTARTSYS)
3965 ret = -EINTR;
3966 }
3967
Pavel Begunkov1e950812020-02-06 19:51:16 +03003968 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003969 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003970 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003971 if (ret < 0)
3972 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003973 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07003974 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003975}
3976
Jens Axboe229a7b62020-06-22 10:13:11 -06003977static int io_send(struct io_kiocb *req, bool force_nonblock,
3978 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07003979{
Jens Axboefddafac2020-01-04 20:19:44 -07003980 struct socket *sock;
3981 int ret;
3982
Jens Axboefddafac2020-01-04 20:19:44 -07003983 sock = sock_from_file(req->file, &ret);
3984 if (sock) {
3985 struct io_sr_msg *sr = &req->sr_msg;
3986 struct msghdr msg;
3987 struct iovec iov;
3988 unsigned flags;
3989
3990 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3991 &msg.msg_iter);
3992 if (ret)
3993 return ret;
3994
3995 msg.msg_name = NULL;
3996 msg.msg_control = NULL;
3997 msg.msg_controllen = 0;
3998 msg.msg_namelen = 0;
3999
4000 flags = req->sr_msg.msg_flags;
4001 if (flags & MSG_DONTWAIT)
4002 req->flags |= REQ_F_NOWAIT;
4003 else if (force_nonblock)
4004 flags |= MSG_DONTWAIT;
4005
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004006 msg.msg_flags = flags;
4007 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07004008 if (force_nonblock && ret == -EAGAIN)
4009 return -EAGAIN;
4010 if (ret == -ERESTARTSYS)
4011 ret = -EINTR;
4012 }
4013
Jens Axboefddafac2020-01-04 20:19:44 -07004014 if (ret < 0)
4015 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004016 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004017 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004018}
4019
Pavel Begunkov1400e692020-07-12 20:41:05 +03004020static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4021 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004022{
4023 struct io_sr_msg *sr = &req->sr_msg;
4024 struct iovec __user *uiov;
4025 size_t iov_len;
4026 int ret;
4027
Pavel Begunkov1400e692020-07-12 20:41:05 +03004028 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4029 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004030 if (ret)
4031 return ret;
4032
4033 if (req->flags & REQ_F_BUFFER_SELECT) {
4034 if (iov_len > 1)
4035 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004036 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004037 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004038 sr->len = iomsg->iov[0].iov_len;
4039 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004040 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004041 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004042 } else {
4043 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004044 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004045 if (ret > 0)
4046 ret = 0;
4047 }
4048
4049 return ret;
4050}
4051
4052#ifdef CONFIG_COMPAT
4053static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004054 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004055{
4056 struct compat_msghdr __user *msg_compat;
4057 struct io_sr_msg *sr = &req->sr_msg;
4058 struct compat_iovec __user *uiov;
4059 compat_uptr_t ptr;
4060 compat_size_t len;
4061 int ret;
4062
Pavel Begunkov270a5942020-07-12 20:41:04 +03004063 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004064 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004065 &ptr, &len);
4066 if (ret)
4067 return ret;
4068
4069 uiov = compat_ptr(ptr);
4070 if (req->flags & REQ_F_BUFFER_SELECT) {
4071 compat_ssize_t clen;
4072
4073 if (len > 1)
4074 return -EINVAL;
4075 if (!access_ok(uiov, sizeof(*uiov)))
4076 return -EFAULT;
4077 if (__get_user(clen, &uiov->iov_len))
4078 return -EFAULT;
4079 if (clen < 0)
4080 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004081 sr->len = iomsg->iov[0].iov_len;
4082 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004083 } else {
4084 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004085 &iomsg->iov,
4086 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004087 if (ret < 0)
4088 return ret;
4089 }
4090
4091 return 0;
4092}
Jens Axboe03b12302019-12-02 18:50:25 -07004093#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004094
Pavel Begunkov1400e692020-07-12 20:41:05 +03004095static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4096 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004097{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004098 iomsg->msg.msg_name = &iomsg->addr;
4099 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004100
4101#ifdef CONFIG_COMPAT
4102 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004103 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004104#endif
4105
Pavel Begunkov1400e692020-07-12 20:41:05 +03004106 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004107}
4108
Jens Axboebcda7ba2020-02-23 16:42:51 -07004109static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
4110 int *cflags, bool needs_lock)
4111{
4112 struct io_sr_msg *sr = &req->sr_msg;
4113 struct io_buffer *kbuf;
4114
4115 if (!(req->flags & REQ_F_BUFFER_SELECT))
4116 return NULL;
4117
4118 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4119 if (IS_ERR(kbuf))
4120 return kbuf;
4121
4122 sr->kbuf = kbuf;
4123 req->flags |= REQ_F_BUFFER_SELECTED;
4124
4125 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
4126 *cflags |= IORING_CQE_F_BUFFER;
4127 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004128}
4129
Jens Axboe3529d8c2019-12-19 18:24:38 -07004130static int io_recvmsg_prep(struct io_kiocb *req,
4131 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004132{
Jens Axboee47293f2019-12-20 08:58:21 -07004133 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004134 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004135 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004136
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004137 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4138 return -EINVAL;
4139
Jens Axboe3529d8c2019-12-19 18:24:38 -07004140 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004141 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004142 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004143 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004144
Jens Axboed8768362020-02-27 14:17:49 -07004145#ifdef CONFIG_COMPAT
4146 if (req->ctx->compat)
4147 sr->msg_flags |= MSG_CMSG_COMPAT;
4148#endif
4149
Jens Axboefddafac2020-01-04 20:19:44 -07004150 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004151 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004152 /* iovec is already imported */
4153 if (req->flags & REQ_F_NEED_CLEANUP)
4154 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004155
Pavel Begunkov1400e692020-07-12 20:41:05 +03004156 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004157 if (!ret)
4158 req->flags |= REQ_F_NEED_CLEANUP;
4159 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004160}
4161
Jens Axboe229a7b62020-06-22 10:13:11 -06004162static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4163 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004164{
Jens Axboe0b416c32019-12-15 10:57:46 -07004165 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004166 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004167 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004168
Jens Axboe0fa03c62019-04-19 13:34:07 -06004169 sock = sock_from_file(req->file, &ret);
4170 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07004171 struct io_buffer *kbuf;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004172 struct io_async_msghdr iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004173 unsigned flags;
4174
Jens Axboe03b12302019-12-02 18:50:25 -07004175 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07004176 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004177 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07004178 /* if iov is set, it's allocated already */
4179 if (!kmsg->iov)
4180 kmsg->iov = kmsg->fast_iov;
4181 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004182 } else {
Pavel Begunkov1400e692020-07-12 20:41:05 +03004183 ret = io_recvmsg_copy_hdr(req, &iomsg);
Jens Axboe03b12302019-12-02 18:50:25 -07004184 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004185 return ret;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004186 kmsg = &iomsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004187 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06004188
Jens Axboe52de1fe2020-02-27 10:15:42 -07004189 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4190 if (IS_ERR(kbuf)) {
4191 return PTR_ERR(kbuf);
4192 } else if (kbuf) {
4193 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4194 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4195 1, req->sr_msg.len);
4196 }
4197
Jens Axboee47293f2019-12-20 08:58:21 -07004198 flags = req->sr_msg.msg_flags;
4199 if (flags & MSG_DONTWAIT)
4200 req->flags |= REQ_F_NOWAIT;
4201 else if (force_nonblock)
4202 flags |= MSG_DONTWAIT;
4203
Pavel Begunkov270a5942020-07-12 20:41:04 +03004204 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
Jens Axboee47293f2019-12-20 08:58:21 -07004205 kmsg->uaddr, flags);
Pavel Begunkov681fda82020-07-15 22:20:45 +03004206 if (force_nonblock && ret == -EAGAIN) {
4207 ret = io_setup_async_msg(req, kmsg);
4208 if (ret != -EAGAIN)
4209 kfree(kbuf);
4210 return ret;
4211 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07004212 if (ret == -ERESTARTSYS)
4213 ret = -EINTR;
Pavel Begunkov681fda82020-07-15 22:20:45 +03004214 if (kbuf)
4215 kfree(kbuf);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004216 }
4217
Pavel Begunkov1e950812020-02-06 19:51:16 +03004218 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004219 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004220 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004221 if (ret < 0)
4222 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004223 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004224 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004225}
4226
Jens Axboe229a7b62020-06-22 10:13:11 -06004227static int io_recv(struct io_kiocb *req, bool force_nonblock,
4228 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004229{
Jens Axboebcda7ba2020-02-23 16:42:51 -07004230 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07004231 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004232 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004233
Jens Axboefddafac2020-01-04 20:19:44 -07004234 sock = sock_from_file(req->file, &ret);
4235 if (sock) {
4236 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004237 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004238 struct msghdr msg;
4239 struct iovec iov;
4240 unsigned flags;
4241
Jens Axboebcda7ba2020-02-23 16:42:51 -07004242 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4243 if (IS_ERR(kbuf))
4244 return PTR_ERR(kbuf);
4245 else if (kbuf)
4246 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004247
Jens Axboebcda7ba2020-02-23 16:42:51 -07004248 ret = import_single_range(READ, buf, sr->len, &iov,
4249 &msg.msg_iter);
4250 if (ret) {
4251 kfree(kbuf);
4252 return ret;
4253 }
4254
4255 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004256 msg.msg_name = NULL;
4257 msg.msg_control = NULL;
4258 msg.msg_controllen = 0;
4259 msg.msg_namelen = 0;
4260 msg.msg_iocb = NULL;
4261 msg.msg_flags = 0;
4262
4263 flags = req->sr_msg.msg_flags;
4264 if (flags & MSG_DONTWAIT)
4265 req->flags |= REQ_F_NOWAIT;
4266 else if (force_nonblock)
4267 flags |= MSG_DONTWAIT;
4268
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004269 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07004270 if (force_nonblock && ret == -EAGAIN)
4271 return -EAGAIN;
4272 if (ret == -ERESTARTSYS)
4273 ret = -EINTR;
4274 }
4275
Jens Axboebcda7ba2020-02-23 16:42:51 -07004276 kfree(kbuf);
4277 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004278 if (ret < 0)
4279 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004280 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004281 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004282}
4283
Jens Axboe3529d8c2019-12-19 18:24:38 -07004284static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004285{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004286 struct io_accept *accept = &req->accept;
4287
Jens Axboe17f2fe32019-10-17 14:42:58 -06004288 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4289 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004290 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004291 return -EINVAL;
4292
Jens Axboed55e5f52019-12-11 16:12:15 -07004293 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4294 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004295 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004296 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004297 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004298}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004299
Jens Axboe229a7b62020-06-22 10:13:11 -06004300static int io_accept(struct io_kiocb *req, bool force_nonblock,
4301 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004302{
4303 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004304 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004305 int ret;
4306
Jiufei Xuee697dee2020-06-10 13:41:59 +08004307 if (req->file->f_flags & O_NONBLOCK)
4308 req->flags |= REQ_F_NOWAIT;
4309
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004310 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004311 accept->addr_len, accept->flags,
4312 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004313 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004314 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004315 if (ret < 0) {
4316 if (ret == -ERESTARTSYS)
4317 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004318 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004319 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004320 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004321 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004322}
4323
Jens Axboe3529d8c2019-12-19 18:24:38 -07004324static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004325{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004326 struct io_connect *conn = &req->connect;
4327 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004328
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004329 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4330 return -EINVAL;
4331 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4332 return -EINVAL;
4333
Jens Axboe3529d8c2019-12-19 18:24:38 -07004334 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4335 conn->addr_len = READ_ONCE(sqe->addr2);
4336
4337 if (!io)
4338 return 0;
4339
4340 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004341 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004342}
4343
Jens Axboe229a7b62020-06-22 10:13:11 -06004344static int io_connect(struct io_kiocb *req, bool force_nonblock,
4345 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004346{
Jens Axboef499a022019-12-02 16:28:46 -07004347 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004348 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004349 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004350
Jens Axboef499a022019-12-02 16:28:46 -07004351 if (req->io) {
4352 io = req->io;
4353 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004354 ret = move_addr_to_kernel(req->connect.addr,
4355 req->connect.addr_len,
4356 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004357 if (ret)
4358 goto out;
4359 io = &__io;
4360 }
4361
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004362 file_flags = force_nonblock ? O_NONBLOCK : 0;
4363
4364 ret = __sys_connect_file(req->file, &io->connect.address,
4365 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004366 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004367 if (req->io)
4368 return -EAGAIN;
4369 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004370 ret = -ENOMEM;
4371 goto out;
4372 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004373 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004374 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004375 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004376 if (ret == -ERESTARTSYS)
4377 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004378out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004379 if (ret < 0)
4380 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004381 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004382 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004383}
YueHaibing469956e2020-03-04 15:53:52 +08004384#else /* !CONFIG_NET */
4385static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4386{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004387 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004388}
4389
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004390static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4391 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004392{
YueHaibing469956e2020-03-04 15:53:52 +08004393 return -EOPNOTSUPP;
4394}
4395
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004396static int io_send(struct io_kiocb *req, bool force_nonblock,
4397 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004398{
4399 return -EOPNOTSUPP;
4400}
4401
4402static int io_recvmsg_prep(struct io_kiocb *req,
4403 const struct io_uring_sqe *sqe)
4404{
4405 return -EOPNOTSUPP;
4406}
4407
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004408static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4409 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004410{
4411 return -EOPNOTSUPP;
4412}
4413
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004414static int io_recv(struct io_kiocb *req, bool force_nonblock,
4415 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004416{
4417 return -EOPNOTSUPP;
4418}
4419
4420static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4421{
4422 return -EOPNOTSUPP;
4423}
4424
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004425static int io_accept(struct io_kiocb *req, bool force_nonblock,
4426 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004427{
4428 return -EOPNOTSUPP;
4429}
4430
4431static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4432{
4433 return -EOPNOTSUPP;
4434}
4435
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004436static int io_connect(struct io_kiocb *req, bool force_nonblock,
4437 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004438{
4439 return -EOPNOTSUPP;
4440}
4441#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004442
Jens Axboed7718a92020-02-14 22:23:12 -07004443struct io_poll_table {
4444 struct poll_table_struct pt;
4445 struct io_kiocb *req;
4446 int error;
4447};
4448
Jens Axboed7718a92020-02-14 22:23:12 -07004449static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4450 __poll_t mask, task_work_func_t func)
4451{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004452 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004453
4454 /* for instances that support it check for an event match first: */
4455 if (mask && !(mask & poll->events))
4456 return 0;
4457
4458 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4459
4460 list_del_init(&poll->wait.entry);
4461
Jens Axboed7718a92020-02-14 22:23:12 -07004462 req->result = mask;
4463 init_task_work(&req->task_work, func);
4464 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004465 * If this fails, then the task is exiting. When a task exits, the
4466 * work gets canceled, so just cancel this request as well instead
4467 * of executing it. We can't safely execute it anyway, as we may not
4468 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004469 */
Jens Axboeb7db41c2020-07-04 08:55:50 -06004470 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004471 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004472 struct task_struct *tsk;
4473
Jens Axboee3aabf92020-05-18 11:04:17 -06004474 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004475 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004476 task_work_add(tsk, &req->task_work, 0);
4477 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004478 }
Jens Axboed7718a92020-02-14 22:23:12 -07004479 return 1;
4480}
4481
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004482static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4483 __acquires(&req->ctx->completion_lock)
4484{
4485 struct io_ring_ctx *ctx = req->ctx;
4486
4487 if (!req->result && !READ_ONCE(poll->canceled)) {
4488 struct poll_table_struct pt = { ._key = poll->events };
4489
4490 req->result = vfs_poll(req->file, &pt) & poll->events;
4491 }
4492
4493 spin_lock_irq(&ctx->completion_lock);
4494 if (!req->result && !READ_ONCE(poll->canceled)) {
4495 add_wait_queue(poll->head, &poll->wait);
4496 return true;
4497 }
4498
4499 return false;
4500}
4501
Jens Axboe807abcb2020-07-17 17:09:27 -06004502static void io_poll_remove_double(struct io_kiocb *req, void *data)
Jens Axboe18bceab2020-05-15 11:56:54 -06004503{
Jens Axboe807abcb2020-07-17 17:09:27 -06004504 struct io_poll_iocb *poll = data;
Jens Axboe18bceab2020-05-15 11:56:54 -06004505
4506 lockdep_assert_held(&req->ctx->completion_lock);
4507
4508 if (poll && poll->head) {
4509 struct wait_queue_head *head = poll->head;
4510
4511 spin_lock(&head->lock);
4512 list_del_init(&poll->wait.entry);
4513 if (poll->wait.private)
4514 refcount_dec(&req->refs);
4515 poll->head = NULL;
4516 spin_unlock(&head->lock);
4517 }
4518}
4519
4520static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4521{
4522 struct io_ring_ctx *ctx = req->ctx;
4523
Jens Axboe807abcb2020-07-17 17:09:27 -06004524 io_poll_remove_double(req, req->io);
Jens Axboe18bceab2020-05-15 11:56:54 -06004525 req->poll.done = true;
4526 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4527 io_commit_cqring(ctx);
4528}
4529
4530static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4531{
4532 struct io_ring_ctx *ctx = req->ctx;
4533
4534 if (io_poll_rewait(req, &req->poll)) {
4535 spin_unlock_irq(&ctx->completion_lock);
4536 return;
4537 }
4538
4539 hash_del(&req->hash_node);
4540 io_poll_complete(req, req->result, 0);
4541 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03004542 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004543 spin_unlock_irq(&ctx->completion_lock);
4544
4545 io_cqring_ev_posted(ctx);
4546}
4547
4548static void io_poll_task_func(struct callback_head *cb)
4549{
4550 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4551 struct io_kiocb *nxt = NULL;
4552
4553 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004554 if (nxt)
4555 __io_req_task_submit(nxt);
Jens Axboe18bceab2020-05-15 11:56:54 -06004556}
4557
4558static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4559 int sync, void *key)
4560{
4561 struct io_kiocb *req = wait->private;
Jens Axboe807abcb2020-07-17 17:09:27 -06004562 struct io_poll_iocb *poll = req->apoll->double_poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004563 __poll_t mask = key_to_poll(key);
4564
4565 /* for instances that support it check for an event match first: */
4566 if (mask && !(mask & poll->events))
4567 return 0;
4568
Jens Axboe807abcb2020-07-17 17:09:27 -06004569 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004570 bool done;
4571
Jens Axboe807abcb2020-07-17 17:09:27 -06004572 spin_lock(&poll->head->lock);
4573 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004574 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004575 list_del_init(&poll->wait.entry);
4576 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004577 if (!done)
4578 __io_async_wake(req, poll, mask, io_poll_task_func);
4579 }
4580 refcount_dec(&req->refs);
4581 return 1;
4582}
4583
4584static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4585 wait_queue_func_t wake_func)
4586{
4587 poll->head = NULL;
4588 poll->done = false;
4589 poll->canceled = false;
4590 poll->events = events;
4591 INIT_LIST_HEAD(&poll->wait.entry);
4592 init_waitqueue_func_entry(&poll->wait, wake_func);
4593}
4594
4595static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004596 struct wait_queue_head *head,
4597 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004598{
4599 struct io_kiocb *req = pt->req;
4600
4601 /*
4602 * If poll->head is already set, it's because the file being polled
4603 * uses multiple waitqueues for poll handling (eg one for read, one
4604 * for write). Setup a separate io_poll_iocb if this happens.
4605 */
4606 if (unlikely(poll->head)) {
4607 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004608 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004609 pt->error = -EINVAL;
4610 return;
4611 }
4612 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4613 if (!poll) {
4614 pt->error = -ENOMEM;
4615 return;
4616 }
4617 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4618 refcount_inc(&req->refs);
4619 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004620 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004621 }
4622
4623 pt->error = 0;
4624 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004625
4626 if (poll->events & EPOLLEXCLUSIVE)
4627 add_wait_queue_exclusive(head, &poll->wait);
4628 else
4629 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004630}
4631
4632static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4633 struct poll_table_struct *p)
4634{
4635 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004636 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004637
Jens Axboe807abcb2020-07-17 17:09:27 -06004638 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004639}
4640
Jens Axboed7718a92020-02-14 22:23:12 -07004641static void io_async_task_func(struct callback_head *cb)
4642{
4643 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4644 struct async_poll *apoll = req->apoll;
4645 struct io_ring_ctx *ctx = req->ctx;
4646
4647 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4648
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004649 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004650 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004651 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004652 }
4653
Jens Axboe31067252020-05-17 17:43:31 -06004654 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004655 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004656 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004657
Jens Axboe807abcb2020-07-17 17:09:27 -06004658 io_poll_remove_double(req, apoll->double_poll);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004659 spin_unlock_irq(&ctx->completion_lock);
4660
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004661 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004662 if (req->flags & REQ_F_WORK_INITIALIZED)
4663 memcpy(&req->work, &apoll->work, sizeof(req->work));
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004664
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004665 if (!READ_ONCE(apoll->poll.canceled))
4666 __io_req_task_submit(req);
4667 else
4668 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004669
Jens Axboe807abcb2020-07-17 17:09:27 -06004670 kfree(apoll->double_poll);
Dan Carpenteraa340842020-07-08 21:47:11 +03004671 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004672}
4673
4674static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4675 void *key)
4676{
4677 struct io_kiocb *req = wait->private;
4678 struct io_poll_iocb *poll = &req->apoll->poll;
4679
4680 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4681 key_to_poll(key));
4682
4683 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4684}
4685
4686static void io_poll_req_insert(struct io_kiocb *req)
4687{
4688 struct io_ring_ctx *ctx = req->ctx;
4689 struct hlist_head *list;
4690
4691 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4692 hlist_add_head(&req->hash_node, list);
4693}
4694
4695static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4696 struct io_poll_iocb *poll,
4697 struct io_poll_table *ipt, __poll_t mask,
4698 wait_queue_func_t wake_func)
4699 __acquires(&ctx->completion_lock)
4700{
4701 struct io_ring_ctx *ctx = req->ctx;
4702 bool cancel = false;
4703
Jens Axboe18bceab2020-05-15 11:56:54 -06004704 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004705 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004706 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004707
4708 ipt->pt._key = mask;
4709 ipt->req = req;
4710 ipt->error = -EINVAL;
4711
Jens Axboed7718a92020-02-14 22:23:12 -07004712 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4713
4714 spin_lock_irq(&ctx->completion_lock);
4715 if (likely(poll->head)) {
4716 spin_lock(&poll->head->lock);
4717 if (unlikely(list_empty(&poll->wait.entry))) {
4718 if (ipt->error)
4719 cancel = true;
4720 ipt->error = 0;
4721 mask = 0;
4722 }
4723 if (mask || ipt->error)
4724 list_del_init(&poll->wait.entry);
4725 else if (cancel)
4726 WRITE_ONCE(poll->canceled, true);
4727 else if (!poll->done) /* actually waiting for an event */
4728 io_poll_req_insert(req);
4729 spin_unlock(&poll->head->lock);
4730 }
4731
4732 return mask;
4733}
4734
4735static bool io_arm_poll_handler(struct io_kiocb *req)
4736{
4737 const struct io_op_def *def = &io_op_defs[req->opcode];
4738 struct io_ring_ctx *ctx = req->ctx;
4739 struct async_poll *apoll;
4740 struct io_poll_table ipt;
4741 __poll_t mask, ret;
4742
4743 if (!req->file || !file_can_poll(req->file))
4744 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004745 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004746 return false;
4747 if (!def->pollin && !def->pollout)
4748 return false;
4749
4750 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4751 if (unlikely(!apoll))
4752 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004753 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004754
4755 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004756 if (req->flags & REQ_F_WORK_INITIALIZED)
4757 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004758
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004759 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004760 req->apoll = apoll;
4761 INIT_HLIST_NODE(&req->hash_node);
4762
Nathan Chancellor8755d972020-03-02 16:01:19 -07004763 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004764 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004765 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004766 if (def->pollout)
4767 mask |= POLLOUT | POLLWRNORM;
4768 mask |= POLLERR | POLLPRI;
4769
4770 ipt.pt._qproc = io_async_queue_proc;
4771
4772 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4773 io_async_wake);
4774 if (ret) {
Jens Axboe807abcb2020-07-17 17:09:27 -06004775 io_poll_remove_double(req, apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004776 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004777 if (req->flags & REQ_F_WORK_INITIALIZED)
4778 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe807abcb2020-07-17 17:09:27 -06004779 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004780 kfree(apoll);
4781 return false;
4782 }
4783 spin_unlock_irq(&ctx->completion_lock);
4784 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4785 apoll->poll.events);
4786 return true;
4787}
4788
4789static bool __io_poll_remove_one(struct io_kiocb *req,
4790 struct io_poll_iocb *poll)
4791{
Jens Axboeb41e9852020-02-17 09:52:41 -07004792 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004793
4794 spin_lock(&poll->head->lock);
4795 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004796 if (!list_empty(&poll->wait.entry)) {
4797 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004798 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004799 }
4800 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004801 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004802 return do_complete;
4803}
4804
4805static bool io_poll_remove_one(struct io_kiocb *req)
4806{
4807 bool do_complete;
4808
4809 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe807abcb2020-07-17 17:09:27 -06004810 io_poll_remove_double(req, req->io);
Jens Axboed7718a92020-02-14 22:23:12 -07004811 do_complete = __io_poll_remove_one(req, &req->poll);
4812 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004813 struct async_poll *apoll = req->apoll;
4814
Jens Axboe807abcb2020-07-17 17:09:27 -06004815 io_poll_remove_double(req, apoll->double_poll);
4816
Jens Axboed7718a92020-02-14 22:23:12 -07004817 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004818 do_complete = __io_poll_remove_one(req, &apoll->poll);
4819 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004820 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004821 /*
4822 * restore ->work because we will call
4823 * io_req_work_drop_env below when dropping the
4824 * final reference.
4825 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004826 if (req->flags & REQ_F_WORK_INITIALIZED)
4827 memcpy(&req->work, &apoll->work,
4828 sizeof(req->work));
Jens Axboe807abcb2020-07-17 17:09:27 -06004829 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004830 kfree(apoll);
4831 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004832 }
4833
Jens Axboeb41e9852020-02-17 09:52:41 -07004834 if (do_complete) {
4835 io_cqring_fill_event(req, -ECANCELED);
4836 io_commit_cqring(req->ctx);
4837 req->flags |= REQ_F_COMP_LOCKED;
4838 io_put_req(req);
4839 }
4840
4841 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004842}
4843
4844static void io_poll_remove_all(struct io_ring_ctx *ctx)
4845{
Jens Axboe78076bb2019-12-04 19:56:40 -07004846 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004847 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004848 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004849
4850 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004851 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4852 struct hlist_head *list;
4853
4854 list = &ctx->cancel_hash[i];
4855 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004856 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004857 }
4858 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004859
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004860 if (posted)
4861 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004862}
4863
Jens Axboe47f46762019-11-09 17:43:02 -07004864static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4865{
Jens Axboe78076bb2019-12-04 19:56:40 -07004866 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004867 struct io_kiocb *req;
4868
Jens Axboe78076bb2019-12-04 19:56:40 -07004869 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4870 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004871 if (sqe_addr != req->user_data)
4872 continue;
4873 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004874 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004875 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004876 }
4877
4878 return -ENOENT;
4879}
4880
Jens Axboe3529d8c2019-12-19 18:24:38 -07004881static int io_poll_remove_prep(struct io_kiocb *req,
4882 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004883{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004884 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4885 return -EINVAL;
4886 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4887 sqe->poll_events)
4888 return -EINVAL;
4889
Jens Axboe0969e782019-12-17 18:40:57 -07004890 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004891 return 0;
4892}
4893
4894/*
4895 * Find a running poll command that matches one specified in sqe->addr,
4896 * and remove it if found.
4897 */
4898static int io_poll_remove(struct io_kiocb *req)
4899{
4900 struct io_ring_ctx *ctx = req->ctx;
4901 u64 addr;
4902 int ret;
4903
Jens Axboe0969e782019-12-17 18:40:57 -07004904 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004905 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004906 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004907 spin_unlock_irq(&ctx->completion_lock);
4908
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004909 if (ret < 0)
4910 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004911 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004912 return 0;
4913}
4914
Jens Axboe221c5eb2019-01-17 09:41:58 -07004915static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4916 void *key)
4917{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004918 struct io_kiocb *req = wait->private;
4919 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004920
Jens Axboed7718a92020-02-14 22:23:12 -07004921 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004922}
4923
Jens Axboe221c5eb2019-01-17 09:41:58 -07004924static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4925 struct poll_table_struct *p)
4926{
4927 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4928
Jens Axboe807abcb2020-07-17 17:09:27 -06004929 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07004930}
4931
Jens Axboe3529d8c2019-12-19 18:24:38 -07004932static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004933{
4934 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004935 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004936
4937 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4938 return -EINVAL;
4939 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4940 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004941 if (!poll->file)
4942 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004943
Jiufei Xue5769a352020-06-17 17:53:55 +08004944 events = READ_ONCE(sqe->poll32_events);
4945#ifdef __BIG_ENDIAN
4946 events = swahw32(events);
4947#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004948 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4949 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004950
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004951 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004952 return 0;
4953}
4954
Pavel Begunkov014db002020-03-03 21:33:12 +03004955static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004956{
4957 struct io_poll_iocb *poll = &req->poll;
4958 struct io_ring_ctx *ctx = req->ctx;
4959 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004960 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004961
Pavel Begunkovd5e16d82020-07-24 20:07:20 +03004962 /* ->work is in union with hash_node and others */
4963 io_req_work_drop_env(req);
4964 req->flags &= ~REQ_F_WORK_INITIALIZED;
4965
Jens Axboe78076bb2019-12-04 19:56:40 -07004966 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004967 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004968
Jens Axboed7718a92020-02-14 22:23:12 -07004969 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4970 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004971
Jens Axboe8c838782019-03-12 15:48:16 -06004972 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004973 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004974 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004975 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004976 spin_unlock_irq(&ctx->completion_lock);
4977
Jens Axboe8c838782019-03-12 15:48:16 -06004978 if (mask) {
4979 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004980 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004981 }
Jens Axboe8c838782019-03-12 15:48:16 -06004982 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004983}
4984
Jens Axboe5262f562019-09-17 12:26:57 -06004985static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4986{
Jens Axboead8a48a2019-11-15 08:49:11 -07004987 struct io_timeout_data *data = container_of(timer,
4988 struct io_timeout_data, timer);
4989 struct io_kiocb *req = data->req;
4990 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004991 unsigned long flags;
4992
Jens Axboe5262f562019-09-17 12:26:57 -06004993 atomic_inc(&ctx->cq_timeouts);
4994
4995 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004996 /*
Jens Axboe11365042019-10-16 09:08:32 -06004997 * We could be racing with timeout deletion. If the list is empty,
4998 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004999 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005000 if (!list_empty(&req->timeout.list))
5001 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005002
Jens Axboe78e19bb2019-11-06 15:21:34 -07005003 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005004 io_commit_cqring(ctx);
5005 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5006
5007 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005008 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005009 io_put_req(req);
5010 return HRTIMER_NORESTART;
5011}
5012
Jens Axboe47f46762019-11-09 17:43:02 -07005013static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5014{
5015 struct io_kiocb *req;
5016 int ret = -ENOENT;
5017
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005018 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Jens Axboe47f46762019-11-09 17:43:02 -07005019 if (user_data == req->user_data) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005020 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005021 ret = 0;
5022 break;
5023 }
5024 }
5025
5026 if (ret == -ENOENT)
5027 return ret;
5028
Jens Axboe2d283902019-12-04 11:08:05 -07005029 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005030 if (ret == -1)
5031 return -EALREADY;
5032
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005033 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005034 io_cqring_fill_event(req, -ECANCELED);
5035 io_put_req(req);
5036 return 0;
5037}
5038
Jens Axboe3529d8c2019-12-19 18:24:38 -07005039static int io_timeout_remove_prep(struct io_kiocb *req,
5040 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005041{
Jens Axboeb29472e2019-12-17 18:50:29 -07005042 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5043 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005044 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5045 return -EINVAL;
5046 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005047 return -EINVAL;
5048
5049 req->timeout.addr = READ_ONCE(sqe->addr);
5050 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5051 if (req->timeout.flags)
5052 return -EINVAL;
5053
Jens Axboeb29472e2019-12-17 18:50:29 -07005054 return 0;
5055}
5056
Jens Axboe11365042019-10-16 09:08:32 -06005057/*
5058 * Remove or update an existing timeout command
5059 */
Jens Axboefc4df992019-12-10 14:38:45 -07005060static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005061{
5062 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005063 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005064
Jens Axboe11365042019-10-16 09:08:32 -06005065 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005066 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005067
Jens Axboe47f46762019-11-09 17:43:02 -07005068 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005069 io_commit_cqring(ctx);
5070 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005071 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005072 if (ret < 0)
5073 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005074 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005075 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005076}
5077
Jens Axboe3529d8c2019-12-19 18:24:38 -07005078static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005079 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005080{
Jens Axboead8a48a2019-11-15 08:49:11 -07005081 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005082 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005083 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005084
Jens Axboead8a48a2019-11-15 08:49:11 -07005085 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005086 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005087 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005088 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005089 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005090 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005091 flags = READ_ONCE(sqe->timeout_flags);
5092 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005093 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005094
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005095 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005096
Jens Axboe3529d8c2019-12-19 18:24:38 -07005097 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005098 return -ENOMEM;
5099
5100 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005101 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005102
5103 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005104 return -EFAULT;
5105
Jens Axboe11365042019-10-16 09:08:32 -06005106 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005107 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005108 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005109 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005110
Jens Axboead8a48a2019-11-15 08:49:11 -07005111 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5112 return 0;
5113}
5114
Jens Axboefc4df992019-12-10 14:38:45 -07005115static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005116{
Jens Axboead8a48a2019-11-15 08:49:11 -07005117 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005118 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005119 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005120 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005121
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005122 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005123
Jens Axboe5262f562019-09-17 12:26:57 -06005124 /*
5125 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005126 * timeout event to be satisfied. If it isn't set, then this is
5127 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005128 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005129 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005130 entry = ctx->timeout_list.prev;
5131 goto add;
5132 }
Jens Axboe5262f562019-09-17 12:26:57 -06005133
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005134 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5135 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005136
5137 /*
5138 * Insertion sort, ensuring the first entry in the list is always
5139 * the one we need first.
5140 */
Jens Axboe5262f562019-09-17 12:26:57 -06005141 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005142 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5143 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005144
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005145 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005146 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005147 /* nxt.seq is behind @tail, otherwise would've been completed */
5148 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005149 break;
5150 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005151add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005152 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005153 data->timer.function = io_timeout_fn;
5154 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005155 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005156 return 0;
5157}
5158
Jens Axboe62755e32019-10-28 21:49:21 -06005159static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005160{
Jens Axboe62755e32019-10-28 21:49:21 -06005161 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005162
Jens Axboe62755e32019-10-28 21:49:21 -06005163 return req->user_data == (unsigned long) data;
5164}
5165
Jens Axboee977d6d2019-11-05 12:39:45 -07005166static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005167{
Jens Axboe62755e32019-10-28 21:49:21 -06005168 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005169 int ret = 0;
5170
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005171 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005172 switch (cancel_ret) {
5173 case IO_WQ_CANCEL_OK:
5174 ret = 0;
5175 break;
5176 case IO_WQ_CANCEL_RUNNING:
5177 ret = -EALREADY;
5178 break;
5179 case IO_WQ_CANCEL_NOTFOUND:
5180 ret = -ENOENT;
5181 break;
5182 }
5183
Jens Axboee977d6d2019-11-05 12:39:45 -07005184 return ret;
5185}
5186
Jens Axboe47f46762019-11-09 17:43:02 -07005187static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5188 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005189 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005190{
5191 unsigned long flags;
5192 int ret;
5193
5194 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5195 if (ret != -ENOENT) {
5196 spin_lock_irqsave(&ctx->completion_lock, flags);
5197 goto done;
5198 }
5199
5200 spin_lock_irqsave(&ctx->completion_lock, flags);
5201 ret = io_timeout_cancel(ctx, sqe_addr);
5202 if (ret != -ENOENT)
5203 goto done;
5204 ret = io_poll_cancel(ctx, sqe_addr);
5205done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005206 if (!ret)
5207 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005208 io_cqring_fill_event(req, ret);
5209 io_commit_cqring(ctx);
5210 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5211 io_cqring_ev_posted(ctx);
5212
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005213 if (ret < 0)
5214 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005215 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005216}
5217
Jens Axboe3529d8c2019-12-19 18:24:38 -07005218static int io_async_cancel_prep(struct io_kiocb *req,
5219 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005220{
Jens Axboefbf23842019-12-17 18:45:56 -07005221 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005222 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005223 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5224 return -EINVAL;
5225 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005226 return -EINVAL;
5227
Jens Axboefbf23842019-12-17 18:45:56 -07005228 req->cancel.addr = READ_ONCE(sqe->addr);
5229 return 0;
5230}
5231
Pavel Begunkov014db002020-03-03 21:33:12 +03005232static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005233{
5234 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005235
Pavel Begunkov014db002020-03-03 21:33:12 +03005236 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005237 return 0;
5238}
5239
Jens Axboe05f3fb32019-12-09 11:22:50 -07005240static int io_files_update_prep(struct io_kiocb *req,
5241 const struct io_uring_sqe *sqe)
5242{
Daniele Albano61710e42020-07-18 14:15:16 -06005243 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5244 return -EINVAL;
5245 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005246 return -EINVAL;
5247
5248 req->files_update.offset = READ_ONCE(sqe->off);
5249 req->files_update.nr_args = READ_ONCE(sqe->len);
5250 if (!req->files_update.nr_args)
5251 return -EINVAL;
5252 req->files_update.arg = READ_ONCE(sqe->addr);
5253 return 0;
5254}
5255
Jens Axboe229a7b62020-06-22 10:13:11 -06005256static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5257 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005258{
5259 struct io_ring_ctx *ctx = req->ctx;
5260 struct io_uring_files_update up;
5261 int ret;
5262
Jens Axboef86cd202020-01-29 13:46:44 -07005263 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005264 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005265
5266 up.offset = req->files_update.offset;
5267 up.fds = req->files_update.arg;
5268
5269 mutex_lock(&ctx->uring_lock);
5270 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5271 mutex_unlock(&ctx->uring_lock);
5272
5273 if (ret < 0)
5274 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005275 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005276 return 0;
5277}
5278
Jens Axboe3529d8c2019-12-19 18:24:38 -07005279static int io_req_defer_prep(struct io_kiocb *req,
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005280 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005281{
Jens Axboee7815732019-12-17 19:45:06 -07005282 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005283
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005284 if (!sqe)
5285 return 0;
5286
Pavel Begunkov710c2bf2020-06-27 14:04:58 +03005287 if (io_op_defs[req->opcode].file_table) {
5288 io_req_init_async(req);
5289 ret = io_grab_files(req);
5290 if (unlikely(ret))
5291 return ret;
5292 }
5293
Jens Axboed625c6e2019-12-17 19:53:05 -07005294 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005295 case IORING_OP_NOP:
5296 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005297 case IORING_OP_READV:
5298 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005299 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005300 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005301 break;
5302 case IORING_OP_WRITEV:
5303 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005304 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005305 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005306 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005307 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005308 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005309 break;
5310 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005311 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005312 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005313 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005314 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005315 break;
5316 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005317 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005318 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005319 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005320 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005321 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005322 break;
5323 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005324 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005325 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005326 break;
Jens Axboef499a022019-12-02 16:28:46 -07005327 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005328 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005329 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005330 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005331 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005332 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005333 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005334 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005335 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005336 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005337 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005338 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005339 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005340 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005341 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005342 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005343 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005344 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005345 case IORING_OP_FALLOCATE:
5346 ret = io_fallocate_prep(req, sqe);
5347 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005348 case IORING_OP_OPENAT:
5349 ret = io_openat_prep(req, sqe);
5350 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005351 case IORING_OP_CLOSE:
5352 ret = io_close_prep(req, sqe);
5353 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005354 case IORING_OP_FILES_UPDATE:
5355 ret = io_files_update_prep(req, sqe);
5356 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005357 case IORING_OP_STATX:
5358 ret = io_statx_prep(req, sqe);
5359 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005360 case IORING_OP_FADVISE:
5361 ret = io_fadvise_prep(req, sqe);
5362 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005363 case IORING_OP_MADVISE:
5364 ret = io_madvise_prep(req, sqe);
5365 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005366 case IORING_OP_OPENAT2:
5367 ret = io_openat2_prep(req, sqe);
5368 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005369 case IORING_OP_EPOLL_CTL:
5370 ret = io_epoll_ctl_prep(req, sqe);
5371 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005372 case IORING_OP_SPLICE:
5373 ret = io_splice_prep(req, sqe);
5374 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005375 case IORING_OP_PROVIDE_BUFFERS:
5376 ret = io_provide_buffers_prep(req, sqe);
5377 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005378 case IORING_OP_REMOVE_BUFFERS:
5379 ret = io_remove_buffers_prep(req, sqe);
5380 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005381 case IORING_OP_TEE:
5382 ret = io_tee_prep(req, sqe);
5383 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005384 default:
Jens Axboee7815732019-12-17 19:45:06 -07005385 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5386 req->opcode);
5387 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005388 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005389 }
5390
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005391 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005392}
5393
Jens Axboe3529d8c2019-12-19 18:24:38 -07005394static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005395{
Jackie Liua197f662019-11-08 08:09:12 -07005396 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005397 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005398
Bob Liu9d858b22019-11-13 18:06:25 +08005399 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005400 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005401 return 0;
5402
Pavel Begunkov650b5482020-05-17 14:02:11 +03005403 if (!req->io) {
5404 if (io_alloc_async_ctx(req))
5405 return -EAGAIN;
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005406 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov650b5482020-05-17 14:02:11 +03005407 if (ret < 0)
5408 return ret;
5409 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005410 io_prep_async_link(req);
Jens Axboe2d283902019-12-04 11:08:05 -07005411
Jens Axboede0617e2019-04-06 21:51:27 -06005412 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005413 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005414 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005415 return 0;
5416 }
5417
Jens Axboe915967f2019-11-21 09:01:20 -07005418 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005419 list_add_tail(&req->list, &ctx->defer_list);
5420 spin_unlock_irq(&ctx->completion_lock);
5421 return -EIOCBQUEUED;
5422}
5423
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005424static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005425{
5426 struct io_async_ctx *io = req->io;
5427
5428 switch (req->opcode) {
5429 case IORING_OP_READV:
5430 case IORING_OP_READ_FIXED:
5431 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005432 if (req->flags & REQ_F_BUFFER_SELECTED)
5433 kfree((void *)(unsigned long)req->rw.addr);
5434 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005435 case IORING_OP_WRITEV:
5436 case IORING_OP_WRITE_FIXED:
5437 case IORING_OP_WRITE:
5438 if (io->rw.iov != io->rw.fast_iov)
5439 kfree(io->rw.iov);
5440 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005441 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005442 if (req->flags & REQ_F_BUFFER_SELECTED)
5443 kfree(req->sr_msg.kbuf);
5444 /* fallthrough */
5445 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005446 if (io->msg.iov != io->msg.fast_iov)
5447 kfree(io->msg.iov);
5448 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005449 case IORING_OP_RECV:
5450 if (req->flags & REQ_F_BUFFER_SELECTED)
5451 kfree(req->sr_msg.kbuf);
5452 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005453 case IORING_OP_OPENAT:
5454 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005455 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005456 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005457 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005458 io_put_file(req, req->splice.file_in,
5459 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5460 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005461 }
5462
5463 req->flags &= ~REQ_F_NEED_CLEANUP;
5464}
5465
Jens Axboe3529d8c2019-12-19 18:24:38 -07005466static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005467 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005468{
Jackie Liua197f662019-11-08 08:09:12 -07005469 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005470 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005471
Jens Axboed625c6e2019-12-17 19:53:05 -07005472 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005473 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005474 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005475 break;
5476 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005477 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005478 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005479 if (sqe) {
5480 ret = io_read_prep(req, sqe, force_nonblock);
5481 if (ret < 0)
5482 break;
5483 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005484 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005485 break;
5486 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005487 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005488 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005489 if (sqe) {
5490 ret = io_write_prep(req, sqe, force_nonblock);
5491 if (ret < 0)
5492 break;
5493 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005494 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005495 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005496 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005497 if (sqe) {
5498 ret = io_prep_fsync(req, sqe);
5499 if (ret < 0)
5500 break;
5501 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005502 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005503 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005504 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005505 if (sqe) {
5506 ret = io_poll_add_prep(req, sqe);
5507 if (ret)
5508 break;
5509 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005510 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005511 break;
5512 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005513 if (sqe) {
5514 ret = io_poll_remove_prep(req, sqe);
5515 if (ret < 0)
5516 break;
5517 }
Jens Axboefc4df992019-12-10 14:38:45 -07005518 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005519 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005520 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005521 if (sqe) {
5522 ret = io_prep_sfr(req, sqe);
5523 if (ret < 0)
5524 break;
5525 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005526 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005527 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005528 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005529 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005530 if (sqe) {
5531 ret = io_sendmsg_prep(req, sqe);
5532 if (ret < 0)
5533 break;
5534 }
Jens Axboefddafac2020-01-04 20:19:44 -07005535 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005536 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005537 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005538 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005539 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005540 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005541 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005542 if (sqe) {
5543 ret = io_recvmsg_prep(req, sqe);
5544 if (ret)
5545 break;
5546 }
Jens Axboefddafac2020-01-04 20:19:44 -07005547 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005548 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005549 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005550 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005551 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005552 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005553 if (sqe) {
5554 ret = io_timeout_prep(req, sqe, false);
5555 if (ret)
5556 break;
5557 }
Jens Axboefc4df992019-12-10 14:38:45 -07005558 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005559 break;
Jens Axboe11365042019-10-16 09:08:32 -06005560 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005561 if (sqe) {
5562 ret = io_timeout_remove_prep(req, sqe);
5563 if (ret)
5564 break;
5565 }
Jens Axboefc4df992019-12-10 14:38:45 -07005566 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005567 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005568 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005569 if (sqe) {
5570 ret = io_accept_prep(req, sqe);
5571 if (ret)
5572 break;
5573 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005574 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005575 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005576 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005577 if (sqe) {
5578 ret = io_connect_prep(req, sqe);
5579 if (ret)
5580 break;
5581 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005582 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005583 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005584 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005585 if (sqe) {
5586 ret = io_async_cancel_prep(req, sqe);
5587 if (ret)
5588 break;
5589 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005590 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005591 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005592 case IORING_OP_FALLOCATE:
5593 if (sqe) {
5594 ret = io_fallocate_prep(req, sqe);
5595 if (ret)
5596 break;
5597 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005598 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005599 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005600 case IORING_OP_OPENAT:
5601 if (sqe) {
5602 ret = io_openat_prep(req, sqe);
5603 if (ret)
5604 break;
5605 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005606 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005607 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005608 case IORING_OP_CLOSE:
5609 if (sqe) {
5610 ret = io_close_prep(req, sqe);
5611 if (ret)
5612 break;
5613 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005614 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005615 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005616 case IORING_OP_FILES_UPDATE:
5617 if (sqe) {
5618 ret = io_files_update_prep(req, sqe);
5619 if (ret)
5620 break;
5621 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005622 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005623 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005624 case IORING_OP_STATX:
5625 if (sqe) {
5626 ret = io_statx_prep(req, sqe);
5627 if (ret)
5628 break;
5629 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005630 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005631 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005632 case IORING_OP_FADVISE:
5633 if (sqe) {
5634 ret = io_fadvise_prep(req, sqe);
5635 if (ret)
5636 break;
5637 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005638 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005639 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005640 case IORING_OP_MADVISE:
5641 if (sqe) {
5642 ret = io_madvise_prep(req, sqe);
5643 if (ret)
5644 break;
5645 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005646 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005647 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005648 case IORING_OP_OPENAT2:
5649 if (sqe) {
5650 ret = io_openat2_prep(req, sqe);
5651 if (ret)
5652 break;
5653 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005654 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005655 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005656 case IORING_OP_EPOLL_CTL:
5657 if (sqe) {
5658 ret = io_epoll_ctl_prep(req, sqe);
5659 if (ret)
5660 break;
5661 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005662 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005663 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005664 case IORING_OP_SPLICE:
5665 if (sqe) {
5666 ret = io_splice_prep(req, sqe);
5667 if (ret < 0)
5668 break;
5669 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005670 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005671 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005672 case IORING_OP_PROVIDE_BUFFERS:
5673 if (sqe) {
5674 ret = io_provide_buffers_prep(req, sqe);
5675 if (ret)
5676 break;
5677 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005678 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005679 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005680 case IORING_OP_REMOVE_BUFFERS:
5681 if (sqe) {
5682 ret = io_remove_buffers_prep(req, sqe);
5683 if (ret)
5684 break;
5685 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005686 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005687 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005688 case IORING_OP_TEE:
5689 if (sqe) {
5690 ret = io_tee_prep(req, sqe);
5691 if (ret < 0)
5692 break;
5693 }
5694 ret = io_tee(req, force_nonblock);
5695 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005696 default:
5697 ret = -EINVAL;
5698 break;
5699 }
5700
5701 if (ret)
5702 return ret;
5703
Jens Axboeb5325762020-05-19 21:20:27 -06005704 /* If the op doesn't have a file, we're not polling for it */
5705 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005706 const bool in_async = io_wq_current_is_worker();
5707
Jens Axboe11ba8202020-01-15 21:51:17 -07005708 /* workqueue context doesn't hold uring_lock, grab it now */
5709 if (in_async)
5710 mutex_lock(&ctx->uring_lock);
5711
Jens Axboe2b188cc2019-01-07 10:46:33 -07005712 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005713
5714 if (in_async)
5715 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005716 }
5717
5718 return 0;
5719}
5720
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005721static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Jens Axboedef596e2019-01-09 08:59:42 -07005722{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005723 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005724 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005725 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005726
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005727 timeout = io_prep_linked_timeout(req);
5728 if (timeout)
5729 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005730
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005731 /* if NO_CANCEL is set, we must still run the work */
5732 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5733 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005734 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005735 }
Jens Axboe31b51512019-01-18 22:56:34 -07005736
Jens Axboe561fb042019-10-24 07:25:42 -06005737 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005738 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005739 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005740 /*
5741 * We can get EAGAIN for polled IO even though we're
5742 * forcing a sync submission from here, since we can't
5743 * wait for request slots on the block side.
5744 */
5745 if (ret != -EAGAIN)
5746 break;
5747 cond_resched();
5748 } while (1);
5749 }
Jens Axboe31b51512019-01-18 22:56:34 -07005750
Jens Axboe561fb042019-10-24 07:25:42 -06005751 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005752 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005753 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005754 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005755
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005756 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005757}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005758
Jens Axboe65e19f52019-10-26 07:20:21 -06005759static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5760 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005761{
Jens Axboe65e19f52019-10-26 07:20:21 -06005762 struct fixed_file_table *table;
5763
Jens Axboe05f3fb32019-12-09 11:22:50 -07005764 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005765 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005766}
5767
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005768static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5769 int fd, struct file **out_file, bool fixed)
5770{
5771 struct io_ring_ctx *ctx = req->ctx;
5772 struct file *file;
5773
5774 if (fixed) {
5775 if (unlikely(!ctx->file_data ||
5776 (unsigned) fd >= ctx->nr_user_files))
5777 return -EBADF;
5778 fd = array_index_nospec(fd, ctx->nr_user_files);
5779 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005780 if (file) {
5781 req->fixed_file_refs = ctx->file_data->cur_refs;
5782 percpu_ref_get(req->fixed_file_refs);
5783 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005784 } else {
5785 trace_io_uring_file_get(ctx, fd);
5786 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005787 }
5788
Jens Axboefd2206e2020-06-02 16:40:47 -06005789 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5790 *out_file = file;
5791 return 0;
5792 }
5793 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005794}
5795
Jens Axboe3529d8c2019-12-19 18:24:38 -07005796static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005797 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005798{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005799 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005800
Jens Axboe63ff8222020-05-07 14:56:15 -06005801 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005802 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005803 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005804
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005805 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005806}
5807
Jackie Liua197f662019-11-08 08:09:12 -07005808static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005809{
Jens Axboefcb323c2019-10-24 12:39:47 -06005810 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005811 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005812
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005813 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005814 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005815 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005816 return -EBADF;
5817
Jens Axboefcb323c2019-10-24 12:39:47 -06005818 rcu_read_lock();
5819 spin_lock_irq(&ctx->inflight_lock);
5820 /*
5821 * We use the f_ops->flush() handler to ensure that we can flush
5822 * out work accessing these files if the fd is closed. Check if
5823 * the fd has changed since we started down this path, and disallow
5824 * this operation if it has.
5825 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005826 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005827 list_add(&req->inflight_entry, &ctx->inflight_list);
5828 req->flags |= REQ_F_INFLIGHT;
5829 req->work.files = current->files;
5830 ret = 0;
5831 }
5832 spin_unlock_irq(&ctx->inflight_lock);
5833 rcu_read_unlock();
5834
5835 return ret;
5836}
5837
Jens Axboe2665abf2019-11-05 12:40:47 -07005838static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5839{
Jens Axboead8a48a2019-11-15 08:49:11 -07005840 struct io_timeout_data *data = container_of(timer,
5841 struct io_timeout_data, timer);
5842 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005843 struct io_ring_ctx *ctx = req->ctx;
5844 struct io_kiocb *prev = NULL;
5845 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005846
5847 spin_lock_irqsave(&ctx->completion_lock, flags);
5848
5849 /*
5850 * We don't expect the list to be empty, that will only happen if we
5851 * race with the completion of the linked work.
5852 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005853 if (!list_empty(&req->link_list)) {
5854 prev = list_entry(req->link_list.prev, struct io_kiocb,
5855 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005856 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005857 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005858 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5859 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005860 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005861 }
5862
5863 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5864
5865 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005866 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005867 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005868 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005869 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005870 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005871 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005872 return HRTIMER_NORESTART;
5873}
5874
Jens Axboead8a48a2019-11-15 08:49:11 -07005875static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005876{
Jens Axboe76a46e02019-11-10 23:34:16 -07005877 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005878
Jens Axboe76a46e02019-11-10 23:34:16 -07005879 /*
5880 * If the list is now empty, then our linked request finished before
5881 * we got a chance to setup the timer
5882 */
5883 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005884 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005885 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005886
Jens Axboead8a48a2019-11-15 08:49:11 -07005887 data->timer.function = io_link_timeout_fn;
5888 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5889 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005890 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005891 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005892
Jens Axboe2665abf2019-11-05 12:40:47 -07005893 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005894 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005895}
5896
Jens Axboead8a48a2019-11-15 08:49:11 -07005897static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005898{
5899 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005900
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005901 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005902 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005903 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07005904 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005905
Pavel Begunkov44932332019-12-05 16:16:35 +03005906 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5907 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005908 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005909 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005910
Jens Axboe76a46e02019-11-10 23:34:16 -07005911 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005912 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005913}
5914
Jens Axboef13fad72020-06-22 09:34:30 -06005915static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5916 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005917{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005918 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005919 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005920 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005921 int ret;
5922
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005923again:
5924 linked_timeout = io_prep_linked_timeout(req);
5925
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005926 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5927 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005928 if (old_creds)
5929 revert_creds(old_creds);
5930 if (old_creds == req->work.creds)
5931 old_creds = NULL; /* restored original creds */
5932 else
5933 old_creds = override_creds(req->work.creds);
5934 }
5935
Jens Axboef13fad72020-06-22 09:34:30 -06005936 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06005937
5938 /*
5939 * We async punt it if the file wasn't marked NOWAIT, or if the file
5940 * doesn't support non-blocking read/write attempts
5941 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005942 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005943 if (io_arm_poll_handler(req)) {
5944 if (linked_timeout)
5945 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005946 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005947 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005948punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005949 io_req_init_async(req);
5950
Jens Axboef86cd202020-01-29 13:46:44 -07005951 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005952 ret = io_grab_files(req);
5953 if (ret)
5954 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005955 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005956
5957 /*
5958 * Queued up for async execution, worker will release
5959 * submit reference when the iocb is actually submitted.
5960 */
5961 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005962 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005963 }
Jens Axboee65ef562019-03-12 10:16:44 -06005964
Pavel Begunkov652532a2020-07-03 22:15:07 +03005965 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005966err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03005967 /* un-prep timeout, so it'll be killed as any other linked */
5968 req->flags &= ~REQ_F_LINK_TIMEOUT;
5969 req_set_fail_links(req);
5970 io_put_req(req);
5971 io_req_complete(req, ret);
5972 goto exit;
5973 }
5974
Jens Axboee65ef562019-03-12 10:16:44 -06005975 /* drop submission reference */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03005976 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03005977 if (linked_timeout)
5978 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06005979
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005980 if (nxt) {
5981 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005982
5983 if (req->flags & REQ_F_FORCE_ASYNC)
5984 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005985 goto again;
5986 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005987exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005988 if (old_creds)
5989 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005990}
5991
Jens Axboef13fad72020-06-22 09:34:30 -06005992static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5993 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005994{
5995 int ret;
5996
Jens Axboe3529d8c2019-12-19 18:24:38 -07005997 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005998 if (ret) {
5999 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006000fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006001 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006002 io_put_req(req);
6003 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006004 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006005 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006006 if (!req->io) {
6007 ret = -EAGAIN;
6008 if (io_alloc_async_ctx(req))
6009 goto fail_req;
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006010 ret = io_req_defer_prep(req, sqe);
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006011 if (unlikely(ret < 0))
6012 goto fail_req;
6013 }
6014
Jens Axboece35a472019-12-17 08:04:44 -07006015 /*
6016 * Never try inline submit of IOSQE_ASYNC is set, go straight
6017 * to async execution.
6018 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006019 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006020 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6021 io_queue_async_work(req);
6022 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006023 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006024 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006025}
6026
Jens Axboef13fad72020-06-22 09:34:30 -06006027static inline void io_queue_link_head(struct io_kiocb *req,
6028 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006029{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006030 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006031 io_put_req(req);
6032 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006033 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006034 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006035}
6036
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006037static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006038 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006039{
Jackie Liua197f662019-11-08 08:09:12 -07006040 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006041 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006042
Jens Axboe9e645e112019-05-10 16:07:28 -06006043 /*
6044 * If we already have a head request, queue this one for async
6045 * submittal once the head completes. If we don't have a head but
6046 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6047 * submitted sync once the chain is complete. If none of those
6048 * conditions are true (normal request), then just queue it.
6049 */
6050 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006051 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006052
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006053 /*
6054 * Taking sequential execution of a link, draining both sides
6055 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6056 * requests in the link. So, it drains the head and the
6057 * next after the link request. The last one is done via
6058 * drain_next flag to persist the effect across calls.
6059 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006060 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006061 head->flags |= REQ_F_IO_DRAIN;
6062 ctx->drain_next = 1;
6063 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006064 if (io_alloc_async_ctx(req))
6065 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06006066
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006067 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006068 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006069 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006070 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006071 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006072 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006073 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006074 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006075 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006076
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006077 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006078 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006079 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006080 *link = NULL;
6081 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006082 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006083 if (unlikely(ctx->drain_next)) {
6084 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006085 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006086 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006087 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006088 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006089 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006090
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006091 if (io_alloc_async_ctx(req))
6092 return -EAGAIN;
6093
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006094 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov711be032020-01-17 03:57:59 +03006095 if (ret)
6096 req->flags |= REQ_F_FAIL_LINK;
6097 *link = req;
6098 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006099 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006100 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006101 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006102
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006103 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006104}
6105
Jens Axboe9a56a232019-01-09 09:06:50 -07006106/*
6107 * Batched submission is done, ensure local IO is flushed out.
6108 */
6109static void io_submit_state_end(struct io_submit_state *state)
6110{
Jens Axboef13fad72020-06-22 09:34:30 -06006111 if (!list_empty(&state->comp.list))
6112 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006113 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006114 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006115 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006116 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006117}
6118
6119/*
6120 * Start submission side cache.
6121 */
6122static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006123 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006124{
6125 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006126#ifdef CONFIG_BLOCK
6127 state->plug.nowait = true;
6128#endif
Jens Axboe013538b2020-06-22 09:29:15 -06006129 state->comp.nr = 0;
6130 INIT_LIST_HEAD(&state->comp.list);
6131 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006132 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006133 state->file = NULL;
6134 state->ios_left = max_ios;
6135}
6136
Jens Axboe2b188cc2019-01-07 10:46:33 -07006137static void io_commit_sqring(struct io_ring_ctx *ctx)
6138{
Hristo Venev75b28af2019-08-26 17:23:46 +00006139 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006140
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006141 /*
6142 * Ensure any loads from the SQEs are done at this point,
6143 * since once we write the new head, the application could
6144 * write new data to them.
6145 */
6146 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147}
6148
6149/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006150 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006151 * that is mapped by userspace. This means that care needs to be taken to
6152 * ensure that reads are stable, as we cannot rely on userspace always
6153 * being a good citizen. If members of the sqe are validated and then later
6154 * used, it's important that those reads are done through READ_ONCE() to
6155 * prevent a re-load down the line.
6156 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006157static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006158{
Hristo Venev75b28af2019-08-26 17:23:46 +00006159 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006160 unsigned head;
6161
6162 /*
6163 * The cached sq head (or cq tail) serves two purposes:
6164 *
6165 * 1) allows us to batch the cost of updating the user visible
6166 * head updates.
6167 * 2) allows the kernel side to track the head on its own, even
6168 * though the application is the one updating it.
6169 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006170 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006171 if (likely(head < ctx->sq_entries))
6172 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006173
6174 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006175 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006176 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006177 return NULL;
6178}
6179
6180static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6181{
6182 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006183}
6184
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006185#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6186 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6187 IOSQE_BUFFER_SELECT)
6188
6189static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6190 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006191 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006192{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006193 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006194 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006195
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006196 /*
6197 * All io need record the previous position, if LINK vs DARIN,
6198 * it can be used to mark the position of the first IO in the
6199 * link list.
6200 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03006201 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006202 req->opcode = READ_ONCE(sqe->opcode);
6203 req->user_data = READ_ONCE(sqe->user_data);
6204 req->io = NULL;
6205 req->file = NULL;
6206 req->ctx = ctx;
6207 req->flags = 0;
6208 /* one is dropped after submission, the other at completion */
6209 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006210 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006211 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006212
6213 if (unlikely(req->opcode >= IORING_OP_LAST))
6214 return -EINVAL;
6215
Jens Axboe9d8426a2020-06-16 18:42:49 -06006216 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6217 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006218
6219 sqe_flags = READ_ONCE(sqe->flags);
6220 /* enforce forwards compatibility on users */
6221 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6222 return -EINVAL;
6223
6224 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6225 !io_op_defs[req->opcode].buffer_select)
6226 return -EOPNOTSUPP;
6227
6228 id = READ_ONCE(sqe->personality);
6229 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006230 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006231 req->work.creds = idr_find(&ctx->personality_idr, id);
6232 if (unlikely(!req->work.creds))
6233 return -EINVAL;
6234 get_cred(req->work.creds);
6235 }
6236
6237 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006238 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006239
Jens Axboe63ff8222020-05-07 14:56:15 -06006240 if (!io_op_defs[req->opcode].needs_file)
6241 return 0;
6242
6243 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006244}
6245
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006246static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006247 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006248{
Jens Axboeac8691c2020-06-01 08:30:41 -06006249 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006250 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006251 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006252
Jens Axboec4a2ed72019-11-21 21:01:26 -07006253 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006254 if (test_bit(0, &ctx->sq_check_overflow)) {
6255 if (!list_empty(&ctx->cq_overflow_list) &&
6256 !io_cqring_overflow_flush(ctx, false))
6257 return -EBUSY;
6258 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006259
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006260 /* make sure SQ entry isn't read before tail */
6261 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006262
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006263 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6264 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006265
Jens Axboe013538b2020-06-22 09:29:15 -06006266 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006267
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006268 ctx->ring_fd = ring_fd;
6269 ctx->ring_file = ring_file;
6270
Jens Axboe6c271ce2019-01-10 11:22:30 -07006271 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006272 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006273 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006274 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006275
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006276 sqe = io_get_sqe(ctx);
6277 if (unlikely(!sqe)) {
6278 io_consume_sqe(ctx);
6279 break;
6280 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006281 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006282 if (unlikely(!req)) {
6283 if (!submitted)
6284 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006285 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006286 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006287
Jens Axboeac8691c2020-06-01 08:30:41 -06006288 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006289 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006290 /* will complete beyond this point, count as submitted */
6291 submitted++;
6292
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006293 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006294fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006295 io_put_req(req);
6296 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006297 break;
6298 }
6299
Jens Axboe354420f2020-01-08 18:55:15 -07006300 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006301 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006302 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006303 if (err)
6304 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006305 }
6306
Pavel Begunkov9466f432020-01-25 22:34:01 +03006307 if (unlikely(submitted != nr)) {
6308 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6309
6310 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6311 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006312 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006313 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006314 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006315
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006316 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6317 io_commit_sqring(ctx);
6318
Jens Axboe6c271ce2019-01-10 11:22:30 -07006319 return submitted;
6320}
6321
6322static int io_sq_thread(void *data)
6323{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006324 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006325 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006326 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006327 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006328 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006329
Jens Axboe0f158b42020-05-14 17:18:39 -06006330 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006331
Jens Axboe181e4482019-11-25 08:52:30 -07006332 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006333
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006334 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006335 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006336 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006337
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006338 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006339 unsigned nr_events = 0;
6340
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006341 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006342 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006343 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006344 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006345 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006346 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006347 }
6348
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006349 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006350
6351 /*
6352 * If submit got -EBUSY, flag us as needing the application
6353 * to enter the kernel to reap and flush events.
6354 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006355 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006356 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006357 * Drop cur_mm before scheduling, we can't hold it for
6358 * long periods (or over schedule()). Do this before
6359 * adding ourselves to the waitqueue, as the unuse/drop
6360 * may sleep.
6361 */
Jens Axboe4349f302020-07-09 15:07:01 -06006362 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006363
6364 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006365 * We're polling. If we're within the defined idle
6366 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006367 * to sleep. The exception is if we got EBUSY doing
6368 * more IO, we should wait for the application to
6369 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006370 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006371 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006372 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6373 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006374 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006375 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006376 continue;
6377 }
6378
Jens Axboe6c271ce2019-01-10 11:22:30 -07006379 prepare_to_wait(&ctx->sqo_wait, &wait,
6380 TASK_INTERRUPTIBLE);
6381
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006382 /*
6383 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006384 * to check if there are new reqs added to iopoll_list,
6385 * it is because reqs may have been punted to io worker
6386 * and will be added to iopoll_list later, hence check
6387 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006388 */
6389 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006390 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006391 finish_wait(&ctx->sqo_wait, &wait);
6392 continue;
6393 }
6394
Jens Axboe6c271ce2019-01-10 11:22:30 -07006395 /* Tell userspace we may need a wakeup call */
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08006396 spin_lock_irq(&ctx->completion_lock);
Hristo Venev75b28af2019-08-26 17:23:46 +00006397 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08006398 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006399
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006400 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006401 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006402 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006403 finish_wait(&ctx->sqo_wait, &wait);
6404 break;
6405 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006406 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006407 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006408 continue;
6409 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006410 if (signal_pending(current))
6411 flush_signals(current);
6412 schedule();
6413 finish_wait(&ctx->sqo_wait, &wait);
6414
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08006415 spin_lock_irq(&ctx->completion_lock);
Hristo Venev75b28af2019-08-26 17:23:46 +00006416 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08006417 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006418 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006419 continue;
6420 }
6421 finish_wait(&ctx->sqo_wait, &wait);
6422
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08006423 spin_lock_irq(&ctx->completion_lock);
Hristo Venev75b28af2019-08-26 17:23:46 +00006424 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08006425 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006426 }
6427
Jens Axboe8a4955f2019-12-09 14:52:35 -07006428 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006429 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6430 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006431 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006432 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006433 }
6434
Jens Axboe4c6e2772020-07-01 11:29:10 -06006435 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006436
Jens Axboe4349f302020-07-09 15:07:01 -06006437 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006438 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006439
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006440 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006441
Jens Axboe6c271ce2019-01-10 11:22:30 -07006442 return 0;
6443}
6444
Jens Axboebda52162019-09-24 13:47:15 -06006445struct io_wait_queue {
6446 struct wait_queue_entry wq;
6447 struct io_ring_ctx *ctx;
6448 unsigned to_wait;
6449 unsigned nr_timeouts;
6450};
6451
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006452static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006453{
6454 struct io_ring_ctx *ctx = iowq->ctx;
6455
6456 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006457 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006458 * started waiting. For timeouts, we always want to return to userspace,
6459 * regardless of event count.
6460 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006461 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006462 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6463}
6464
6465static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6466 int wake_flags, void *key)
6467{
6468 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6469 wq);
6470
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006471 /* use noflush == true, as we can't safely rely on locking context */
6472 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006473 return -1;
6474
6475 return autoremove_wake_function(curr, mode, wake_flags, key);
6476}
6477
Jens Axboe2b188cc2019-01-07 10:46:33 -07006478/*
6479 * Wait until events become available, if we don't already have some. The
6480 * application must reap them itself, as they reside on the shared cq ring.
6481 */
6482static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6483 const sigset_t __user *sig, size_t sigsz)
6484{
Jens Axboebda52162019-09-24 13:47:15 -06006485 struct io_wait_queue iowq = {
6486 .wq = {
6487 .private = current,
6488 .func = io_wake_function,
6489 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6490 },
6491 .ctx = ctx,
6492 .to_wait = min_events,
6493 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006494 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006495 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006496
Jens Axboeb41e9852020-02-17 09:52:41 -07006497 do {
6498 if (io_cqring_events(ctx, false) >= min_events)
6499 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006500 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006501 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006502 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006503
6504 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006505#ifdef CONFIG_COMPAT
6506 if (in_compat_syscall())
6507 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006508 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006509 else
6510#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006511 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006512
Jens Axboe2b188cc2019-01-07 10:46:33 -07006513 if (ret)
6514 return ret;
6515 }
6516
Jens Axboebda52162019-09-24 13:47:15 -06006517 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006518 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006519 do {
6520 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6521 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006522 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006523 if (io_run_task_work())
6524 continue;
Jens Axboebda52162019-09-24 13:47:15 -06006525 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006526 if (current->jobctl & JOBCTL_TASK_WORK) {
6527 spin_lock_irq(&current->sighand->siglock);
6528 current->jobctl &= ~JOBCTL_TASK_WORK;
6529 recalc_sigpending();
6530 spin_unlock_irq(&current->sighand->siglock);
6531 continue;
6532 }
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006533 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006534 break;
6535 }
Jens Axboebda52162019-09-24 13:47:15 -06006536 if (io_should_wake(&iowq, false))
6537 break;
6538 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006539 } while (1);
6540 finish_wait(&ctx->wait, &iowq.wq);
6541
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006542 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006543
Hristo Venev75b28af2019-08-26 17:23:46 +00006544 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006545}
6546
Jens Axboe6b063142019-01-10 22:13:58 -07006547static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6548{
6549#if defined(CONFIG_UNIX)
6550 if (ctx->ring_sock) {
6551 struct sock *sock = ctx->ring_sock->sk;
6552 struct sk_buff *skb;
6553
6554 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6555 kfree_skb(skb);
6556 }
6557#else
6558 int i;
6559
Jens Axboe65e19f52019-10-26 07:20:21 -06006560 for (i = 0; i < ctx->nr_user_files; i++) {
6561 struct file *file;
6562
6563 file = io_file_from_index(ctx, i);
6564 if (file)
6565 fput(file);
6566 }
Jens Axboe6b063142019-01-10 22:13:58 -07006567#endif
6568}
6569
Jens Axboe05f3fb32019-12-09 11:22:50 -07006570static void io_file_ref_kill(struct percpu_ref *ref)
6571{
6572 struct fixed_file_data *data;
6573
6574 data = container_of(ref, struct fixed_file_data, refs);
6575 complete(&data->done);
6576}
6577
Jens Axboe6b063142019-01-10 22:13:58 -07006578static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6579{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006580 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006581 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006582 unsigned nr_tables, i;
6583
Jens Axboe05f3fb32019-12-09 11:22:50 -07006584 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006585 return -ENXIO;
6586
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006587 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006588 if (!list_empty(&data->ref_list))
6589 ref_node = list_first_entry(&data->ref_list,
6590 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006591 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006592 if (ref_node)
6593 percpu_ref_kill(&ref_node->refs);
6594
6595 percpu_ref_kill(&data->refs);
6596
6597 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006598 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006599 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006600
Jens Axboe6b063142019-01-10 22:13:58 -07006601 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006602 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6603 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006604 kfree(data->table[i].files);
6605 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006606 percpu_ref_exit(&data->refs);
6607 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006608 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006609 ctx->nr_user_files = 0;
6610 return 0;
6611}
6612
Jens Axboe6c271ce2019-01-10 11:22:30 -07006613static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6614{
6615 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006616 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006617 /*
6618 * The park is a bit of a work-around, without it we get
6619 * warning spews on shutdown with SQPOLL set and affinity
6620 * set to a single CPU.
6621 */
Jens Axboe06058632019-04-13 09:26:03 -06006622 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006623 kthread_stop(ctx->sqo_thread);
6624 ctx->sqo_thread = NULL;
6625 }
6626}
6627
Jens Axboe6b063142019-01-10 22:13:58 -07006628static void io_finish_async(struct io_ring_ctx *ctx)
6629{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006630 io_sq_thread_stop(ctx);
6631
Jens Axboe561fb042019-10-24 07:25:42 -06006632 if (ctx->io_wq) {
6633 io_wq_destroy(ctx->io_wq);
6634 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006635 }
6636}
6637
6638#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006639/*
6640 * Ensure the UNIX gc is aware of our file set, so we are certain that
6641 * the io_uring can be safely unregistered on process exit, even if we have
6642 * loops in the file referencing.
6643 */
6644static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6645{
6646 struct sock *sk = ctx->ring_sock->sk;
6647 struct scm_fp_list *fpl;
6648 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006649 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006650
Jens Axboe6b063142019-01-10 22:13:58 -07006651 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6652 if (!fpl)
6653 return -ENOMEM;
6654
6655 skb = alloc_skb(0, GFP_KERNEL);
6656 if (!skb) {
6657 kfree(fpl);
6658 return -ENOMEM;
6659 }
6660
6661 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006662
Jens Axboe08a45172019-10-03 08:11:03 -06006663 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006664 fpl->user = get_uid(ctx->user);
6665 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006666 struct file *file = io_file_from_index(ctx, i + offset);
6667
6668 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006669 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006670 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006671 unix_inflight(fpl->user, fpl->fp[nr_files]);
6672 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006673 }
6674
Jens Axboe08a45172019-10-03 08:11:03 -06006675 if (nr_files) {
6676 fpl->max = SCM_MAX_FD;
6677 fpl->count = nr_files;
6678 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006679 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006680 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6681 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006682
Jens Axboe08a45172019-10-03 08:11:03 -06006683 for (i = 0; i < nr_files; i++)
6684 fput(fpl->fp[i]);
6685 } else {
6686 kfree_skb(skb);
6687 kfree(fpl);
6688 }
Jens Axboe6b063142019-01-10 22:13:58 -07006689
6690 return 0;
6691}
6692
6693/*
6694 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6695 * causes regular reference counting to break down. We rely on the UNIX
6696 * garbage collection to take care of this problem for us.
6697 */
6698static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6699{
6700 unsigned left, total;
6701 int ret = 0;
6702
6703 total = 0;
6704 left = ctx->nr_user_files;
6705 while (left) {
6706 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006707
6708 ret = __io_sqe_files_scm(ctx, this_files, total);
6709 if (ret)
6710 break;
6711 left -= this_files;
6712 total += this_files;
6713 }
6714
6715 if (!ret)
6716 return 0;
6717
6718 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006719 struct file *file = io_file_from_index(ctx, total);
6720
6721 if (file)
6722 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006723 total++;
6724 }
6725
6726 return ret;
6727}
6728#else
6729static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6730{
6731 return 0;
6732}
6733#endif
6734
Jens Axboe65e19f52019-10-26 07:20:21 -06006735static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6736 unsigned nr_files)
6737{
6738 int i;
6739
6740 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006741 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006742 unsigned this_files;
6743
6744 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6745 table->files = kcalloc(this_files, sizeof(struct file *),
6746 GFP_KERNEL);
6747 if (!table->files)
6748 break;
6749 nr_files -= this_files;
6750 }
6751
6752 if (i == nr_tables)
6753 return 0;
6754
6755 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006756 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006757 kfree(table->files);
6758 }
6759 return 1;
6760}
6761
Jens Axboe05f3fb32019-12-09 11:22:50 -07006762static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006763{
6764#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006765 struct sock *sock = ctx->ring_sock->sk;
6766 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6767 struct sk_buff *skb;
6768 int i;
6769
6770 __skb_queue_head_init(&list);
6771
6772 /*
6773 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6774 * remove this entry and rearrange the file array.
6775 */
6776 skb = skb_dequeue(head);
6777 while (skb) {
6778 struct scm_fp_list *fp;
6779
6780 fp = UNIXCB(skb).fp;
6781 for (i = 0; i < fp->count; i++) {
6782 int left;
6783
6784 if (fp->fp[i] != file)
6785 continue;
6786
6787 unix_notinflight(fp->user, fp->fp[i]);
6788 left = fp->count - 1 - i;
6789 if (left) {
6790 memmove(&fp->fp[i], &fp->fp[i + 1],
6791 left * sizeof(struct file *));
6792 }
6793 fp->count--;
6794 if (!fp->count) {
6795 kfree_skb(skb);
6796 skb = NULL;
6797 } else {
6798 __skb_queue_tail(&list, skb);
6799 }
6800 fput(file);
6801 file = NULL;
6802 break;
6803 }
6804
6805 if (!file)
6806 break;
6807
6808 __skb_queue_tail(&list, skb);
6809
6810 skb = skb_dequeue(head);
6811 }
6812
6813 if (skb_peek(&list)) {
6814 spin_lock_irq(&head->lock);
6815 while ((skb = __skb_dequeue(&list)) != NULL)
6816 __skb_queue_tail(head, skb);
6817 spin_unlock_irq(&head->lock);
6818 }
6819#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006820 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006821#endif
6822}
6823
Jens Axboe05f3fb32019-12-09 11:22:50 -07006824struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006825 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006826 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006827};
6828
Jens Axboe4a38aed22020-05-14 17:21:15 -06006829static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006830{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006831 struct fixed_file_data *file_data = ref_node->file_data;
6832 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006833 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006834
6835 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006836 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006837 io_ring_file_put(ctx, pfile->file);
6838 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006839 }
6840
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006841 spin_lock(&file_data->lock);
6842 list_del(&ref_node->node);
6843 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006844
Xiaoguang Wang05589552020-03-31 14:05:18 +08006845 percpu_ref_exit(&ref_node->refs);
6846 kfree(ref_node);
6847 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006848}
6849
Jens Axboe4a38aed22020-05-14 17:21:15 -06006850static void io_file_put_work(struct work_struct *work)
6851{
6852 struct io_ring_ctx *ctx;
6853 struct llist_node *node;
6854
6855 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6856 node = llist_del_all(&ctx->file_put_llist);
6857
6858 while (node) {
6859 struct fixed_file_ref_node *ref_node;
6860 struct llist_node *next = node->next;
6861
6862 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6863 __io_file_put_work(ref_node);
6864 node = next;
6865 }
6866}
6867
Jens Axboe05f3fb32019-12-09 11:22:50 -07006868static void io_file_data_ref_zero(struct percpu_ref *ref)
6869{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006870 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006871 struct io_ring_ctx *ctx;
6872 bool first_add;
6873 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006874
Xiaoguang Wang05589552020-03-31 14:05:18 +08006875 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006876 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006877
Jens Axboe4a38aed22020-05-14 17:21:15 -06006878 if (percpu_ref_is_dying(&ctx->file_data->refs))
6879 delay = 0;
6880
6881 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6882 if (!delay)
6883 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6884 else if (first_add)
6885 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006886}
6887
6888static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6889 struct io_ring_ctx *ctx)
6890{
6891 struct fixed_file_ref_node *ref_node;
6892
6893 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6894 if (!ref_node)
6895 return ERR_PTR(-ENOMEM);
6896
6897 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6898 0, GFP_KERNEL)) {
6899 kfree(ref_node);
6900 return ERR_PTR(-ENOMEM);
6901 }
6902 INIT_LIST_HEAD(&ref_node->node);
6903 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006904 ref_node->file_data = ctx->file_data;
6905 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006906}
6907
6908static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6909{
6910 percpu_ref_exit(&ref_node->refs);
6911 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006912}
6913
6914static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6915 unsigned nr_args)
6916{
6917 __s32 __user *fds = (__s32 __user *) arg;
6918 unsigned nr_tables;
6919 struct file *file;
6920 int fd, ret = 0;
6921 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006922 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006923
6924 if (ctx->file_data)
6925 return -EBUSY;
6926 if (!nr_args)
6927 return -EINVAL;
6928 if (nr_args > IORING_MAX_FIXED_FILES)
6929 return -EMFILE;
6930
6931 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6932 if (!ctx->file_data)
6933 return -ENOMEM;
6934 ctx->file_data->ctx = ctx;
6935 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006936 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006937 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006938
6939 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6940 ctx->file_data->table = kcalloc(nr_tables,
6941 sizeof(struct fixed_file_table),
6942 GFP_KERNEL);
6943 if (!ctx->file_data->table) {
6944 kfree(ctx->file_data);
6945 ctx->file_data = NULL;
6946 return -ENOMEM;
6947 }
6948
Xiaoguang Wang05589552020-03-31 14:05:18 +08006949 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006950 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6951 kfree(ctx->file_data->table);
6952 kfree(ctx->file_data);
6953 ctx->file_data = NULL;
6954 return -ENOMEM;
6955 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006956
6957 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6958 percpu_ref_exit(&ctx->file_data->refs);
6959 kfree(ctx->file_data->table);
6960 kfree(ctx->file_data);
6961 ctx->file_data = NULL;
6962 return -ENOMEM;
6963 }
6964
6965 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6966 struct fixed_file_table *table;
6967 unsigned index;
6968
6969 ret = -EFAULT;
6970 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6971 break;
6972 /* allow sparse sets */
6973 if (fd == -1) {
6974 ret = 0;
6975 continue;
6976 }
6977
6978 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6979 index = i & IORING_FILE_TABLE_MASK;
6980 file = fget(fd);
6981
6982 ret = -EBADF;
6983 if (!file)
6984 break;
6985
6986 /*
6987 * Don't allow io_uring instances to be registered. If UNIX
6988 * isn't enabled, then this causes a reference cycle and this
6989 * instance can never get freed. If UNIX is enabled we'll
6990 * handle it just fine, but there's still no point in allowing
6991 * a ring fd as it doesn't support regular read/write anyway.
6992 */
6993 if (file->f_op == &io_uring_fops) {
6994 fput(file);
6995 break;
6996 }
6997 ret = 0;
6998 table->files[index] = file;
6999 }
7000
7001 if (ret) {
7002 for (i = 0; i < ctx->nr_user_files; i++) {
7003 file = io_file_from_index(ctx, i);
7004 if (file)
7005 fput(file);
7006 }
7007 for (i = 0; i < nr_tables; i++)
7008 kfree(ctx->file_data->table[i].files);
7009
Yang Yingliang667e57d2020-07-10 14:14:20 +00007010 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007011 kfree(ctx->file_data->table);
7012 kfree(ctx->file_data);
7013 ctx->file_data = NULL;
7014 ctx->nr_user_files = 0;
7015 return ret;
7016 }
7017
7018 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007019 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007020 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007021 return ret;
7022 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007023
Xiaoguang Wang05589552020-03-31 14:05:18 +08007024 ref_node = alloc_fixed_file_ref_node(ctx);
7025 if (IS_ERR(ref_node)) {
7026 io_sqe_files_unregister(ctx);
7027 return PTR_ERR(ref_node);
7028 }
7029
7030 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007031 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007032 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007033 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007034 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007035 return ret;
7036}
7037
Jens Axboec3a31e62019-10-03 13:59:56 -06007038static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7039 int index)
7040{
7041#if defined(CONFIG_UNIX)
7042 struct sock *sock = ctx->ring_sock->sk;
7043 struct sk_buff_head *head = &sock->sk_receive_queue;
7044 struct sk_buff *skb;
7045
7046 /*
7047 * See if we can merge this file into an existing skb SCM_RIGHTS
7048 * file set. If there's no room, fall back to allocating a new skb
7049 * and filling it in.
7050 */
7051 spin_lock_irq(&head->lock);
7052 skb = skb_peek(head);
7053 if (skb) {
7054 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7055
7056 if (fpl->count < SCM_MAX_FD) {
7057 __skb_unlink(skb, head);
7058 spin_unlock_irq(&head->lock);
7059 fpl->fp[fpl->count] = get_file(file);
7060 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7061 fpl->count++;
7062 spin_lock_irq(&head->lock);
7063 __skb_queue_head(head, skb);
7064 } else {
7065 skb = NULL;
7066 }
7067 }
7068 spin_unlock_irq(&head->lock);
7069
7070 if (skb) {
7071 fput(file);
7072 return 0;
7073 }
7074
7075 return __io_sqe_files_scm(ctx, 1, index);
7076#else
7077 return 0;
7078#endif
7079}
7080
Hillf Dantona5318d32020-03-23 17:47:15 +08007081static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007082 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007083{
Hillf Dantona5318d32020-03-23 17:47:15 +08007084 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007085 struct percpu_ref *refs = data->cur_refs;
7086 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007087
Jens Axboe05f3fb32019-12-09 11:22:50 -07007088 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007089 if (!pfile)
7090 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007091
Xiaoguang Wang05589552020-03-31 14:05:18 +08007092 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007093 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007094 list_add(&pfile->list, &ref_node->file_list);
7095
Hillf Dantona5318d32020-03-23 17:47:15 +08007096 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007097}
7098
7099static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7100 struct io_uring_files_update *up,
7101 unsigned nr_args)
7102{
7103 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007104 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007105 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007106 __s32 __user *fds;
7107 int fd, i, err;
7108 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007109 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007110
Jens Axboe05f3fb32019-12-09 11:22:50 -07007111 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007112 return -EOVERFLOW;
7113 if (done > ctx->nr_user_files)
7114 return -EINVAL;
7115
Xiaoguang Wang05589552020-03-31 14:05:18 +08007116 ref_node = alloc_fixed_file_ref_node(ctx);
7117 if (IS_ERR(ref_node))
7118 return PTR_ERR(ref_node);
7119
Jens Axboec3a31e62019-10-03 13:59:56 -06007120 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007121 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007122 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007123 struct fixed_file_table *table;
7124 unsigned index;
7125
Jens Axboec3a31e62019-10-03 13:59:56 -06007126 err = 0;
7127 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7128 err = -EFAULT;
7129 break;
7130 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007131 i = array_index_nospec(up->offset, ctx->nr_user_files);
7132 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007133 index = i & IORING_FILE_TABLE_MASK;
7134 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007135 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007136 err = io_queue_file_removal(data, file);
7137 if (err)
7138 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007139 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007140 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007141 }
7142 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007143 file = fget(fd);
7144 if (!file) {
7145 err = -EBADF;
7146 break;
7147 }
7148 /*
7149 * Don't allow io_uring instances to be registered. If
7150 * UNIX isn't enabled, then this causes a reference
7151 * cycle and this instance can never get freed. If UNIX
7152 * is enabled we'll handle it just fine, but there's
7153 * still no point in allowing a ring fd as it doesn't
7154 * support regular read/write anyway.
7155 */
7156 if (file->f_op == &io_uring_fops) {
7157 fput(file);
7158 err = -EBADF;
7159 break;
7160 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007161 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007162 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007163 if (err) {
7164 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007165 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007166 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007167 }
7168 nr_args--;
7169 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007170 up->offset++;
7171 }
7172
Xiaoguang Wang05589552020-03-31 14:05:18 +08007173 if (needs_switch) {
7174 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007175 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007176 list_add(&ref_node->node, &data->ref_list);
7177 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007178 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007179 percpu_ref_get(&ctx->file_data->refs);
7180 } else
7181 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007182
7183 return done ? done : err;
7184}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007185
Jens Axboe05f3fb32019-12-09 11:22:50 -07007186static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7187 unsigned nr_args)
7188{
7189 struct io_uring_files_update up;
7190
7191 if (!ctx->file_data)
7192 return -ENXIO;
7193 if (!nr_args)
7194 return -EINVAL;
7195 if (copy_from_user(&up, arg, sizeof(up)))
7196 return -EFAULT;
7197 if (up.resv)
7198 return -EINVAL;
7199
7200 return __io_sqe_files_update(ctx, &up, nr_args);
7201}
Jens Axboec3a31e62019-10-03 13:59:56 -06007202
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007203static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007204{
7205 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7206
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007207 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007208 io_put_req(req);
7209}
7210
Pavel Begunkov24369c22020-01-28 03:15:48 +03007211static int io_init_wq_offload(struct io_ring_ctx *ctx,
7212 struct io_uring_params *p)
7213{
7214 struct io_wq_data data;
7215 struct fd f;
7216 struct io_ring_ctx *ctx_attach;
7217 unsigned int concurrency;
7218 int ret = 0;
7219
7220 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007221 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007222 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007223
7224 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7225 /* Do QD, or 4 * CPUS, whatever is smallest */
7226 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7227
7228 ctx->io_wq = io_wq_create(concurrency, &data);
7229 if (IS_ERR(ctx->io_wq)) {
7230 ret = PTR_ERR(ctx->io_wq);
7231 ctx->io_wq = NULL;
7232 }
7233 return ret;
7234 }
7235
7236 f = fdget(p->wq_fd);
7237 if (!f.file)
7238 return -EBADF;
7239
7240 if (f.file->f_op != &io_uring_fops) {
7241 ret = -EINVAL;
7242 goto out_fput;
7243 }
7244
7245 ctx_attach = f.file->private_data;
7246 /* @io_wq is protected by holding the fd */
7247 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7248 ret = -EINVAL;
7249 goto out_fput;
7250 }
7251
7252 ctx->io_wq = ctx_attach->io_wq;
7253out_fput:
7254 fdput(f);
7255 return ret;
7256}
7257
Jens Axboe6c271ce2019-01-10 11:22:30 -07007258static int io_sq_offload_start(struct io_ring_ctx *ctx,
7259 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007260{
7261 int ret;
7262
Jens Axboe6c271ce2019-01-10 11:22:30 -07007263 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov8eb06d72020-06-30 15:20:39 +03007264 mmgrab(current->mm);
7265 ctx->sqo_mm = current->mm;
7266
Jens Axboe3ec482d2019-04-08 10:51:01 -06007267 ret = -EPERM;
7268 if (!capable(CAP_SYS_ADMIN))
7269 goto err;
7270
Jens Axboe917257d2019-04-13 09:28:55 -06007271 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7272 if (!ctx->sq_thread_idle)
7273 ctx->sq_thread_idle = HZ;
7274
Jens Axboe6c271ce2019-01-10 11:22:30 -07007275 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007276 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007277
Jens Axboe917257d2019-04-13 09:28:55 -06007278 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007279 if (cpu >= nr_cpu_ids)
7280 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007281 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007282 goto err;
7283
Jens Axboe6c271ce2019-01-10 11:22:30 -07007284 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7285 ctx, cpu,
7286 "io_uring-sq");
7287 } else {
7288 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7289 "io_uring-sq");
7290 }
7291 if (IS_ERR(ctx->sqo_thread)) {
7292 ret = PTR_ERR(ctx->sqo_thread);
7293 ctx->sqo_thread = NULL;
7294 goto err;
7295 }
7296 wake_up_process(ctx->sqo_thread);
7297 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7298 /* Can't have SQ_AFF without SQPOLL */
7299 ret = -EINVAL;
7300 goto err;
7301 }
7302
Pavel Begunkov24369c22020-01-28 03:15:48 +03007303 ret = io_init_wq_offload(ctx, p);
7304 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007305 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007306
7307 return 0;
7308err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007309 io_finish_async(ctx);
Pavel Begunkov8eb06d72020-06-30 15:20:39 +03007310 if (ctx->sqo_mm) {
7311 mmdrop(ctx->sqo_mm);
7312 ctx->sqo_mm = NULL;
7313 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007314 return ret;
7315}
7316
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007317static inline void __io_unaccount_mem(struct user_struct *user,
7318 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007319{
7320 atomic_long_sub(nr_pages, &user->locked_vm);
7321}
7322
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007323static inline int __io_account_mem(struct user_struct *user,
7324 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007325{
7326 unsigned long page_limit, cur_pages, new_pages;
7327
7328 /* Don't allow more pages than we can safely lock */
7329 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7330
7331 do {
7332 cur_pages = atomic_long_read(&user->locked_vm);
7333 new_pages = cur_pages + nr_pages;
7334 if (new_pages > page_limit)
7335 return -ENOMEM;
7336 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7337 new_pages) != cur_pages);
7338
7339 return 0;
7340}
7341
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007342static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7343 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007344{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007345 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007346 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007347
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007348 if (ctx->sqo_mm) {
7349 if (acct == ACCT_LOCKED)
7350 ctx->sqo_mm->locked_vm -= nr_pages;
7351 else if (acct == ACCT_PINNED)
7352 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7353 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007354}
7355
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007356static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7357 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007358{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007359 int ret;
7360
7361 if (ctx->limit_mem) {
7362 ret = __io_account_mem(ctx->user, nr_pages);
7363 if (ret)
7364 return ret;
7365 }
7366
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007367 if (ctx->sqo_mm) {
7368 if (acct == ACCT_LOCKED)
7369 ctx->sqo_mm->locked_vm += nr_pages;
7370 else if (acct == ACCT_PINNED)
7371 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7372 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007373
7374 return 0;
7375}
7376
Jens Axboe2b188cc2019-01-07 10:46:33 -07007377static void io_mem_free(void *ptr)
7378{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007379 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007380
Mark Rutland52e04ef2019-04-30 17:30:21 +01007381 if (!ptr)
7382 return;
7383
7384 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007385 if (put_page_testzero(page))
7386 free_compound_page(page);
7387}
7388
7389static void *io_mem_alloc(size_t size)
7390{
7391 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7392 __GFP_NORETRY;
7393
7394 return (void *) __get_free_pages(gfp_flags, get_order(size));
7395}
7396
Hristo Venev75b28af2019-08-26 17:23:46 +00007397static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7398 size_t *sq_offset)
7399{
7400 struct io_rings *rings;
7401 size_t off, sq_array_size;
7402
7403 off = struct_size(rings, cqes, cq_entries);
7404 if (off == SIZE_MAX)
7405 return SIZE_MAX;
7406
7407#ifdef CONFIG_SMP
7408 off = ALIGN(off, SMP_CACHE_BYTES);
7409 if (off == 0)
7410 return SIZE_MAX;
7411#endif
7412
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007413 if (sq_offset)
7414 *sq_offset = off;
7415
Hristo Venev75b28af2019-08-26 17:23:46 +00007416 sq_array_size = array_size(sizeof(u32), sq_entries);
7417 if (sq_array_size == SIZE_MAX)
7418 return SIZE_MAX;
7419
7420 if (check_add_overflow(off, sq_array_size, &off))
7421 return SIZE_MAX;
7422
Hristo Venev75b28af2019-08-26 17:23:46 +00007423 return off;
7424}
7425
Jens Axboe2b188cc2019-01-07 10:46:33 -07007426static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7427{
Hristo Venev75b28af2019-08-26 17:23:46 +00007428 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007429
Hristo Venev75b28af2019-08-26 17:23:46 +00007430 pages = (size_t)1 << get_order(
7431 rings_size(sq_entries, cq_entries, NULL));
7432 pages += (size_t)1 << get_order(
7433 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007434
Hristo Venev75b28af2019-08-26 17:23:46 +00007435 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007436}
7437
Jens Axboeedafcce2019-01-09 09:16:05 -07007438static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7439{
7440 int i, j;
7441
7442 if (!ctx->user_bufs)
7443 return -ENXIO;
7444
7445 for (i = 0; i < ctx->nr_user_bufs; i++) {
7446 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7447
7448 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007449 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007450
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007451 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007452 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007453 imu->nr_bvecs = 0;
7454 }
7455
7456 kfree(ctx->user_bufs);
7457 ctx->user_bufs = NULL;
7458 ctx->nr_user_bufs = 0;
7459 return 0;
7460}
7461
7462static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7463 void __user *arg, unsigned index)
7464{
7465 struct iovec __user *src;
7466
7467#ifdef CONFIG_COMPAT
7468 if (ctx->compat) {
7469 struct compat_iovec __user *ciovs;
7470 struct compat_iovec ciov;
7471
7472 ciovs = (struct compat_iovec __user *) arg;
7473 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7474 return -EFAULT;
7475
Jens Axboed55e5f52019-12-11 16:12:15 -07007476 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007477 dst->iov_len = ciov.iov_len;
7478 return 0;
7479 }
7480#endif
7481 src = (struct iovec __user *) arg;
7482 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7483 return -EFAULT;
7484 return 0;
7485}
7486
7487static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7488 unsigned nr_args)
7489{
7490 struct vm_area_struct **vmas = NULL;
7491 struct page **pages = NULL;
7492 int i, j, got_pages = 0;
7493 int ret = -EINVAL;
7494
7495 if (ctx->user_bufs)
7496 return -EBUSY;
7497 if (!nr_args || nr_args > UIO_MAXIOV)
7498 return -EINVAL;
7499
7500 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7501 GFP_KERNEL);
7502 if (!ctx->user_bufs)
7503 return -ENOMEM;
7504
7505 for (i = 0; i < nr_args; i++) {
7506 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7507 unsigned long off, start, end, ubuf;
7508 int pret, nr_pages;
7509 struct iovec iov;
7510 size_t size;
7511
7512 ret = io_copy_iov(ctx, &iov, arg, i);
7513 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007514 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007515
7516 /*
7517 * Don't impose further limits on the size and buffer
7518 * constraints here, we'll -EINVAL later when IO is
7519 * submitted if they are wrong.
7520 */
7521 ret = -EFAULT;
7522 if (!iov.iov_base || !iov.iov_len)
7523 goto err;
7524
7525 /* arbitrary limit, but we need something */
7526 if (iov.iov_len > SZ_1G)
7527 goto err;
7528
7529 ubuf = (unsigned long) iov.iov_base;
7530 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7531 start = ubuf >> PAGE_SHIFT;
7532 nr_pages = end - start;
7533
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007534 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007535 if (ret)
7536 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007537
7538 ret = 0;
7539 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007540 kvfree(vmas);
7541 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007542 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007543 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007544 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007545 sizeof(struct vm_area_struct *),
7546 GFP_KERNEL);
7547 if (!pages || !vmas) {
7548 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007549 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007550 goto err;
7551 }
7552 got_pages = nr_pages;
7553 }
7554
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007555 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007556 GFP_KERNEL);
7557 ret = -ENOMEM;
7558 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007559 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007560 goto err;
7561 }
7562
7563 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007564 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007565 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007566 FOLL_WRITE | FOLL_LONGTERM,
7567 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007568 if (pret == nr_pages) {
7569 /* don't support file backed memory */
7570 for (j = 0; j < nr_pages; j++) {
7571 struct vm_area_struct *vma = vmas[j];
7572
7573 if (vma->vm_file &&
7574 !is_file_hugepages(vma->vm_file)) {
7575 ret = -EOPNOTSUPP;
7576 break;
7577 }
7578 }
7579 } else {
7580 ret = pret < 0 ? pret : -EFAULT;
7581 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007582 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007583 if (ret) {
7584 /*
7585 * if we did partial map, or found file backed vmas,
7586 * release any pages we did get
7587 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007588 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007589 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007590 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007591 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007592 goto err;
7593 }
7594
7595 off = ubuf & ~PAGE_MASK;
7596 size = iov.iov_len;
7597 for (j = 0; j < nr_pages; j++) {
7598 size_t vec_len;
7599
7600 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7601 imu->bvec[j].bv_page = pages[j];
7602 imu->bvec[j].bv_len = vec_len;
7603 imu->bvec[j].bv_offset = off;
7604 off = 0;
7605 size -= vec_len;
7606 }
7607 /* store original address for later verification */
7608 imu->ubuf = ubuf;
7609 imu->len = iov.iov_len;
7610 imu->nr_bvecs = nr_pages;
7611
7612 ctx->nr_user_bufs++;
7613 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007614 kvfree(pages);
7615 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007616 return 0;
7617err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007618 kvfree(pages);
7619 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007620 io_sqe_buffer_unregister(ctx);
7621 return ret;
7622}
7623
Jens Axboe9b402842019-04-11 11:45:41 -06007624static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7625{
7626 __s32 __user *fds = arg;
7627 int fd;
7628
7629 if (ctx->cq_ev_fd)
7630 return -EBUSY;
7631
7632 if (copy_from_user(&fd, fds, sizeof(*fds)))
7633 return -EFAULT;
7634
7635 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7636 if (IS_ERR(ctx->cq_ev_fd)) {
7637 int ret = PTR_ERR(ctx->cq_ev_fd);
7638 ctx->cq_ev_fd = NULL;
7639 return ret;
7640 }
7641
7642 return 0;
7643}
7644
7645static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7646{
7647 if (ctx->cq_ev_fd) {
7648 eventfd_ctx_put(ctx->cq_ev_fd);
7649 ctx->cq_ev_fd = NULL;
7650 return 0;
7651 }
7652
7653 return -ENXIO;
7654}
7655
Jens Axboe5a2e7452020-02-23 16:23:11 -07007656static int __io_destroy_buffers(int id, void *p, void *data)
7657{
7658 struct io_ring_ctx *ctx = data;
7659 struct io_buffer *buf = p;
7660
Jens Axboe067524e2020-03-02 16:32:28 -07007661 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007662 return 0;
7663}
7664
7665static void io_destroy_buffers(struct io_ring_ctx *ctx)
7666{
7667 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7668 idr_destroy(&ctx->io_buffer_idr);
7669}
7670
Jens Axboe2b188cc2019-01-07 10:46:33 -07007671static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7672{
Jens Axboe6b063142019-01-10 22:13:58 -07007673 io_finish_async(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007674 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007675 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007676 ctx->sqo_mm = NULL;
7677 }
Jens Axboedef596e2019-01-09 08:59:42 -07007678
Jens Axboeedafcce2019-01-09 09:16:05 -07007679 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007680 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007681 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007682 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007683 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007684
Jens Axboe2b188cc2019-01-07 10:46:33 -07007685#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007686 if (ctx->ring_sock) {
7687 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007688 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007689 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007690#endif
7691
Hristo Venev75b28af2019-08-26 17:23:46 +00007692 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007693 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007694
7695 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007696 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007697 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007698 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007699 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007700 kfree(ctx);
7701}
7702
7703static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7704{
7705 struct io_ring_ctx *ctx = file->private_data;
7706 __poll_t mask = 0;
7707
7708 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007709 /*
7710 * synchronizes with barrier from wq_has_sleeper call in
7711 * io_commit_cqring
7712 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007713 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007714 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7715 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007716 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007717 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007718 mask |= EPOLLIN | EPOLLRDNORM;
7719
7720 return mask;
7721}
7722
7723static int io_uring_fasync(int fd, struct file *file, int on)
7724{
7725 struct io_ring_ctx *ctx = file->private_data;
7726
7727 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7728}
7729
Jens Axboe071698e2020-01-28 10:04:42 -07007730static int io_remove_personalities(int id, void *p, void *data)
7731{
7732 struct io_ring_ctx *ctx = data;
7733 const struct cred *cred;
7734
7735 cred = idr_remove(&ctx->personality_idr, id);
7736 if (cred)
7737 put_cred(cred);
7738 return 0;
7739}
7740
Jens Axboe85faa7b2020-04-09 18:14:00 -06007741static void io_ring_exit_work(struct work_struct *work)
7742{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007743 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
7744 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007745
Jens Axboe56952e92020-06-17 15:00:04 -06007746 /*
7747 * If we're doing polled IO and end up having requests being
7748 * submitted async (out-of-line), then completions can come in while
7749 * we're waiting for refs to drop. We need to reap these manually,
7750 * as nobody else will be looking for them.
7751 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007752 do {
Jens Axboe56952e92020-06-17 15:00:04 -06007753 if (ctx->rings)
7754 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007755 io_iopoll_try_reap_events(ctx);
7756 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06007757 io_ring_ctx_free(ctx);
7758}
7759
Jens Axboe2b188cc2019-01-07 10:46:33 -07007760static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7761{
7762 mutex_lock(&ctx->uring_lock);
7763 percpu_ref_kill(&ctx->refs);
7764 mutex_unlock(&ctx->uring_lock);
7765
Jens Axboe5262f562019-09-17 12:26:57 -06007766 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007767 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007768
7769 if (ctx->io_wq)
7770 io_wq_cancel_all(ctx->io_wq);
7771
Jens Axboe15dff282019-11-13 09:09:23 -07007772 /* if we failed setting up the ctx, we might not have any rings */
7773 if (ctx->rings)
7774 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007775 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07007776 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06007777
7778 /*
7779 * Do this upfront, so we won't have a grace period where the ring
7780 * is closed but resources aren't reaped yet. This can cause
7781 * spurious failure in setting up a new ring.
7782 */
Jens Axboe760618f2020-07-24 12:53:31 -06007783 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7784 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06007785
Jens Axboe85faa7b2020-04-09 18:14:00 -06007786 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7787 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007788}
7789
7790static int io_uring_release(struct inode *inode, struct file *file)
7791{
7792 struct io_ring_ctx *ctx = file->private_data;
7793
7794 file->private_data = NULL;
7795 io_ring_ctx_wait_and_kill(ctx);
7796 return 0;
7797}
7798
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007799static bool io_wq_files_match(struct io_wq_work *work, void *data)
7800{
7801 struct files_struct *files = data;
7802
7803 return work->files == files;
7804}
7805
Jens Axboefcb323c2019-10-24 12:39:47 -06007806static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7807 struct files_struct *files)
7808{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007809 if (list_empty_careful(&ctx->inflight_list))
7810 return;
7811
7812 /* cancel all at once, should be faster than doing it one by one*/
7813 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7814
Jens Axboefcb323c2019-10-24 12:39:47 -06007815 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007816 struct io_kiocb *cancel_req = NULL, *req;
7817 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007818
7819 spin_lock_irq(&ctx->inflight_lock);
7820 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007821 if (req->work.files != files)
7822 continue;
7823 /* req is being completed, ignore */
7824 if (!refcount_inc_not_zero(&req->refs))
7825 continue;
7826 cancel_req = req;
7827 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007828 }
Jens Axboe768134d2019-11-10 20:30:53 -07007829 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007830 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007831 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007832 spin_unlock_irq(&ctx->inflight_lock);
7833
Jens Axboe768134d2019-11-10 20:30:53 -07007834 /* We need to keep going until we don't find a matching req */
7835 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007836 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007837
Jens Axboe2ca10252020-02-13 17:17:35 -07007838 if (cancel_req->flags & REQ_F_OVERFLOW) {
7839 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03007840 list_del(&cancel_req->compl.list);
Jens Axboe2ca10252020-02-13 17:17:35 -07007841 cancel_req->flags &= ~REQ_F_OVERFLOW;
7842 if (list_empty(&ctx->cq_overflow_list)) {
7843 clear_bit(0, &ctx->sq_check_overflow);
7844 clear_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08007845 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
Jens Axboe2ca10252020-02-13 17:17:35 -07007846 }
7847 spin_unlock_irq(&ctx->completion_lock);
7848
7849 WRITE_ONCE(ctx->rings->cq_overflow,
7850 atomic_inc_return(&ctx->cached_cq_overflow));
7851
7852 /*
7853 * Put inflight ref and overflow ref. If that's
7854 * all we had, then we're done with this request.
7855 */
7856 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007857 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007858 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007859 continue;
7860 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007861 } else {
7862 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7863 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007864 }
7865
Jens Axboefcb323c2019-10-24 12:39:47 -06007866 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007867 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007868 }
7869}
7870
Pavel Begunkov801dd572020-06-15 10:33:14 +03007871static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007872{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007873 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7874 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007875
Pavel Begunkov801dd572020-06-15 10:33:14 +03007876 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007877}
7878
Jens Axboefcb323c2019-10-24 12:39:47 -06007879static int io_uring_flush(struct file *file, void *data)
7880{
7881 struct io_ring_ctx *ctx = file->private_data;
7882
7883 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007884
7885 /*
7886 * If the task is going away, cancel work it may have pending
7887 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007888 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7889 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007890
Jens Axboefcb323c2019-10-24 12:39:47 -06007891 return 0;
7892}
7893
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007894static void *io_uring_validate_mmap_request(struct file *file,
7895 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007896{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007897 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007898 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007899 struct page *page;
7900 void *ptr;
7901
7902 switch (offset) {
7903 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007904 case IORING_OFF_CQ_RING:
7905 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007906 break;
7907 case IORING_OFF_SQES:
7908 ptr = ctx->sq_sqes;
7909 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007910 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007911 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007912 }
7913
7914 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007915 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007916 return ERR_PTR(-EINVAL);
7917
7918 return ptr;
7919}
7920
7921#ifdef CONFIG_MMU
7922
7923static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7924{
7925 size_t sz = vma->vm_end - vma->vm_start;
7926 unsigned long pfn;
7927 void *ptr;
7928
7929 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7930 if (IS_ERR(ptr))
7931 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007932
7933 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7934 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7935}
7936
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007937#else /* !CONFIG_MMU */
7938
7939static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7940{
7941 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7942}
7943
7944static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7945{
7946 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7947}
7948
7949static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7950 unsigned long addr, unsigned long len,
7951 unsigned long pgoff, unsigned long flags)
7952{
7953 void *ptr;
7954
7955 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7956 if (IS_ERR(ptr))
7957 return PTR_ERR(ptr);
7958
7959 return (unsigned long) ptr;
7960}
7961
7962#endif /* !CONFIG_MMU */
7963
Jens Axboe2b188cc2019-01-07 10:46:33 -07007964SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7965 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7966 size_t, sigsz)
7967{
7968 struct io_ring_ctx *ctx;
7969 long ret = -EBADF;
7970 int submitted = 0;
7971 struct fd f;
7972
Jens Axboe4c6e2772020-07-01 11:29:10 -06007973 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07007974
Jens Axboe6c271ce2019-01-10 11:22:30 -07007975 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007976 return -EINVAL;
7977
7978 f = fdget(fd);
7979 if (!f.file)
7980 return -EBADF;
7981
7982 ret = -EOPNOTSUPP;
7983 if (f.file->f_op != &io_uring_fops)
7984 goto out_fput;
7985
7986 ret = -ENXIO;
7987 ctx = f.file->private_data;
7988 if (!percpu_ref_tryget(&ctx->refs))
7989 goto out_fput;
7990
Jens Axboe6c271ce2019-01-10 11:22:30 -07007991 /*
7992 * For SQ polling, the thread will do all submissions and completions.
7993 * Just return the requested submit count, and wake the thread if
7994 * we were asked to.
7995 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007996 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007997 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007998 if (!list_empty_careful(&ctx->cq_overflow_list))
7999 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008000 if (flags & IORING_ENTER_SQ_WAKEUP)
8001 wake_up(&ctx->sqo_wait);
8002 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008003 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07008004 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03008005 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008006 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008007
8008 if (submitted != to_submit)
8009 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008010 }
8011 if (flags & IORING_ENTER_GETEVENTS) {
8012 min_complete = min(min_complete, ctx->cq_entries);
8013
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008014 /*
8015 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8016 * space applications don't need to do io completion events
8017 * polling again, they can rely on io_sq_thread to do polling
8018 * work, which can reduce cpu usage and uring_lock contention.
8019 */
8020 if (ctx->flags & IORING_SETUP_IOPOLL &&
8021 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008022 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008023 } else {
8024 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8025 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008026 }
8027
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008028out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008029 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008030out_fput:
8031 fdput(f);
8032 return submitted ? submitted : ret;
8033}
8034
Tobias Klauserbebdb652020-02-26 18:38:32 +01008035#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008036static int io_uring_show_cred(int id, void *p, void *data)
8037{
8038 const struct cred *cred = p;
8039 struct seq_file *m = data;
8040 struct user_namespace *uns = seq_user_ns(m);
8041 struct group_info *gi;
8042 kernel_cap_t cap;
8043 unsigned __capi;
8044 int g;
8045
8046 seq_printf(m, "%5d\n", id);
8047 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8048 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8049 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8050 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8051 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8052 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8053 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8054 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8055 seq_puts(m, "\n\tGroups:\t");
8056 gi = cred->group_info;
8057 for (g = 0; g < gi->ngroups; g++) {
8058 seq_put_decimal_ull(m, g ? " " : "",
8059 from_kgid_munged(uns, gi->gid[g]));
8060 }
8061 seq_puts(m, "\n\tCapEff:\t");
8062 cap = cred->cap_effective;
8063 CAP_FOR_EACH_U32(__capi)
8064 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8065 seq_putc(m, '\n');
8066 return 0;
8067}
8068
8069static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8070{
8071 int i;
8072
8073 mutex_lock(&ctx->uring_lock);
8074 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
8075 for (i = 0; i < ctx->nr_user_files; i++) {
8076 struct fixed_file_table *table;
8077 struct file *f;
8078
8079 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8080 f = table->files[i & IORING_FILE_TABLE_MASK];
8081 if (f)
8082 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8083 else
8084 seq_printf(m, "%5u: <none>\n", i);
8085 }
8086 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
8087 for (i = 0; i < ctx->nr_user_bufs; i++) {
8088 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8089
8090 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8091 (unsigned int) buf->len);
8092 }
8093 if (!idr_is_empty(&ctx->personality_idr)) {
8094 seq_printf(m, "Personalities:\n");
8095 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8096 }
Jens Axboed7718a92020-02-14 22:23:12 -07008097 seq_printf(m, "PollList:\n");
8098 spin_lock_irq(&ctx->completion_lock);
8099 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8100 struct hlist_head *list = &ctx->cancel_hash[i];
8101 struct io_kiocb *req;
8102
8103 hlist_for_each_entry(req, list, hash_node)
8104 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8105 req->task->task_works != NULL);
8106 }
8107 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008108 mutex_unlock(&ctx->uring_lock);
8109}
8110
8111static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8112{
8113 struct io_ring_ctx *ctx = f->private_data;
8114
8115 if (percpu_ref_tryget(&ctx->refs)) {
8116 __io_uring_show_fdinfo(ctx, m);
8117 percpu_ref_put(&ctx->refs);
8118 }
8119}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008120#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008121
Jens Axboe2b188cc2019-01-07 10:46:33 -07008122static const struct file_operations io_uring_fops = {
8123 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008124 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008125 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008126#ifndef CONFIG_MMU
8127 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8128 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8129#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008130 .poll = io_uring_poll,
8131 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008132#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008133 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008134#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008135};
8136
8137static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8138 struct io_uring_params *p)
8139{
Hristo Venev75b28af2019-08-26 17:23:46 +00008140 struct io_rings *rings;
8141 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008142
Hristo Venev75b28af2019-08-26 17:23:46 +00008143 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8144 if (size == SIZE_MAX)
8145 return -EOVERFLOW;
8146
8147 rings = io_mem_alloc(size);
8148 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008149 return -ENOMEM;
8150
Hristo Venev75b28af2019-08-26 17:23:46 +00008151 ctx->rings = rings;
8152 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8153 rings->sq_ring_mask = p->sq_entries - 1;
8154 rings->cq_ring_mask = p->cq_entries - 1;
8155 rings->sq_ring_entries = p->sq_entries;
8156 rings->cq_ring_entries = p->cq_entries;
8157 ctx->sq_mask = rings->sq_ring_mask;
8158 ctx->cq_mask = rings->cq_ring_mask;
8159 ctx->sq_entries = rings->sq_ring_entries;
8160 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008161
8162 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008163 if (size == SIZE_MAX) {
8164 io_mem_free(ctx->rings);
8165 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008166 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008167 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008168
8169 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008170 if (!ctx->sq_sqes) {
8171 io_mem_free(ctx->rings);
8172 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008173 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008174 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008175
Jens Axboe2b188cc2019-01-07 10:46:33 -07008176 return 0;
8177}
8178
8179/*
8180 * Allocate an anonymous fd, this is what constitutes the application
8181 * visible backing of an io_uring instance. The application mmaps this
8182 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8183 * we have to tie this fd to a socket for file garbage collection purposes.
8184 */
8185static int io_uring_get_fd(struct io_ring_ctx *ctx)
8186{
8187 struct file *file;
8188 int ret;
8189
8190#if defined(CONFIG_UNIX)
8191 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8192 &ctx->ring_sock);
8193 if (ret)
8194 return ret;
8195#endif
8196
8197 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8198 if (ret < 0)
8199 goto err;
8200
8201 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8202 O_RDWR | O_CLOEXEC);
8203 if (IS_ERR(file)) {
8204 put_unused_fd(ret);
8205 ret = PTR_ERR(file);
8206 goto err;
8207 }
8208
8209#if defined(CONFIG_UNIX)
8210 ctx->ring_sock->file = file;
8211#endif
8212 fd_install(ret, file);
8213 return ret;
8214err:
8215#if defined(CONFIG_UNIX)
8216 sock_release(ctx->ring_sock);
8217 ctx->ring_sock = NULL;
8218#endif
8219 return ret;
8220}
8221
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008222static int io_uring_create(unsigned entries, struct io_uring_params *p,
8223 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008224{
8225 struct user_struct *user = NULL;
8226 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008227 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008228 int ret;
8229
Jens Axboe8110c1a2019-12-28 15:39:54 -07008230 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008231 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008232 if (entries > IORING_MAX_ENTRIES) {
8233 if (!(p->flags & IORING_SETUP_CLAMP))
8234 return -EINVAL;
8235 entries = IORING_MAX_ENTRIES;
8236 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008237
8238 /*
8239 * Use twice as many entries for the CQ ring. It's possible for the
8240 * application to drive a higher depth than the size of the SQ ring,
8241 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008242 * some flexibility in overcommitting a bit. If the application has
8243 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8244 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008245 */
8246 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008247 if (p->flags & IORING_SETUP_CQSIZE) {
8248 /*
8249 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8250 * to a power-of-two, if it isn't already. We do NOT impose
8251 * any cq vs sq ring sizing.
8252 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008253 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008254 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008255 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8256 if (!(p->flags & IORING_SETUP_CLAMP))
8257 return -EINVAL;
8258 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8259 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008260 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8261 } else {
8262 p->cq_entries = 2 * p->sq_entries;
8263 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008264
8265 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008266 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008267
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008268 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008269 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008270 ring_pages(p->sq_entries, p->cq_entries));
8271 if (ret) {
8272 free_uid(user);
8273 return ret;
8274 }
8275 }
8276
8277 ctx = io_ring_ctx_alloc(p);
8278 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008279 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008280 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008281 p->cq_entries));
8282 free_uid(user);
8283 return -ENOMEM;
8284 }
8285 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008286 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008287 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008288
8289 ret = io_allocate_scq_urings(ctx, p);
8290 if (ret)
8291 goto err;
8292
Jens Axboe6c271ce2019-01-10 11:22:30 -07008293 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008294 if (ret)
8295 goto err;
8296
Jens Axboe2b188cc2019-01-07 10:46:33 -07008297 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008298 p->sq_off.head = offsetof(struct io_rings, sq.head);
8299 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8300 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8301 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8302 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8303 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8304 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008305
8306 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008307 p->cq_off.head = offsetof(struct io_rings, cq.head);
8308 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8309 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8310 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8311 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8312 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008313 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008314
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008315 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8316 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008317 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8318 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008319
8320 if (copy_to_user(params, p, sizeof(*p))) {
8321 ret = -EFAULT;
8322 goto err;
8323 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008324 /*
8325 * Install ring fd as the very last thing, so we don't risk someone
8326 * having closed it before we finish setup
8327 */
8328 ret = io_uring_get_fd(ctx);
8329 if (ret < 0)
8330 goto err;
8331
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008332 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008333 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8334 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008335 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008336 return ret;
8337err:
8338 io_ring_ctx_wait_and_kill(ctx);
8339 return ret;
8340}
8341
8342/*
8343 * Sets up an aio uring context, and returns the fd. Applications asks for a
8344 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8345 * params structure passed in.
8346 */
8347static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8348{
8349 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008350 int i;
8351
8352 if (copy_from_user(&p, params, sizeof(p)))
8353 return -EFAULT;
8354 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8355 if (p.resv[i])
8356 return -EINVAL;
8357 }
8358
Jens Axboe6c271ce2019-01-10 11:22:30 -07008359 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008360 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008361 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008362 return -EINVAL;
8363
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008364 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008365}
8366
8367SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8368 struct io_uring_params __user *, params)
8369{
8370 return io_uring_setup(entries, params);
8371}
8372
Jens Axboe66f4af92020-01-16 15:36:52 -07008373static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8374{
8375 struct io_uring_probe *p;
8376 size_t size;
8377 int i, ret;
8378
8379 size = struct_size(p, ops, nr_args);
8380 if (size == SIZE_MAX)
8381 return -EOVERFLOW;
8382 p = kzalloc(size, GFP_KERNEL);
8383 if (!p)
8384 return -ENOMEM;
8385
8386 ret = -EFAULT;
8387 if (copy_from_user(p, arg, size))
8388 goto out;
8389 ret = -EINVAL;
8390 if (memchr_inv(p, 0, size))
8391 goto out;
8392
8393 p->last_op = IORING_OP_LAST - 1;
8394 if (nr_args > IORING_OP_LAST)
8395 nr_args = IORING_OP_LAST;
8396
8397 for (i = 0; i < nr_args; i++) {
8398 p->ops[i].op = i;
8399 if (!io_op_defs[i].not_supported)
8400 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8401 }
8402 p->ops_len = i;
8403
8404 ret = 0;
8405 if (copy_to_user(arg, p, size))
8406 ret = -EFAULT;
8407out:
8408 kfree(p);
8409 return ret;
8410}
8411
Jens Axboe071698e2020-01-28 10:04:42 -07008412static int io_register_personality(struct io_ring_ctx *ctx)
8413{
8414 const struct cred *creds = get_current_cred();
8415 int id;
8416
8417 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8418 USHRT_MAX, GFP_KERNEL);
8419 if (id < 0)
8420 put_cred(creds);
8421 return id;
8422}
8423
8424static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8425{
8426 const struct cred *old_creds;
8427
8428 old_creds = idr_remove(&ctx->personality_idr, id);
8429 if (old_creds) {
8430 put_cred(old_creds);
8431 return 0;
8432 }
8433
8434 return -EINVAL;
8435}
8436
8437static bool io_register_op_must_quiesce(int op)
8438{
8439 switch (op) {
8440 case IORING_UNREGISTER_FILES:
8441 case IORING_REGISTER_FILES_UPDATE:
8442 case IORING_REGISTER_PROBE:
8443 case IORING_REGISTER_PERSONALITY:
8444 case IORING_UNREGISTER_PERSONALITY:
8445 return false;
8446 default:
8447 return true;
8448 }
8449}
8450
Jens Axboeedafcce2019-01-09 09:16:05 -07008451static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8452 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008453 __releases(ctx->uring_lock)
8454 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008455{
8456 int ret;
8457
Jens Axboe35fa71a2019-04-22 10:23:23 -06008458 /*
8459 * We're inside the ring mutex, if the ref is already dying, then
8460 * someone else killed the ctx or is already going through
8461 * io_uring_register().
8462 */
8463 if (percpu_ref_is_dying(&ctx->refs))
8464 return -ENXIO;
8465
Jens Axboe071698e2020-01-28 10:04:42 -07008466 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008467 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008468
Jens Axboe05f3fb32019-12-09 11:22:50 -07008469 /*
8470 * Drop uring mutex before waiting for references to exit. If
8471 * another thread is currently inside io_uring_enter() it might
8472 * need to grab the uring_lock to make progress. If we hold it
8473 * here across the drain wait, then we can deadlock. It's safe
8474 * to drop the mutex here, since no new references will come in
8475 * after we've killed the percpu ref.
8476 */
8477 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008478 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008479 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008480 if (ret) {
8481 percpu_ref_resurrect(&ctx->refs);
8482 ret = -EINTR;
8483 goto out;
8484 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008485 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008486
8487 switch (opcode) {
8488 case IORING_REGISTER_BUFFERS:
8489 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8490 break;
8491 case IORING_UNREGISTER_BUFFERS:
8492 ret = -EINVAL;
8493 if (arg || nr_args)
8494 break;
8495 ret = io_sqe_buffer_unregister(ctx);
8496 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008497 case IORING_REGISTER_FILES:
8498 ret = io_sqe_files_register(ctx, arg, nr_args);
8499 break;
8500 case IORING_UNREGISTER_FILES:
8501 ret = -EINVAL;
8502 if (arg || nr_args)
8503 break;
8504 ret = io_sqe_files_unregister(ctx);
8505 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008506 case IORING_REGISTER_FILES_UPDATE:
8507 ret = io_sqe_files_update(ctx, arg, nr_args);
8508 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008509 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008510 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008511 ret = -EINVAL;
8512 if (nr_args != 1)
8513 break;
8514 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008515 if (ret)
8516 break;
8517 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8518 ctx->eventfd_async = 1;
8519 else
8520 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008521 break;
8522 case IORING_UNREGISTER_EVENTFD:
8523 ret = -EINVAL;
8524 if (arg || nr_args)
8525 break;
8526 ret = io_eventfd_unregister(ctx);
8527 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008528 case IORING_REGISTER_PROBE:
8529 ret = -EINVAL;
8530 if (!arg || nr_args > 256)
8531 break;
8532 ret = io_probe(ctx, arg, nr_args);
8533 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008534 case IORING_REGISTER_PERSONALITY:
8535 ret = -EINVAL;
8536 if (arg || nr_args)
8537 break;
8538 ret = io_register_personality(ctx);
8539 break;
8540 case IORING_UNREGISTER_PERSONALITY:
8541 ret = -EINVAL;
8542 if (arg)
8543 break;
8544 ret = io_unregister_personality(ctx, nr_args);
8545 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008546 default:
8547 ret = -EINVAL;
8548 break;
8549 }
8550
Jens Axboe071698e2020-01-28 10:04:42 -07008551 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008552 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008553 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008554out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008555 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008556 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008557 return ret;
8558}
8559
8560SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8561 void __user *, arg, unsigned int, nr_args)
8562{
8563 struct io_ring_ctx *ctx;
8564 long ret = -EBADF;
8565 struct fd f;
8566
8567 f = fdget(fd);
8568 if (!f.file)
8569 return -EBADF;
8570
8571 ret = -EOPNOTSUPP;
8572 if (f.file->f_op != &io_uring_fops)
8573 goto out_fput;
8574
8575 ctx = f.file->private_data;
8576
8577 mutex_lock(&ctx->uring_lock);
8578 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8579 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008580 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8581 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008582out_fput:
8583 fdput(f);
8584 return ret;
8585}
8586
Jens Axboe2b188cc2019-01-07 10:46:33 -07008587static int __init io_uring_init(void)
8588{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008589#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8590 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8591 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8592} while (0)
8593
8594#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8595 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8596 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8597 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8598 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8599 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8600 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8601 BUILD_BUG_SQE_ELEM(8, __u64, off);
8602 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8603 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008604 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008605 BUILD_BUG_SQE_ELEM(24, __u32, len);
8606 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8607 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8608 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8609 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008610 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8611 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008612 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8613 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8614 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8615 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8616 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8617 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8618 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8619 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008620 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008621 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8622 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8623 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008624 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008625
Jens Axboed3656342019-12-18 09:50:26 -07008626 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008627 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008628 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8629 return 0;
8630};
8631__initcall(io_uring_init);