blob: 17d7bafaf8cf07fd26b0cb4bf103c210ea217de6 [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 /*
323 * ->poll_list is protected by the ctx->uring_lock for
324 * 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 */
328 struct list_head poll_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;
Jens Axboeb29472e2019-12-17 18:50:29 -0700399};
400
Jens Axboe9adbd452019-12-20 08:45:55 -0700401struct io_rw {
402 /* NOTE: kiocb has the file as the first member, so don't do it here */
403 struct kiocb kiocb;
404 u64 addr;
405 u64 len;
406};
407
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700408struct io_connect {
409 struct file *file;
410 struct sockaddr __user *addr;
411 int addr_len;
412};
413
Jens Axboee47293f2019-12-20 08:58:21 -0700414struct io_sr_msg {
415 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700416 union {
417 struct user_msghdr __user *msg;
418 void __user *buf;
419 };
Jens Axboee47293f2019-12-20 08:58:21 -0700420 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700421 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700422 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700423 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700424};
425
Jens Axboe15b71ab2019-12-11 11:20:36 -0700426struct io_open {
427 struct file *file;
428 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700429 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700430 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600431 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700432};
433
Jens Axboe05f3fb32019-12-09 11:22:50 -0700434struct io_files_update {
435 struct file *file;
436 u64 arg;
437 u32 nr_args;
438 u32 offset;
439};
440
Jens Axboe4840e412019-12-25 22:03:45 -0700441struct io_fadvise {
442 struct file *file;
443 u64 offset;
444 u32 len;
445 u32 advice;
446};
447
Jens Axboec1ca7572019-12-25 22:18:28 -0700448struct io_madvise {
449 struct file *file;
450 u64 addr;
451 u32 len;
452 u32 advice;
453};
454
Jens Axboe3e4827b2020-01-08 15:18:09 -0700455struct io_epoll {
456 struct file *file;
457 int epfd;
458 int op;
459 int fd;
460 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461};
462
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300463struct io_splice {
464 struct file *file_out;
465 struct file *file_in;
466 loff_t off_out;
467 loff_t off_in;
468 u64 len;
469 unsigned int flags;
470};
471
Jens Axboeddf0322d2020-02-23 16:41:33 -0700472struct io_provide_buf {
473 struct file *file;
474 __u64 addr;
475 __s32 len;
476 __u32 bgid;
477 __u16 nbufs;
478 __u16 bid;
479};
480
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700481struct io_statx {
482 struct file *file;
483 int dfd;
484 unsigned int mask;
485 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700486 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700487 struct statx __user *buffer;
488};
489
Jens Axboef499a022019-12-02 16:28:46 -0700490struct io_async_connect {
491 struct sockaddr_storage address;
492};
493
Jens Axboe03b12302019-12-02 18:50:25 -0700494struct io_async_msghdr {
495 struct iovec fast_iov[UIO_FASTIOV];
496 struct iovec *iov;
497 struct sockaddr __user *uaddr;
498 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700499 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700500};
501
Jens Axboef67676d2019-12-02 11:03:47 -0700502struct io_async_rw {
503 struct iovec fast_iov[UIO_FASTIOV];
504 struct iovec *iov;
505 ssize_t nr_segs;
506 ssize_t size;
Jens Axboebcf5a062020-05-22 09:24:42 -0600507 struct wait_page_queue wpq;
508 struct callback_head task_work;
Jens Axboef67676d2019-12-02 11:03:47 -0700509};
510
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700511struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700512 union {
513 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700514 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700515 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700516 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700517 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700518};
519
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300520enum {
521 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
522 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
523 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
524 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
525 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700526 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300527
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300528 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300529 REQ_F_LINK_NEXT_BIT,
530 REQ_F_FAIL_LINK_BIT,
531 REQ_F_INFLIGHT_BIT,
532 REQ_F_CUR_POS_BIT,
533 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300534 REQ_F_LINK_TIMEOUT_BIT,
535 REQ_F_TIMEOUT_BIT,
536 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300537 REQ_F_TIMEOUT_NOSEQ_BIT,
538 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300539 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700540 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700541 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700542 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600543 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300544 REQ_F_QUEUE_TIMEOUT_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800545 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300546 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700547
548 /* not a real bit, just to check we're not overflowing the space */
549 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300550};
551
552enum {
553 /* ctx owns file */
554 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
555 /* drain existing IO first */
556 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
557 /* linked sqes */
558 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
559 /* doesn't sever on completion < 0 */
560 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
561 /* IOSQE_ASYNC */
562 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700563 /* IOSQE_BUFFER_SELECT */
564 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300565
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300566 /* head of a link */
567 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568 /* already grabbed next link */
569 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
570 /* fail rest of links */
571 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
572 /* on inflight list */
573 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
574 /* read/write uses file position */
575 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
576 /* must not punt to workers */
577 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300578 /* has linked timeout */
579 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
580 /* timeout request */
581 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
582 /* regular file */
583 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300584 /* no timeout sequence */
585 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
586 /* completion under lock */
587 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300588 /* needs cleanup */
589 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700590 /* in overflow list */
591 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700592 /* already went through poll handler */
593 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700594 /* buffer already selected */
595 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600596 /* doesn't need file table for this request */
597 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300598 /* needs to queue linked timeout */
599 REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800600 /* io_wq_work is initialized */
601 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300602 /* req->task is refcounted */
603 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700604};
605
606struct async_poll {
607 struct io_poll_iocb poll;
608 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609};
610
Jens Axboe09bb8392019-03-13 12:39:28 -0600611/*
612 * NOTE! Each of the iocb union members has the file pointer
613 * as the first entry in their struct definition. So you can
614 * access the file pointer through any of the sub-structs,
615 * or directly as just 'ki_filp' in this struct.
616 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700618 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600619 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700620 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700621 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700622 struct io_accept accept;
623 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700624 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700625 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700626 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700627 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700628 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700629 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700630 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700631 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700632 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700633 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300634 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700635 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700636 struct io_statx statx;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700637 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700639 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300640 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700641 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800642 /* polled IO has completed */
643 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700645 u16 buf_index;
646
Jens Axboe2b188cc2019-01-07 10:46:33 -0700647 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700648 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700650 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600651 struct task_struct *task;
652 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700653 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600654 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600655 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700656
Jens Axboed7718a92020-02-14 22:23:12 -0700657 struct list_head link_list;
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 Axboeb41e9852020-02-17 09:52:41 -0700671 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700672 struct hlist_node hash_node;
673 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700674 };
675 struct io_wq_work work;
676 };
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 Axboe9a56a232019-01-09 09:06:50 -0700681struct io_submit_state {
682 struct blk_plug plug;
683
684 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700685 * io_kiocb alloc cache
686 */
687 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300688 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700689
690 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700691 * File reference cache
692 */
693 struct file *file;
694 unsigned int fd;
695 unsigned int has_refs;
696 unsigned int used_refs;
697 unsigned int ios_left;
698};
699
Jens Axboed3656342019-12-18 09:50:26 -0700700struct io_op_def {
701 /* needs req->io allocated for deferral/async */
702 unsigned async_ctx : 1;
703 /* needs current->mm setup, does mm access */
704 unsigned needs_mm : 1;
705 /* needs req->file assigned */
706 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600707 /* don't fail if file grab fails */
708 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700709 /* hash wq insertion if file is a regular file */
710 unsigned hash_reg_file : 1;
711 /* unbound wq insertion if file is a non-regular file */
712 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700713 /* opcode is not supported by this kernel */
714 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700715 /* needs file table */
716 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700717 /* needs ->fs */
718 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700719 /* set if opcode supports polled "wait" */
720 unsigned pollin : 1;
721 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700722 /* op supports buffer selection */
723 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700724};
725
726static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_NOP] = {},
728 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700729 .async_ctx = 1,
730 .needs_mm = 1,
731 .needs_file = 1,
732 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700733 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700734 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700735 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300736 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700737 .async_ctx = 1,
738 .needs_mm = 1,
739 .needs_file = 1,
740 .hash_reg_file = 1,
741 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700742 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700743 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300744 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700745 .needs_file = 1,
746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .needs_file = 1,
749 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700750 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700751 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300752 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .needs_file = 1,
754 .hash_reg_file = 1,
755 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700756 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700757 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300758 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700759 .needs_file = 1,
760 .unbound_nonreg_file = 1,
761 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300762 [IORING_OP_POLL_REMOVE] = {},
763 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700764 .needs_file = 1,
765 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300766 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700767 .async_ctx = 1,
768 .needs_mm = 1,
769 .needs_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700771 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700772 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .async_ctx = 1,
776 .needs_mm = 1,
777 .needs_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700779 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700780 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700781 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700782 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300783 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700784 .async_ctx = 1,
785 .needs_mm = 1,
786 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300787 [IORING_OP_TIMEOUT_REMOVE] = {},
788 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700789 .needs_mm = 1,
790 .needs_file = 1,
791 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700792 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700793 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_ASYNC_CANCEL] = {},
796 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .async_ctx = 1,
798 .needs_mm = 1,
799 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300800 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700801 .async_ctx = 1,
802 .needs_mm = 1,
803 .needs_file = 1,
804 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700805 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700806 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300807 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700808 .needs_file = 1,
809 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300810 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700811 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700812 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600815 .needs_file = 1,
816 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700817 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700818 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300819 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700820 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700821 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700825 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600826 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700829 .needs_mm = 1,
830 .needs_file = 1,
831 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700832 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700833 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700836 .needs_mm = 1,
837 .needs_file = 1,
838 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700839 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700840 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300841 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700842 .needs_file = 1,
843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700845 .needs_mm = 1,
846 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300847 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700848 .needs_mm = 1,
849 .needs_file = 1,
850 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700851 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700852 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300853 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700854 .needs_mm = 1,
855 .needs_file = 1,
856 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700857 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700858 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700861 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700862 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700863 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700864 [IORING_OP_EPOLL_CTL] = {
865 .unbound_nonreg_file = 1,
866 .file_table = 1,
867 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300868 [IORING_OP_SPLICE] = {
869 .needs_file = 1,
870 .hash_reg_file = 1,
871 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700872 },
873 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700874 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300875 [IORING_OP_TEE] = {
876 .needs_file = 1,
877 .hash_reg_file = 1,
878 .unbound_nonreg_file = 1,
879 },
Jens Axboed3656342019-12-18 09:50:26 -0700880};
881
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700882enum io_mem_account {
883 ACCT_LOCKED,
884 ACCT_PINNED,
885};
886
Jens Axboe561fb042019-10-24 07:25:42 -0600887static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700888static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800889static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700890static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700891static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
892static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700893static int __io_sqe_files_update(struct io_ring_ctx *ctx,
894 struct io_uring_files_update *ip,
895 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700896static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300897static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700898static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
899 int fd, struct file **out_file, bool fixed);
900static void __io_queue_sqe(struct io_kiocb *req,
901 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600902
Jens Axboeb63534c2020-06-04 11:28:00 -0600903static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
904 struct iovec **iovec, struct iov_iter *iter,
905 bool needs_lock);
906static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
907 struct iovec *iovec, struct iovec *fast_iov,
908 struct iov_iter *iter);
909
Jens Axboe2b188cc2019-01-07 10:46:33 -0700910static struct kmem_cache *req_cachep;
911
912static const struct file_operations io_uring_fops;
913
914struct sock *io_uring_get_socket(struct file *file)
915{
916#if defined(CONFIG_UNIX)
917 if (file->f_op == &io_uring_fops) {
918 struct io_ring_ctx *ctx = file->private_data;
919
920 return ctx->ring_sock->sk;
921 }
922#endif
923 return NULL;
924}
925EXPORT_SYMBOL(io_uring_get_socket);
926
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300927static void io_get_req_task(struct io_kiocb *req)
928{
929 if (req->flags & REQ_F_TASK_PINNED)
930 return;
931 get_task_struct(req->task);
932 req->flags |= REQ_F_TASK_PINNED;
933}
934
935/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
936static void __io_put_req_task(struct io_kiocb *req)
937{
938 if (req->flags & REQ_F_TASK_PINNED)
939 put_task_struct(req->task);
940}
941
Jens Axboe4a38aed22020-05-14 17:21:15 -0600942static void io_file_put_work(struct work_struct *work);
943
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800944/*
945 * Note: must call io_req_init_async() for the first time you
946 * touch any members of io_wq_work.
947 */
948static inline void io_req_init_async(struct io_kiocb *req)
949{
950 if (req->flags & REQ_F_WORK_INITIALIZED)
951 return;
952
953 memset(&req->work, 0, sizeof(req->work));
954 req->flags |= REQ_F_WORK_INITIALIZED;
955}
956
Pavel Begunkov0cdaf762020-05-17 14:13:40 +0300957static inline bool io_async_submit(struct io_ring_ctx *ctx)
958{
959 return ctx->flags & IORING_SETUP_SQPOLL;
960}
961
Jens Axboe2b188cc2019-01-07 10:46:33 -0700962static void io_ring_ctx_ref_free(struct percpu_ref *ref)
963{
964 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
965
Jens Axboe0f158b42020-05-14 17:18:39 -0600966 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700967}
968
969static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
970{
971 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700972 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700973
974 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
975 if (!ctx)
976 return NULL;
977
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700978 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
979 if (!ctx->fallback_req)
980 goto err;
981
Jens Axboe78076bb2019-12-04 19:56:40 -0700982 /*
983 * Use 5 bits less than the max cq entries, that should give us around
984 * 32 entries per hash list if totally full and uniformly spread.
985 */
986 hash_bits = ilog2(p->cq_entries);
987 hash_bits -= 5;
988 if (hash_bits <= 0)
989 hash_bits = 1;
990 ctx->cancel_hash_bits = hash_bits;
991 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
992 GFP_KERNEL);
993 if (!ctx->cancel_hash)
994 goto err;
995 __hash_init(ctx->cancel_hash, 1U << hash_bits);
996
Roman Gushchin21482892019-05-07 10:01:48 -0700997 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700998 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
999 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001000
1001 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001002 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001003 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001004 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001005 init_completion(&ctx->ref_comp);
1006 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001007 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001008 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009 mutex_init(&ctx->uring_lock);
1010 init_waitqueue_head(&ctx->wait);
1011 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001012 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001013 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001014 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001015 init_waitqueue_head(&ctx->inflight_wait);
1016 spin_lock_init(&ctx->inflight_lock);
1017 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001018 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1019 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001020 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001021err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001022 if (ctx->fallback_req)
1023 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001024 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001025 kfree(ctx);
1026 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001027}
1028
Bob Liu9d858b22019-11-13 18:06:25 +08001029static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06001030{
Jackie Liua197f662019-11-08 08:09:12 -07001031 struct io_ring_ctx *ctx = req->ctx;
1032
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001033 return req->sequence != ctx->cached_cq_tail
1034 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -06001035}
1036
Bob Liu9d858b22019-11-13 18:06:25 +08001037static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001038{
Pavel Begunkov87987892020-01-18 01:22:30 +03001039 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +08001040 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001041
Bob Liu9d858b22019-11-13 18:06:25 +08001042 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001043}
1044
Jens Axboede0617e2019-04-06 21:51:27 -06001045static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046{
Hristo Venev75b28af2019-08-26 17:23:46 +00001047 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001048
Pavel Begunkov07910152020-01-17 03:52:46 +03001049 /* order cqe stores with ring update */
1050 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001051
Pavel Begunkov07910152020-01-17 03:52:46 +03001052 if (wq_has_sleeper(&ctx->cq_wait)) {
1053 wake_up_interruptible(&ctx->cq_wait);
1054 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001055 }
1056}
1057
Jens Axboecccf0ee2020-01-27 16:34:48 -07001058static inline void io_req_work_grab_env(struct io_kiocb *req,
1059 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001060{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001061 if (!req->work.mm && def->needs_mm) {
1062 mmgrab(current->mm);
1063 req->work.mm = current->mm;
1064 }
1065 if (!req->work.creds)
1066 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001067 if (!req->work.fs && def->needs_fs) {
1068 spin_lock(&current->fs->lock);
1069 if (!current->fs->in_exec) {
1070 req->work.fs = current->fs;
1071 req->work.fs->users++;
1072 } else {
1073 req->work.flags |= IO_WQ_WORK_CANCEL;
1074 }
1075 spin_unlock(&current->fs->lock);
1076 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001077}
1078
1079static inline void io_req_work_drop_env(struct io_kiocb *req)
1080{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001081 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1082 return;
1083
Jens Axboecccf0ee2020-01-27 16:34:48 -07001084 if (req->work.mm) {
1085 mmdrop(req->work.mm);
1086 req->work.mm = NULL;
1087 }
1088 if (req->work.creds) {
1089 put_cred(req->work.creds);
1090 req->work.creds = NULL;
1091 }
Jens Axboeff002b32020-02-07 16:05:21 -07001092 if (req->work.fs) {
1093 struct fs_struct *fs = req->work.fs;
1094
1095 spin_lock(&req->work.fs->lock);
1096 if (--fs->users)
1097 fs = NULL;
1098 spin_unlock(&req->work.fs->lock);
1099 if (fs)
1100 free_fs_struct(fs);
1101 }
Jens Axboe561fb042019-10-24 07:25:42 -06001102}
1103
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001104static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001105 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001106{
Jens Axboed3656342019-12-18 09:50:26 -07001107 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001108
Jens Axboed3656342019-12-18 09:50:26 -07001109 if (req->flags & REQ_F_ISREG) {
1110 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001111 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001112 } else {
1113 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001114 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001115 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001116
Pavel Begunkov59960b92020-06-15 16:36:30 +03001117 io_req_init_async(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001118 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001119
Jens Axboe94ae5e72019-11-14 19:39:52 -07001120 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001121}
1122
Jackie Liua197f662019-11-08 08:09:12 -07001123static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001124{
Jackie Liua197f662019-11-08 08:09:12 -07001125 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001126 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001127
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001128 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001129
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001130 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1131 &req->work, req->flags);
1132 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001133
1134 if (link)
1135 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001136}
1137
Jens Axboe5262f562019-09-17 12:26:57 -06001138static void io_kill_timeout(struct io_kiocb *req)
1139{
1140 int ret;
1141
Jens Axboe2d283902019-12-04 11:08:05 -07001142 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001143 if (ret != -1) {
1144 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001145 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001146 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001147 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001148 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001149 }
1150}
1151
1152static void io_kill_timeouts(struct io_ring_ctx *ctx)
1153{
1154 struct io_kiocb *req, *tmp;
1155
1156 spin_lock_irq(&ctx->completion_lock);
1157 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1158 io_kill_timeout(req);
1159 spin_unlock_irq(&ctx->completion_lock);
1160}
1161
Pavel Begunkov04518942020-05-26 20:34:05 +03001162static void __io_queue_deferred(struct io_ring_ctx *ctx)
1163{
1164 do {
1165 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1166 struct io_kiocb, list);
1167
1168 if (req_need_defer(req))
1169 break;
1170 list_del_init(&req->list);
1171 io_queue_async_work(req);
1172 } while (!list_empty(&ctx->defer_list));
1173}
1174
Pavel Begunkov360428f2020-05-30 14:54:17 +03001175static void io_flush_timeouts(struct io_ring_ctx *ctx)
1176{
1177 while (!list_empty(&ctx->timeout_list)) {
1178 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1179 struct io_kiocb, list);
1180
1181 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1182 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001183 if (req->timeout.target_seq != ctx->cached_cq_tail
1184 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001185 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001186
Pavel Begunkov360428f2020-05-30 14:54:17 +03001187 list_del_init(&req->list);
1188 io_kill_timeout(req);
1189 }
1190}
1191
Jens Axboede0617e2019-04-06 21:51:27 -06001192static void io_commit_cqring(struct io_ring_ctx *ctx)
1193{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001194 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001195 __io_commit_cqring(ctx);
1196
Pavel Begunkov04518942020-05-26 20:34:05 +03001197 if (unlikely(!list_empty(&ctx->defer_list)))
1198 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001199}
1200
Jens Axboe2b188cc2019-01-07 10:46:33 -07001201static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1202{
Hristo Venev75b28af2019-08-26 17:23:46 +00001203 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001204 unsigned tail;
1205
1206 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001207 /*
1208 * writes to the cq entry need to come after reading head; the
1209 * control dependency is enough as we're using WRITE_ONCE to
1210 * fill the cq entry
1211 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001212 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001213 return NULL;
1214
1215 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001216 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001217}
1218
Jens Axboef2842ab2020-01-08 11:04:00 -07001219static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1220{
Jens Axboef0b493e2020-02-01 21:30:11 -07001221 if (!ctx->cq_ev_fd)
1222 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001223 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1224 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001225 if (!ctx->eventfd_async)
1226 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001227 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001228}
1229
Jens Axboeb41e9852020-02-17 09:52:41 -07001230static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001231{
1232 if (waitqueue_active(&ctx->wait))
1233 wake_up(&ctx->wait);
1234 if (waitqueue_active(&ctx->sqo_wait))
1235 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001236 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001237 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001238}
1239
Jens Axboec4a2ed72019-11-21 21:01:26 -07001240/* Returns true if there are no backlogged entries after the flush */
1241static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001243 struct io_rings *rings = ctx->rings;
1244 struct io_uring_cqe *cqe;
1245 struct io_kiocb *req;
1246 unsigned long flags;
1247 LIST_HEAD(list);
1248
1249 if (!force) {
1250 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001251 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001252 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1253 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001254 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001255 }
1256
1257 spin_lock_irqsave(&ctx->completion_lock, flags);
1258
1259 /* if force is set, the ring is going away. always drop after that */
1260 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001261 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001262
Jens Axboec4a2ed72019-11-21 21:01:26 -07001263 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001264 while (!list_empty(&ctx->cq_overflow_list)) {
1265 cqe = io_get_cqring(ctx);
1266 if (!cqe && !force)
1267 break;
1268
1269 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1270 list);
1271 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001272 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001273 if (cqe) {
1274 WRITE_ONCE(cqe->user_data, req->user_data);
1275 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001276 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001277 } else {
1278 WRITE_ONCE(ctx->rings->cq_overflow,
1279 atomic_inc_return(&ctx->cached_cq_overflow));
1280 }
1281 }
1282
1283 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001284 if (cqe) {
1285 clear_bit(0, &ctx->sq_check_overflow);
1286 clear_bit(0, &ctx->cq_check_overflow);
1287 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001288 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1289 io_cqring_ev_posted(ctx);
1290
1291 while (!list_empty(&list)) {
1292 req = list_first_entry(&list, struct io_kiocb, list);
1293 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001294 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001295 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001296
1297 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001298}
1299
Jens Axboebcda7ba2020-02-23 16:42:51 -07001300static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001301{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001302 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001303 struct io_uring_cqe *cqe;
1304
Jens Axboe78e19bb2019-11-06 15:21:34 -07001305 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001306
Jens Axboe2b188cc2019-01-07 10:46:33 -07001307 /*
1308 * If we can't get a cq entry, userspace overflowed the
1309 * submission (by quite a lot). Increment the overflow count in
1310 * the ring.
1311 */
1312 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001313 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001314 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001316 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001317 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318 WRITE_ONCE(ctx->rings->cq_overflow,
1319 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001320 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001321 if (list_empty(&ctx->cq_overflow_list)) {
1322 set_bit(0, &ctx->sq_check_overflow);
1323 set_bit(0, &ctx->cq_check_overflow);
1324 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001325 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001326 refcount_inc(&req->refs);
1327 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001328 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001329 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330 }
1331}
1332
Jens Axboebcda7ba2020-02-23 16:42:51 -07001333static void io_cqring_fill_event(struct io_kiocb *req, long res)
1334{
1335 __io_cqring_fill_event(req, res, 0);
1336}
1337
Jens Axboee1e16092020-06-22 09:17:17 -06001338static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001340 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001341 unsigned long flags;
1342
1343 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001344 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001345 io_commit_cqring(ctx);
1346 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1347
Jens Axboe8c838782019-03-12 15:48:16 -06001348 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349}
1350
Jens Axboee1e16092020-06-22 09:17:17 -06001351static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001352{
Jens Axboee1e16092020-06-22 09:17:17 -06001353 io_cqring_add_event(req, res, cflags);
1354 io_put_req(req);
1355}
1356
1357static void io_req_complete(struct io_kiocb *req, long res)
1358{
1359 __io_req_complete(req, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001360}
1361
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001362static inline bool io_is_fallback_req(struct io_kiocb *req)
1363{
1364 return req == (struct io_kiocb *)
1365 ((unsigned long) req->ctx->fallback_req & ~1UL);
1366}
1367
1368static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1369{
1370 struct io_kiocb *req;
1371
1372 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001373 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001374 return req;
1375
1376 return NULL;
1377}
1378
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001379static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1380 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381{
Jens Axboefd6fab22019-03-14 16:30:06 -06001382 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001383 struct io_kiocb *req;
1384
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001385 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001386 size_t sz;
1387 int ret;
1388
1389 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001390 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1391
1392 /*
1393 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1394 * retry single alloc to be on the safe side.
1395 */
1396 if (unlikely(ret <= 0)) {
1397 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1398 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001399 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001400 ret = 1;
1401 }
Jens Axboe2579f912019-01-09 09:10:43 -07001402 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001403 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001404 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001405 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001406 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001407 }
1408
Jens Axboe2579f912019-01-09 09:10:43 -07001409 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001410fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001411 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412}
1413
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001414static inline void io_put_file(struct io_kiocb *req, struct file *file,
1415 bool fixed)
1416{
1417 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001418 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001419 else
1420 fput(file);
1421}
1422
Jens Axboec6ca97b302019-12-28 12:11:08 -07001423static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001425 if (req->flags & REQ_F_NEED_CLEANUP)
1426 io_cleanup_req(req);
1427
YueHaibing96fd84d2020-01-07 22:22:44 +08001428 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001429 if (req->file)
1430 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001431 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001432 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001433}
1434
1435static void __io_free_req(struct io_kiocb *req)
1436{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001437 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001438
Jens Axboefcb323c2019-10-24 12:39:47 -06001439 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001440 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001441 unsigned long flags;
1442
1443 spin_lock_irqsave(&ctx->inflight_lock, flags);
1444 list_del(&req->inflight_entry);
1445 if (waitqueue_active(&ctx->inflight_wait))
1446 wake_up(&ctx->inflight_wait);
1447 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1448 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001449
1450 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001451 if (likely(!io_is_fallback_req(req)))
1452 kmem_cache_free(req_cachep, req);
1453 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001454 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001455}
1456
Jens Axboec6ca97b302019-12-28 12:11:08 -07001457struct req_batch {
1458 void *reqs[IO_IOPOLL_BATCH];
1459 int to_free;
1460 int need_iter;
1461};
1462
1463static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1464{
1465 if (!rb->to_free)
1466 return;
1467 if (rb->need_iter) {
1468 int i, inflight = 0;
1469 unsigned long flags;
1470
1471 for (i = 0; i < rb->to_free; i++) {
1472 struct io_kiocb *req = rb->reqs[i];
1473
Jens Axboec6ca97b302019-12-28 12:11:08 -07001474 if (req->flags & REQ_F_INFLIGHT)
1475 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001476 __io_req_aux_free(req);
1477 }
1478 if (!inflight)
1479 goto do_free;
1480
1481 spin_lock_irqsave(&ctx->inflight_lock, flags);
1482 for (i = 0; i < rb->to_free; i++) {
1483 struct io_kiocb *req = rb->reqs[i];
1484
Jens Axboe10fef4b2020-01-09 07:52:28 -07001485 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001486 list_del(&req->inflight_entry);
1487 if (!--inflight)
1488 break;
1489 }
1490 }
1491 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1492
1493 if (waitqueue_active(&ctx->inflight_wait))
1494 wake_up(&ctx->inflight_wait);
1495 }
1496do_free:
1497 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1498 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001499 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001500}
1501
Jackie Liua197f662019-11-08 08:09:12 -07001502static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001503{
Jackie Liua197f662019-11-08 08:09:12 -07001504 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001505 int ret;
1506
Jens Axboe2d283902019-12-04 11:08:05 -07001507 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001508 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001509 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001510 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001511 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001512 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001513 return true;
1514 }
1515
1516 return false;
1517}
1518
Jens Axboeba816ad2019-09-28 11:36:45 -06001519static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001520{
Jens Axboe2665abf2019-11-05 12:40:47 -07001521 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001522 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001523
Jens Axboe4d7dd462019-11-20 13:03:52 -07001524 /* Already got next link */
1525 if (req->flags & REQ_F_LINK_NEXT)
1526 return;
1527
Jens Axboe9e645e112019-05-10 16:07:28 -06001528 /*
1529 * The list should never be empty when we are called here. But could
1530 * potentially happen if the chain is messed up, check to be on the
1531 * safe side.
1532 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001533 while (!list_empty(&req->link_list)) {
1534 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1535 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001536
Pavel Begunkov44932332019-12-05 16:16:35 +03001537 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1538 (nxt->flags & REQ_F_TIMEOUT))) {
1539 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001540 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001541 req->flags &= ~REQ_F_LINK_TIMEOUT;
1542 continue;
1543 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001544
Pavel Begunkov44932332019-12-05 16:16:35 +03001545 list_del_init(&req->link_list);
1546 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001547 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001548 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001549 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001550 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001551
Jens Axboe4d7dd462019-11-20 13:03:52 -07001552 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001553 if (wake_ev)
1554 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001555}
1556
1557/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001558 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001559 */
1560static void io_fail_links(struct io_kiocb *req)
1561{
Jens Axboe2665abf2019-11-05 12:40:47 -07001562 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001563 unsigned long flags;
1564
1565 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001566
1567 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001568 struct io_kiocb *link = list_first_entry(&req->link_list,
1569 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001570
Pavel Begunkov44932332019-12-05 16:16:35 +03001571 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001572 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001573
1574 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001575 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001576 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001577 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001578 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001579 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001580 }
Jens Axboe5d960722019-11-19 15:31:28 -07001581 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001582 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001583
1584 io_commit_cqring(ctx);
1585 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1586 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001587}
1588
Jens Axboe4d7dd462019-11-20 13:03:52 -07001589static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001590{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001591 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001592 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001593
Jens Axboe9e645e112019-05-10 16:07:28 -06001594 /*
1595 * If LINK is set, we have dependent requests in this chain. If we
1596 * didn't fail this request, queue the first one up, moving any other
1597 * dependencies to the next request. In case of failure, fail the rest
1598 * of the chain.
1599 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001600 if (req->flags & REQ_F_FAIL_LINK) {
1601 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001602 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1603 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001604 struct io_ring_ctx *ctx = req->ctx;
1605 unsigned long flags;
1606
1607 /*
1608 * If this is a timeout link, we could be racing with the
1609 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001610 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001611 */
1612 spin_lock_irqsave(&ctx->completion_lock, flags);
1613 io_req_link_next(req, nxt);
1614 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1615 } else {
1616 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001617 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001618}
Jens Axboe9e645e112019-05-10 16:07:28 -06001619
Jackie Liuc69f8db2019-11-09 11:00:08 +08001620static void io_free_req(struct io_kiocb *req)
1621{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001622 struct io_kiocb *nxt = NULL;
1623
1624 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001625 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001626
1627 if (nxt)
1628 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001629}
1630
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001631static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1632{
1633 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001634 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1635
1636 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1637 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001638
1639 *workptr = &nxt->work;
1640 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001641 if (link)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03001642 nxt->flags |= REQ_F_QUEUE_TIMEOUT;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001643}
1644
Jens Axboeba816ad2019-09-28 11:36:45 -06001645/*
1646 * Drop reference to request, return next in chain (if there is one) if this
1647 * was the last reference to this request.
1648 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001649__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001650static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001651{
Jens Axboe2a44f462020-02-25 13:25:41 -07001652 if (refcount_dec_and_test(&req->refs)) {
1653 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001654 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001655 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001656}
1657
Jens Axboe2b188cc2019-01-07 10:46:33 -07001658static void io_put_req(struct io_kiocb *req)
1659{
Jens Axboedef596e2019-01-09 08:59:42 -07001660 if (refcount_dec_and_test(&req->refs))
1661 io_free_req(req);
1662}
1663
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001664static void io_steal_work(struct io_kiocb *req,
1665 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001666{
1667 /*
1668 * It's in an io-wq worker, so there always should be at least
1669 * one reference, which will be dropped in io_put_work() just
1670 * after the current handler returns.
1671 *
1672 * It also means, that if the counter dropped to 1, then there is
1673 * no asynchronous users left, so it's safe to steal the next work.
1674 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001675 if (refcount_read(&req->refs) == 1) {
1676 struct io_kiocb *nxt = NULL;
1677
1678 io_req_find_next(req, &nxt);
1679 if (nxt)
1680 io_wq_assign_next(workptr, nxt);
1681 }
1682}
1683
Jens Axboe978db572019-11-14 22:39:04 -07001684/*
1685 * Must only be used if we don't need to care about links, usually from
1686 * within the completion handling itself.
1687 */
1688static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001689{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001690 /* drop both submit and complete references */
1691 if (refcount_sub_and_test(2, &req->refs))
1692 __io_free_req(req);
1693}
1694
Jens Axboe978db572019-11-14 22:39:04 -07001695static void io_double_put_req(struct io_kiocb *req)
1696{
1697 /* drop both submit and complete references */
1698 if (refcount_sub_and_test(2, &req->refs))
1699 io_free_req(req);
1700}
1701
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001702static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001703{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001704 struct io_rings *rings = ctx->rings;
1705
Jens Axboead3eb2c2019-12-18 17:12:20 -07001706 if (test_bit(0, &ctx->cq_check_overflow)) {
1707 /*
1708 * noflush == true is from the waitqueue handler, just ensure
1709 * we wake up the task, and the next invocation will flush the
1710 * entries. We cannot safely to it from here.
1711 */
1712 if (noflush && !list_empty(&ctx->cq_overflow_list))
1713 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001714
Jens Axboead3eb2c2019-12-18 17:12:20 -07001715 io_cqring_overflow_flush(ctx, false);
1716 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001717
Jens Axboea3a0e432019-08-20 11:03:11 -06001718 /* See comment at the top of this file */
1719 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001720 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001721}
1722
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001723static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1724{
1725 struct io_rings *rings = ctx->rings;
1726
1727 /* make sure SQ entry isn't read before tail */
1728 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1729}
1730
Jens Axboe8237e042019-12-28 10:48:22 -07001731static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001732{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001733 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001734 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001735
Jens Axboe9d9e88a2020-05-13 12:53:19 -06001736 if (req->file || req->io)
Jens Axboec6ca97b302019-12-28 12:11:08 -07001737 rb->need_iter++;
1738
1739 rb->reqs[rb->to_free++] = req;
1740 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1741 io_free_req_many(req->ctx, rb);
1742 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001743}
1744
Jens Axboebcda7ba2020-02-23 16:42:51 -07001745static int io_put_kbuf(struct io_kiocb *req)
1746{
Jens Axboe4d954c22020-02-27 07:31:19 -07001747 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001748 int cflags;
1749
Jens Axboe4d954c22020-02-27 07:31:19 -07001750 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001751 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1752 cflags |= IORING_CQE_F_BUFFER;
1753 req->rw.addr = 0;
1754 kfree(kbuf);
1755 return cflags;
1756}
1757
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001758static void io_iopoll_queue(struct list_head *again)
1759{
1760 struct io_kiocb *req;
1761
1762 do {
1763 req = list_first_entry(again, struct io_kiocb, list);
1764 list_del(&req->list);
1765 refcount_inc(&req->refs);
1766 io_queue_async_work(req);
1767 } while (!list_empty(again));
1768}
1769
Jens Axboedef596e2019-01-09 08:59:42 -07001770/*
1771 * Find and free completed poll iocbs
1772 */
1773static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1774 struct list_head *done)
1775{
Jens Axboe8237e042019-12-28 10:48:22 -07001776 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001777 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001778 LIST_HEAD(again);
1779
1780 /* order with ->result store in io_complete_rw_iopoll() */
1781 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001782
Jens Axboec6ca97b302019-12-28 12:11:08 -07001783 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001784 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001785 int cflags = 0;
1786
Jens Axboedef596e2019-01-09 08:59:42 -07001787 req = list_first_entry(done, struct io_kiocb, list);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001788 if (READ_ONCE(req->result) == -EAGAIN) {
1789 req->iopoll_completed = 0;
1790 list_move_tail(&req->list, &again);
1791 continue;
1792 }
Jens Axboedef596e2019-01-09 08:59:42 -07001793 list_del(&req->list);
1794
Jens Axboebcda7ba2020-02-23 16:42:51 -07001795 if (req->flags & REQ_F_BUFFER_SELECTED)
1796 cflags = io_put_kbuf(req);
1797
1798 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001799 (*nr_events)++;
1800
Jens Axboe8237e042019-12-28 10:48:22 -07001801 if (refcount_dec_and_test(&req->refs) &&
1802 !io_req_multi_free(&rb, req))
1803 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001804 }
Jens Axboedef596e2019-01-09 08:59:42 -07001805
Jens Axboe09bb8392019-03-13 12:39:28 -06001806 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001807 if (ctx->flags & IORING_SETUP_SQPOLL)
1808 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001809 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001810
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001811 if (!list_empty(&again))
1812 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001813}
1814
Jens Axboedef596e2019-01-09 08:59:42 -07001815static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1816 long min)
1817{
1818 struct io_kiocb *req, *tmp;
1819 LIST_HEAD(done);
1820 bool spin;
1821 int ret;
1822
1823 /*
1824 * Only spin for completions if we don't have multiple devices hanging
1825 * off our complete list, and we're under the requested amount.
1826 */
1827 spin = !ctx->poll_multi_file && *nr_events < min;
1828
1829 ret = 0;
1830 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001831 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001832
1833 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001834 * Move completed and retryable entries to our local lists.
1835 * If we find a request that requires polling, break out
1836 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001837 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08001838 if (READ_ONCE(req->iopoll_completed)) {
Jens Axboedef596e2019-01-09 08:59:42 -07001839 list_move_tail(&req->list, &done);
1840 continue;
1841 }
1842 if (!list_empty(&done))
1843 break;
1844
1845 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1846 if (ret < 0)
1847 break;
1848
1849 if (ret && spin)
1850 spin = false;
1851 ret = 0;
1852 }
1853
1854 if (!list_empty(&done))
1855 io_iopoll_complete(ctx, nr_events, &done);
1856
1857 return ret;
1858}
1859
1860/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001861 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001862 * non-spinning poll check - we'll still enter the driver poll loop, but only
1863 * as a non-spinning completion check.
1864 */
1865static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1866 long min)
1867{
Jens Axboe08f54392019-08-21 22:19:11 -06001868 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001869 int ret;
1870
1871 ret = io_do_iopoll(ctx, nr_events, min);
1872 if (ret < 0)
1873 return ret;
1874 if (!min || *nr_events >= min)
1875 return 0;
1876 }
1877
1878 return 1;
1879}
1880
1881/*
1882 * We can't just wait for polled events to come to us, we have to actively
1883 * find and complete them.
1884 */
1885static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1886{
1887 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1888 return;
1889
1890 mutex_lock(&ctx->uring_lock);
1891 while (!list_empty(&ctx->poll_list)) {
1892 unsigned int nr_events = 0;
1893
1894 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001895
1896 /*
1897 * Ensure we allow local-to-the-cpu processing to take place,
1898 * in this case we need to ensure that we reap all events.
1899 */
1900 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001901 }
1902 mutex_unlock(&ctx->uring_lock);
1903}
1904
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001905static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1906 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001907{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001908 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001909
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001910 /*
1911 * We disallow the app entering submit/complete with polling, but we
1912 * still need to lock the ring to prevent racing with polled issue
1913 * that got punted to a workqueue.
1914 */
1915 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001916 do {
1917 int tmin = 0;
1918
Jens Axboe500f9fb2019-08-19 12:15:59 -06001919 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001920 * Don't enter poll loop if we already have events pending.
1921 * If we do, we can potentially be spinning for commands that
1922 * already triggered a CQE (eg in error).
1923 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001924 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001925 break;
1926
1927 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001928 * If a submit got punted to a workqueue, we can have the
1929 * application entering polling for a command before it gets
1930 * issued. That app will hold the uring_lock for the duration
1931 * of the poll right here, so we need to take a breather every
1932 * now and then to ensure that the issue has a chance to add
1933 * the poll to the issued list. Otherwise we can spin here
1934 * forever, while the workqueue is stuck trying to acquire the
1935 * very same mutex.
1936 */
1937 if (!(++iters & 7)) {
1938 mutex_unlock(&ctx->uring_lock);
1939 mutex_lock(&ctx->uring_lock);
1940 }
1941
Jens Axboedef596e2019-01-09 08:59:42 -07001942 if (*nr_events < min)
1943 tmin = min - *nr_events;
1944
1945 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1946 if (ret <= 0)
1947 break;
1948 ret = 0;
1949 } while (min && !*nr_events && !need_resched());
1950
Jens Axboe500f9fb2019-08-19 12:15:59 -06001951 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001952 return ret;
1953}
1954
Jens Axboe491381ce2019-10-17 09:20:46 -06001955static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956{
Jens Axboe491381ce2019-10-17 09:20:46 -06001957 /*
1958 * Tell lockdep we inherited freeze protection from submission
1959 * thread.
1960 */
1961 if (req->flags & REQ_F_ISREG) {
1962 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001963
Jens Axboe491381ce2019-10-17 09:20:46 -06001964 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001965 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001966 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967}
1968
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001969static inline void req_set_fail_links(struct io_kiocb *req)
1970{
1971 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1972 req->flags |= REQ_F_FAIL_LINK;
1973}
1974
Jens Axboeba816ad2019-09-28 11:36:45 -06001975static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976{
Jens Axboe9adbd452019-12-20 08:45:55 -07001977 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001978 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001979
Jens Axboe491381ce2019-10-17 09:20:46 -06001980 if (kiocb->ki_flags & IOCB_WRITE)
1981 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001982
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001983 if (res != req->result)
1984 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001985 if (req->flags & REQ_F_BUFFER_SELECTED)
1986 cflags = io_put_kbuf(req);
Jens Axboee1e16092020-06-22 09:17:17 -06001987 io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001988}
1989
Jens Axboeb63534c2020-06-04 11:28:00 -06001990static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
1991{
1992 struct mm_struct *mm = current->mm;
1993
1994 if (mm) {
1995 kthread_unuse_mm(mm);
1996 mmput(mm);
1997 }
1998}
1999
Pavel Begunkovd3cac642020-06-25 12:38:13 +03002000static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
Jens Axboeb63534c2020-06-04 11:28:00 -06002001{
Pavel Begunkovd3cac642020-06-25 12:38:13 +03002002 if (!current->mm) {
Jens Axboeb63534c2020-06-04 11:28:00 -06002003 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
2004 return -EFAULT;
2005 kthread_use_mm(ctx->sqo_mm);
2006 }
2007
2008 return 0;
2009}
2010
Pavel Begunkovd3cac642020-06-25 12:38:13 +03002011static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
2012 struct io_kiocb *req)
2013{
2014 if (!io_op_defs[req->opcode].needs_mm)
2015 return 0;
2016 return __io_sq_thread_acquire_mm(ctx);
2017}
2018
Jens Axboeb63534c2020-06-04 11:28:00 -06002019#ifdef CONFIG_BLOCK
2020static bool io_resubmit_prep(struct io_kiocb *req, int error)
2021{
2022 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2023 ssize_t ret = -ECANCELED;
2024 struct iov_iter iter;
2025 int rw;
2026
2027 if (error) {
2028 ret = error;
2029 goto end_req;
2030 }
2031
2032 switch (req->opcode) {
2033 case IORING_OP_READV:
2034 case IORING_OP_READ_FIXED:
2035 case IORING_OP_READ:
2036 rw = READ;
2037 break;
2038 case IORING_OP_WRITEV:
2039 case IORING_OP_WRITE_FIXED:
2040 case IORING_OP_WRITE:
2041 rw = WRITE;
2042 break;
2043 default:
2044 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2045 req->opcode);
2046 goto end_req;
2047 }
2048
2049 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2050 if (ret < 0)
2051 goto end_req;
2052 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2053 if (!ret)
2054 return true;
2055 kfree(iovec);
2056end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002057 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002058 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002059 return false;
2060}
2061
2062static void io_rw_resubmit(struct callback_head *cb)
2063{
2064 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2065 struct io_ring_ctx *ctx = req->ctx;
2066 int err;
2067
2068 __set_current_state(TASK_RUNNING);
2069
2070 err = io_sq_thread_acquire_mm(ctx, req);
2071
2072 if (io_resubmit_prep(req, err)) {
2073 refcount_inc(&req->refs);
2074 io_queue_async_work(req);
2075 }
2076}
2077#endif
2078
2079static bool io_rw_reissue(struct io_kiocb *req, long res)
2080{
2081#ifdef CONFIG_BLOCK
2082 struct task_struct *tsk;
2083 int ret;
2084
2085 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2086 return false;
2087
2088 tsk = req->task;
2089 init_task_work(&req->task_work, io_rw_resubmit);
2090 ret = task_work_add(tsk, &req->task_work, true);
2091 if (!ret)
2092 return true;
2093#endif
2094 return false;
2095}
2096
Jens Axboeba816ad2019-09-28 11:36:45 -06002097static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2098{
Jens Axboe9adbd452019-12-20 08:45:55 -07002099 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002100
Jens Axboeb63534c2020-06-04 11:28:00 -06002101 if (!io_rw_reissue(req, res)) {
2102 io_complete_rw_common(kiocb, res);
2103 io_put_req(req);
2104 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002105}
2106
Jens Axboedef596e2019-01-09 08:59:42 -07002107static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2108{
Jens Axboe9adbd452019-12-20 08:45:55 -07002109 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002110
Jens Axboe491381ce2019-10-17 09:20:46 -06002111 if (kiocb->ki_flags & IOCB_WRITE)
2112 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002113
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002114 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002115 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002116
2117 WRITE_ONCE(req->result, res);
2118 /* order with io_poll_complete() checking ->result */
2119 if (res != -EAGAIN) {
2120 smp_wmb();
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002121 WRITE_ONCE(req->iopoll_completed, 1);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002122 }
Jens Axboedef596e2019-01-09 08:59:42 -07002123}
2124
2125/*
2126 * After the iocb has been issued, it's safe to be found on the poll list.
2127 * Adding the kiocb to the list AFTER submission ensures that we don't
2128 * find it from a io_iopoll_getevents() thread before the issuer is done
2129 * accessing the kiocb cookie.
2130 */
2131static void io_iopoll_req_issued(struct io_kiocb *req)
2132{
2133 struct io_ring_ctx *ctx = req->ctx;
2134
2135 /*
2136 * Track whether we have multiple files in our lists. This will impact
2137 * how we do polling eventually, not spinning if we're on potentially
2138 * different devices.
2139 */
2140 if (list_empty(&ctx->poll_list)) {
2141 ctx->poll_multi_file = false;
2142 } else if (!ctx->poll_multi_file) {
2143 struct io_kiocb *list_req;
2144
2145 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2146 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002147 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002148 ctx->poll_multi_file = true;
2149 }
2150
2151 /*
2152 * For fast devices, IO may have already completed. If it has, add
2153 * it to the front so we find it first.
2154 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002155 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002156 list_add(&req->list, &ctx->poll_list);
2157 else
2158 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002159
2160 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2161 wq_has_sleeper(&ctx->sqo_wait))
2162 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002163}
2164
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002165static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002166{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002167 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002168
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002169 if (diff)
2170 fput_many(state->file, diff);
2171 state->file = NULL;
2172}
2173
2174static inline void io_state_file_put(struct io_submit_state *state)
2175{
2176 if (state->file)
2177 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002178}
2179
2180/*
2181 * Get as many references to a file as we have IOs left in this submission,
2182 * assuming most submissions are for one file, or at least that each file
2183 * has more than one submission.
2184 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002185static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002186{
2187 if (!state)
2188 return fget(fd);
2189
2190 if (state->file) {
2191 if (state->fd == fd) {
2192 state->used_refs++;
2193 state->ios_left--;
2194 return state->file;
2195 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002196 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002197 }
2198 state->file = fget_many(fd, state->ios_left);
2199 if (!state->file)
2200 return NULL;
2201
2202 state->fd = fd;
2203 state->has_refs = state->ios_left;
2204 state->used_refs = 1;
2205 state->ios_left--;
2206 return state->file;
2207}
2208
Jens Axboe4503b762020-06-01 10:00:27 -06002209static bool io_bdev_nowait(struct block_device *bdev)
2210{
2211#ifdef CONFIG_BLOCK
2212 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2213#else
2214 return true;
2215#endif
2216}
2217
Jens Axboe2b188cc2019-01-07 10:46:33 -07002218/*
2219 * If we tracked the file through the SCM inflight mechanism, we could support
2220 * any file. For now, just ensure that anything potentially problematic is done
2221 * inline.
2222 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002223static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002224{
2225 umode_t mode = file_inode(file)->i_mode;
2226
Jens Axboe4503b762020-06-01 10:00:27 -06002227 if (S_ISBLK(mode)) {
2228 if (io_bdev_nowait(file->f_inode->i_bdev))
2229 return true;
2230 return false;
2231 }
2232 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002233 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002234 if (S_ISREG(mode)) {
2235 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2236 file->f_op != &io_uring_fops)
2237 return true;
2238 return false;
2239 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002240
Jens Axboec5b85622020-06-09 19:23:05 -06002241 /* any ->read/write should understand O_NONBLOCK */
2242 if (file->f_flags & O_NONBLOCK)
2243 return true;
2244
Jens Axboeaf197f52020-04-28 13:15:06 -06002245 if (!(file->f_mode & FMODE_NOWAIT))
2246 return false;
2247
2248 if (rw == READ)
2249 return file->f_op->read_iter != NULL;
2250
2251 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002252}
2253
Jens Axboe3529d8c2019-12-19 18:24:38 -07002254static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2255 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002256{
Jens Axboedef596e2019-01-09 08:59:42 -07002257 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002258 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002259 unsigned ioprio;
2260 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002261
Jens Axboe491381ce2019-10-17 09:20:46 -06002262 if (S_ISREG(file_inode(req->file)->i_mode))
2263 req->flags |= REQ_F_ISREG;
2264
Jens Axboe2b188cc2019-01-07 10:46:33 -07002265 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002266 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2267 req->flags |= REQ_F_CUR_POS;
2268 kiocb->ki_pos = req->file->f_pos;
2269 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002270 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002271 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2272 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2273 if (unlikely(ret))
2274 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275
2276 ioprio = READ_ONCE(sqe->ioprio);
2277 if (ioprio) {
2278 ret = ioprio_check_cap(ioprio);
2279 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002280 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281
2282 kiocb->ki_ioprio = ioprio;
2283 } else
2284 kiocb->ki_ioprio = get_current_ioprio();
2285
Stefan Bühler8449eed2019-04-27 20:34:19 +02002286 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002287 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002288 req->flags |= REQ_F_NOWAIT;
2289
Jens Axboeb63534c2020-06-04 11:28:00 -06002290 if (kiocb->ki_flags & IOCB_DIRECT)
2291 io_get_req_task(req);
2292
Stefan Bühler8449eed2019-04-27 20:34:19 +02002293 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002294 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002295
Jens Axboedef596e2019-01-09 08:59:42 -07002296 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002297 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2298 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002299 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002300
Jens Axboedef596e2019-01-09 08:59:42 -07002301 kiocb->ki_flags |= IOCB_HIPRI;
2302 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002303 req->result = 0;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002304 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002305 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002306 if (kiocb->ki_flags & IOCB_HIPRI)
2307 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002308 kiocb->ki_complete = io_complete_rw;
2309 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002310
Jens Axboe3529d8c2019-12-19 18:24:38 -07002311 req->rw.addr = READ_ONCE(sqe->addr);
2312 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002313 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002314 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002315}
2316
2317static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2318{
2319 switch (ret) {
2320 case -EIOCBQUEUED:
2321 break;
2322 case -ERESTARTSYS:
2323 case -ERESTARTNOINTR:
2324 case -ERESTARTNOHAND:
2325 case -ERESTART_RESTARTBLOCK:
2326 /*
2327 * We can't just restart the syscall, since previously
2328 * submitted sqes may already be in progress. Just fail this
2329 * IO with EINTR.
2330 */
2331 ret = -EINTR;
2332 /* fall through */
2333 default:
2334 kiocb->ki_complete(kiocb, ret, 0);
2335 }
2336}
2337
Pavel Begunkov014db002020-03-03 21:33:12 +03002338static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002339{
Jens Axboeba042912019-12-25 16:33:42 -07002340 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2341
2342 if (req->flags & REQ_F_CUR_POS)
2343 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002344 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002345 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002346 else
2347 io_rw_done(kiocb, ret);
2348}
2349
Jens Axboe9adbd452019-12-20 08:45:55 -07002350static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002351 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002352{
Jens Axboe9adbd452019-12-20 08:45:55 -07002353 struct io_ring_ctx *ctx = req->ctx;
2354 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002355 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002356 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002357 size_t offset;
2358 u64 buf_addr;
2359
2360 /* attempt to use fixed buffers without having provided iovecs */
2361 if (unlikely(!ctx->user_bufs))
2362 return -EFAULT;
2363
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002364 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002365 if (unlikely(buf_index >= ctx->nr_user_bufs))
2366 return -EFAULT;
2367
2368 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2369 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002370 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002371
2372 /* overflow */
2373 if (buf_addr + len < buf_addr)
2374 return -EFAULT;
2375 /* not inside the mapped region */
2376 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2377 return -EFAULT;
2378
2379 /*
2380 * May not be a start of buffer, set size appropriately
2381 * and advance us to the beginning.
2382 */
2383 offset = buf_addr - imu->ubuf;
2384 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002385
2386 if (offset) {
2387 /*
2388 * Don't use iov_iter_advance() here, as it's really slow for
2389 * using the latter parts of a big fixed buffer - it iterates
2390 * over each segment manually. We can cheat a bit here, because
2391 * we know that:
2392 *
2393 * 1) it's a BVEC iter, we set it up
2394 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2395 * first and last bvec
2396 *
2397 * So just find our index, and adjust the iterator afterwards.
2398 * If the offset is within the first bvec (or the whole first
2399 * bvec, just use iov_iter_advance(). This makes it easier
2400 * since we can just skip the first segment, which may not
2401 * be PAGE_SIZE aligned.
2402 */
2403 const struct bio_vec *bvec = imu->bvec;
2404
2405 if (offset <= bvec->bv_len) {
2406 iov_iter_advance(iter, offset);
2407 } else {
2408 unsigned long seg_skip;
2409
2410 /* skip first vec */
2411 offset -= bvec->bv_len;
2412 seg_skip = 1 + (offset >> PAGE_SHIFT);
2413
2414 iter->bvec = bvec + seg_skip;
2415 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002416 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002417 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002418 }
2419 }
2420
Jens Axboe5e559562019-11-13 16:12:46 -07002421 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002422}
2423
Jens Axboebcda7ba2020-02-23 16:42:51 -07002424static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2425{
2426 if (needs_lock)
2427 mutex_unlock(&ctx->uring_lock);
2428}
2429
2430static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2431{
2432 /*
2433 * "Normal" inline submissions always hold the uring_lock, since we
2434 * grab it from the system call. Same is true for the SQPOLL offload.
2435 * The only exception is when we've detached the request and issue it
2436 * from an async worker thread, grab the lock for that case.
2437 */
2438 if (needs_lock)
2439 mutex_lock(&ctx->uring_lock);
2440}
2441
2442static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2443 int bgid, struct io_buffer *kbuf,
2444 bool needs_lock)
2445{
2446 struct io_buffer *head;
2447
2448 if (req->flags & REQ_F_BUFFER_SELECTED)
2449 return kbuf;
2450
2451 io_ring_submit_lock(req->ctx, needs_lock);
2452
2453 lockdep_assert_held(&req->ctx->uring_lock);
2454
2455 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2456 if (head) {
2457 if (!list_empty(&head->list)) {
2458 kbuf = list_last_entry(&head->list, struct io_buffer,
2459 list);
2460 list_del(&kbuf->list);
2461 } else {
2462 kbuf = head;
2463 idr_remove(&req->ctx->io_buffer_idr, bgid);
2464 }
2465 if (*len > kbuf->len)
2466 *len = kbuf->len;
2467 } else {
2468 kbuf = ERR_PTR(-ENOBUFS);
2469 }
2470
2471 io_ring_submit_unlock(req->ctx, needs_lock);
2472
2473 return kbuf;
2474}
2475
Jens Axboe4d954c22020-02-27 07:31:19 -07002476static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2477 bool needs_lock)
2478{
2479 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002480 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002481
2482 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002483 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002484 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2485 if (IS_ERR(kbuf))
2486 return kbuf;
2487 req->rw.addr = (u64) (unsigned long) kbuf;
2488 req->flags |= REQ_F_BUFFER_SELECTED;
2489 return u64_to_user_ptr(kbuf->addr);
2490}
2491
2492#ifdef CONFIG_COMPAT
2493static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2494 bool needs_lock)
2495{
2496 struct compat_iovec __user *uiov;
2497 compat_ssize_t clen;
2498 void __user *buf;
2499 ssize_t len;
2500
2501 uiov = u64_to_user_ptr(req->rw.addr);
2502 if (!access_ok(uiov, sizeof(*uiov)))
2503 return -EFAULT;
2504 if (__get_user(clen, &uiov->iov_len))
2505 return -EFAULT;
2506 if (clen < 0)
2507 return -EINVAL;
2508
2509 len = clen;
2510 buf = io_rw_buffer_select(req, &len, needs_lock);
2511 if (IS_ERR(buf))
2512 return PTR_ERR(buf);
2513 iov[0].iov_base = buf;
2514 iov[0].iov_len = (compat_size_t) len;
2515 return 0;
2516}
2517#endif
2518
2519static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2520 bool needs_lock)
2521{
2522 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2523 void __user *buf;
2524 ssize_t len;
2525
2526 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2527 return -EFAULT;
2528
2529 len = iov[0].iov_len;
2530 if (len < 0)
2531 return -EINVAL;
2532 buf = io_rw_buffer_select(req, &len, needs_lock);
2533 if (IS_ERR(buf))
2534 return PTR_ERR(buf);
2535 iov[0].iov_base = buf;
2536 iov[0].iov_len = len;
2537 return 0;
2538}
2539
2540static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2541 bool needs_lock)
2542{
Jens Axboedddb3e22020-06-04 11:27:01 -06002543 if (req->flags & REQ_F_BUFFER_SELECTED) {
2544 struct io_buffer *kbuf;
2545
2546 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2547 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2548 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002549 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002550 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002551 if (!req->rw.len)
2552 return 0;
2553 else if (req->rw.len > 1)
2554 return -EINVAL;
2555
2556#ifdef CONFIG_COMPAT
2557 if (req->ctx->compat)
2558 return io_compat_import(req, iov, needs_lock);
2559#endif
2560
2561 return __io_iov_buffer_select(req, iov, needs_lock);
2562}
2563
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002564static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002565 struct iovec **iovec, struct iov_iter *iter,
2566 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002567{
Jens Axboe9adbd452019-12-20 08:45:55 -07002568 void __user *buf = u64_to_user_ptr(req->rw.addr);
2569 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002570 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002571 u8 opcode;
2572
Jens Axboed625c6e2019-12-17 19:53:05 -07002573 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002574 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002575 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002576 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002577 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002578
Jens Axboebcda7ba2020-02-23 16:42:51 -07002579 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002580 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002581 return -EINVAL;
2582
Jens Axboe3a6820f2019-12-22 15:19:35 -07002583 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002584 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002585 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2586 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002587 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002588 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002589 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002590 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002591 }
2592
Jens Axboe3a6820f2019-12-22 15:19:35 -07002593 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2594 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002595 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002596 }
2597
Jens Axboef67676d2019-12-02 11:03:47 -07002598 if (req->io) {
2599 struct io_async_rw *iorw = &req->io->rw;
2600
2601 *iovec = iorw->iov;
2602 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2603 if (iorw->iov == iorw->fast_iov)
2604 *iovec = NULL;
2605 return iorw->size;
2606 }
2607
Jens Axboe4d954c22020-02-27 07:31:19 -07002608 if (req->flags & REQ_F_BUFFER_SELECT) {
2609 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002610 if (!ret) {
2611 ret = (*iovec)->iov_len;
2612 iov_iter_init(iter, rw, *iovec, 1, ret);
2613 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002614 *iovec = NULL;
2615 return ret;
2616 }
2617
Jens Axboe2b188cc2019-01-07 10:46:33 -07002618#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002619 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002620 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2621 iovec, iter);
2622#endif
2623
2624 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2625}
2626
Jens Axboe32960612019-09-23 11:05:34 -06002627/*
2628 * For files that don't have ->read_iter() and ->write_iter(), handle them
2629 * by looping over ->read() or ->write() manually.
2630 */
2631static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2632 struct iov_iter *iter)
2633{
2634 ssize_t ret = 0;
2635
2636 /*
2637 * Don't support polled IO through this interface, and we can't
2638 * support non-blocking either. For the latter, this just causes
2639 * the kiocb to be handled from an async context.
2640 */
2641 if (kiocb->ki_flags & IOCB_HIPRI)
2642 return -EOPNOTSUPP;
2643 if (kiocb->ki_flags & IOCB_NOWAIT)
2644 return -EAGAIN;
2645
2646 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002647 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002648 ssize_t nr;
2649
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002650 if (!iov_iter_is_bvec(iter)) {
2651 iovec = iov_iter_iovec(iter);
2652 } else {
2653 /* fixed buffers import bvec */
2654 iovec.iov_base = kmap(iter->bvec->bv_page)
2655 + iter->iov_offset;
2656 iovec.iov_len = min(iter->count,
2657 iter->bvec->bv_len - iter->iov_offset);
2658 }
2659
Jens Axboe32960612019-09-23 11:05:34 -06002660 if (rw == READ) {
2661 nr = file->f_op->read(file, iovec.iov_base,
2662 iovec.iov_len, &kiocb->ki_pos);
2663 } else {
2664 nr = file->f_op->write(file, iovec.iov_base,
2665 iovec.iov_len, &kiocb->ki_pos);
2666 }
2667
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002668 if (iov_iter_is_bvec(iter))
2669 kunmap(iter->bvec->bv_page);
2670
Jens Axboe32960612019-09-23 11:05:34 -06002671 if (nr < 0) {
2672 if (!ret)
2673 ret = nr;
2674 break;
2675 }
2676 ret += nr;
2677 if (nr != iovec.iov_len)
2678 break;
2679 iov_iter_advance(iter, nr);
2680 }
2681
2682 return ret;
2683}
2684
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002685static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002686 struct iovec *iovec, struct iovec *fast_iov,
2687 struct iov_iter *iter)
2688{
2689 req->io->rw.nr_segs = iter->nr_segs;
2690 req->io->rw.size = io_size;
2691 req->io->rw.iov = iovec;
2692 if (!req->io->rw.iov) {
2693 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002694 if (req->io->rw.iov != fast_iov)
2695 memcpy(req->io->rw.iov, fast_iov,
2696 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002697 } else {
2698 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002699 }
2700}
2701
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002702static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2703{
2704 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2705 return req->io == NULL;
2706}
2707
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002708static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002709{
Jens Axboed3656342019-12-18 09:50:26 -07002710 if (!io_op_defs[req->opcode].async_ctx)
2711 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002712
2713 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002714}
2715
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002716static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2717 struct iovec *iovec, struct iovec *fast_iov,
2718 struct iov_iter *iter)
2719{
Jens Axboe980ad262020-01-24 23:08:54 -07002720 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002721 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002722 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002723 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002724 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002725
Jens Axboe5d204bc2020-01-31 12:06:52 -07002726 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2727 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002728 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002729}
2730
Jens Axboe3529d8c2019-12-19 18:24:38 -07002731static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2732 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002733{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002734 struct io_async_ctx *io;
2735 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002736 ssize_t ret;
2737
Jens Axboe3529d8c2019-12-19 18:24:38 -07002738 ret = io_prep_rw(req, sqe, force_nonblock);
2739 if (ret)
2740 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002741
Jens Axboe3529d8c2019-12-19 18:24:38 -07002742 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2743 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002744
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002745 /* either don't need iovec imported or already have it */
2746 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002747 return 0;
2748
2749 io = req->io;
2750 io->rw.iov = io->rw.fast_iov;
2751 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002752 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002753 req->io = io;
2754 if (ret < 0)
2755 return ret;
2756
2757 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2758 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002759}
2760
Jens Axboebcf5a062020-05-22 09:24:42 -06002761static void __io_async_buf_error(struct io_kiocb *req, int error)
2762{
2763 struct io_ring_ctx *ctx = req->ctx;
2764
2765 spin_lock_irq(&ctx->completion_lock);
2766 io_cqring_fill_event(req, error);
2767 io_commit_cqring(ctx);
2768 spin_unlock_irq(&ctx->completion_lock);
2769
2770 io_cqring_ev_posted(ctx);
2771 req_set_fail_links(req);
2772 io_double_put_req(req);
2773}
2774
2775static void io_async_buf_cancel(struct callback_head *cb)
2776{
2777 struct io_async_rw *rw;
2778 struct io_kiocb *req;
2779
2780 rw = container_of(cb, struct io_async_rw, task_work);
2781 req = rw->wpq.wait.private;
2782 __io_async_buf_error(req, -ECANCELED);
2783}
2784
2785static void io_async_buf_retry(struct callback_head *cb)
2786{
2787 struct io_async_rw *rw;
2788 struct io_ring_ctx *ctx;
2789 struct io_kiocb *req;
2790
2791 rw = container_of(cb, struct io_async_rw, task_work);
2792 req = rw->wpq.wait.private;
2793 ctx = req->ctx;
2794
2795 __set_current_state(TASK_RUNNING);
Pavel Begunkovd3cac642020-06-25 12:38:13 +03002796 if (!__io_sq_thread_acquire_mm(ctx)) {
Jens Axboebcf5a062020-05-22 09:24:42 -06002797 mutex_lock(&ctx->uring_lock);
2798 __io_queue_sqe(req, NULL);
2799 mutex_unlock(&ctx->uring_lock);
2800 } else {
2801 __io_async_buf_error(req, -EFAULT);
2802 }
2803}
2804
2805static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2806 int sync, void *arg)
2807{
2808 struct wait_page_queue *wpq;
2809 struct io_kiocb *req = wait->private;
2810 struct io_async_rw *rw = &req->io->rw;
2811 struct wait_page_key *key = arg;
2812 struct task_struct *tsk;
2813 int ret;
2814
2815 wpq = container_of(wait, struct wait_page_queue, wait);
2816
2817 ret = wake_page_match(wpq, key);
2818 if (ret != 1)
2819 return ret;
2820
2821 list_del_init(&wait->entry);
2822
2823 init_task_work(&rw->task_work, io_async_buf_retry);
2824 /* submit ref gets dropped, acquire a new one */
2825 refcount_inc(&req->refs);
2826 tsk = req->task;
2827 ret = task_work_add(tsk, &rw->task_work, true);
2828 if (unlikely(ret)) {
2829 /* queue just for cancelation */
2830 init_task_work(&rw->task_work, io_async_buf_cancel);
2831 tsk = io_wq_get_task(req->ctx->io_wq);
2832 task_work_add(tsk, &rw->task_work, true);
2833 }
2834 wake_up_process(tsk);
2835 return 1;
2836}
2837
2838static bool io_rw_should_retry(struct io_kiocb *req)
2839{
2840 struct kiocb *kiocb = &req->rw.kiocb;
2841 int ret;
2842
2843 /* never retry for NOWAIT, we just complete with -EAGAIN */
2844 if (req->flags & REQ_F_NOWAIT)
2845 return false;
2846
2847 /* already tried, or we're doing O_DIRECT */
2848 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
2849 return false;
2850 /*
2851 * just use poll if we can, and don't attempt if the fs doesn't
2852 * support callback based unlocks
2853 */
2854 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
2855 return false;
2856
2857 /*
2858 * If request type doesn't require req->io to defer in general,
2859 * we need to allocate it here
2860 */
2861 if (!req->io && __io_alloc_async_ctx(req))
2862 return false;
2863
2864 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
2865 io_async_buf_func, req);
2866 if (!ret) {
2867 io_get_req_task(req);
2868 return true;
2869 }
2870
2871 return false;
2872}
2873
2874static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
2875{
2876 if (req->file->f_op->read_iter)
2877 return call_read_iter(req->file, &req->rw.kiocb, iter);
2878 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
2879}
2880
Pavel Begunkov014db002020-03-03 21:33:12 +03002881static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882{
2883 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002884 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002885 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002886 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002887 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002888
Jens Axboebcda7ba2020-02-23 16:42:51 -07002889 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002890 if (ret < 0)
2891 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002892
Jens Axboefd6c2e42019-12-18 12:19:41 -07002893 /* Ensure we clear previously set non-block flag */
2894 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002895 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002896
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002897 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002898 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002899 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002900 req->result = io_size;
2901
Pavel Begunkov24c74672020-06-21 13:09:51 +03002902 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06002903 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002904 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002905
Jens Axboe31b51512019-01-18 22:56:34 -07002906 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002907 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002908 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06002909 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06002910 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002911
Jens Axboebcf5a062020-05-22 09:24:42 -06002912 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002913
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002914 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06002915 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002916 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002917 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002918 iter.count = iov_count;
2919 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07002920copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002921 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002922 inline_vecs, &iter);
2923 if (ret)
2924 goto out_free;
Jens Axboebcf5a062020-05-22 09:24:42 -06002925 /* if we can retry, do so with the callbacks armed */
2926 if (io_rw_should_retry(req)) {
2927 ret2 = io_iter_do_read(req, &iter);
2928 if (ret2 == -EIOCBQUEUED) {
2929 goto out_free;
2930 } else if (ret2 != -EAGAIN) {
2931 kiocb_done(kiocb, ret2);
2932 goto out_free;
2933 }
2934 }
2935 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07002936 return -EAGAIN;
2937 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002938 }
Jens Axboef67676d2019-12-02 11:03:47 -07002939out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08002940 if (!(req->flags & REQ_F_NEED_CLEANUP))
2941 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002942 return ret;
2943}
2944
Jens Axboe3529d8c2019-12-19 18:24:38 -07002945static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2946 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002947{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002948 struct io_async_ctx *io;
2949 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002950 ssize_t ret;
2951
Jens Axboe3529d8c2019-12-19 18:24:38 -07002952 ret = io_prep_rw(req, sqe, force_nonblock);
2953 if (ret)
2954 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002955
Jens Axboe3529d8c2019-12-19 18:24:38 -07002956 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2957 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002958
Jens Axboe4ed734b2020-03-20 11:23:41 -06002959 req->fsize = rlimit(RLIMIT_FSIZE);
2960
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002961 /* either don't need iovec imported or already have it */
2962 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002963 return 0;
2964
2965 io = req->io;
2966 io->rw.iov = io->rw.fast_iov;
2967 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002968 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002969 req->io = io;
2970 if (ret < 0)
2971 return ret;
2972
2973 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2974 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002975}
2976
Pavel Begunkov014db002020-03-03 21:33:12 +03002977static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002978{
2979 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002980 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002981 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002982 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002983 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002984
Jens Axboebcda7ba2020-02-23 16:42:51 -07002985 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002986 if (ret < 0)
2987 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002988
Jens Axboefd6c2e42019-12-18 12:19:41 -07002989 /* Ensure we clear previously set non-block flag */
2990 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002991 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002992
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002993 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002994 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002995 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002996 req->result = io_size;
2997
Pavel Begunkov24c74672020-06-21 13:09:51 +03002998 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06002999 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003000 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003001
Jens Axboe10d59342019-12-09 20:16:22 -07003002 /* file path doesn't support NOWAIT for non-direct_IO */
3003 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3004 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003005 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003006
Jens Axboe31b51512019-01-18 22:56:34 -07003007 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003008 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003009 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003010 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003011 ssize_t ret2;
3012
Jens Axboe2b188cc2019-01-07 10:46:33 -07003013 /*
3014 * Open-code file_start_write here to grab freeze protection,
3015 * which will be released by another thread in
3016 * io_complete_rw(). Fool lockdep by telling it the lock got
3017 * released so that it doesn't complain about the held lock when
3018 * we return to userspace.
3019 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003020 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003021 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003022 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003023 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003024 SB_FREEZE_WRITE);
3025 }
3026 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003027
Jens Axboe4ed734b2020-03-20 11:23:41 -06003028 if (!force_nonblock)
3029 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3030
Jens Axboe9adbd452019-12-20 08:45:55 -07003031 if (req->file->f_op->write_iter)
3032 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003033 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003034 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003035
3036 if (!force_nonblock)
3037 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3038
Jens Axboefaac9962020-02-07 15:45:22 -07003039 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003040 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003041 * retry them without IOCB_NOWAIT.
3042 */
3043 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3044 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003045 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03003046 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07003047 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003048 iter.count = iov_count;
3049 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003050copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003051 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003052 inline_vecs, &iter);
3053 if (ret)
3054 goto out_free;
3055 return -EAGAIN;
3056 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003057 }
Jens Axboe31b51512019-01-18 22:56:34 -07003058out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003059 if (!(req->flags & REQ_F_NEED_CLEANUP))
3060 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003061 return ret;
3062}
3063
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003064static int __io_splice_prep(struct io_kiocb *req,
3065 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003066{
3067 struct io_splice* sp = &req->splice;
3068 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3069 int ret;
3070
3071 if (req->flags & REQ_F_NEED_CLEANUP)
3072 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003073 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3074 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003075
3076 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003077 sp->len = READ_ONCE(sqe->len);
3078 sp->flags = READ_ONCE(sqe->splice_flags);
3079
3080 if (unlikely(sp->flags & ~valid_flags))
3081 return -EINVAL;
3082
3083 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3084 (sp->flags & SPLICE_F_FD_IN_FIXED));
3085 if (ret)
3086 return ret;
3087 req->flags |= REQ_F_NEED_CLEANUP;
3088
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003089 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3090 /*
3091 * Splice operation will be punted aync, and here need to
3092 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3093 */
3094 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003095 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003096 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003097
3098 return 0;
3099}
3100
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003101static int io_tee_prep(struct io_kiocb *req,
3102 const struct io_uring_sqe *sqe)
3103{
3104 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3105 return -EINVAL;
3106 return __io_splice_prep(req, sqe);
3107}
3108
3109static int io_tee(struct io_kiocb *req, bool force_nonblock)
3110{
3111 struct io_splice *sp = &req->splice;
3112 struct file *in = sp->file_in;
3113 struct file *out = sp->file_out;
3114 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3115 long ret = 0;
3116
3117 if (force_nonblock)
3118 return -EAGAIN;
3119 if (sp->len)
3120 ret = do_tee(in, out, sp->len, flags);
3121
3122 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3123 req->flags &= ~REQ_F_NEED_CLEANUP;
3124
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003125 if (ret != sp->len)
3126 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003127 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003128 return 0;
3129}
3130
3131static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3132{
3133 struct io_splice* sp = &req->splice;
3134
3135 sp->off_in = READ_ONCE(sqe->splice_off_in);
3136 sp->off_out = READ_ONCE(sqe->off);
3137 return __io_splice_prep(req, sqe);
3138}
3139
Pavel Begunkov014db002020-03-03 21:33:12 +03003140static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003141{
3142 struct io_splice *sp = &req->splice;
3143 struct file *in = sp->file_in;
3144 struct file *out = sp->file_out;
3145 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3146 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003147 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003148
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003149 if (force_nonblock)
3150 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003151
3152 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3153 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003154
Jens Axboe948a7742020-05-17 14:21:38 -06003155 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003156 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003157
3158 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3159 req->flags &= ~REQ_F_NEED_CLEANUP;
3160
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003161 if (ret != sp->len)
3162 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003163 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003164 return 0;
3165}
3166
Jens Axboe2b188cc2019-01-07 10:46:33 -07003167/*
3168 * IORING_OP_NOP just posts a completion event, nothing else.
3169 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07003170static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003171{
3172 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003173
Jens Axboedef596e2019-01-09 08:59:42 -07003174 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3175 return -EINVAL;
3176
Jens Axboee1e16092020-06-22 09:17:17 -06003177 io_req_complete(req, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003178 return 0;
3179}
3180
Jens Axboe3529d8c2019-12-19 18:24:38 -07003181static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003182{
Jens Axboe6b063142019-01-10 22:13:58 -07003183 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003184
Jens Axboe09bb8392019-03-13 12:39:28 -06003185 if (!req->file)
3186 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003187
Jens Axboe6b063142019-01-10 22:13:58 -07003188 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003189 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003190 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003191 return -EINVAL;
3192
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003193 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3194 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3195 return -EINVAL;
3196
3197 req->sync.off = READ_ONCE(sqe->off);
3198 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003199 return 0;
3200}
3201
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003202static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003203{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003204 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003205 int ret;
3206
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003207 /* fsync always requires a blocking context */
3208 if (force_nonblock)
3209 return -EAGAIN;
3210
Jens Axboe9adbd452019-12-20 08:45:55 -07003211 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003212 end > 0 ? end : LLONG_MAX,
3213 req->sync.flags & IORING_FSYNC_DATASYNC);
3214 if (ret < 0)
3215 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003216 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003217 return 0;
3218}
3219
Jens Axboed63d1b52019-12-10 10:38:56 -07003220static int io_fallocate_prep(struct io_kiocb *req,
3221 const struct io_uring_sqe *sqe)
3222{
3223 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3224 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003225 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3226 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003227
3228 req->sync.off = READ_ONCE(sqe->off);
3229 req->sync.len = READ_ONCE(sqe->addr);
3230 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003231 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07003232 return 0;
3233}
3234
Pavel Begunkov014db002020-03-03 21:33:12 +03003235static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003236{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003237 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003238
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003239 /* fallocate always requiring blocking context */
3240 if (force_nonblock)
3241 return -EAGAIN;
3242
3243 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3244 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3245 req->sync.len);
3246 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3247 if (ret < 0)
3248 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003249 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003250 return 0;
3251}
3252
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003253static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003254{
Jens Axboef8748882020-01-08 17:47:02 -07003255 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003256 int ret;
3257
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003258 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003259 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003260 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003261 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003262 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003263 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003264
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003265 /* open.how should be already initialised */
3266 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003267 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003268
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003269 req->open.dfd = READ_ONCE(sqe->fd);
3270 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003271 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003272 if (IS_ERR(req->open.filename)) {
3273 ret = PTR_ERR(req->open.filename);
3274 req->open.filename = NULL;
3275 return ret;
3276 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003277 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003278 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003279 return 0;
3280}
3281
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003282static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3283{
3284 u64 flags, mode;
3285
3286 if (req->flags & REQ_F_NEED_CLEANUP)
3287 return 0;
3288 mode = READ_ONCE(sqe->len);
3289 flags = READ_ONCE(sqe->open_flags);
3290 req->open.how = build_open_how(flags, mode);
3291 return __io_openat_prep(req, sqe);
3292}
3293
Jens Axboecebdb982020-01-08 17:59:24 -07003294static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3295{
3296 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003297 size_t len;
3298 int ret;
3299
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003300 if (req->flags & REQ_F_NEED_CLEANUP)
3301 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003302 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3303 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003304 if (len < OPEN_HOW_SIZE_VER0)
3305 return -EINVAL;
3306
3307 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3308 len);
3309 if (ret)
3310 return ret;
3311
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003312 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003313}
3314
Pavel Begunkov014db002020-03-03 21:33:12 +03003315static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003316{
3317 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003318 struct file *file;
3319 int ret;
3320
Jens Axboef86cd202020-01-29 13:46:44 -07003321 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003322 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003323
Jens Axboecebdb982020-01-08 17:59:24 -07003324 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003325 if (ret)
3326 goto err;
3327
Jens Axboe4022e7a2020-03-19 19:23:18 -06003328 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003329 if (ret < 0)
3330 goto err;
3331
3332 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3333 if (IS_ERR(file)) {
3334 put_unused_fd(ret);
3335 ret = PTR_ERR(file);
3336 } else {
3337 fsnotify_open(file);
3338 fd_install(ret, file);
3339 }
3340err:
3341 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003342 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003343 if (ret < 0)
3344 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003345 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003346 return 0;
3347}
3348
Pavel Begunkov014db002020-03-03 21:33:12 +03003349static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003350{
Pavel Begunkov014db002020-03-03 21:33:12 +03003351 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003352}
3353
Jens Axboe067524e2020-03-02 16:32:28 -07003354static int io_remove_buffers_prep(struct io_kiocb *req,
3355 const struct io_uring_sqe *sqe)
3356{
3357 struct io_provide_buf *p = &req->pbuf;
3358 u64 tmp;
3359
3360 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3361 return -EINVAL;
3362
3363 tmp = READ_ONCE(sqe->fd);
3364 if (!tmp || tmp > USHRT_MAX)
3365 return -EINVAL;
3366
3367 memset(p, 0, sizeof(*p));
3368 p->nbufs = tmp;
3369 p->bgid = READ_ONCE(sqe->buf_group);
3370 return 0;
3371}
3372
3373static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3374 int bgid, unsigned nbufs)
3375{
3376 unsigned i = 0;
3377
3378 /* shouldn't happen */
3379 if (!nbufs)
3380 return 0;
3381
3382 /* the head kbuf is the list itself */
3383 while (!list_empty(&buf->list)) {
3384 struct io_buffer *nxt;
3385
3386 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3387 list_del(&nxt->list);
3388 kfree(nxt);
3389 if (++i == nbufs)
3390 return i;
3391 }
3392 i++;
3393 kfree(buf);
3394 idr_remove(&ctx->io_buffer_idr, bgid);
3395
3396 return i;
3397}
3398
3399static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3400{
3401 struct io_provide_buf *p = &req->pbuf;
3402 struct io_ring_ctx *ctx = req->ctx;
3403 struct io_buffer *head;
3404 int ret = 0;
3405
3406 io_ring_submit_lock(ctx, !force_nonblock);
3407
3408 lockdep_assert_held(&ctx->uring_lock);
3409
3410 ret = -ENOENT;
3411 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3412 if (head)
3413 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3414
3415 io_ring_submit_lock(ctx, !force_nonblock);
3416 if (ret < 0)
3417 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003418 io_req_complete(req, ret);
Jens Axboe067524e2020-03-02 16:32:28 -07003419 return 0;
3420}
3421
Jens Axboeddf0322d2020-02-23 16:41:33 -07003422static int io_provide_buffers_prep(struct io_kiocb *req,
3423 const struct io_uring_sqe *sqe)
3424{
3425 struct io_provide_buf *p = &req->pbuf;
3426 u64 tmp;
3427
3428 if (sqe->ioprio || sqe->rw_flags)
3429 return -EINVAL;
3430
3431 tmp = READ_ONCE(sqe->fd);
3432 if (!tmp || tmp > USHRT_MAX)
3433 return -E2BIG;
3434 p->nbufs = tmp;
3435 p->addr = READ_ONCE(sqe->addr);
3436 p->len = READ_ONCE(sqe->len);
3437
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003438 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003439 return -EFAULT;
3440
3441 p->bgid = READ_ONCE(sqe->buf_group);
3442 tmp = READ_ONCE(sqe->off);
3443 if (tmp > USHRT_MAX)
3444 return -E2BIG;
3445 p->bid = tmp;
3446 return 0;
3447}
3448
3449static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3450{
3451 struct io_buffer *buf;
3452 u64 addr = pbuf->addr;
3453 int i, bid = pbuf->bid;
3454
3455 for (i = 0; i < pbuf->nbufs; i++) {
3456 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3457 if (!buf)
3458 break;
3459
3460 buf->addr = addr;
3461 buf->len = pbuf->len;
3462 buf->bid = bid;
3463 addr += pbuf->len;
3464 bid++;
3465 if (!*head) {
3466 INIT_LIST_HEAD(&buf->list);
3467 *head = buf;
3468 } else {
3469 list_add_tail(&buf->list, &(*head)->list);
3470 }
3471 }
3472
3473 return i ? i : -ENOMEM;
3474}
3475
Jens Axboeddf0322d2020-02-23 16:41:33 -07003476static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3477{
3478 struct io_provide_buf *p = &req->pbuf;
3479 struct io_ring_ctx *ctx = req->ctx;
3480 struct io_buffer *head, *list;
3481 int ret = 0;
3482
3483 io_ring_submit_lock(ctx, !force_nonblock);
3484
3485 lockdep_assert_held(&ctx->uring_lock);
3486
3487 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3488
3489 ret = io_add_buffers(p, &head);
3490 if (ret < 0)
3491 goto out;
3492
3493 if (!list) {
3494 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3495 GFP_KERNEL);
3496 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003497 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003498 goto out;
3499 }
3500 }
3501out:
3502 io_ring_submit_unlock(ctx, !force_nonblock);
3503 if (ret < 0)
3504 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003505 io_req_complete(req, ret);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003506 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003507}
3508
Jens Axboe3e4827b2020-01-08 15:18:09 -07003509static int io_epoll_ctl_prep(struct io_kiocb *req,
3510 const struct io_uring_sqe *sqe)
3511{
3512#if defined(CONFIG_EPOLL)
3513 if (sqe->ioprio || sqe->buf_index)
3514 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003515 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3516 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003517
3518 req->epoll.epfd = READ_ONCE(sqe->fd);
3519 req->epoll.op = READ_ONCE(sqe->len);
3520 req->epoll.fd = READ_ONCE(sqe->off);
3521
3522 if (ep_op_has_event(req->epoll.op)) {
3523 struct epoll_event __user *ev;
3524
3525 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3526 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3527 return -EFAULT;
3528 }
3529
3530 return 0;
3531#else
3532 return -EOPNOTSUPP;
3533#endif
3534}
3535
Pavel Begunkov014db002020-03-03 21:33:12 +03003536static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003537{
3538#if defined(CONFIG_EPOLL)
3539 struct io_epoll *ie = &req->epoll;
3540 int ret;
3541
3542 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3543 if (force_nonblock && ret == -EAGAIN)
3544 return -EAGAIN;
3545
3546 if (ret < 0)
3547 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003548 io_req_complete(req, ret);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003549 return 0;
3550#else
3551 return -EOPNOTSUPP;
3552#endif
3553}
3554
Jens Axboec1ca7572019-12-25 22:18:28 -07003555static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3556{
3557#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3558 if (sqe->ioprio || sqe->buf_index || sqe->off)
3559 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003560 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3561 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003562
3563 req->madvise.addr = READ_ONCE(sqe->addr);
3564 req->madvise.len = READ_ONCE(sqe->len);
3565 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3566 return 0;
3567#else
3568 return -EOPNOTSUPP;
3569#endif
3570}
3571
Pavel Begunkov014db002020-03-03 21:33:12 +03003572static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003573{
3574#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3575 struct io_madvise *ma = &req->madvise;
3576 int ret;
3577
3578 if (force_nonblock)
3579 return -EAGAIN;
3580
3581 ret = do_madvise(ma->addr, ma->len, ma->advice);
3582 if (ret < 0)
3583 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003584 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003585 return 0;
3586#else
3587 return -EOPNOTSUPP;
3588#endif
3589}
3590
Jens Axboe4840e412019-12-25 22:03:45 -07003591static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3592{
3593 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3594 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003595 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3596 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003597
3598 req->fadvise.offset = READ_ONCE(sqe->off);
3599 req->fadvise.len = READ_ONCE(sqe->len);
3600 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3601 return 0;
3602}
3603
Pavel Begunkov014db002020-03-03 21:33:12 +03003604static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003605{
3606 struct io_fadvise *fa = &req->fadvise;
3607 int ret;
3608
Jens Axboe3e694262020-02-01 09:22:49 -07003609 if (force_nonblock) {
3610 switch (fa->advice) {
3611 case POSIX_FADV_NORMAL:
3612 case POSIX_FADV_RANDOM:
3613 case POSIX_FADV_SEQUENTIAL:
3614 break;
3615 default:
3616 return -EAGAIN;
3617 }
3618 }
Jens Axboe4840e412019-12-25 22:03:45 -07003619
3620 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3621 if (ret < 0)
3622 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003623 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003624 return 0;
3625}
3626
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003627static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3628{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003629 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3630 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003631 if (sqe->ioprio || sqe->buf_index)
3632 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003633 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003634 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003635
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003636 req->statx.dfd = READ_ONCE(sqe->fd);
3637 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003638 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003639 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3640 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003641
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003642 return 0;
3643}
3644
Pavel Begunkov014db002020-03-03 21:33:12 +03003645static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003646{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003647 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003648 int ret;
3649
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003650 if (force_nonblock) {
3651 /* only need file table for an actual valid fd */
3652 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3653 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003654 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003655 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003656
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003657 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3658 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003659
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003660 if (ret < 0)
3661 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003662 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003663 return 0;
3664}
3665
Jens Axboeb5dba592019-12-11 14:02:38 -07003666static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3667{
3668 /*
3669 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003670 * leave the 'file' in an undeterminate state, and here need to modify
3671 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003672 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003673 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003674 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3675
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003676 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3677 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003678 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3679 sqe->rw_flags || sqe->buf_index)
3680 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003681 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003682 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003683
3684 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003685 if ((req->file && req->file->f_op == &io_uring_fops) ||
3686 req->close.fd == req->ctx->ring_fd)
3687 return -EBADF;
3688
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003689 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003690 return 0;
3691}
3692
Pavel Begunkov014db002020-03-03 21:33:12 +03003693static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003694{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003695 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003696 int ret;
3697
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003698 /* might be already done during nonblock submission */
3699 if (!close->put_file) {
3700 ret = __close_fd_get_file(close->fd, &close->put_file);
3701 if (ret < 0)
3702 return (ret == -ENOENT) ? -EBADF : ret;
3703 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003704
3705 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003706 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003707 /* was never set, but play safe */
3708 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003709 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003710 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003711 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003712 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003713
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003714 /* No ->flush() or already async, safely close from here */
3715 ret = filp_close(close->put_file, req->work.files);
3716 if (ret < 0)
3717 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003718 fput(close->put_file);
3719 close->put_file = NULL;
Jens Axboee1e16092020-06-22 09:17:17 -06003720 io_req_complete(req, ret);
Jens Axboe1a417f42020-01-31 17:16:48 -07003721 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003722}
3723
Jens Axboe3529d8c2019-12-19 18:24:38 -07003724static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003725{
3726 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003727
3728 if (!req->file)
3729 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003730
3731 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3732 return -EINVAL;
3733 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3734 return -EINVAL;
3735
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003736 req->sync.off = READ_ONCE(sqe->off);
3737 req->sync.len = READ_ONCE(sqe->len);
3738 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003739 return 0;
3740}
3741
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003742static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003743{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003744 int ret;
3745
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003746 /* sync_file_range always requires a blocking context */
3747 if (force_nonblock)
3748 return -EAGAIN;
3749
Jens Axboe9adbd452019-12-20 08:45:55 -07003750 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003751 req->sync.flags);
3752 if (ret < 0)
3753 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003754 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003755 return 0;
3756}
3757
YueHaibing469956e2020-03-04 15:53:52 +08003758#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003759static int io_setup_async_msg(struct io_kiocb *req,
3760 struct io_async_msghdr *kmsg)
3761{
3762 if (req->io)
3763 return -EAGAIN;
3764 if (io_alloc_async_ctx(req)) {
3765 if (kmsg->iov != kmsg->fast_iov)
3766 kfree(kmsg->iov);
3767 return -ENOMEM;
3768 }
3769 req->flags |= REQ_F_NEED_CLEANUP;
3770 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3771 return -EAGAIN;
3772}
3773
Jens Axboe3529d8c2019-12-19 18:24:38 -07003774static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003775{
Jens Axboee47293f2019-12-20 08:58:21 -07003776 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003777 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003778 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003779
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003780 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3781 return -EINVAL;
3782
Jens Axboee47293f2019-12-20 08:58:21 -07003783 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3784 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003785 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003786
Jens Axboed8768362020-02-27 14:17:49 -07003787#ifdef CONFIG_COMPAT
3788 if (req->ctx->compat)
3789 sr->msg_flags |= MSG_CMSG_COMPAT;
3790#endif
3791
Jens Axboefddafac2020-01-04 20:19:44 -07003792 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003793 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003794 /* iovec is already imported */
3795 if (req->flags & REQ_F_NEED_CLEANUP)
3796 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003797
Jens Axboed9688562019-12-09 19:35:20 -07003798 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003799 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003800 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003801 if (!ret)
3802 req->flags |= REQ_F_NEED_CLEANUP;
3803 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003804}
3805
Pavel Begunkov014db002020-03-03 21:33:12 +03003806static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003807{
Jens Axboe0b416c32019-12-15 10:57:46 -07003808 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003809 struct socket *sock;
3810 int ret;
3811
Jens Axboe03b12302019-12-02 18:50:25 -07003812 sock = sock_from_file(req->file, &ret);
3813 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003814 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003815 unsigned flags;
3816
Jens Axboe03b12302019-12-02 18:50:25 -07003817 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003818 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003819 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003820 /* if iov is set, it's allocated already */
3821 if (!kmsg->iov)
3822 kmsg->iov = kmsg->fast_iov;
3823 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003824 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003825 struct io_sr_msg *sr = &req->sr_msg;
3826
Jens Axboe0b416c32019-12-15 10:57:46 -07003827 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003828 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003829
3830 io.msg.iov = io.msg.fast_iov;
3831 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3832 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003833 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003834 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003835 }
3836
Jens Axboee47293f2019-12-20 08:58:21 -07003837 flags = req->sr_msg.msg_flags;
3838 if (flags & MSG_DONTWAIT)
3839 req->flags |= REQ_F_NOWAIT;
3840 else if (force_nonblock)
3841 flags |= MSG_DONTWAIT;
3842
Jens Axboe0b416c32019-12-15 10:57:46 -07003843 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003844 if (force_nonblock && ret == -EAGAIN)
3845 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003846 if (ret == -ERESTARTSYS)
3847 ret = -EINTR;
3848 }
3849
Pavel Begunkov1e950812020-02-06 19:51:16 +03003850 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003851 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003852 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003853 if (ret < 0)
3854 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003855 io_req_complete(req, ret);
Jens Axboe03b12302019-12-02 18:50:25 -07003856 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003857}
3858
Pavel Begunkov014db002020-03-03 21:33:12 +03003859static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003860{
Jens Axboefddafac2020-01-04 20:19:44 -07003861 struct socket *sock;
3862 int ret;
3863
Jens Axboefddafac2020-01-04 20:19:44 -07003864 sock = sock_from_file(req->file, &ret);
3865 if (sock) {
3866 struct io_sr_msg *sr = &req->sr_msg;
3867 struct msghdr msg;
3868 struct iovec iov;
3869 unsigned flags;
3870
3871 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3872 &msg.msg_iter);
3873 if (ret)
3874 return ret;
3875
3876 msg.msg_name = NULL;
3877 msg.msg_control = NULL;
3878 msg.msg_controllen = 0;
3879 msg.msg_namelen = 0;
3880
3881 flags = req->sr_msg.msg_flags;
3882 if (flags & MSG_DONTWAIT)
3883 req->flags |= REQ_F_NOWAIT;
3884 else if (force_nonblock)
3885 flags |= MSG_DONTWAIT;
3886
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003887 msg.msg_flags = flags;
3888 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003889 if (force_nonblock && ret == -EAGAIN)
3890 return -EAGAIN;
3891 if (ret == -ERESTARTSYS)
3892 ret = -EINTR;
3893 }
3894
Jens Axboefddafac2020-01-04 20:19:44 -07003895 if (ret < 0)
3896 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003897 io_req_complete(req, ret);
Jens Axboefddafac2020-01-04 20:19:44 -07003898 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003899}
3900
Jens Axboe52de1fe2020-02-27 10:15:42 -07003901static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3902{
3903 struct io_sr_msg *sr = &req->sr_msg;
3904 struct iovec __user *uiov;
3905 size_t iov_len;
3906 int ret;
3907
3908 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3909 &uiov, &iov_len);
3910 if (ret)
3911 return ret;
3912
3913 if (req->flags & REQ_F_BUFFER_SELECT) {
3914 if (iov_len > 1)
3915 return -EINVAL;
3916 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3917 return -EFAULT;
3918 sr->len = io->msg.iov[0].iov_len;
3919 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3920 sr->len);
3921 io->msg.iov = NULL;
3922 } else {
3923 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3924 &io->msg.iov, &io->msg.msg.msg_iter);
3925 if (ret > 0)
3926 ret = 0;
3927 }
3928
3929 return ret;
3930}
3931
3932#ifdef CONFIG_COMPAT
3933static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3934 struct io_async_ctx *io)
3935{
3936 struct compat_msghdr __user *msg_compat;
3937 struct io_sr_msg *sr = &req->sr_msg;
3938 struct compat_iovec __user *uiov;
3939 compat_uptr_t ptr;
3940 compat_size_t len;
3941 int ret;
3942
3943 msg_compat = (struct compat_msghdr __user *) sr->msg;
3944 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3945 &ptr, &len);
3946 if (ret)
3947 return ret;
3948
3949 uiov = compat_ptr(ptr);
3950 if (req->flags & REQ_F_BUFFER_SELECT) {
3951 compat_ssize_t clen;
3952
3953 if (len > 1)
3954 return -EINVAL;
3955 if (!access_ok(uiov, sizeof(*uiov)))
3956 return -EFAULT;
3957 if (__get_user(clen, &uiov->iov_len))
3958 return -EFAULT;
3959 if (clen < 0)
3960 return -EINVAL;
3961 sr->len = io->msg.iov[0].iov_len;
3962 io->msg.iov = NULL;
3963 } else {
3964 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3965 &io->msg.iov,
3966 &io->msg.msg.msg_iter);
3967 if (ret < 0)
3968 return ret;
3969 }
3970
3971 return 0;
3972}
Jens Axboe03b12302019-12-02 18:50:25 -07003973#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003974
3975static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3976{
3977 io->msg.iov = io->msg.fast_iov;
3978
3979#ifdef CONFIG_COMPAT
3980 if (req->ctx->compat)
3981 return __io_compat_recvmsg_copy_hdr(req, io);
3982#endif
3983
3984 return __io_recvmsg_copy_hdr(req, io);
3985}
3986
Jens Axboebcda7ba2020-02-23 16:42:51 -07003987static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3988 int *cflags, bool needs_lock)
3989{
3990 struct io_sr_msg *sr = &req->sr_msg;
3991 struct io_buffer *kbuf;
3992
3993 if (!(req->flags & REQ_F_BUFFER_SELECT))
3994 return NULL;
3995
3996 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3997 if (IS_ERR(kbuf))
3998 return kbuf;
3999
4000 sr->kbuf = kbuf;
4001 req->flags |= REQ_F_BUFFER_SELECTED;
4002
4003 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
4004 *cflags |= IORING_CQE_F_BUFFER;
4005 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004006}
4007
Jens Axboe3529d8c2019-12-19 18:24:38 -07004008static int io_recvmsg_prep(struct io_kiocb *req,
4009 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004010{
Jens Axboee47293f2019-12-20 08:58:21 -07004011 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004012 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004013 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004014
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004015 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4016 return -EINVAL;
4017
Jens Axboe3529d8c2019-12-19 18:24:38 -07004018 sr->msg_flags = READ_ONCE(sqe->msg_flags);
4019 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004020 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004021 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004022
Jens Axboed8768362020-02-27 14:17:49 -07004023#ifdef CONFIG_COMPAT
4024 if (req->ctx->compat)
4025 sr->msg_flags |= MSG_CMSG_COMPAT;
4026#endif
4027
Jens Axboefddafac2020-01-04 20:19:44 -07004028 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004029 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004030 /* iovec is already imported */
4031 if (req->flags & REQ_F_NEED_CLEANUP)
4032 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004033
Jens Axboe52de1fe2020-02-27 10:15:42 -07004034 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004035 if (!ret)
4036 req->flags |= REQ_F_NEED_CLEANUP;
4037 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004038}
4039
Pavel Begunkov014db002020-03-03 21:33:12 +03004040static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07004041{
Jens Axboe0b416c32019-12-15 10:57:46 -07004042 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004043 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004044 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004045
Jens Axboe0fa03c62019-04-19 13:34:07 -06004046 sock = sock_from_file(req->file, &ret);
4047 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07004048 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004049 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004050 unsigned flags;
4051
Jens Axboe03b12302019-12-02 18:50:25 -07004052 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07004053 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004054 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07004055 /* if iov is set, it's allocated already */
4056 if (!kmsg->iov)
4057 kmsg->iov = kmsg->fast_iov;
4058 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004059 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07004060 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004061 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004062
Jens Axboe52de1fe2020-02-27 10:15:42 -07004063 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07004064 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004065 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004066 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06004067
Jens Axboe52de1fe2020-02-27 10:15:42 -07004068 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4069 if (IS_ERR(kbuf)) {
4070 return PTR_ERR(kbuf);
4071 } else if (kbuf) {
4072 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4073 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4074 1, req->sr_msg.len);
4075 }
4076
Jens Axboee47293f2019-12-20 08:58:21 -07004077 flags = req->sr_msg.msg_flags;
4078 if (flags & MSG_DONTWAIT)
4079 req->flags |= REQ_F_NOWAIT;
4080 else if (force_nonblock)
4081 flags |= MSG_DONTWAIT;
4082
4083 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
4084 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004085 if (force_nonblock && ret == -EAGAIN)
4086 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07004087 if (ret == -ERESTARTSYS)
4088 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004089 }
4090
Pavel Begunkov1e950812020-02-06 19:51:16 +03004091 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004092 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004093 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004094 if (ret < 0)
4095 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004096 __io_req_complete(req, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004097 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004098}
4099
Pavel Begunkov014db002020-03-03 21:33:12 +03004100static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07004101{
Jens Axboebcda7ba2020-02-23 16:42:51 -07004102 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07004103 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004104 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004105
Jens Axboefddafac2020-01-04 20:19:44 -07004106 sock = sock_from_file(req->file, &ret);
4107 if (sock) {
4108 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004109 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004110 struct msghdr msg;
4111 struct iovec iov;
4112 unsigned flags;
4113
Jens Axboebcda7ba2020-02-23 16:42:51 -07004114 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4115 if (IS_ERR(kbuf))
4116 return PTR_ERR(kbuf);
4117 else if (kbuf)
4118 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004119
Jens Axboebcda7ba2020-02-23 16:42:51 -07004120 ret = import_single_range(READ, buf, sr->len, &iov,
4121 &msg.msg_iter);
4122 if (ret) {
4123 kfree(kbuf);
4124 return ret;
4125 }
4126
4127 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004128 msg.msg_name = NULL;
4129 msg.msg_control = NULL;
4130 msg.msg_controllen = 0;
4131 msg.msg_namelen = 0;
4132 msg.msg_iocb = NULL;
4133 msg.msg_flags = 0;
4134
4135 flags = req->sr_msg.msg_flags;
4136 if (flags & MSG_DONTWAIT)
4137 req->flags |= REQ_F_NOWAIT;
4138 else if (force_nonblock)
4139 flags |= MSG_DONTWAIT;
4140
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004141 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07004142 if (force_nonblock && ret == -EAGAIN)
4143 return -EAGAIN;
4144 if (ret == -ERESTARTSYS)
4145 ret = -EINTR;
4146 }
4147
Jens Axboebcda7ba2020-02-23 16:42:51 -07004148 kfree(kbuf);
4149 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004150 if (ret < 0)
4151 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004152 __io_req_complete(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004153 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004154}
4155
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004157{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004158 struct io_accept *accept = &req->accept;
4159
Jens Axboe17f2fe32019-10-17 14:42:58 -06004160 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4161 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004162 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004163 return -EINVAL;
4164
Jens Axboed55e5f52019-12-11 16:12:15 -07004165 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4166 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004167 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004168 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004169 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004170}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004171
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004172static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004173{
4174 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004175 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004176 int ret;
4177
Jiufei Xuee697dee2020-06-10 13:41:59 +08004178 if (req->file->f_flags & O_NONBLOCK)
4179 req->flags |= REQ_F_NOWAIT;
4180
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004181 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004182 accept->addr_len, accept->flags,
4183 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004184 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004185 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004186 if (ret < 0) {
4187 if (ret == -ERESTARTSYS)
4188 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004189 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004190 }
Jens Axboee1e16092020-06-22 09:17:17 -06004191 io_req_complete(req, ret);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004192 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004193}
4194
Jens Axboe3529d8c2019-12-19 18:24:38 -07004195static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004196{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004197 struct io_connect *conn = &req->connect;
4198 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004199
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004200 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4201 return -EINVAL;
4202 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4203 return -EINVAL;
4204
Jens Axboe3529d8c2019-12-19 18:24:38 -07004205 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4206 conn->addr_len = READ_ONCE(sqe->addr2);
4207
4208 if (!io)
4209 return 0;
4210
4211 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004212 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004213}
4214
Pavel Begunkov014db002020-03-03 21:33:12 +03004215static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004216{
Jens Axboef499a022019-12-02 16:28:46 -07004217 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004218 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004219 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004220
Jens Axboef499a022019-12-02 16:28:46 -07004221 if (req->io) {
4222 io = req->io;
4223 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004224 ret = move_addr_to_kernel(req->connect.addr,
4225 req->connect.addr_len,
4226 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004227 if (ret)
4228 goto out;
4229 io = &__io;
4230 }
4231
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004232 file_flags = force_nonblock ? O_NONBLOCK : 0;
4233
4234 ret = __sys_connect_file(req->file, &io->connect.address,
4235 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004236 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004237 if (req->io)
4238 return -EAGAIN;
4239 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004240 ret = -ENOMEM;
4241 goto out;
4242 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004243 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004244 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004245 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004246 if (ret == -ERESTARTSYS)
4247 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004248out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004249 if (ret < 0)
4250 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004251 io_req_complete(req, ret);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004252 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004253}
YueHaibing469956e2020-03-04 15:53:52 +08004254#else /* !CONFIG_NET */
4255static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4256{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004257 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004258}
4259
YueHaibing469956e2020-03-04 15:53:52 +08004260static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004261{
YueHaibing469956e2020-03-04 15:53:52 +08004262 return -EOPNOTSUPP;
4263}
4264
4265static int io_send(struct io_kiocb *req, bool force_nonblock)
4266{
4267 return -EOPNOTSUPP;
4268}
4269
4270static int io_recvmsg_prep(struct io_kiocb *req,
4271 const struct io_uring_sqe *sqe)
4272{
4273 return -EOPNOTSUPP;
4274}
4275
4276static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4277{
4278 return -EOPNOTSUPP;
4279}
4280
4281static int io_recv(struct io_kiocb *req, bool force_nonblock)
4282{
4283 return -EOPNOTSUPP;
4284}
4285
4286static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4287{
4288 return -EOPNOTSUPP;
4289}
4290
4291static int io_accept(struct io_kiocb *req, bool force_nonblock)
4292{
4293 return -EOPNOTSUPP;
4294}
4295
4296static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4297{
4298 return -EOPNOTSUPP;
4299}
4300
4301static int io_connect(struct io_kiocb *req, bool force_nonblock)
4302{
4303 return -EOPNOTSUPP;
4304}
4305#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306
Jens Axboed7718a92020-02-14 22:23:12 -07004307struct io_poll_table {
4308 struct poll_table_struct pt;
4309 struct io_kiocb *req;
4310 int error;
4311};
4312
Jens Axboed7718a92020-02-14 22:23:12 -07004313static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4314 __poll_t mask, task_work_func_t func)
4315{
4316 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004317 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004318
4319 /* for instances that support it check for an event match first: */
4320 if (mask && !(mask & poll->events))
4321 return 0;
4322
4323 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4324
4325 list_del_init(&poll->wait.entry);
4326
4327 tsk = req->task;
4328 req->result = mask;
4329 init_task_work(&req->task_work, func);
4330 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004331 * If this fails, then the task is exiting. When a task exits, the
4332 * work gets canceled, so just cancel this request as well instead
4333 * of executing it. We can't safely execute it anyway, as we may not
4334 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004335 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004336 ret = task_work_add(tsk, &req->task_work, true);
4337 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004338 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004339 tsk = io_wq_get_task(req->ctx->io_wq);
4340 task_work_add(tsk, &req->task_work, true);
4341 }
Jens Axboed7718a92020-02-14 22:23:12 -07004342 wake_up_process(tsk);
4343 return 1;
4344}
4345
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004346static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4347 __acquires(&req->ctx->completion_lock)
4348{
4349 struct io_ring_ctx *ctx = req->ctx;
4350
4351 if (!req->result && !READ_ONCE(poll->canceled)) {
4352 struct poll_table_struct pt = { ._key = poll->events };
4353
4354 req->result = vfs_poll(req->file, &pt) & poll->events;
4355 }
4356
4357 spin_lock_irq(&ctx->completion_lock);
4358 if (!req->result && !READ_ONCE(poll->canceled)) {
4359 add_wait_queue(poll->head, &poll->wait);
4360 return true;
4361 }
4362
4363 return false;
4364}
4365
Jens Axboe18bceab2020-05-15 11:56:54 -06004366static void io_poll_remove_double(struct io_kiocb *req)
4367{
4368 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4369
4370 lockdep_assert_held(&req->ctx->completion_lock);
4371
4372 if (poll && poll->head) {
4373 struct wait_queue_head *head = poll->head;
4374
4375 spin_lock(&head->lock);
4376 list_del_init(&poll->wait.entry);
4377 if (poll->wait.private)
4378 refcount_dec(&req->refs);
4379 poll->head = NULL;
4380 spin_unlock(&head->lock);
4381 }
4382}
4383
4384static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4385{
4386 struct io_ring_ctx *ctx = req->ctx;
4387
4388 io_poll_remove_double(req);
4389 req->poll.done = true;
4390 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4391 io_commit_cqring(ctx);
4392}
4393
4394static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4395{
4396 struct io_ring_ctx *ctx = req->ctx;
4397
4398 if (io_poll_rewait(req, &req->poll)) {
4399 spin_unlock_irq(&ctx->completion_lock);
4400 return;
4401 }
4402
4403 hash_del(&req->hash_node);
4404 io_poll_complete(req, req->result, 0);
4405 req->flags |= REQ_F_COMP_LOCKED;
4406 io_put_req_find_next(req, nxt);
4407 spin_unlock_irq(&ctx->completion_lock);
4408
4409 io_cqring_ev_posted(ctx);
4410}
4411
4412static void io_poll_task_func(struct callback_head *cb)
4413{
4414 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4415 struct io_kiocb *nxt = NULL;
4416
4417 io_poll_task_handler(req, &nxt);
4418 if (nxt) {
4419 struct io_ring_ctx *ctx = nxt->ctx;
4420
4421 mutex_lock(&ctx->uring_lock);
4422 __io_queue_sqe(nxt, NULL);
4423 mutex_unlock(&ctx->uring_lock);
4424 }
4425}
4426
4427static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4428 int sync, void *key)
4429{
4430 struct io_kiocb *req = wait->private;
4431 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4432 __poll_t mask = key_to_poll(key);
4433
4434 /* for instances that support it check for an event match first: */
4435 if (mask && !(mask & poll->events))
4436 return 0;
4437
4438 if (req->poll.head) {
4439 bool done;
4440
4441 spin_lock(&req->poll.head->lock);
4442 done = list_empty(&req->poll.wait.entry);
4443 if (!done)
4444 list_del_init(&req->poll.wait.entry);
4445 spin_unlock(&req->poll.head->lock);
4446 if (!done)
4447 __io_async_wake(req, poll, mask, io_poll_task_func);
4448 }
4449 refcount_dec(&req->refs);
4450 return 1;
4451}
4452
4453static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4454 wait_queue_func_t wake_func)
4455{
4456 poll->head = NULL;
4457 poll->done = false;
4458 poll->canceled = false;
4459 poll->events = events;
4460 INIT_LIST_HEAD(&poll->wait.entry);
4461 init_waitqueue_func_entry(&poll->wait, wake_func);
4462}
4463
4464static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4465 struct wait_queue_head *head)
4466{
4467 struct io_kiocb *req = pt->req;
4468
4469 /*
4470 * If poll->head is already set, it's because the file being polled
4471 * uses multiple waitqueues for poll handling (eg one for read, one
4472 * for write). Setup a separate io_poll_iocb if this happens.
4473 */
4474 if (unlikely(poll->head)) {
4475 /* already have a 2nd entry, fail a third attempt */
4476 if (req->io) {
4477 pt->error = -EINVAL;
4478 return;
4479 }
4480 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4481 if (!poll) {
4482 pt->error = -ENOMEM;
4483 return;
4484 }
4485 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4486 refcount_inc(&req->refs);
4487 poll->wait.private = req;
4488 req->io = (void *) poll;
4489 }
4490
4491 pt->error = 0;
4492 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004493
4494 if (poll->events & EPOLLEXCLUSIVE)
4495 add_wait_queue_exclusive(head, &poll->wait);
4496 else
4497 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004498}
4499
4500static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4501 struct poll_table_struct *p)
4502{
4503 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4504
4505 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4506}
4507
Jens Axboed7718a92020-02-14 22:23:12 -07004508static void io_async_task_func(struct callback_head *cb)
4509{
4510 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4511 struct async_poll *apoll = req->apoll;
4512 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004513 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004514
4515 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4516
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004517 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004518 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004519 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004520 }
4521
Jens Axboe31067252020-05-17 17:43:31 -06004522 /* If req is still hashed, it cannot have been canceled. Don't check. */
4523 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004524 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004525 } else {
4526 canceled = READ_ONCE(apoll->poll.canceled);
4527 if (canceled) {
4528 io_cqring_fill_event(req, -ECANCELED);
4529 io_commit_cqring(ctx);
4530 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004531 }
4532
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004533 spin_unlock_irq(&ctx->completion_lock);
4534
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004535 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004536 if (req->flags & REQ_F_WORK_INITIALIZED)
4537 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004538 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004539
Jens Axboe31067252020-05-17 17:43:31 -06004540 if (!canceled) {
4541 __set_current_state(TASK_RUNNING);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004542 if (io_sq_thread_acquire_mm(ctx, req)) {
Jens Axboee1e16092020-06-22 09:17:17 -06004543 io_cqring_add_event(req, -EFAULT, 0);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004544 goto end_req;
4545 }
Jens Axboe31067252020-05-17 17:43:31 -06004546 mutex_lock(&ctx->uring_lock);
4547 __io_queue_sqe(req, NULL);
4548 mutex_unlock(&ctx->uring_lock);
4549 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004550 io_cqring_ev_posted(ctx);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004551end_req:
Jens Axboe2bae0472020-04-13 11:16:34 -06004552 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004553 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004554 }
Jens Axboed7718a92020-02-14 22:23:12 -07004555}
4556
4557static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4558 void *key)
4559{
4560 struct io_kiocb *req = wait->private;
4561 struct io_poll_iocb *poll = &req->apoll->poll;
4562
4563 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4564 key_to_poll(key));
4565
4566 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4567}
4568
4569static void io_poll_req_insert(struct io_kiocb *req)
4570{
4571 struct io_ring_ctx *ctx = req->ctx;
4572 struct hlist_head *list;
4573
4574 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4575 hlist_add_head(&req->hash_node, list);
4576}
4577
4578static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4579 struct io_poll_iocb *poll,
4580 struct io_poll_table *ipt, __poll_t mask,
4581 wait_queue_func_t wake_func)
4582 __acquires(&ctx->completion_lock)
4583{
4584 struct io_ring_ctx *ctx = req->ctx;
4585 bool cancel = false;
4586
Jens Axboe18bceab2020-05-15 11:56:54 -06004587 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004588 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004589 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004590
4591 ipt->pt._key = mask;
4592 ipt->req = req;
4593 ipt->error = -EINVAL;
4594
Jens Axboed7718a92020-02-14 22:23:12 -07004595 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4596
4597 spin_lock_irq(&ctx->completion_lock);
4598 if (likely(poll->head)) {
4599 spin_lock(&poll->head->lock);
4600 if (unlikely(list_empty(&poll->wait.entry))) {
4601 if (ipt->error)
4602 cancel = true;
4603 ipt->error = 0;
4604 mask = 0;
4605 }
4606 if (mask || ipt->error)
4607 list_del_init(&poll->wait.entry);
4608 else if (cancel)
4609 WRITE_ONCE(poll->canceled, true);
4610 else if (!poll->done) /* actually waiting for an event */
4611 io_poll_req_insert(req);
4612 spin_unlock(&poll->head->lock);
4613 }
4614
4615 return mask;
4616}
4617
4618static bool io_arm_poll_handler(struct io_kiocb *req)
4619{
4620 const struct io_op_def *def = &io_op_defs[req->opcode];
4621 struct io_ring_ctx *ctx = req->ctx;
4622 struct async_poll *apoll;
4623 struct io_poll_table ipt;
4624 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004625 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004626
4627 if (!req->file || !file_can_poll(req->file))
4628 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004629 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004630 return false;
4631 if (!def->pollin && !def->pollout)
4632 return false;
4633
4634 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4635 if (unlikely(!apoll))
4636 return false;
4637
4638 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004639 if (req->flags & REQ_F_WORK_INITIALIZED)
4640 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004641 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004642
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004643 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004644 req->apoll = apoll;
4645 INIT_HLIST_NODE(&req->hash_node);
4646
Nathan Chancellor8755d972020-03-02 16:01:19 -07004647 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004648 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004649 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004650 if (def->pollout)
4651 mask |= POLLOUT | POLLWRNORM;
4652 mask |= POLLERR | POLLPRI;
4653
4654 ipt.pt._qproc = io_async_queue_proc;
4655
4656 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4657 io_async_wake);
4658 if (ret) {
4659 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004660 /* only remove double add if we did it here */
4661 if (!had_io)
4662 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004663 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004664 if (req->flags & REQ_F_WORK_INITIALIZED)
4665 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004666 kfree(apoll);
4667 return false;
4668 }
4669 spin_unlock_irq(&ctx->completion_lock);
4670 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4671 apoll->poll.events);
4672 return true;
4673}
4674
4675static bool __io_poll_remove_one(struct io_kiocb *req,
4676 struct io_poll_iocb *poll)
4677{
Jens Axboeb41e9852020-02-17 09:52:41 -07004678 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004679
4680 spin_lock(&poll->head->lock);
4681 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004682 if (!list_empty(&poll->wait.entry)) {
4683 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004684 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004685 }
4686 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004687 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004688 return do_complete;
4689}
4690
4691static bool io_poll_remove_one(struct io_kiocb *req)
4692{
4693 bool do_complete;
4694
4695 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004696 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004697 do_complete = __io_poll_remove_one(req, &req->poll);
4698 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004699 struct async_poll *apoll = req->apoll;
4700
Jens Axboed7718a92020-02-14 22:23:12 -07004701 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004702 do_complete = __io_poll_remove_one(req, &apoll->poll);
4703 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004704 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004705 /*
4706 * restore ->work because we will call
4707 * io_req_work_drop_env below when dropping the
4708 * final reference.
4709 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004710 if (req->flags & REQ_F_WORK_INITIALIZED)
4711 memcpy(&req->work, &apoll->work,
4712 sizeof(req->work));
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004713 kfree(apoll);
4714 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004715 }
4716
Jens Axboeb41e9852020-02-17 09:52:41 -07004717 if (do_complete) {
4718 io_cqring_fill_event(req, -ECANCELED);
4719 io_commit_cqring(req->ctx);
4720 req->flags |= REQ_F_COMP_LOCKED;
4721 io_put_req(req);
4722 }
4723
4724 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004725}
4726
4727static void io_poll_remove_all(struct io_ring_ctx *ctx)
4728{
Jens Axboe78076bb2019-12-04 19:56:40 -07004729 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004730 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004731 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004732
4733 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004734 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4735 struct hlist_head *list;
4736
4737 list = &ctx->cancel_hash[i];
4738 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004739 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004740 }
4741 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004742
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004743 if (posted)
4744 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004745}
4746
Jens Axboe47f46762019-11-09 17:43:02 -07004747static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4748{
Jens Axboe78076bb2019-12-04 19:56:40 -07004749 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004750 struct io_kiocb *req;
4751
Jens Axboe78076bb2019-12-04 19:56:40 -07004752 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4753 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004754 if (sqe_addr != req->user_data)
4755 continue;
4756 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004757 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004758 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004759 }
4760
4761 return -ENOENT;
4762}
4763
Jens Axboe3529d8c2019-12-19 18:24:38 -07004764static int io_poll_remove_prep(struct io_kiocb *req,
4765 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004766{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004767 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4768 return -EINVAL;
4769 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4770 sqe->poll_events)
4771 return -EINVAL;
4772
Jens Axboe0969e782019-12-17 18:40:57 -07004773 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004774 return 0;
4775}
4776
4777/*
4778 * Find a running poll command that matches one specified in sqe->addr,
4779 * and remove it if found.
4780 */
4781static int io_poll_remove(struct io_kiocb *req)
4782{
4783 struct io_ring_ctx *ctx = req->ctx;
4784 u64 addr;
4785 int ret;
4786
Jens Axboe0969e782019-12-17 18:40:57 -07004787 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004788 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004789 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004790 spin_unlock_irq(&ctx->completion_lock);
4791
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004792 if (ret < 0)
4793 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004794 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004795 return 0;
4796}
4797
Jens Axboe221c5eb2019-01-17 09:41:58 -07004798static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4799 void *key)
4800{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004801 struct io_kiocb *req = wait->private;
4802 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004803
Jens Axboed7718a92020-02-14 22:23:12 -07004804 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004805}
4806
Jens Axboe221c5eb2019-01-17 09:41:58 -07004807static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4808 struct poll_table_struct *p)
4809{
4810 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4811
Jens Axboed7718a92020-02-14 22:23:12 -07004812 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004813}
4814
Jens Axboe3529d8c2019-12-19 18:24:38 -07004815static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004816{
4817 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004818 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004819
4820 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4821 return -EINVAL;
4822 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4823 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004824 if (!poll->file)
4825 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004826
Jiufei Xue5769a352020-06-17 17:53:55 +08004827 events = READ_ONCE(sqe->poll32_events);
4828#ifdef __BIG_ENDIAN
4829 events = swahw32(events);
4830#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004831 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4832 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004833
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004834 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004835 return 0;
4836}
4837
Pavel Begunkov014db002020-03-03 21:33:12 +03004838static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004839{
4840 struct io_poll_iocb *poll = &req->poll;
4841 struct io_ring_ctx *ctx = req->ctx;
4842 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004843 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004844
Jens Axboe78076bb2019-12-04 19:56:40 -07004845 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004846 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004847 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004848
Jens Axboed7718a92020-02-14 22:23:12 -07004849 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4850 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004851
Jens Axboe8c838782019-03-12 15:48:16 -06004852 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004853 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004854 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004855 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004856 spin_unlock_irq(&ctx->completion_lock);
4857
Jens Axboe8c838782019-03-12 15:48:16 -06004858 if (mask) {
4859 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004860 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004861 }
Jens Axboe8c838782019-03-12 15:48:16 -06004862 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004863}
4864
Jens Axboe5262f562019-09-17 12:26:57 -06004865static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4866{
Jens Axboead8a48a2019-11-15 08:49:11 -07004867 struct io_timeout_data *data = container_of(timer,
4868 struct io_timeout_data, timer);
4869 struct io_kiocb *req = data->req;
4870 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004871 unsigned long flags;
4872
Jens Axboe5262f562019-09-17 12:26:57 -06004873 atomic_inc(&ctx->cq_timeouts);
4874
4875 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004876 /*
Jens Axboe11365042019-10-16 09:08:32 -06004877 * We could be racing with timeout deletion. If the list is empty,
4878 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004879 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004880 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004881 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004882
Jens Axboe78e19bb2019-11-06 15:21:34 -07004883 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004884 io_commit_cqring(ctx);
4885 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4886
4887 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004888 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004889 io_put_req(req);
4890 return HRTIMER_NORESTART;
4891}
4892
Jens Axboe47f46762019-11-09 17:43:02 -07004893static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4894{
4895 struct io_kiocb *req;
4896 int ret = -ENOENT;
4897
4898 list_for_each_entry(req, &ctx->timeout_list, list) {
4899 if (user_data == req->user_data) {
4900 list_del_init(&req->list);
4901 ret = 0;
4902 break;
4903 }
4904 }
4905
4906 if (ret == -ENOENT)
4907 return ret;
4908
Jens Axboe2d283902019-12-04 11:08:05 -07004909 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004910 if (ret == -1)
4911 return -EALREADY;
4912
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004913 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004914 io_cqring_fill_event(req, -ECANCELED);
4915 io_put_req(req);
4916 return 0;
4917}
4918
Jens Axboe3529d8c2019-12-19 18:24:38 -07004919static int io_timeout_remove_prep(struct io_kiocb *req,
4920 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004921{
Jens Axboeb29472e2019-12-17 18:50:29 -07004922 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4923 return -EINVAL;
4924 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4925 return -EINVAL;
4926
4927 req->timeout.addr = READ_ONCE(sqe->addr);
4928 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4929 if (req->timeout.flags)
4930 return -EINVAL;
4931
Jens Axboeb29472e2019-12-17 18:50:29 -07004932 return 0;
4933}
4934
Jens Axboe11365042019-10-16 09:08:32 -06004935/*
4936 * Remove or update an existing timeout command
4937 */
Jens Axboefc4df992019-12-10 14:38:45 -07004938static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004939{
4940 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004941 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004942
Jens Axboe11365042019-10-16 09:08:32 -06004943 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004944 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004945
Jens Axboe47f46762019-11-09 17:43:02 -07004946 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004947 io_commit_cqring(ctx);
4948 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004949 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004950 if (ret < 0)
4951 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004952 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004953 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004954}
4955
Jens Axboe3529d8c2019-12-19 18:24:38 -07004956static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004957 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004958{
Jens Axboead8a48a2019-11-15 08:49:11 -07004959 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004960 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004961 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06004962
Jens Axboead8a48a2019-11-15 08:49:11 -07004963 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004964 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004965 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004966 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004967 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07004968 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004969 flags = READ_ONCE(sqe->timeout_flags);
4970 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004971 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004972
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004973 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07004974
Jens Axboe3529d8c2019-12-19 18:24:38 -07004975 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004976 return -ENOMEM;
4977
4978 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004979 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004980 req->flags |= REQ_F_TIMEOUT;
4981
4982 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004983 return -EFAULT;
4984
Jens Axboe11365042019-10-16 09:08:32 -06004985 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004986 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004987 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004988 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004989
Jens Axboead8a48a2019-11-15 08:49:11 -07004990 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4991 return 0;
4992}
4993
Jens Axboefc4df992019-12-10 14:38:45 -07004994static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004995{
Jens Axboead8a48a2019-11-15 08:49:11 -07004996 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004997 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004998 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004999 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005000
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005001 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005002
Jens Axboe5262f562019-09-17 12:26:57 -06005003 /*
5004 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005005 * timeout event to be satisfied. If it isn't set, then this is
5006 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005007 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005008 if (!off) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005009 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07005010 entry = ctx->timeout_list.prev;
5011 goto add;
5012 }
Jens Axboe5262f562019-09-17 12:26:57 -06005013
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005014 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5015 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005016
5017 /*
5018 * Insertion sort, ensuring the first entry in the list is always
5019 * the one we need first.
5020 */
Jens Axboe5262f562019-09-17 12:26:57 -06005021 list_for_each_prev(entry, &ctx->timeout_list) {
5022 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06005023
Jens Axboe93bd25b2019-11-11 23:34:31 -07005024 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
5025 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005026 /* nxt.seq is behind @tail, otherwise would've been completed */
5027 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005028 break;
5029 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005030add:
Jens Axboe5262f562019-09-17 12:26:57 -06005031 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005032 data->timer.function = io_timeout_fn;
5033 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005034 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005035 return 0;
5036}
5037
Jens Axboe62755e32019-10-28 21:49:21 -06005038static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005039{
Jens Axboe62755e32019-10-28 21:49:21 -06005040 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005041
Jens Axboe62755e32019-10-28 21:49:21 -06005042 return req->user_data == (unsigned long) data;
5043}
5044
Jens Axboee977d6d2019-11-05 12:39:45 -07005045static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005046{
Jens Axboe62755e32019-10-28 21:49:21 -06005047 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005048 int ret = 0;
5049
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005050 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005051 switch (cancel_ret) {
5052 case IO_WQ_CANCEL_OK:
5053 ret = 0;
5054 break;
5055 case IO_WQ_CANCEL_RUNNING:
5056 ret = -EALREADY;
5057 break;
5058 case IO_WQ_CANCEL_NOTFOUND:
5059 ret = -ENOENT;
5060 break;
5061 }
5062
Jens Axboee977d6d2019-11-05 12:39:45 -07005063 return ret;
5064}
5065
Jens Axboe47f46762019-11-09 17:43:02 -07005066static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5067 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005068 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005069{
5070 unsigned long flags;
5071 int ret;
5072
5073 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5074 if (ret != -ENOENT) {
5075 spin_lock_irqsave(&ctx->completion_lock, flags);
5076 goto done;
5077 }
5078
5079 spin_lock_irqsave(&ctx->completion_lock, flags);
5080 ret = io_timeout_cancel(ctx, sqe_addr);
5081 if (ret != -ENOENT)
5082 goto done;
5083 ret = io_poll_cancel(ctx, sqe_addr);
5084done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005085 if (!ret)
5086 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005087 io_cqring_fill_event(req, ret);
5088 io_commit_cqring(ctx);
5089 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5090 io_cqring_ev_posted(ctx);
5091
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005092 if (ret < 0)
5093 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005094 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005095}
5096
Jens Axboe3529d8c2019-12-19 18:24:38 -07005097static int io_async_cancel_prep(struct io_kiocb *req,
5098 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005099{
Jens Axboefbf23842019-12-17 18:45:56 -07005100 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005101 return -EINVAL;
5102 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
5103 sqe->cancel_flags)
5104 return -EINVAL;
5105
Jens Axboefbf23842019-12-17 18:45:56 -07005106 req->cancel.addr = READ_ONCE(sqe->addr);
5107 return 0;
5108}
5109
Pavel Begunkov014db002020-03-03 21:33:12 +03005110static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005111{
5112 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005113
Pavel Begunkov014db002020-03-03 21:33:12 +03005114 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005115 return 0;
5116}
5117
Jens Axboe05f3fb32019-12-09 11:22:50 -07005118static int io_files_update_prep(struct io_kiocb *req,
5119 const struct io_uring_sqe *sqe)
5120{
5121 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
5122 return -EINVAL;
5123
5124 req->files_update.offset = READ_ONCE(sqe->off);
5125 req->files_update.nr_args = READ_ONCE(sqe->len);
5126 if (!req->files_update.nr_args)
5127 return -EINVAL;
5128 req->files_update.arg = READ_ONCE(sqe->addr);
5129 return 0;
5130}
5131
5132static int io_files_update(struct io_kiocb *req, bool force_nonblock)
5133{
5134 struct io_ring_ctx *ctx = req->ctx;
5135 struct io_uring_files_update up;
5136 int ret;
5137
Jens Axboef86cd202020-01-29 13:46:44 -07005138 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005139 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005140
5141 up.offset = req->files_update.offset;
5142 up.fds = req->files_update.arg;
5143
5144 mutex_lock(&ctx->uring_lock);
5145 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5146 mutex_unlock(&ctx->uring_lock);
5147
5148 if (ret < 0)
5149 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005150 io_req_complete(req, ret);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005151 return 0;
5152}
5153
Jens Axboe3529d8c2019-12-19 18:24:38 -07005154static int io_req_defer_prep(struct io_kiocb *req,
5155 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005156{
Jens Axboee7815732019-12-17 19:45:06 -07005157 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005158
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005159 if (!sqe)
5160 return 0;
5161
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005162 io_req_init_async(req);
5163
Jens Axboef86cd202020-01-29 13:46:44 -07005164 if (io_op_defs[req->opcode].file_table) {
5165 ret = io_grab_files(req);
5166 if (unlikely(ret))
5167 return ret;
5168 }
5169
Jens Axboecccf0ee2020-01-27 16:34:48 -07005170 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
5171
Jens Axboed625c6e2019-12-17 19:53:05 -07005172 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005173 case IORING_OP_NOP:
5174 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005175 case IORING_OP_READV:
5176 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005177 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005178 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005179 break;
5180 case IORING_OP_WRITEV:
5181 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005182 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005183 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005184 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005185 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005186 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005187 break;
5188 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005189 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005190 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005191 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005192 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005193 break;
5194 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005195 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005196 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005197 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005198 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005199 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005200 break;
5201 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005202 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005203 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005204 break;
Jens Axboef499a022019-12-02 16:28:46 -07005205 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005206 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005207 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005208 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005209 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005210 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005211 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005212 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005213 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005214 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005215 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005216 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005217 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005218 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005219 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005220 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005221 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005222 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005223 case IORING_OP_FALLOCATE:
5224 ret = io_fallocate_prep(req, sqe);
5225 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005226 case IORING_OP_OPENAT:
5227 ret = io_openat_prep(req, sqe);
5228 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005229 case IORING_OP_CLOSE:
5230 ret = io_close_prep(req, sqe);
5231 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005232 case IORING_OP_FILES_UPDATE:
5233 ret = io_files_update_prep(req, sqe);
5234 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005235 case IORING_OP_STATX:
5236 ret = io_statx_prep(req, sqe);
5237 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005238 case IORING_OP_FADVISE:
5239 ret = io_fadvise_prep(req, sqe);
5240 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005241 case IORING_OP_MADVISE:
5242 ret = io_madvise_prep(req, sqe);
5243 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005244 case IORING_OP_OPENAT2:
5245 ret = io_openat2_prep(req, sqe);
5246 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005247 case IORING_OP_EPOLL_CTL:
5248 ret = io_epoll_ctl_prep(req, sqe);
5249 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005250 case IORING_OP_SPLICE:
5251 ret = io_splice_prep(req, sqe);
5252 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005253 case IORING_OP_PROVIDE_BUFFERS:
5254 ret = io_provide_buffers_prep(req, sqe);
5255 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005256 case IORING_OP_REMOVE_BUFFERS:
5257 ret = io_remove_buffers_prep(req, sqe);
5258 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005259 case IORING_OP_TEE:
5260 ret = io_tee_prep(req, sqe);
5261 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005262 default:
Jens Axboee7815732019-12-17 19:45:06 -07005263 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5264 req->opcode);
5265 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005266 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005267 }
5268
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005269 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005270}
5271
Jens Axboe3529d8c2019-12-19 18:24:38 -07005272static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005273{
Jackie Liua197f662019-11-08 08:09:12 -07005274 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005275 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005276
Bob Liu9d858b22019-11-13 18:06:25 +08005277 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005278 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005279 return 0;
5280
Pavel Begunkov650b5482020-05-17 14:02:11 +03005281 if (!req->io) {
5282 if (io_alloc_async_ctx(req))
5283 return -EAGAIN;
5284 ret = io_req_defer_prep(req, sqe);
5285 if (ret < 0)
5286 return ret;
5287 }
Jens Axboe2d283902019-12-04 11:08:05 -07005288
Jens Axboede0617e2019-04-06 21:51:27 -06005289 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005290 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005291 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005292 return 0;
5293 }
5294
Jens Axboe915967f2019-11-21 09:01:20 -07005295 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005296 list_add_tail(&req->list, &ctx->defer_list);
5297 spin_unlock_irq(&ctx->completion_lock);
5298 return -EIOCBQUEUED;
5299}
5300
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005301static void io_cleanup_req(struct io_kiocb *req)
5302{
5303 struct io_async_ctx *io = req->io;
5304
5305 switch (req->opcode) {
5306 case IORING_OP_READV:
5307 case IORING_OP_READ_FIXED:
5308 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005309 if (req->flags & REQ_F_BUFFER_SELECTED)
5310 kfree((void *)(unsigned long)req->rw.addr);
5311 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005312 case IORING_OP_WRITEV:
5313 case IORING_OP_WRITE_FIXED:
5314 case IORING_OP_WRITE:
5315 if (io->rw.iov != io->rw.fast_iov)
5316 kfree(io->rw.iov);
5317 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005318 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005319 if (req->flags & REQ_F_BUFFER_SELECTED)
5320 kfree(req->sr_msg.kbuf);
5321 /* fallthrough */
5322 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005323 if (io->msg.iov != io->msg.fast_iov)
5324 kfree(io->msg.iov);
5325 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005326 case IORING_OP_RECV:
5327 if (req->flags & REQ_F_BUFFER_SELECTED)
5328 kfree(req->sr_msg.kbuf);
5329 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005330 case IORING_OP_OPENAT:
5331 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005332 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005333 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005334 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005335 io_put_file(req, req->splice.file_in,
5336 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5337 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005338 }
5339
5340 req->flags &= ~REQ_F_NEED_CLEANUP;
5341}
5342
Jens Axboe3529d8c2019-12-19 18:24:38 -07005343static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005344 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005345{
Jackie Liua197f662019-11-08 08:09:12 -07005346 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005347 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005348
Jens Axboed625c6e2019-12-17 19:53:05 -07005349 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005350 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005351 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005352 break;
5353 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005354 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005355 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005356 if (sqe) {
5357 ret = io_read_prep(req, sqe, force_nonblock);
5358 if (ret < 0)
5359 break;
5360 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005361 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005362 break;
5363 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005364 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005365 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005366 if (sqe) {
5367 ret = io_write_prep(req, sqe, force_nonblock);
5368 if (ret < 0)
5369 break;
5370 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005371 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005372 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005373 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005374 if (sqe) {
5375 ret = io_prep_fsync(req, sqe);
5376 if (ret < 0)
5377 break;
5378 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005379 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005380 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005381 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005382 if (sqe) {
5383 ret = io_poll_add_prep(req, sqe);
5384 if (ret)
5385 break;
5386 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005387 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005388 break;
5389 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005390 if (sqe) {
5391 ret = io_poll_remove_prep(req, sqe);
5392 if (ret < 0)
5393 break;
5394 }
Jens Axboefc4df992019-12-10 14:38:45 -07005395 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005396 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005397 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005398 if (sqe) {
5399 ret = io_prep_sfr(req, sqe);
5400 if (ret < 0)
5401 break;
5402 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005403 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005404 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005405 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005406 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005407 if (sqe) {
5408 ret = io_sendmsg_prep(req, sqe);
5409 if (ret < 0)
5410 break;
5411 }
Jens Axboefddafac2020-01-04 20:19:44 -07005412 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005413 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005414 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005415 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005416 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005417 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005418 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005419 if (sqe) {
5420 ret = io_recvmsg_prep(req, sqe);
5421 if (ret)
5422 break;
5423 }
Jens Axboefddafac2020-01-04 20:19:44 -07005424 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005425 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005426 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005427 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005428 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005429 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005430 if (sqe) {
5431 ret = io_timeout_prep(req, sqe, false);
5432 if (ret)
5433 break;
5434 }
Jens Axboefc4df992019-12-10 14:38:45 -07005435 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005436 break;
Jens Axboe11365042019-10-16 09:08:32 -06005437 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005438 if (sqe) {
5439 ret = io_timeout_remove_prep(req, sqe);
5440 if (ret)
5441 break;
5442 }
Jens Axboefc4df992019-12-10 14:38:45 -07005443 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005444 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005445 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005446 if (sqe) {
5447 ret = io_accept_prep(req, sqe);
5448 if (ret)
5449 break;
5450 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005451 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005452 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005453 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005454 if (sqe) {
5455 ret = io_connect_prep(req, sqe);
5456 if (ret)
5457 break;
5458 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005459 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005460 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005461 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005462 if (sqe) {
5463 ret = io_async_cancel_prep(req, sqe);
5464 if (ret)
5465 break;
5466 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005467 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005468 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005469 case IORING_OP_FALLOCATE:
5470 if (sqe) {
5471 ret = io_fallocate_prep(req, sqe);
5472 if (ret)
5473 break;
5474 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005475 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005476 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005477 case IORING_OP_OPENAT:
5478 if (sqe) {
5479 ret = io_openat_prep(req, sqe);
5480 if (ret)
5481 break;
5482 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005483 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005484 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005485 case IORING_OP_CLOSE:
5486 if (sqe) {
5487 ret = io_close_prep(req, sqe);
5488 if (ret)
5489 break;
5490 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005491 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005492 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005493 case IORING_OP_FILES_UPDATE:
5494 if (sqe) {
5495 ret = io_files_update_prep(req, sqe);
5496 if (ret)
5497 break;
5498 }
5499 ret = io_files_update(req, force_nonblock);
5500 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005501 case IORING_OP_STATX:
5502 if (sqe) {
5503 ret = io_statx_prep(req, sqe);
5504 if (ret)
5505 break;
5506 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005507 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005508 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005509 case IORING_OP_FADVISE:
5510 if (sqe) {
5511 ret = io_fadvise_prep(req, sqe);
5512 if (ret)
5513 break;
5514 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005515 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005516 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005517 case IORING_OP_MADVISE:
5518 if (sqe) {
5519 ret = io_madvise_prep(req, sqe);
5520 if (ret)
5521 break;
5522 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005523 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005524 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005525 case IORING_OP_OPENAT2:
5526 if (sqe) {
5527 ret = io_openat2_prep(req, sqe);
5528 if (ret)
5529 break;
5530 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005531 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005532 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005533 case IORING_OP_EPOLL_CTL:
5534 if (sqe) {
5535 ret = io_epoll_ctl_prep(req, sqe);
5536 if (ret)
5537 break;
5538 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005539 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005540 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005541 case IORING_OP_SPLICE:
5542 if (sqe) {
5543 ret = io_splice_prep(req, sqe);
5544 if (ret < 0)
5545 break;
5546 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005547 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005548 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005549 case IORING_OP_PROVIDE_BUFFERS:
5550 if (sqe) {
5551 ret = io_provide_buffers_prep(req, sqe);
5552 if (ret)
5553 break;
5554 }
5555 ret = io_provide_buffers(req, force_nonblock);
5556 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005557 case IORING_OP_REMOVE_BUFFERS:
5558 if (sqe) {
5559 ret = io_remove_buffers_prep(req, sqe);
5560 if (ret)
5561 break;
5562 }
5563 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005564 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005565 case IORING_OP_TEE:
5566 if (sqe) {
5567 ret = io_tee_prep(req, sqe);
5568 if (ret < 0)
5569 break;
5570 }
5571 ret = io_tee(req, force_nonblock);
5572 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005573 default:
5574 ret = -EINVAL;
5575 break;
5576 }
5577
5578 if (ret)
5579 return ret;
5580
Jens Axboeb5325762020-05-19 21:20:27 -06005581 /* If the op doesn't have a file, we're not polling for it */
5582 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005583 const bool in_async = io_wq_current_is_worker();
5584
Jens Axboe9e645e112019-05-10 16:07:28 -06005585 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005586 return -EAGAIN;
5587
Jens Axboe11ba8202020-01-15 21:51:17 -07005588 /* workqueue context doesn't hold uring_lock, grab it now */
5589 if (in_async)
5590 mutex_lock(&ctx->uring_lock);
5591
Jens Axboe2b188cc2019-01-07 10:46:33 -07005592 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005593
5594 if (in_async)
5595 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005596 }
5597
5598 return 0;
5599}
5600
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005601static void io_arm_async_linked_timeout(struct io_kiocb *req)
5602{
5603 struct io_kiocb *link;
5604
5605 /* link head's timeout is queued in io_queue_async_work() */
5606 if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5607 return;
5608
5609 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5610 io_queue_linked_timeout(link);
5611}
5612
Jens Axboe561fb042019-10-24 07:25:42 -06005613static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboedef596e2019-01-09 08:59:42 -07005614{
Jens Axboe561fb042019-10-24 07:25:42 -06005615 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005616 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005617 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005618
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005619 io_arm_async_linked_timeout(req);
5620
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005621 /* if NO_CANCEL is set, we must still run the work */
5622 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5623 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005624 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005625 }
Jens Axboe31b51512019-01-18 22:56:34 -07005626
Jens Axboe561fb042019-10-24 07:25:42 -06005627 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005628 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005629 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005630 /*
5631 * We can get EAGAIN for polled IO even though we're
5632 * forcing a sync submission from here, since we can't
5633 * wait for request slots on the block side.
5634 */
5635 if (ret != -EAGAIN)
5636 break;
5637 cond_resched();
5638 } while (1);
5639 }
Jens Axboe31b51512019-01-18 22:56:34 -07005640
Jens Axboe561fb042019-10-24 07:25:42 -06005641 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005642 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005643 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005644 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005645
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005646 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005647}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005648
Jens Axboe65e19f52019-10-26 07:20:21 -06005649static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5650 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005651{
Jens Axboe65e19f52019-10-26 07:20:21 -06005652 struct fixed_file_table *table;
5653
Jens Axboe05f3fb32019-12-09 11:22:50 -07005654 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005655 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005656}
5657
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005658static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5659 int fd, struct file **out_file, bool fixed)
5660{
5661 struct io_ring_ctx *ctx = req->ctx;
5662 struct file *file;
5663
5664 if (fixed) {
5665 if (unlikely(!ctx->file_data ||
5666 (unsigned) fd >= ctx->nr_user_files))
5667 return -EBADF;
5668 fd = array_index_nospec(fd, ctx->nr_user_files);
5669 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005670 if (file) {
5671 req->fixed_file_refs = ctx->file_data->cur_refs;
5672 percpu_ref_get(req->fixed_file_refs);
5673 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005674 } else {
5675 trace_io_uring_file_get(ctx, fd);
5676 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005677 }
5678
Jens Axboefd2206e2020-06-02 16:40:47 -06005679 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5680 *out_file = file;
5681 return 0;
5682 }
5683 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005684}
5685
Jens Axboe3529d8c2019-12-19 18:24:38 -07005686static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005687 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005688{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005689 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005690
Jens Axboe63ff8222020-05-07 14:56:15 -06005691 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005692 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005693 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005694
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005695 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005696}
5697
Jackie Liua197f662019-11-08 08:09:12 -07005698static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005699{
Jens Axboefcb323c2019-10-24 12:39:47 -06005700 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005701 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005702
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005703 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005704 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005705 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005706 return -EBADF;
5707
Jens Axboefcb323c2019-10-24 12:39:47 -06005708 rcu_read_lock();
5709 spin_lock_irq(&ctx->inflight_lock);
5710 /*
5711 * We use the f_ops->flush() handler to ensure that we can flush
5712 * out work accessing these files if the fd is closed. Check if
5713 * the fd has changed since we started down this path, and disallow
5714 * this operation if it has.
5715 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005716 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005717 list_add(&req->inflight_entry, &ctx->inflight_list);
5718 req->flags |= REQ_F_INFLIGHT;
5719 req->work.files = current->files;
5720 ret = 0;
5721 }
5722 spin_unlock_irq(&ctx->inflight_lock);
5723 rcu_read_unlock();
5724
5725 return ret;
5726}
5727
Jens Axboe2665abf2019-11-05 12:40:47 -07005728static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5729{
Jens Axboead8a48a2019-11-15 08:49:11 -07005730 struct io_timeout_data *data = container_of(timer,
5731 struct io_timeout_data, timer);
5732 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005733 struct io_ring_ctx *ctx = req->ctx;
5734 struct io_kiocb *prev = NULL;
5735 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005736
5737 spin_lock_irqsave(&ctx->completion_lock, flags);
5738
5739 /*
5740 * We don't expect the list to be empty, that will only happen if we
5741 * race with the completion of the linked work.
5742 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005743 if (!list_empty(&req->link_list)) {
5744 prev = list_entry(req->link_list.prev, struct io_kiocb,
5745 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005746 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005747 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005748 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5749 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005750 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005751 }
5752
5753 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5754
5755 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005756 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005757 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005758 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005759 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005760 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005761 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005762 return HRTIMER_NORESTART;
5763}
5764
Jens Axboead8a48a2019-11-15 08:49:11 -07005765static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005766{
Jens Axboe76a46e02019-11-10 23:34:16 -07005767 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005768
Jens Axboe76a46e02019-11-10 23:34:16 -07005769 /*
5770 * If the list is now empty, then our linked request finished before
5771 * we got a chance to setup the timer
5772 */
5773 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005774 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005775 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005776
Jens Axboead8a48a2019-11-15 08:49:11 -07005777 data->timer.function = io_link_timeout_fn;
5778 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5779 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005780 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005781 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005782
Jens Axboe2665abf2019-11-05 12:40:47 -07005783 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005784 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005785}
5786
Jens Axboead8a48a2019-11-15 08:49:11 -07005787static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005788{
5789 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005790
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005791 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005792 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005793 /* for polled retry, if flag is set, we already went through here */
5794 if (req->flags & REQ_F_POLLED)
5795 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005796
Pavel Begunkov44932332019-12-05 16:16:35 +03005797 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5798 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005799 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005800 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005801
Jens Axboe76a46e02019-11-10 23:34:16 -07005802 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005803 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005804}
5805
Jens Axboe3529d8c2019-12-19 18:24:38 -07005806static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005807{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005808 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005809 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005810 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005811 int ret;
5812
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005813again:
5814 linked_timeout = io_prep_linked_timeout(req);
5815
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005816 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5817 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005818 if (old_creds)
5819 revert_creds(old_creds);
5820 if (old_creds == req->work.creds)
5821 old_creds = NULL; /* restored original creds */
5822 else
5823 old_creds = override_creds(req->work.creds);
5824 }
5825
Pavel Begunkov014db002020-03-03 21:33:12 +03005826 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005827
5828 /*
5829 * We async punt it if the file wasn't marked NOWAIT, or if the file
5830 * doesn't support non-blocking read/write attempts
5831 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005832 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005833 if (io_arm_poll_handler(req)) {
5834 if (linked_timeout)
5835 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005836 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005837 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005838punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005839 io_req_init_async(req);
5840
Jens Axboef86cd202020-01-29 13:46:44 -07005841 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005842 ret = io_grab_files(req);
5843 if (ret)
5844 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005845 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005846
5847 /*
5848 * Queued up for async execution, worker will release
5849 * submit reference when the iocb is actually submitted.
5850 */
5851 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005852 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005853 }
Jens Axboee65ef562019-03-12 10:16:44 -06005854
Jens Axboefcb323c2019-10-24 12:39:47 -06005855err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005856 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005857 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005858 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005859
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005860 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005861 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005862 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005863 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005864 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005865 }
5866
Jens Axboee65ef562019-03-12 10:16:44 -06005867 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005868 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005869 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005870 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06005871 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005872 if (nxt) {
5873 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005874
5875 if (req->flags & REQ_F_FORCE_ASYNC)
5876 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005877 goto again;
5878 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005879exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005880 if (old_creds)
5881 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005882}
5883
Jens Axboe3529d8c2019-12-19 18:24:38 -07005884static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005885{
5886 int ret;
5887
Jens Axboe3529d8c2019-12-19 18:24:38 -07005888 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005889 if (ret) {
5890 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005891fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005892 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005893 io_put_req(req);
5894 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005895 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005896 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005897 if (!req->io) {
5898 ret = -EAGAIN;
5899 if (io_alloc_async_ctx(req))
5900 goto fail_req;
5901 ret = io_req_defer_prep(req, sqe);
5902 if (unlikely(ret < 0))
5903 goto fail_req;
5904 }
5905
Jens Axboece35a472019-12-17 08:04:44 -07005906 /*
5907 * Never try inline submit of IOSQE_ASYNC is set, go straight
5908 * to async execution.
5909 */
5910 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5911 io_queue_async_work(req);
5912 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005913 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005914 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005915}
5916
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005917static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005918{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005919 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06005920 io_put_req(req);
5921 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005922 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005923 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005924}
5925
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005926static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005927 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005928{
Jackie Liua197f662019-11-08 08:09:12 -07005929 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005930 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005931
Jens Axboe9e645e112019-05-10 16:07:28 -06005932 /*
5933 * If we already have a head request, queue this one for async
5934 * submittal once the head completes. If we don't have a head but
5935 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5936 * submitted sync once the chain is complete. If none of those
5937 * conditions are true (normal request), then just queue it.
5938 */
5939 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005940 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005941
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005942 /*
5943 * Taking sequential execution of a link, draining both sides
5944 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5945 * requests in the link. So, it drains the head and the
5946 * next after the link request. The last one is done via
5947 * drain_next flag to persist the effect across calls.
5948 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005949 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005950 head->flags |= REQ_F_IO_DRAIN;
5951 ctx->drain_next = 1;
5952 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005953 if (io_alloc_async_ctx(req))
5954 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005955
Jens Axboe3529d8c2019-12-19 18:24:38 -07005956 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005957 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005958 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005959 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005960 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005961 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005962 trace_io_uring_link(ctx, req, head);
5963 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005964
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005965 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005966 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005967 io_queue_link_head(head);
5968 *link = NULL;
5969 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005970 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005971 if (unlikely(ctx->drain_next)) {
5972 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005973 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005974 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005975 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005976 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005977 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005978
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005979 if (io_alloc_async_ctx(req))
5980 return -EAGAIN;
5981
Pavel Begunkov711be032020-01-17 03:57:59 +03005982 ret = io_req_defer_prep(req, sqe);
5983 if (ret)
5984 req->flags |= REQ_F_FAIL_LINK;
5985 *link = req;
5986 } else {
5987 io_queue_sqe(req, sqe);
5988 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005989 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005990
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005991 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005992}
5993
Jens Axboe9a56a232019-01-09 09:06:50 -07005994/*
5995 * Batched submission is done, ensure local IO is flushed out.
5996 */
5997static void io_submit_state_end(struct io_submit_state *state)
5998{
5999 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006000 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006001 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006002 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006003}
6004
6005/*
6006 * Start submission side cache.
6007 */
6008static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08006009 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006010{
6011 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006012#ifdef CONFIG_BLOCK
6013 state->plug.nowait = true;
6014#endif
Jens Axboe2579f912019-01-09 09:10:43 -07006015 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006016 state->file = NULL;
6017 state->ios_left = max_ios;
6018}
6019
Jens Axboe2b188cc2019-01-07 10:46:33 -07006020static void io_commit_sqring(struct io_ring_ctx *ctx)
6021{
Hristo Venev75b28af2019-08-26 17:23:46 +00006022 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006023
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006024 /*
6025 * Ensure any loads from the SQEs are done at this point,
6026 * since once we write the new head, the application could
6027 * write new data to them.
6028 */
6029 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006030}
6031
6032/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006033 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006034 * that is mapped by userspace. This means that care needs to be taken to
6035 * ensure that reads are stable, as we cannot rely on userspace always
6036 * being a good citizen. If members of the sqe are validated and then later
6037 * used, it's important that those reads are done through READ_ONCE() to
6038 * prevent a re-load down the line.
6039 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006040static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006041{
Hristo Venev75b28af2019-08-26 17:23:46 +00006042 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006043 unsigned head;
6044
6045 /*
6046 * The cached sq head (or cq tail) serves two purposes:
6047 *
6048 * 1) allows us to batch the cost of updating the user visible
6049 * head updates.
6050 * 2) allows the kernel side to track the head on its own, even
6051 * though the application is the one updating it.
6052 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006053 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006054 if (likely(head < ctx->sq_entries))
6055 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006056
6057 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006058 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006059 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006060 return NULL;
6061}
6062
6063static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6064{
6065 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066}
6067
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006068#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6069 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6070 IOSQE_BUFFER_SELECT)
6071
6072static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6073 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006074 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006075{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006076 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006077 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006078
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006079 /*
6080 * All io need record the previous position, if LINK vs DARIN,
6081 * it can be used to mark the position of the first IO in the
6082 * link list.
6083 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03006084 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006085 req->opcode = READ_ONCE(sqe->opcode);
6086 req->user_data = READ_ONCE(sqe->user_data);
6087 req->io = NULL;
6088 req->file = NULL;
6089 req->ctx = ctx;
6090 req->flags = 0;
6091 /* one is dropped after submission, the other at completion */
6092 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006093 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006094 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006095
6096 if (unlikely(req->opcode >= IORING_OP_LAST))
6097 return -EINVAL;
6098
Jens Axboe9d8426a2020-06-16 18:42:49 -06006099 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6100 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006101
6102 sqe_flags = READ_ONCE(sqe->flags);
6103 /* enforce forwards compatibility on users */
6104 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6105 return -EINVAL;
6106
6107 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6108 !io_op_defs[req->opcode].buffer_select)
6109 return -EOPNOTSUPP;
6110
6111 id = READ_ONCE(sqe->personality);
6112 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006113 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006114 req->work.creds = idr_find(&ctx->personality_idr, id);
6115 if (unlikely(!req->work.creds))
6116 return -EINVAL;
6117 get_cred(req->work.creds);
6118 }
6119
6120 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006121 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006122
Jens Axboe63ff8222020-05-07 14:56:15 -06006123 if (!io_op_defs[req->opcode].needs_file)
6124 return 0;
6125
6126 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006127}
6128
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006129static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006130 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006131{
Jens Axboeac8691c2020-06-01 08:30:41 -06006132 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006133 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006134 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006135
Jens Axboec4a2ed72019-11-21 21:01:26 -07006136 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006137 if (test_bit(0, &ctx->sq_check_overflow)) {
6138 if (!list_empty(&ctx->cq_overflow_list) &&
6139 !io_cqring_overflow_flush(ctx, false))
6140 return -EBUSY;
6141 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006142
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006143 /* make sure SQ entry isn't read before tail */
6144 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006145
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006146 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6147 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006148
Jens Axboeac8691c2020-06-01 08:30:41 -06006149 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006150
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006151 ctx->ring_fd = ring_fd;
6152 ctx->ring_file = ring_file;
6153
Jens Axboe6c271ce2019-01-10 11:22:30 -07006154 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006155 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006156 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006157 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006158
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006159 sqe = io_get_sqe(ctx);
6160 if (unlikely(!sqe)) {
6161 io_consume_sqe(ctx);
6162 break;
6163 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006164 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006165 if (unlikely(!req)) {
6166 if (!submitted)
6167 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006168 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006169 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006170
Jens Axboeac8691c2020-06-01 08:30:41 -06006171 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006172 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006173 /* will complete beyond this point, count as submitted */
6174 submitted++;
6175
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006176 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006177fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006178 io_put_req(req);
6179 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006180 break;
6181 }
6182
Jens Axboe354420f2020-01-08 18:55:15 -07006183 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006184 true, io_async_submit(ctx));
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08006185 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006186 if (err)
6187 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006188 }
6189
Pavel Begunkov9466f432020-01-25 22:34:01 +03006190 if (unlikely(submitted != nr)) {
6191 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6192
6193 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6194 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006195 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006196 io_queue_link_head(link);
Jens Axboeac8691c2020-06-01 08:30:41 -06006197 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006198
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006199 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6200 io_commit_sqring(ctx);
6201
Jens Axboe6c271ce2019-01-10 11:22:30 -07006202 return submitted;
6203}
6204
6205static int io_sq_thread(void *data)
6206{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006207 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006208 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006209 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006210 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006211 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006212
Jens Axboe0f158b42020-05-14 17:18:39 -06006213 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006214
Jens Axboe181e4482019-11-25 08:52:30 -07006215 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006216
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006217 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006218 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006219 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006220
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006221 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006222 unsigned nr_events = 0;
6223
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006224 mutex_lock(&ctx->uring_lock);
6225 if (!list_empty(&ctx->poll_list))
6226 io_iopoll_getevents(ctx, &nr_events, 0);
6227 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006228 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006229 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006230 }
6231
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006232 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006233
6234 /*
6235 * If submit got -EBUSY, flag us as needing the application
6236 * to enter the kernel to reap and flush events.
6237 */
6238 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006239 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006240 * Drop cur_mm before scheduling, we can't hold it for
6241 * long periods (or over schedule()). Do this before
6242 * adding ourselves to the waitqueue, as the unuse/drop
6243 * may sleep.
6244 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006245 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006246
6247 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006248 * We're polling. If we're within the defined idle
6249 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006250 * to sleep. The exception is if we got EBUSY doing
6251 * more IO, we should wait for the application to
6252 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006253 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006254 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07006255 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6256 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006257 if (current->task_works)
6258 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006259 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006260 continue;
6261 }
6262
Jens Axboe6c271ce2019-01-10 11:22:30 -07006263 prepare_to_wait(&ctx->sqo_wait, &wait,
6264 TASK_INTERRUPTIBLE);
6265
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006266 /*
6267 * While doing polled IO, before going to sleep, we need
6268 * to check if there are new reqs added to poll_list, it
6269 * is because reqs may have been punted to io worker and
6270 * will be added to poll_list later, hence check the
6271 * poll_list again.
6272 */
6273 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6274 !list_empty_careful(&ctx->poll_list)) {
6275 finish_wait(&ctx->sqo_wait, &wait);
6276 continue;
6277 }
6278
Jens Axboe6c271ce2019-01-10 11:22:30 -07006279 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006280 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006281 /* make sure to read SQ tail after writing flags */
6282 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006283
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006284 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006285 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006286 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006287 finish_wait(&ctx->sqo_wait, &wait);
6288 break;
6289 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006290 if (current->task_works) {
6291 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006292 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006293 continue;
6294 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006295 if (signal_pending(current))
6296 flush_signals(current);
6297 schedule();
6298 finish_wait(&ctx->sqo_wait, &wait);
6299
Hristo Venev75b28af2019-08-26 17:23:46 +00006300 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006301 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006302 continue;
6303 }
6304 finish_wait(&ctx->sqo_wait, &wait);
6305
Hristo Venev75b28af2019-08-26 17:23:46 +00006306 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006307 }
6308
Jens Axboe8a4955f2019-12-09 14:52:35 -07006309 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006310 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6311 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006312 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006313 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006314 }
6315
Jens Axboeb41e9852020-02-17 09:52:41 -07006316 if (current->task_works)
6317 task_work_run();
6318
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006319 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006320 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006321
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006322 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006323
Jens Axboe6c271ce2019-01-10 11:22:30 -07006324 return 0;
6325}
6326
Jens Axboebda52162019-09-24 13:47:15 -06006327struct io_wait_queue {
6328 struct wait_queue_entry wq;
6329 struct io_ring_ctx *ctx;
6330 unsigned to_wait;
6331 unsigned nr_timeouts;
6332};
6333
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006334static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006335{
6336 struct io_ring_ctx *ctx = iowq->ctx;
6337
6338 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006339 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006340 * started waiting. For timeouts, we always want to return to userspace,
6341 * regardless of event count.
6342 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006343 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006344 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6345}
6346
6347static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6348 int wake_flags, void *key)
6349{
6350 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6351 wq);
6352
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006353 /* use noflush == true, as we can't safely rely on locking context */
6354 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006355 return -1;
6356
6357 return autoremove_wake_function(curr, mode, wake_flags, key);
6358}
6359
Jens Axboe2b188cc2019-01-07 10:46:33 -07006360/*
6361 * Wait until events become available, if we don't already have some. The
6362 * application must reap them itself, as they reside on the shared cq ring.
6363 */
6364static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6365 const sigset_t __user *sig, size_t sigsz)
6366{
Jens Axboebda52162019-09-24 13:47:15 -06006367 struct io_wait_queue iowq = {
6368 .wq = {
6369 .private = current,
6370 .func = io_wake_function,
6371 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6372 },
6373 .ctx = ctx,
6374 .to_wait = min_events,
6375 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006376 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006377 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006378
Jens Axboeb41e9852020-02-17 09:52:41 -07006379 do {
6380 if (io_cqring_events(ctx, false) >= min_events)
6381 return 0;
6382 if (!current->task_works)
6383 break;
6384 task_work_run();
6385 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006386
6387 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006388#ifdef CONFIG_COMPAT
6389 if (in_compat_syscall())
6390 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006391 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006392 else
6393#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006394 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006395
Jens Axboe2b188cc2019-01-07 10:46:33 -07006396 if (ret)
6397 return ret;
6398 }
6399
Jens Axboebda52162019-09-24 13:47:15 -06006400 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006401 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006402 do {
6403 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6404 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006405 if (current->task_works)
6406 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006407 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006408 break;
6409 schedule();
6410 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006411 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006412 break;
6413 }
6414 } while (1);
6415 finish_wait(&ctx->wait, &iowq.wq);
6416
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006417 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006418
Hristo Venev75b28af2019-08-26 17:23:46 +00006419 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006420}
6421
Jens Axboe6b063142019-01-10 22:13:58 -07006422static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6423{
6424#if defined(CONFIG_UNIX)
6425 if (ctx->ring_sock) {
6426 struct sock *sock = ctx->ring_sock->sk;
6427 struct sk_buff *skb;
6428
6429 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6430 kfree_skb(skb);
6431 }
6432#else
6433 int i;
6434
Jens Axboe65e19f52019-10-26 07:20:21 -06006435 for (i = 0; i < ctx->nr_user_files; i++) {
6436 struct file *file;
6437
6438 file = io_file_from_index(ctx, i);
6439 if (file)
6440 fput(file);
6441 }
Jens Axboe6b063142019-01-10 22:13:58 -07006442#endif
6443}
6444
Jens Axboe05f3fb32019-12-09 11:22:50 -07006445static void io_file_ref_kill(struct percpu_ref *ref)
6446{
6447 struct fixed_file_data *data;
6448
6449 data = container_of(ref, struct fixed_file_data, refs);
6450 complete(&data->done);
6451}
6452
Jens Axboe6b063142019-01-10 22:13:58 -07006453static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6454{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006455 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006456 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006457 unsigned nr_tables, i;
6458
Jens Axboe05f3fb32019-12-09 11:22:50 -07006459 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006460 return -ENXIO;
6461
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006462 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006463 if (!list_empty(&data->ref_list))
6464 ref_node = list_first_entry(&data->ref_list,
6465 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006466 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006467 if (ref_node)
6468 percpu_ref_kill(&ref_node->refs);
6469
6470 percpu_ref_kill(&data->refs);
6471
6472 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006473 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006474 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006475
Jens Axboe6b063142019-01-10 22:13:58 -07006476 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006477 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6478 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006479 kfree(data->table[i].files);
6480 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006481 percpu_ref_exit(&data->refs);
6482 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006483 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006484 ctx->nr_user_files = 0;
6485 return 0;
6486}
6487
Jens Axboe6c271ce2019-01-10 11:22:30 -07006488static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6489{
6490 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006491 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006492 /*
6493 * The park is a bit of a work-around, without it we get
6494 * warning spews on shutdown with SQPOLL set and affinity
6495 * set to a single CPU.
6496 */
Jens Axboe06058632019-04-13 09:26:03 -06006497 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006498 kthread_stop(ctx->sqo_thread);
6499 ctx->sqo_thread = NULL;
6500 }
6501}
6502
Jens Axboe6b063142019-01-10 22:13:58 -07006503static void io_finish_async(struct io_ring_ctx *ctx)
6504{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006505 io_sq_thread_stop(ctx);
6506
Jens Axboe561fb042019-10-24 07:25:42 -06006507 if (ctx->io_wq) {
6508 io_wq_destroy(ctx->io_wq);
6509 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006510 }
6511}
6512
6513#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006514/*
6515 * Ensure the UNIX gc is aware of our file set, so we are certain that
6516 * the io_uring can be safely unregistered on process exit, even if we have
6517 * loops in the file referencing.
6518 */
6519static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6520{
6521 struct sock *sk = ctx->ring_sock->sk;
6522 struct scm_fp_list *fpl;
6523 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006524 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006525
Jens Axboe6b063142019-01-10 22:13:58 -07006526 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6527 if (!fpl)
6528 return -ENOMEM;
6529
6530 skb = alloc_skb(0, GFP_KERNEL);
6531 if (!skb) {
6532 kfree(fpl);
6533 return -ENOMEM;
6534 }
6535
6536 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006537
Jens Axboe08a45172019-10-03 08:11:03 -06006538 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006539 fpl->user = get_uid(ctx->user);
6540 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006541 struct file *file = io_file_from_index(ctx, i + offset);
6542
6543 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006544 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006545 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006546 unix_inflight(fpl->user, fpl->fp[nr_files]);
6547 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006548 }
6549
Jens Axboe08a45172019-10-03 08:11:03 -06006550 if (nr_files) {
6551 fpl->max = SCM_MAX_FD;
6552 fpl->count = nr_files;
6553 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006554 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006555 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6556 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006557
Jens Axboe08a45172019-10-03 08:11:03 -06006558 for (i = 0; i < nr_files; i++)
6559 fput(fpl->fp[i]);
6560 } else {
6561 kfree_skb(skb);
6562 kfree(fpl);
6563 }
Jens Axboe6b063142019-01-10 22:13:58 -07006564
6565 return 0;
6566}
6567
6568/*
6569 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6570 * causes regular reference counting to break down. We rely on the UNIX
6571 * garbage collection to take care of this problem for us.
6572 */
6573static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6574{
6575 unsigned left, total;
6576 int ret = 0;
6577
6578 total = 0;
6579 left = ctx->nr_user_files;
6580 while (left) {
6581 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006582
6583 ret = __io_sqe_files_scm(ctx, this_files, total);
6584 if (ret)
6585 break;
6586 left -= this_files;
6587 total += this_files;
6588 }
6589
6590 if (!ret)
6591 return 0;
6592
6593 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006594 struct file *file = io_file_from_index(ctx, total);
6595
6596 if (file)
6597 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006598 total++;
6599 }
6600
6601 return ret;
6602}
6603#else
6604static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6605{
6606 return 0;
6607}
6608#endif
6609
Jens Axboe65e19f52019-10-26 07:20:21 -06006610static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6611 unsigned nr_files)
6612{
6613 int i;
6614
6615 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006616 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006617 unsigned this_files;
6618
6619 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6620 table->files = kcalloc(this_files, sizeof(struct file *),
6621 GFP_KERNEL);
6622 if (!table->files)
6623 break;
6624 nr_files -= this_files;
6625 }
6626
6627 if (i == nr_tables)
6628 return 0;
6629
6630 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006631 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006632 kfree(table->files);
6633 }
6634 return 1;
6635}
6636
Jens Axboe05f3fb32019-12-09 11:22:50 -07006637static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006638{
6639#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006640 struct sock *sock = ctx->ring_sock->sk;
6641 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6642 struct sk_buff *skb;
6643 int i;
6644
6645 __skb_queue_head_init(&list);
6646
6647 /*
6648 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6649 * remove this entry and rearrange the file array.
6650 */
6651 skb = skb_dequeue(head);
6652 while (skb) {
6653 struct scm_fp_list *fp;
6654
6655 fp = UNIXCB(skb).fp;
6656 for (i = 0; i < fp->count; i++) {
6657 int left;
6658
6659 if (fp->fp[i] != file)
6660 continue;
6661
6662 unix_notinflight(fp->user, fp->fp[i]);
6663 left = fp->count - 1 - i;
6664 if (left) {
6665 memmove(&fp->fp[i], &fp->fp[i + 1],
6666 left * sizeof(struct file *));
6667 }
6668 fp->count--;
6669 if (!fp->count) {
6670 kfree_skb(skb);
6671 skb = NULL;
6672 } else {
6673 __skb_queue_tail(&list, skb);
6674 }
6675 fput(file);
6676 file = NULL;
6677 break;
6678 }
6679
6680 if (!file)
6681 break;
6682
6683 __skb_queue_tail(&list, skb);
6684
6685 skb = skb_dequeue(head);
6686 }
6687
6688 if (skb_peek(&list)) {
6689 spin_lock_irq(&head->lock);
6690 while ((skb = __skb_dequeue(&list)) != NULL)
6691 __skb_queue_tail(head, skb);
6692 spin_unlock_irq(&head->lock);
6693 }
6694#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006695 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006696#endif
6697}
6698
Jens Axboe05f3fb32019-12-09 11:22:50 -07006699struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006700 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006701 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006702};
6703
Jens Axboe4a38aed22020-05-14 17:21:15 -06006704static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006705{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006706 struct fixed_file_data *file_data = ref_node->file_data;
6707 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006708 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006709
6710 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006711 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006712 io_ring_file_put(ctx, pfile->file);
6713 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006714 }
6715
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006716 spin_lock(&file_data->lock);
6717 list_del(&ref_node->node);
6718 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006719
Xiaoguang Wang05589552020-03-31 14:05:18 +08006720 percpu_ref_exit(&ref_node->refs);
6721 kfree(ref_node);
6722 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006723}
6724
Jens Axboe4a38aed22020-05-14 17:21:15 -06006725static void io_file_put_work(struct work_struct *work)
6726{
6727 struct io_ring_ctx *ctx;
6728 struct llist_node *node;
6729
6730 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6731 node = llist_del_all(&ctx->file_put_llist);
6732
6733 while (node) {
6734 struct fixed_file_ref_node *ref_node;
6735 struct llist_node *next = node->next;
6736
6737 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6738 __io_file_put_work(ref_node);
6739 node = next;
6740 }
6741}
6742
Jens Axboe05f3fb32019-12-09 11:22:50 -07006743static void io_file_data_ref_zero(struct percpu_ref *ref)
6744{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006745 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006746 struct io_ring_ctx *ctx;
6747 bool first_add;
6748 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006749
Xiaoguang Wang05589552020-03-31 14:05:18 +08006750 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006751 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006752
Jens Axboe4a38aed22020-05-14 17:21:15 -06006753 if (percpu_ref_is_dying(&ctx->file_data->refs))
6754 delay = 0;
6755
6756 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6757 if (!delay)
6758 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6759 else if (first_add)
6760 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006761}
6762
6763static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6764 struct io_ring_ctx *ctx)
6765{
6766 struct fixed_file_ref_node *ref_node;
6767
6768 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6769 if (!ref_node)
6770 return ERR_PTR(-ENOMEM);
6771
6772 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6773 0, GFP_KERNEL)) {
6774 kfree(ref_node);
6775 return ERR_PTR(-ENOMEM);
6776 }
6777 INIT_LIST_HEAD(&ref_node->node);
6778 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006779 ref_node->file_data = ctx->file_data;
6780 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006781}
6782
6783static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6784{
6785 percpu_ref_exit(&ref_node->refs);
6786 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006787}
6788
6789static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6790 unsigned nr_args)
6791{
6792 __s32 __user *fds = (__s32 __user *) arg;
6793 unsigned nr_tables;
6794 struct file *file;
6795 int fd, ret = 0;
6796 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006797 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006798
6799 if (ctx->file_data)
6800 return -EBUSY;
6801 if (!nr_args)
6802 return -EINVAL;
6803 if (nr_args > IORING_MAX_FIXED_FILES)
6804 return -EMFILE;
6805
6806 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6807 if (!ctx->file_data)
6808 return -ENOMEM;
6809 ctx->file_data->ctx = ctx;
6810 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006811 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006812 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006813
6814 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6815 ctx->file_data->table = kcalloc(nr_tables,
6816 sizeof(struct fixed_file_table),
6817 GFP_KERNEL);
6818 if (!ctx->file_data->table) {
6819 kfree(ctx->file_data);
6820 ctx->file_data = NULL;
6821 return -ENOMEM;
6822 }
6823
Xiaoguang Wang05589552020-03-31 14:05:18 +08006824 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006825 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6826 kfree(ctx->file_data->table);
6827 kfree(ctx->file_data);
6828 ctx->file_data = NULL;
6829 return -ENOMEM;
6830 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006831
6832 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6833 percpu_ref_exit(&ctx->file_data->refs);
6834 kfree(ctx->file_data->table);
6835 kfree(ctx->file_data);
6836 ctx->file_data = NULL;
6837 return -ENOMEM;
6838 }
6839
6840 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6841 struct fixed_file_table *table;
6842 unsigned index;
6843
6844 ret = -EFAULT;
6845 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6846 break;
6847 /* allow sparse sets */
6848 if (fd == -1) {
6849 ret = 0;
6850 continue;
6851 }
6852
6853 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6854 index = i & IORING_FILE_TABLE_MASK;
6855 file = fget(fd);
6856
6857 ret = -EBADF;
6858 if (!file)
6859 break;
6860
6861 /*
6862 * Don't allow io_uring instances to be registered. If UNIX
6863 * isn't enabled, then this causes a reference cycle and this
6864 * instance can never get freed. If UNIX is enabled we'll
6865 * handle it just fine, but there's still no point in allowing
6866 * a ring fd as it doesn't support regular read/write anyway.
6867 */
6868 if (file->f_op == &io_uring_fops) {
6869 fput(file);
6870 break;
6871 }
6872 ret = 0;
6873 table->files[index] = file;
6874 }
6875
6876 if (ret) {
6877 for (i = 0; i < ctx->nr_user_files; i++) {
6878 file = io_file_from_index(ctx, i);
6879 if (file)
6880 fput(file);
6881 }
6882 for (i = 0; i < nr_tables; i++)
6883 kfree(ctx->file_data->table[i].files);
6884
6885 kfree(ctx->file_data->table);
6886 kfree(ctx->file_data);
6887 ctx->file_data = NULL;
6888 ctx->nr_user_files = 0;
6889 return ret;
6890 }
6891
6892 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006893 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006894 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006895 return ret;
6896 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006897
Xiaoguang Wang05589552020-03-31 14:05:18 +08006898 ref_node = alloc_fixed_file_ref_node(ctx);
6899 if (IS_ERR(ref_node)) {
6900 io_sqe_files_unregister(ctx);
6901 return PTR_ERR(ref_node);
6902 }
6903
6904 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006905 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006906 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006907 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006908 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006909 return ret;
6910}
6911
Jens Axboec3a31e62019-10-03 13:59:56 -06006912static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6913 int index)
6914{
6915#if defined(CONFIG_UNIX)
6916 struct sock *sock = ctx->ring_sock->sk;
6917 struct sk_buff_head *head = &sock->sk_receive_queue;
6918 struct sk_buff *skb;
6919
6920 /*
6921 * See if we can merge this file into an existing skb SCM_RIGHTS
6922 * file set. If there's no room, fall back to allocating a new skb
6923 * and filling it in.
6924 */
6925 spin_lock_irq(&head->lock);
6926 skb = skb_peek(head);
6927 if (skb) {
6928 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6929
6930 if (fpl->count < SCM_MAX_FD) {
6931 __skb_unlink(skb, head);
6932 spin_unlock_irq(&head->lock);
6933 fpl->fp[fpl->count] = get_file(file);
6934 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6935 fpl->count++;
6936 spin_lock_irq(&head->lock);
6937 __skb_queue_head(head, skb);
6938 } else {
6939 skb = NULL;
6940 }
6941 }
6942 spin_unlock_irq(&head->lock);
6943
6944 if (skb) {
6945 fput(file);
6946 return 0;
6947 }
6948
6949 return __io_sqe_files_scm(ctx, 1, index);
6950#else
6951 return 0;
6952#endif
6953}
6954
Hillf Dantona5318d32020-03-23 17:47:15 +08006955static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006956 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006957{
Hillf Dantona5318d32020-03-23 17:47:15 +08006958 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006959 struct percpu_ref *refs = data->cur_refs;
6960 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006961
Jens Axboe05f3fb32019-12-09 11:22:50 -07006962 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006963 if (!pfile)
6964 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006965
Xiaoguang Wang05589552020-03-31 14:05:18 +08006966 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006967 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006968 list_add(&pfile->list, &ref_node->file_list);
6969
Hillf Dantona5318d32020-03-23 17:47:15 +08006970 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006971}
6972
6973static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6974 struct io_uring_files_update *up,
6975 unsigned nr_args)
6976{
6977 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006978 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006979 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006980 __s32 __user *fds;
6981 int fd, i, err;
6982 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006983 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006984
Jens Axboe05f3fb32019-12-09 11:22:50 -07006985 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006986 return -EOVERFLOW;
6987 if (done > ctx->nr_user_files)
6988 return -EINVAL;
6989
Xiaoguang Wang05589552020-03-31 14:05:18 +08006990 ref_node = alloc_fixed_file_ref_node(ctx);
6991 if (IS_ERR(ref_node))
6992 return PTR_ERR(ref_node);
6993
Jens Axboec3a31e62019-10-03 13:59:56 -06006994 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006995 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006996 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006997 struct fixed_file_table *table;
6998 unsigned index;
6999
Jens Axboec3a31e62019-10-03 13:59:56 -06007000 err = 0;
7001 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7002 err = -EFAULT;
7003 break;
7004 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007005 i = array_index_nospec(up->offset, ctx->nr_user_files);
7006 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007007 index = i & IORING_FILE_TABLE_MASK;
7008 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007009 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007010 err = io_queue_file_removal(data, file);
7011 if (err)
7012 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007013 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007014 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007015 }
7016 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007017 file = fget(fd);
7018 if (!file) {
7019 err = -EBADF;
7020 break;
7021 }
7022 /*
7023 * Don't allow io_uring instances to be registered. If
7024 * UNIX isn't enabled, then this causes a reference
7025 * cycle and this instance can never get freed. If UNIX
7026 * is enabled we'll handle it just fine, but there's
7027 * still no point in allowing a ring fd as it doesn't
7028 * support regular read/write anyway.
7029 */
7030 if (file->f_op == &io_uring_fops) {
7031 fput(file);
7032 err = -EBADF;
7033 break;
7034 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007035 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007036 err = io_sqe_file_register(ctx, file, i);
7037 if (err)
7038 break;
7039 }
7040 nr_args--;
7041 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007042 up->offset++;
7043 }
7044
Xiaoguang Wang05589552020-03-31 14:05:18 +08007045 if (needs_switch) {
7046 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007047 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007048 list_add(&ref_node->node, &data->ref_list);
7049 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007050 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007051 percpu_ref_get(&ctx->file_data->refs);
7052 } else
7053 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007054
7055 return done ? done : err;
7056}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007057
Jens Axboe05f3fb32019-12-09 11:22:50 -07007058static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7059 unsigned nr_args)
7060{
7061 struct io_uring_files_update up;
7062
7063 if (!ctx->file_data)
7064 return -ENXIO;
7065 if (!nr_args)
7066 return -EINVAL;
7067 if (copy_from_user(&up, arg, sizeof(up)))
7068 return -EFAULT;
7069 if (up.resv)
7070 return -EINVAL;
7071
7072 return __io_sqe_files_update(ctx, &up, nr_args);
7073}
Jens Axboec3a31e62019-10-03 13:59:56 -06007074
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007075static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007076{
7077 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7078
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007079 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007080 io_put_req(req);
7081}
7082
Pavel Begunkov24369c22020-01-28 03:15:48 +03007083static int io_init_wq_offload(struct io_ring_ctx *ctx,
7084 struct io_uring_params *p)
7085{
7086 struct io_wq_data data;
7087 struct fd f;
7088 struct io_ring_ctx *ctx_attach;
7089 unsigned int concurrency;
7090 int ret = 0;
7091
7092 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007093 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007094 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007095
7096 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7097 /* Do QD, or 4 * CPUS, whatever is smallest */
7098 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7099
7100 ctx->io_wq = io_wq_create(concurrency, &data);
7101 if (IS_ERR(ctx->io_wq)) {
7102 ret = PTR_ERR(ctx->io_wq);
7103 ctx->io_wq = NULL;
7104 }
7105 return ret;
7106 }
7107
7108 f = fdget(p->wq_fd);
7109 if (!f.file)
7110 return -EBADF;
7111
7112 if (f.file->f_op != &io_uring_fops) {
7113 ret = -EINVAL;
7114 goto out_fput;
7115 }
7116
7117 ctx_attach = f.file->private_data;
7118 /* @io_wq is protected by holding the fd */
7119 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7120 ret = -EINVAL;
7121 goto out_fput;
7122 }
7123
7124 ctx->io_wq = ctx_attach->io_wq;
7125out_fput:
7126 fdput(f);
7127 return ret;
7128}
7129
Jens Axboe6c271ce2019-01-10 11:22:30 -07007130static int io_sq_offload_start(struct io_ring_ctx *ctx,
7131 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007132{
7133 int ret;
7134
7135 mmgrab(current->mm);
7136 ctx->sqo_mm = current->mm;
7137
Jens Axboe6c271ce2019-01-10 11:22:30 -07007138 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007139 ret = -EPERM;
7140 if (!capable(CAP_SYS_ADMIN))
7141 goto err;
7142
Jens Axboe917257d2019-04-13 09:28:55 -06007143 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7144 if (!ctx->sq_thread_idle)
7145 ctx->sq_thread_idle = HZ;
7146
Jens Axboe6c271ce2019-01-10 11:22:30 -07007147 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007148 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007149
Jens Axboe917257d2019-04-13 09:28:55 -06007150 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007151 if (cpu >= nr_cpu_ids)
7152 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007153 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007154 goto err;
7155
Jens Axboe6c271ce2019-01-10 11:22:30 -07007156 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7157 ctx, cpu,
7158 "io_uring-sq");
7159 } else {
7160 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7161 "io_uring-sq");
7162 }
7163 if (IS_ERR(ctx->sqo_thread)) {
7164 ret = PTR_ERR(ctx->sqo_thread);
7165 ctx->sqo_thread = NULL;
7166 goto err;
7167 }
7168 wake_up_process(ctx->sqo_thread);
7169 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7170 /* Can't have SQ_AFF without SQPOLL */
7171 ret = -EINVAL;
7172 goto err;
7173 }
7174
Pavel Begunkov24369c22020-01-28 03:15:48 +03007175 ret = io_init_wq_offload(ctx, p);
7176 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007177 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007178
7179 return 0;
7180err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007181 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007182 mmdrop(ctx->sqo_mm);
7183 ctx->sqo_mm = NULL;
7184 return ret;
7185}
7186
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007187static inline void __io_unaccount_mem(struct user_struct *user,
7188 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007189{
7190 atomic_long_sub(nr_pages, &user->locked_vm);
7191}
7192
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007193static inline int __io_account_mem(struct user_struct *user,
7194 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007195{
7196 unsigned long page_limit, cur_pages, new_pages;
7197
7198 /* Don't allow more pages than we can safely lock */
7199 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7200
7201 do {
7202 cur_pages = atomic_long_read(&user->locked_vm);
7203 new_pages = cur_pages + nr_pages;
7204 if (new_pages > page_limit)
7205 return -ENOMEM;
7206 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7207 new_pages) != cur_pages);
7208
7209 return 0;
7210}
7211
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007212static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7213 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007214{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007215 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007216 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007217
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007218 if (ctx->sqo_mm) {
7219 if (acct == ACCT_LOCKED)
7220 ctx->sqo_mm->locked_vm -= nr_pages;
7221 else if (acct == ACCT_PINNED)
7222 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7223 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007224}
7225
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007226static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7227 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007228{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007229 int ret;
7230
7231 if (ctx->limit_mem) {
7232 ret = __io_account_mem(ctx->user, nr_pages);
7233 if (ret)
7234 return ret;
7235 }
7236
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007237 if (ctx->sqo_mm) {
7238 if (acct == ACCT_LOCKED)
7239 ctx->sqo_mm->locked_vm += nr_pages;
7240 else if (acct == ACCT_PINNED)
7241 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7242 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007243
7244 return 0;
7245}
7246
Jens Axboe2b188cc2019-01-07 10:46:33 -07007247static void io_mem_free(void *ptr)
7248{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007249 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007250
Mark Rutland52e04ef2019-04-30 17:30:21 +01007251 if (!ptr)
7252 return;
7253
7254 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007255 if (put_page_testzero(page))
7256 free_compound_page(page);
7257}
7258
7259static void *io_mem_alloc(size_t size)
7260{
7261 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7262 __GFP_NORETRY;
7263
7264 return (void *) __get_free_pages(gfp_flags, get_order(size));
7265}
7266
Hristo Venev75b28af2019-08-26 17:23:46 +00007267static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7268 size_t *sq_offset)
7269{
7270 struct io_rings *rings;
7271 size_t off, sq_array_size;
7272
7273 off = struct_size(rings, cqes, cq_entries);
7274 if (off == SIZE_MAX)
7275 return SIZE_MAX;
7276
7277#ifdef CONFIG_SMP
7278 off = ALIGN(off, SMP_CACHE_BYTES);
7279 if (off == 0)
7280 return SIZE_MAX;
7281#endif
7282
7283 sq_array_size = array_size(sizeof(u32), sq_entries);
7284 if (sq_array_size == SIZE_MAX)
7285 return SIZE_MAX;
7286
7287 if (check_add_overflow(off, sq_array_size, &off))
7288 return SIZE_MAX;
7289
7290 if (sq_offset)
7291 *sq_offset = off;
7292
7293 return off;
7294}
7295
Jens Axboe2b188cc2019-01-07 10:46:33 -07007296static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7297{
Hristo Venev75b28af2019-08-26 17:23:46 +00007298 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007299
Hristo Venev75b28af2019-08-26 17:23:46 +00007300 pages = (size_t)1 << get_order(
7301 rings_size(sq_entries, cq_entries, NULL));
7302 pages += (size_t)1 << get_order(
7303 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007304
Hristo Venev75b28af2019-08-26 17:23:46 +00007305 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007306}
7307
Jens Axboeedafcce2019-01-09 09:16:05 -07007308static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7309{
7310 int i, j;
7311
7312 if (!ctx->user_bufs)
7313 return -ENXIO;
7314
7315 for (i = 0; i < ctx->nr_user_bufs; i++) {
7316 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7317
7318 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007319 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007320
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007321 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007322 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007323 imu->nr_bvecs = 0;
7324 }
7325
7326 kfree(ctx->user_bufs);
7327 ctx->user_bufs = NULL;
7328 ctx->nr_user_bufs = 0;
7329 return 0;
7330}
7331
7332static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7333 void __user *arg, unsigned index)
7334{
7335 struct iovec __user *src;
7336
7337#ifdef CONFIG_COMPAT
7338 if (ctx->compat) {
7339 struct compat_iovec __user *ciovs;
7340 struct compat_iovec ciov;
7341
7342 ciovs = (struct compat_iovec __user *) arg;
7343 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7344 return -EFAULT;
7345
Jens Axboed55e5f52019-12-11 16:12:15 -07007346 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007347 dst->iov_len = ciov.iov_len;
7348 return 0;
7349 }
7350#endif
7351 src = (struct iovec __user *) arg;
7352 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7353 return -EFAULT;
7354 return 0;
7355}
7356
7357static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7358 unsigned nr_args)
7359{
7360 struct vm_area_struct **vmas = NULL;
7361 struct page **pages = NULL;
7362 int i, j, got_pages = 0;
7363 int ret = -EINVAL;
7364
7365 if (ctx->user_bufs)
7366 return -EBUSY;
7367 if (!nr_args || nr_args > UIO_MAXIOV)
7368 return -EINVAL;
7369
7370 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7371 GFP_KERNEL);
7372 if (!ctx->user_bufs)
7373 return -ENOMEM;
7374
7375 for (i = 0; i < nr_args; i++) {
7376 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7377 unsigned long off, start, end, ubuf;
7378 int pret, nr_pages;
7379 struct iovec iov;
7380 size_t size;
7381
7382 ret = io_copy_iov(ctx, &iov, arg, i);
7383 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007384 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007385
7386 /*
7387 * Don't impose further limits on the size and buffer
7388 * constraints here, we'll -EINVAL later when IO is
7389 * submitted if they are wrong.
7390 */
7391 ret = -EFAULT;
7392 if (!iov.iov_base || !iov.iov_len)
7393 goto err;
7394
7395 /* arbitrary limit, but we need something */
7396 if (iov.iov_len > SZ_1G)
7397 goto err;
7398
7399 ubuf = (unsigned long) iov.iov_base;
7400 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7401 start = ubuf >> PAGE_SHIFT;
7402 nr_pages = end - start;
7403
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007404 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007405 if (ret)
7406 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007407
7408 ret = 0;
7409 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007410 kvfree(vmas);
7411 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007412 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007413 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007414 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007415 sizeof(struct vm_area_struct *),
7416 GFP_KERNEL);
7417 if (!pages || !vmas) {
7418 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007419 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007420 goto err;
7421 }
7422 got_pages = nr_pages;
7423 }
7424
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007425 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007426 GFP_KERNEL);
7427 ret = -ENOMEM;
7428 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007429 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007430 goto err;
7431 }
7432
7433 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007434 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007435 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007436 FOLL_WRITE | FOLL_LONGTERM,
7437 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007438 if (pret == nr_pages) {
7439 /* don't support file backed memory */
7440 for (j = 0; j < nr_pages; j++) {
7441 struct vm_area_struct *vma = vmas[j];
7442
7443 if (vma->vm_file &&
7444 !is_file_hugepages(vma->vm_file)) {
7445 ret = -EOPNOTSUPP;
7446 break;
7447 }
7448 }
7449 } else {
7450 ret = pret < 0 ? pret : -EFAULT;
7451 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007452 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007453 if (ret) {
7454 /*
7455 * if we did partial map, or found file backed vmas,
7456 * release any pages we did get
7457 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007458 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007459 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007460 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007461 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007462 goto err;
7463 }
7464
7465 off = ubuf & ~PAGE_MASK;
7466 size = iov.iov_len;
7467 for (j = 0; j < nr_pages; j++) {
7468 size_t vec_len;
7469
7470 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7471 imu->bvec[j].bv_page = pages[j];
7472 imu->bvec[j].bv_len = vec_len;
7473 imu->bvec[j].bv_offset = off;
7474 off = 0;
7475 size -= vec_len;
7476 }
7477 /* store original address for later verification */
7478 imu->ubuf = ubuf;
7479 imu->len = iov.iov_len;
7480 imu->nr_bvecs = nr_pages;
7481
7482 ctx->nr_user_bufs++;
7483 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007484 kvfree(pages);
7485 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007486 return 0;
7487err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007488 kvfree(pages);
7489 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007490 io_sqe_buffer_unregister(ctx);
7491 return ret;
7492}
7493
Jens Axboe9b402842019-04-11 11:45:41 -06007494static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7495{
7496 __s32 __user *fds = arg;
7497 int fd;
7498
7499 if (ctx->cq_ev_fd)
7500 return -EBUSY;
7501
7502 if (copy_from_user(&fd, fds, sizeof(*fds)))
7503 return -EFAULT;
7504
7505 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7506 if (IS_ERR(ctx->cq_ev_fd)) {
7507 int ret = PTR_ERR(ctx->cq_ev_fd);
7508 ctx->cq_ev_fd = NULL;
7509 return ret;
7510 }
7511
7512 return 0;
7513}
7514
7515static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7516{
7517 if (ctx->cq_ev_fd) {
7518 eventfd_ctx_put(ctx->cq_ev_fd);
7519 ctx->cq_ev_fd = NULL;
7520 return 0;
7521 }
7522
7523 return -ENXIO;
7524}
7525
Jens Axboe5a2e7452020-02-23 16:23:11 -07007526static int __io_destroy_buffers(int id, void *p, void *data)
7527{
7528 struct io_ring_ctx *ctx = data;
7529 struct io_buffer *buf = p;
7530
Jens Axboe067524e2020-03-02 16:32:28 -07007531 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007532 return 0;
7533}
7534
7535static void io_destroy_buffers(struct io_ring_ctx *ctx)
7536{
7537 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7538 idr_destroy(&ctx->io_buffer_idr);
7539}
7540
Jens Axboe2b188cc2019-01-07 10:46:33 -07007541static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7542{
Jens Axboe6b063142019-01-10 22:13:58 -07007543 io_finish_async(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007544 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007545 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007546 ctx->sqo_mm = NULL;
7547 }
Jens Axboedef596e2019-01-09 08:59:42 -07007548
7549 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007550 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007551 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007552 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007553 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007554 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007555
Jens Axboe2b188cc2019-01-07 10:46:33 -07007556#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007557 if (ctx->ring_sock) {
7558 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007559 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007560 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007561#endif
7562
Hristo Venev75b28af2019-08-26 17:23:46 +00007563 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007564 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007565
7566 percpu_ref_exit(&ctx->refs);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007567 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7568 ACCT_LOCKED);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007569 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007570 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007571 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007572 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007573 kfree(ctx);
7574}
7575
7576static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7577{
7578 struct io_ring_ctx *ctx = file->private_data;
7579 __poll_t mask = 0;
7580
7581 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007582 /*
7583 * synchronizes with barrier from wq_has_sleeper call in
7584 * io_commit_cqring
7585 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007586 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007587 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7588 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007589 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007590 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007591 mask |= EPOLLIN | EPOLLRDNORM;
7592
7593 return mask;
7594}
7595
7596static int io_uring_fasync(int fd, struct file *file, int on)
7597{
7598 struct io_ring_ctx *ctx = file->private_data;
7599
7600 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7601}
7602
Jens Axboe071698e2020-01-28 10:04:42 -07007603static int io_remove_personalities(int id, void *p, void *data)
7604{
7605 struct io_ring_ctx *ctx = data;
7606 const struct cred *cred;
7607
7608 cred = idr_remove(&ctx->personality_idr, id);
7609 if (cred)
7610 put_cred(cred);
7611 return 0;
7612}
7613
Jens Axboe85faa7b2020-04-09 18:14:00 -06007614static void io_ring_exit_work(struct work_struct *work)
7615{
7616 struct io_ring_ctx *ctx;
7617
7618 ctx = container_of(work, struct io_ring_ctx, exit_work);
7619 if (ctx->rings)
7620 io_cqring_overflow_flush(ctx, true);
7621
Jens Axboe56952e92020-06-17 15:00:04 -06007622 /*
7623 * If we're doing polled IO and end up having requests being
7624 * submitted async (out-of-line), then completions can come in while
7625 * we're waiting for refs to drop. We need to reap these manually,
7626 * as nobody else will be looking for them.
7627 */
7628 while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7629 io_iopoll_reap_events(ctx);
7630 if (ctx->rings)
7631 io_cqring_overflow_flush(ctx, true);
7632 }
Jens Axboe85faa7b2020-04-09 18:14:00 -06007633 io_ring_ctx_free(ctx);
7634}
7635
Jens Axboe2b188cc2019-01-07 10:46:33 -07007636static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7637{
7638 mutex_lock(&ctx->uring_lock);
7639 percpu_ref_kill(&ctx->refs);
7640 mutex_unlock(&ctx->uring_lock);
7641
Jens Axboe5262f562019-09-17 12:26:57 -06007642 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007643 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007644
7645 if (ctx->io_wq)
7646 io_wq_cancel_all(ctx->io_wq);
7647
Jens Axboedef596e2019-01-09 08:59:42 -07007648 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007649 /* if we failed setting up the ctx, we might not have any rings */
7650 if (ctx->rings)
7651 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007652 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007653 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7654 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007655}
7656
7657static int io_uring_release(struct inode *inode, struct file *file)
7658{
7659 struct io_ring_ctx *ctx = file->private_data;
7660
7661 file->private_data = NULL;
7662 io_ring_ctx_wait_and_kill(ctx);
7663 return 0;
7664}
7665
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007666static bool io_wq_files_match(struct io_wq_work *work, void *data)
7667{
7668 struct files_struct *files = data;
7669
7670 return work->files == files;
7671}
7672
Jens Axboefcb323c2019-10-24 12:39:47 -06007673static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7674 struct files_struct *files)
7675{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007676 if (list_empty_careful(&ctx->inflight_list))
7677 return;
7678
7679 /* cancel all at once, should be faster than doing it one by one*/
7680 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7681
Jens Axboefcb323c2019-10-24 12:39:47 -06007682 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007683 struct io_kiocb *cancel_req = NULL, *req;
7684 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007685
7686 spin_lock_irq(&ctx->inflight_lock);
7687 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007688 if (req->work.files != files)
7689 continue;
7690 /* req is being completed, ignore */
7691 if (!refcount_inc_not_zero(&req->refs))
7692 continue;
7693 cancel_req = req;
7694 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007695 }
Jens Axboe768134d2019-11-10 20:30:53 -07007696 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007697 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007698 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007699 spin_unlock_irq(&ctx->inflight_lock);
7700
Jens Axboe768134d2019-11-10 20:30:53 -07007701 /* We need to keep going until we don't find a matching req */
7702 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007703 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007704
Jens Axboe2ca10252020-02-13 17:17:35 -07007705 if (cancel_req->flags & REQ_F_OVERFLOW) {
7706 spin_lock_irq(&ctx->completion_lock);
7707 list_del(&cancel_req->list);
7708 cancel_req->flags &= ~REQ_F_OVERFLOW;
7709 if (list_empty(&ctx->cq_overflow_list)) {
7710 clear_bit(0, &ctx->sq_check_overflow);
7711 clear_bit(0, &ctx->cq_check_overflow);
7712 }
7713 spin_unlock_irq(&ctx->completion_lock);
7714
7715 WRITE_ONCE(ctx->rings->cq_overflow,
7716 atomic_inc_return(&ctx->cached_cq_overflow));
7717
7718 /*
7719 * Put inflight ref and overflow ref. If that's
7720 * all we had, then we're done with this request.
7721 */
7722 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007723 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007724 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007725 continue;
7726 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007727 } else {
7728 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7729 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007730 }
7731
Jens Axboefcb323c2019-10-24 12:39:47 -06007732 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007733 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007734 }
7735}
7736
Pavel Begunkov801dd572020-06-15 10:33:14 +03007737static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007738{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007739 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7740 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007741
Pavel Begunkov801dd572020-06-15 10:33:14 +03007742 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007743}
7744
Jens Axboefcb323c2019-10-24 12:39:47 -06007745static int io_uring_flush(struct file *file, void *data)
7746{
7747 struct io_ring_ctx *ctx = file->private_data;
7748
7749 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007750
7751 /*
7752 * If the task is going away, cancel work it may have pending
7753 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007754 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7755 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007756
Jens Axboefcb323c2019-10-24 12:39:47 -06007757 return 0;
7758}
7759
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007760static void *io_uring_validate_mmap_request(struct file *file,
7761 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007762{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007763 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007764 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007765 struct page *page;
7766 void *ptr;
7767
7768 switch (offset) {
7769 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007770 case IORING_OFF_CQ_RING:
7771 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007772 break;
7773 case IORING_OFF_SQES:
7774 ptr = ctx->sq_sqes;
7775 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007776 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007777 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007778 }
7779
7780 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007781 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007782 return ERR_PTR(-EINVAL);
7783
7784 return ptr;
7785}
7786
7787#ifdef CONFIG_MMU
7788
7789static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7790{
7791 size_t sz = vma->vm_end - vma->vm_start;
7792 unsigned long pfn;
7793 void *ptr;
7794
7795 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7796 if (IS_ERR(ptr))
7797 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007798
7799 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7800 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7801}
7802
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007803#else /* !CONFIG_MMU */
7804
7805static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7806{
7807 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7808}
7809
7810static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7811{
7812 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7813}
7814
7815static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7816 unsigned long addr, unsigned long len,
7817 unsigned long pgoff, unsigned long flags)
7818{
7819 void *ptr;
7820
7821 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7822 if (IS_ERR(ptr))
7823 return PTR_ERR(ptr);
7824
7825 return (unsigned long) ptr;
7826}
7827
7828#endif /* !CONFIG_MMU */
7829
Jens Axboe2b188cc2019-01-07 10:46:33 -07007830SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7831 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7832 size_t, sigsz)
7833{
7834 struct io_ring_ctx *ctx;
7835 long ret = -EBADF;
7836 int submitted = 0;
7837 struct fd f;
7838
Jens Axboeb41e9852020-02-17 09:52:41 -07007839 if (current->task_works)
7840 task_work_run();
7841
Jens Axboe6c271ce2019-01-10 11:22:30 -07007842 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007843 return -EINVAL;
7844
7845 f = fdget(fd);
7846 if (!f.file)
7847 return -EBADF;
7848
7849 ret = -EOPNOTSUPP;
7850 if (f.file->f_op != &io_uring_fops)
7851 goto out_fput;
7852
7853 ret = -ENXIO;
7854 ctx = f.file->private_data;
7855 if (!percpu_ref_tryget(&ctx->refs))
7856 goto out_fput;
7857
Jens Axboe6c271ce2019-01-10 11:22:30 -07007858 /*
7859 * For SQ polling, the thread will do all submissions and completions.
7860 * Just return the requested submit count, and wake the thread if
7861 * we were asked to.
7862 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007863 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007864 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007865 if (!list_empty_careful(&ctx->cq_overflow_list))
7866 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007867 if (flags & IORING_ENTER_SQ_WAKEUP)
7868 wake_up(&ctx->sqo_wait);
7869 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007870 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007871 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007872 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007873 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007874
7875 if (submitted != to_submit)
7876 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007877 }
7878 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007879 unsigned nr_events = 0;
7880
Jens Axboe2b188cc2019-01-07 10:46:33 -07007881 min_complete = min(min_complete, ctx->cq_entries);
7882
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007883 /*
7884 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7885 * space applications don't need to do io completion events
7886 * polling again, they can rely on io_sq_thread to do polling
7887 * work, which can reduce cpu usage and uring_lock contention.
7888 */
7889 if (ctx->flags & IORING_SETUP_IOPOLL &&
7890 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007891 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007892 } else {
7893 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7894 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007895 }
7896
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007897out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007898 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007899out_fput:
7900 fdput(f);
7901 return submitted ? submitted : ret;
7902}
7903
Tobias Klauserbebdb652020-02-26 18:38:32 +01007904#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007905static int io_uring_show_cred(int id, void *p, void *data)
7906{
7907 const struct cred *cred = p;
7908 struct seq_file *m = data;
7909 struct user_namespace *uns = seq_user_ns(m);
7910 struct group_info *gi;
7911 kernel_cap_t cap;
7912 unsigned __capi;
7913 int g;
7914
7915 seq_printf(m, "%5d\n", id);
7916 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7917 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7918 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7919 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7920 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7921 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7922 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7923 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7924 seq_puts(m, "\n\tGroups:\t");
7925 gi = cred->group_info;
7926 for (g = 0; g < gi->ngroups; g++) {
7927 seq_put_decimal_ull(m, g ? " " : "",
7928 from_kgid_munged(uns, gi->gid[g]));
7929 }
7930 seq_puts(m, "\n\tCapEff:\t");
7931 cap = cred->cap_effective;
7932 CAP_FOR_EACH_U32(__capi)
7933 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7934 seq_putc(m, '\n');
7935 return 0;
7936}
7937
7938static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7939{
7940 int i;
7941
7942 mutex_lock(&ctx->uring_lock);
7943 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7944 for (i = 0; i < ctx->nr_user_files; i++) {
7945 struct fixed_file_table *table;
7946 struct file *f;
7947
7948 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7949 f = table->files[i & IORING_FILE_TABLE_MASK];
7950 if (f)
7951 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7952 else
7953 seq_printf(m, "%5u: <none>\n", i);
7954 }
7955 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7956 for (i = 0; i < ctx->nr_user_bufs; i++) {
7957 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7958
7959 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7960 (unsigned int) buf->len);
7961 }
7962 if (!idr_is_empty(&ctx->personality_idr)) {
7963 seq_printf(m, "Personalities:\n");
7964 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7965 }
Jens Axboed7718a92020-02-14 22:23:12 -07007966 seq_printf(m, "PollList:\n");
7967 spin_lock_irq(&ctx->completion_lock);
7968 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7969 struct hlist_head *list = &ctx->cancel_hash[i];
7970 struct io_kiocb *req;
7971
7972 hlist_for_each_entry(req, list, hash_node)
7973 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7974 req->task->task_works != NULL);
7975 }
7976 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007977 mutex_unlock(&ctx->uring_lock);
7978}
7979
7980static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7981{
7982 struct io_ring_ctx *ctx = f->private_data;
7983
7984 if (percpu_ref_tryget(&ctx->refs)) {
7985 __io_uring_show_fdinfo(ctx, m);
7986 percpu_ref_put(&ctx->refs);
7987 }
7988}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007989#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007990
Jens Axboe2b188cc2019-01-07 10:46:33 -07007991static const struct file_operations io_uring_fops = {
7992 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007993 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007994 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007995#ifndef CONFIG_MMU
7996 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7997 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7998#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007999 .poll = io_uring_poll,
8000 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008001#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008002 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008003#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008004};
8005
8006static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8007 struct io_uring_params *p)
8008{
Hristo Venev75b28af2019-08-26 17:23:46 +00008009 struct io_rings *rings;
8010 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008011
Hristo Venev75b28af2019-08-26 17:23:46 +00008012 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8013 if (size == SIZE_MAX)
8014 return -EOVERFLOW;
8015
8016 rings = io_mem_alloc(size);
8017 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008018 return -ENOMEM;
8019
Hristo Venev75b28af2019-08-26 17:23:46 +00008020 ctx->rings = rings;
8021 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8022 rings->sq_ring_mask = p->sq_entries - 1;
8023 rings->cq_ring_mask = p->cq_entries - 1;
8024 rings->sq_ring_entries = p->sq_entries;
8025 rings->cq_ring_entries = p->cq_entries;
8026 ctx->sq_mask = rings->sq_ring_mask;
8027 ctx->cq_mask = rings->cq_ring_mask;
8028 ctx->sq_entries = rings->sq_ring_entries;
8029 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008030
8031 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008032 if (size == SIZE_MAX) {
8033 io_mem_free(ctx->rings);
8034 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008035 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008036 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008037
8038 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008039 if (!ctx->sq_sqes) {
8040 io_mem_free(ctx->rings);
8041 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008042 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008043 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008044
Jens Axboe2b188cc2019-01-07 10:46:33 -07008045 return 0;
8046}
8047
8048/*
8049 * Allocate an anonymous fd, this is what constitutes the application
8050 * visible backing of an io_uring instance. The application mmaps this
8051 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8052 * we have to tie this fd to a socket for file garbage collection purposes.
8053 */
8054static int io_uring_get_fd(struct io_ring_ctx *ctx)
8055{
8056 struct file *file;
8057 int ret;
8058
8059#if defined(CONFIG_UNIX)
8060 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8061 &ctx->ring_sock);
8062 if (ret)
8063 return ret;
8064#endif
8065
8066 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8067 if (ret < 0)
8068 goto err;
8069
8070 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8071 O_RDWR | O_CLOEXEC);
8072 if (IS_ERR(file)) {
8073 put_unused_fd(ret);
8074 ret = PTR_ERR(file);
8075 goto err;
8076 }
8077
8078#if defined(CONFIG_UNIX)
8079 ctx->ring_sock->file = file;
8080#endif
8081 fd_install(ret, file);
8082 return ret;
8083err:
8084#if defined(CONFIG_UNIX)
8085 sock_release(ctx->ring_sock);
8086 ctx->ring_sock = NULL;
8087#endif
8088 return ret;
8089}
8090
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008091static int io_uring_create(unsigned entries, struct io_uring_params *p,
8092 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008093{
8094 struct user_struct *user = NULL;
8095 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008096 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008097 int ret;
8098
Jens Axboe8110c1a2019-12-28 15:39:54 -07008099 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008100 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008101 if (entries > IORING_MAX_ENTRIES) {
8102 if (!(p->flags & IORING_SETUP_CLAMP))
8103 return -EINVAL;
8104 entries = IORING_MAX_ENTRIES;
8105 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008106
8107 /*
8108 * Use twice as many entries for the CQ ring. It's possible for the
8109 * application to drive a higher depth than the size of the SQ ring,
8110 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008111 * some flexibility in overcommitting a bit. If the application has
8112 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8113 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008114 */
8115 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008116 if (p->flags & IORING_SETUP_CQSIZE) {
8117 /*
8118 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8119 * to a power-of-two, if it isn't already. We do NOT impose
8120 * any cq vs sq ring sizing.
8121 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008122 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008123 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008124 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8125 if (!(p->flags & IORING_SETUP_CLAMP))
8126 return -EINVAL;
8127 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8128 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008129 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8130 } else {
8131 p->cq_entries = 2 * p->sq_entries;
8132 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008133
8134 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008135 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008136
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008137 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008138 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008139 ring_pages(p->sq_entries, p->cq_entries));
8140 if (ret) {
8141 free_uid(user);
8142 return ret;
8143 }
8144 }
8145
8146 ctx = io_ring_ctx_alloc(p);
8147 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008148 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008149 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008150 p->cq_entries));
8151 free_uid(user);
8152 return -ENOMEM;
8153 }
8154 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008155 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008156 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008157
8158 ret = io_allocate_scq_urings(ctx, p);
8159 if (ret)
8160 goto err;
8161
Jens Axboe6c271ce2019-01-10 11:22:30 -07008162 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008163 if (ret)
8164 goto err;
8165
Jens Axboe2b188cc2019-01-07 10:46:33 -07008166 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008167 p->sq_off.head = offsetof(struct io_rings, sq.head);
8168 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8169 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8170 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8171 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8172 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8173 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008174
8175 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008176 p->cq_off.head = offsetof(struct io_rings, cq.head);
8177 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8178 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8179 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8180 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8181 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008182 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008183
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008184 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8185 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008186 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8187 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008188
8189 if (copy_to_user(params, p, sizeof(*p))) {
8190 ret = -EFAULT;
8191 goto err;
8192 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008193 /*
8194 * Install ring fd as the very last thing, so we don't risk someone
8195 * having closed it before we finish setup
8196 */
8197 ret = io_uring_get_fd(ctx);
8198 if (ret < 0)
8199 goto err;
8200
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008201 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008202 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8203 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008204 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008205 return ret;
8206err:
8207 io_ring_ctx_wait_and_kill(ctx);
8208 return ret;
8209}
8210
8211/*
8212 * Sets up an aio uring context, and returns the fd. Applications asks for a
8213 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8214 * params structure passed in.
8215 */
8216static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8217{
8218 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008219 int i;
8220
8221 if (copy_from_user(&p, params, sizeof(p)))
8222 return -EFAULT;
8223 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8224 if (p.resv[i])
8225 return -EINVAL;
8226 }
8227
Jens Axboe6c271ce2019-01-10 11:22:30 -07008228 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008229 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008230 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008231 return -EINVAL;
8232
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008233 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008234}
8235
8236SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8237 struct io_uring_params __user *, params)
8238{
8239 return io_uring_setup(entries, params);
8240}
8241
Jens Axboe66f4af92020-01-16 15:36:52 -07008242static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8243{
8244 struct io_uring_probe *p;
8245 size_t size;
8246 int i, ret;
8247
8248 size = struct_size(p, ops, nr_args);
8249 if (size == SIZE_MAX)
8250 return -EOVERFLOW;
8251 p = kzalloc(size, GFP_KERNEL);
8252 if (!p)
8253 return -ENOMEM;
8254
8255 ret = -EFAULT;
8256 if (copy_from_user(p, arg, size))
8257 goto out;
8258 ret = -EINVAL;
8259 if (memchr_inv(p, 0, size))
8260 goto out;
8261
8262 p->last_op = IORING_OP_LAST - 1;
8263 if (nr_args > IORING_OP_LAST)
8264 nr_args = IORING_OP_LAST;
8265
8266 for (i = 0; i < nr_args; i++) {
8267 p->ops[i].op = i;
8268 if (!io_op_defs[i].not_supported)
8269 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8270 }
8271 p->ops_len = i;
8272
8273 ret = 0;
8274 if (copy_to_user(arg, p, size))
8275 ret = -EFAULT;
8276out:
8277 kfree(p);
8278 return ret;
8279}
8280
Jens Axboe071698e2020-01-28 10:04:42 -07008281static int io_register_personality(struct io_ring_ctx *ctx)
8282{
8283 const struct cred *creds = get_current_cred();
8284 int id;
8285
8286 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8287 USHRT_MAX, GFP_KERNEL);
8288 if (id < 0)
8289 put_cred(creds);
8290 return id;
8291}
8292
8293static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8294{
8295 const struct cred *old_creds;
8296
8297 old_creds = idr_remove(&ctx->personality_idr, id);
8298 if (old_creds) {
8299 put_cred(old_creds);
8300 return 0;
8301 }
8302
8303 return -EINVAL;
8304}
8305
8306static bool io_register_op_must_quiesce(int op)
8307{
8308 switch (op) {
8309 case IORING_UNREGISTER_FILES:
8310 case IORING_REGISTER_FILES_UPDATE:
8311 case IORING_REGISTER_PROBE:
8312 case IORING_REGISTER_PERSONALITY:
8313 case IORING_UNREGISTER_PERSONALITY:
8314 return false;
8315 default:
8316 return true;
8317 }
8318}
8319
Jens Axboeedafcce2019-01-09 09:16:05 -07008320static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8321 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008322 __releases(ctx->uring_lock)
8323 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008324{
8325 int ret;
8326
Jens Axboe35fa71a2019-04-22 10:23:23 -06008327 /*
8328 * We're inside the ring mutex, if the ref is already dying, then
8329 * someone else killed the ctx or is already going through
8330 * io_uring_register().
8331 */
8332 if (percpu_ref_is_dying(&ctx->refs))
8333 return -ENXIO;
8334
Jens Axboe071698e2020-01-28 10:04:42 -07008335 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008336 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008337
Jens Axboe05f3fb32019-12-09 11:22:50 -07008338 /*
8339 * Drop uring mutex before waiting for references to exit. If
8340 * another thread is currently inside io_uring_enter() it might
8341 * need to grab the uring_lock to make progress. If we hold it
8342 * here across the drain wait, then we can deadlock. It's safe
8343 * to drop the mutex here, since no new references will come in
8344 * after we've killed the percpu ref.
8345 */
8346 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008347 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008348 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008349 if (ret) {
8350 percpu_ref_resurrect(&ctx->refs);
8351 ret = -EINTR;
8352 goto out;
8353 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008354 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008355
8356 switch (opcode) {
8357 case IORING_REGISTER_BUFFERS:
8358 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8359 break;
8360 case IORING_UNREGISTER_BUFFERS:
8361 ret = -EINVAL;
8362 if (arg || nr_args)
8363 break;
8364 ret = io_sqe_buffer_unregister(ctx);
8365 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008366 case IORING_REGISTER_FILES:
8367 ret = io_sqe_files_register(ctx, arg, nr_args);
8368 break;
8369 case IORING_UNREGISTER_FILES:
8370 ret = -EINVAL;
8371 if (arg || nr_args)
8372 break;
8373 ret = io_sqe_files_unregister(ctx);
8374 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008375 case IORING_REGISTER_FILES_UPDATE:
8376 ret = io_sqe_files_update(ctx, arg, nr_args);
8377 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008378 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008379 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008380 ret = -EINVAL;
8381 if (nr_args != 1)
8382 break;
8383 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008384 if (ret)
8385 break;
8386 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8387 ctx->eventfd_async = 1;
8388 else
8389 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008390 break;
8391 case IORING_UNREGISTER_EVENTFD:
8392 ret = -EINVAL;
8393 if (arg || nr_args)
8394 break;
8395 ret = io_eventfd_unregister(ctx);
8396 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008397 case IORING_REGISTER_PROBE:
8398 ret = -EINVAL;
8399 if (!arg || nr_args > 256)
8400 break;
8401 ret = io_probe(ctx, arg, nr_args);
8402 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008403 case IORING_REGISTER_PERSONALITY:
8404 ret = -EINVAL;
8405 if (arg || nr_args)
8406 break;
8407 ret = io_register_personality(ctx);
8408 break;
8409 case IORING_UNREGISTER_PERSONALITY:
8410 ret = -EINVAL;
8411 if (arg)
8412 break;
8413 ret = io_unregister_personality(ctx, nr_args);
8414 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008415 default:
8416 ret = -EINVAL;
8417 break;
8418 }
8419
Jens Axboe071698e2020-01-28 10:04:42 -07008420 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008421 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008422 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008423out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008424 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008425 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008426 return ret;
8427}
8428
8429SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8430 void __user *, arg, unsigned int, nr_args)
8431{
8432 struct io_ring_ctx *ctx;
8433 long ret = -EBADF;
8434 struct fd f;
8435
8436 f = fdget(fd);
8437 if (!f.file)
8438 return -EBADF;
8439
8440 ret = -EOPNOTSUPP;
8441 if (f.file->f_op != &io_uring_fops)
8442 goto out_fput;
8443
8444 ctx = f.file->private_data;
8445
8446 mutex_lock(&ctx->uring_lock);
8447 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8448 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008449 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8450 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008451out_fput:
8452 fdput(f);
8453 return ret;
8454}
8455
Jens Axboe2b188cc2019-01-07 10:46:33 -07008456static int __init io_uring_init(void)
8457{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008458#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8459 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8460 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8461} while (0)
8462
8463#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8464 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8465 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8466 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8467 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8468 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8469 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8470 BUILD_BUG_SQE_ELEM(8, __u64, off);
8471 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8472 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008473 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008474 BUILD_BUG_SQE_ELEM(24, __u32, len);
8475 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8476 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8477 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8478 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008479 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8480 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008481 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8482 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8483 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8484 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8485 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8486 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8487 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8488 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008489 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008490 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8491 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8492 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008493 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008494
Jens Axboed3656342019-12-18 09:50:26 -07008495 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008496 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008497 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8498 return 0;
8499};
8500__initcall(io_uring_init);