blob: 82b35948ac5bf92e2ad46ea8b352743934e62d51 [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_FAIL_LINK_BIT,
530 REQ_F_INFLIGHT_BIT,
531 REQ_F_CUR_POS_BIT,
532 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300533 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300534 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300535 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300536 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700537 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700538 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700539 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600540 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300541 REQ_F_QUEUE_TIMEOUT_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800542 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300543 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700544
545 /* not a real bit, just to check we're not overflowing the space */
546 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300547};
548
549enum {
550 /* ctx owns file */
551 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
552 /* drain existing IO first */
553 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
554 /* linked sqes */
555 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
556 /* doesn't sever on completion < 0 */
557 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
558 /* IOSQE_ASYNC */
559 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700560 /* IOSQE_BUFFER_SELECT */
561 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300563 /* head of a link */
564 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300565 /* fail rest of links */
566 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
567 /* on inflight list */
568 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
569 /* read/write uses file position */
570 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
571 /* must not punt to workers */
572 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300573 /* has linked timeout */
574 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575 /* regular file */
576 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577 /* completion under lock */
578 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300579 /* needs cleanup */
580 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700581 /* in overflow list */
582 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700583 /* already went through poll handler */
584 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700585 /* buffer already selected */
586 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600587 /* doesn't need file table for this request */
588 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300589 /* needs to queue linked timeout */
590 REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800591 /* io_wq_work is initialized */
592 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300593 /* req->task is refcounted */
594 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700595};
596
597struct async_poll {
598 struct io_poll_iocb poll;
599 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300600};
601
Jens Axboe09bb8392019-03-13 12:39:28 -0600602/*
603 * NOTE! Each of the iocb union members has the file pointer
604 * as the first entry in their struct definition. So you can
605 * access the file pointer through any of the sub-structs,
606 * or directly as just 'ki_filp' in this struct.
607 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700609 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600610 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700611 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700612 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700613 struct io_accept accept;
614 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700615 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700616 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700617 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700618 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700619 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700620 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700621 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700622 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700623 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700624 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300625 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700626 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700627 struct io_statx statx;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700628 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700629
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700630 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300631 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700632 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800633 /* polled IO has completed */
634 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700635
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700636 u16 buf_index;
637
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700639 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700641 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600642 struct task_struct *task;
643 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600645 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600646 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700647
Jens Axboed7718a92020-02-14 22:23:12 -0700648 struct list_head link_list;
649
Jens Axboefcb323c2019-10-24 12:39:47 -0600650 struct list_head inflight_entry;
651
Xiaoguang Wang05589552020-03-31 14:05:18 +0800652 struct percpu_ref *fixed_file_refs;
653
Jens Axboeb41e9852020-02-17 09:52:41 -0700654 union {
655 /*
656 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700657 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
658 * async armed poll handlers for regular commands. The latter
659 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700660 */
661 struct {
Jens Axboed7718a92020-02-14 22:23:12 -0700662 struct hlist_node hash_node;
663 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700664 };
665 struct io_wq_work work;
666 };
Pavel Begunkov8ef77762020-06-27 14:04:59 +0300667 struct callback_head task_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700668};
669
Jens Axboedef596e2019-01-09 08:59:42 -0700670#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700671
Jens Axboe013538b2020-06-22 09:29:15 -0600672struct io_comp_state {
673 unsigned int nr;
674 struct list_head list;
675 struct io_ring_ctx *ctx;
676};
677
Jens Axboe9a56a232019-01-09 09:06:50 -0700678struct io_submit_state {
679 struct blk_plug plug;
680
681 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700682 * io_kiocb alloc cache
683 */
684 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300685 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700686
687 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600688 * Batch completion logic
689 */
690 struct io_comp_state comp;
691
692 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700693 * File reference cache
694 */
695 struct file *file;
696 unsigned int fd;
697 unsigned int has_refs;
698 unsigned int used_refs;
699 unsigned int ios_left;
700};
701
Jens Axboed3656342019-12-18 09:50:26 -0700702struct io_op_def {
703 /* needs req->io allocated for deferral/async */
704 unsigned async_ctx : 1;
705 /* needs current->mm setup, does mm access */
706 unsigned needs_mm : 1;
707 /* needs req->file assigned */
708 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600709 /* don't fail if file grab fails */
710 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700711 /* hash wq insertion if file is a regular file */
712 unsigned hash_reg_file : 1;
713 /* unbound wq insertion if file is a non-regular file */
714 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700715 /* opcode is not supported by this kernel */
716 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700717 /* needs file table */
718 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700719 /* needs ->fs */
720 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700721 /* set if opcode supports polled "wait" */
722 unsigned pollin : 1;
723 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700724 /* op supports buffer selection */
725 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700726};
727
728static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_NOP] = {},
730 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700731 .async_ctx = 1,
732 .needs_mm = 1,
733 .needs_file = 1,
734 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700735 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700736 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700739 .async_ctx = 1,
740 .needs_mm = 1,
741 .needs_file = 1,
742 .hash_reg_file = 1,
743 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700744 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700745 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300746 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700747 .needs_file = 1,
748 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300749 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700750 .needs_file = 1,
751 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700752 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700753 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300754 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700755 .needs_file = 1,
756 .hash_reg_file = 1,
757 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700758 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700759 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700761 .needs_file = 1,
762 .unbound_nonreg_file = 1,
763 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300764 [IORING_OP_POLL_REMOVE] = {},
765 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700766 .needs_file = 1,
767 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300768 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700769 .async_ctx = 1,
770 .needs_mm = 1,
771 .needs_file = 1,
772 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700773 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700774 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700777 .async_ctx = 1,
778 .needs_mm = 1,
779 .needs_file = 1,
780 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700781 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700782 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700783 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .async_ctx = 1,
787 .needs_mm = 1,
788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_TIMEOUT_REMOVE] = {},
790 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700791 .needs_mm = 1,
792 .needs_file = 1,
793 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700794 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700795 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_ASYNC_CANCEL] = {},
798 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700799 .async_ctx = 1,
800 .needs_mm = 1,
801 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300802 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700803 .async_ctx = 1,
804 .needs_mm = 1,
805 .needs_file = 1,
806 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700807 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .needs_file = 1,
811 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300812 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700813 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700814 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700815 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600817 .needs_file = 1,
818 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700819 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700822 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700823 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700826 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700827 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600828 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700831 .needs_mm = 1,
832 .needs_file = 1,
833 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700834 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700835 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700836 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300837 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700838 .needs_mm = 1,
839 .needs_file = 1,
840 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700841 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700842 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300843 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700844 .needs_file = 1,
845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700847 .needs_mm = 1,
848 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300849 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700850 .needs_mm = 1,
851 .needs_file = 1,
852 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700853 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700856 .needs_mm = 1,
857 .needs_file = 1,
858 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700859 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700860 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700863 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700864 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700865 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700866 [IORING_OP_EPOLL_CTL] = {
867 .unbound_nonreg_file = 1,
868 .file_table = 1,
869 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300870 [IORING_OP_SPLICE] = {
871 .needs_file = 1,
872 .hash_reg_file = 1,
873 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700874 },
875 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700876 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300877 [IORING_OP_TEE] = {
878 .needs_file = 1,
879 .hash_reg_file = 1,
880 .unbound_nonreg_file = 1,
881 },
Jens Axboed3656342019-12-18 09:50:26 -0700882};
883
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700884enum io_mem_account {
885 ACCT_LOCKED,
886 ACCT_PINNED,
887};
888
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +0300889static bool io_rw_reissue(struct io_kiocb *req, long res);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700890static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800891static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600892static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700893static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700894static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
895static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700896static int __io_sqe_files_update(struct io_ring_ctx *ctx,
897 struct io_uring_files_update *ip,
898 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700899static int io_grab_files(struct io_kiocb *req);
Jens Axboe2237d762020-06-26 13:44:16 -0600900static void io_complete_rw_common(struct kiocb *kiocb, long res,
901 struct io_comp_state *cs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300902static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700903static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
904 int fd, struct file **out_file, bool fixed);
905static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600906 const struct io_uring_sqe *sqe,
907 struct io_comp_state *cs);
Jens Axboede0617e2019-04-06 21:51:27 -0600908
Jens Axboeb63534c2020-06-04 11:28:00 -0600909static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
910 struct iovec **iovec, struct iov_iter *iter,
911 bool needs_lock);
912static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
913 struct iovec *iovec, struct iovec *fast_iov,
914 struct iov_iter *iter);
915
Jens Axboe2b188cc2019-01-07 10:46:33 -0700916static struct kmem_cache *req_cachep;
917
918static const struct file_operations io_uring_fops;
919
920struct sock *io_uring_get_socket(struct file *file)
921{
922#if defined(CONFIG_UNIX)
923 if (file->f_op == &io_uring_fops) {
924 struct io_ring_ctx *ctx = file->private_data;
925
926 return ctx->ring_sock->sk;
927 }
928#endif
929 return NULL;
930}
931EXPORT_SYMBOL(io_uring_get_socket);
932
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300933static void io_get_req_task(struct io_kiocb *req)
934{
935 if (req->flags & REQ_F_TASK_PINNED)
936 return;
937 get_task_struct(req->task);
938 req->flags |= REQ_F_TASK_PINNED;
939}
940
941/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
942static void __io_put_req_task(struct io_kiocb *req)
943{
944 if (req->flags & REQ_F_TASK_PINNED)
945 put_task_struct(req->task);
946}
947
Jens Axboec40f6372020-06-25 15:39:59 -0600948static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
949{
950 struct mm_struct *mm = current->mm;
951
952 if (mm) {
953 kthread_unuse_mm(mm);
954 mmput(mm);
955 }
956}
957
958static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
959{
960 if (!current->mm) {
961 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
962 return -EFAULT;
963 kthread_use_mm(ctx->sqo_mm);
964 }
965
966 return 0;
967}
968
969static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
970 struct io_kiocb *req)
971{
972 if (!io_op_defs[req->opcode].needs_mm)
973 return 0;
974 return __io_sq_thread_acquire_mm(ctx);
975}
976
977static inline void req_set_fail_links(struct io_kiocb *req)
978{
979 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
980 req->flags |= REQ_F_FAIL_LINK;
981}
982
Jens Axboe4a38aed22020-05-14 17:21:15 -0600983static void io_file_put_work(struct work_struct *work);
984
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800985/*
986 * Note: must call io_req_init_async() for the first time you
987 * touch any members of io_wq_work.
988 */
989static inline void io_req_init_async(struct io_kiocb *req)
990{
991 if (req->flags & REQ_F_WORK_INITIALIZED)
992 return;
993
994 memset(&req->work, 0, sizeof(req->work));
995 req->flags |= REQ_F_WORK_INITIALIZED;
996}
997
Pavel Begunkov0cdaf762020-05-17 14:13:40 +0300998static inline bool io_async_submit(struct io_ring_ctx *ctx)
999{
1000 return ctx->flags & IORING_SETUP_SQPOLL;
1001}
1002
Jens Axboe2b188cc2019-01-07 10:46:33 -07001003static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1004{
1005 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1006
Jens Axboe0f158b42020-05-14 17:18:39 -06001007 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008}
1009
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001010static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1011{
1012 return !req->timeout.off;
1013}
1014
Jens Axboe2b188cc2019-01-07 10:46:33 -07001015static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1016{
1017 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001018 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001019
1020 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1021 if (!ctx)
1022 return NULL;
1023
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001024 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1025 if (!ctx->fallback_req)
1026 goto err;
1027
Jens Axboe78076bb2019-12-04 19:56:40 -07001028 /*
1029 * Use 5 bits less than the max cq entries, that should give us around
1030 * 32 entries per hash list if totally full and uniformly spread.
1031 */
1032 hash_bits = ilog2(p->cq_entries);
1033 hash_bits -= 5;
1034 if (hash_bits <= 0)
1035 hash_bits = 1;
1036 ctx->cancel_hash_bits = hash_bits;
1037 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1038 GFP_KERNEL);
1039 if (!ctx->cancel_hash)
1040 goto err;
1041 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1042
Roman Gushchin21482892019-05-07 10:01:48 -07001043 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001044 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1045 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046
1047 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001048 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001050 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001051 init_completion(&ctx->ref_comp);
1052 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001053 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001054 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001055 mutex_init(&ctx->uring_lock);
1056 init_waitqueue_head(&ctx->wait);
1057 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001058 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001059 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001060 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001061 init_waitqueue_head(&ctx->inflight_wait);
1062 spin_lock_init(&ctx->inflight_lock);
1063 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001064 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1065 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001066 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001067err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001068 if (ctx->fallback_req)
1069 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001070 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001071 kfree(ctx);
1072 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073}
1074
Bob Liu9d858b22019-11-13 18:06:25 +08001075static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06001076{
Jackie Liua197f662019-11-08 08:09:12 -07001077 struct io_ring_ctx *ctx = req->ctx;
1078
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001079 return req->sequence != ctx->cached_cq_tail
1080 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -06001081}
1082
Bob Liu9d858b22019-11-13 18:06:25 +08001083static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001084{
Pavel Begunkov87987892020-01-18 01:22:30 +03001085 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +08001086 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001087
Bob Liu9d858b22019-11-13 18:06:25 +08001088 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001089}
1090
Jens Axboede0617e2019-04-06 21:51:27 -06001091static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001092{
Hristo Venev75b28af2019-08-26 17:23:46 +00001093 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094
Pavel Begunkov07910152020-01-17 03:52:46 +03001095 /* order cqe stores with ring update */
1096 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001097
Pavel Begunkov07910152020-01-17 03:52:46 +03001098 if (wq_has_sleeper(&ctx->cq_wait)) {
1099 wake_up_interruptible(&ctx->cq_wait);
1100 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101 }
1102}
1103
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001104static void io_req_work_grab_env(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -06001105{
Pavel Begunkov351fd532020-06-29 19:18:40 +03001106 const struct io_op_def *def = &io_op_defs[req->opcode];
1107
Pavel Begunkovedcdfcc2020-06-29 19:18:41 +03001108 io_req_init_async(req);
1109
Jens Axboecccf0ee2020-01-27 16:34:48 -07001110 if (!req->work.mm && def->needs_mm) {
1111 mmgrab(current->mm);
1112 req->work.mm = current->mm;
1113 }
1114 if (!req->work.creds)
1115 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001116 if (!req->work.fs && def->needs_fs) {
1117 spin_lock(&current->fs->lock);
1118 if (!current->fs->in_exec) {
1119 req->work.fs = current->fs;
1120 req->work.fs->users++;
1121 } else {
1122 req->work.flags |= IO_WQ_WORK_CANCEL;
1123 }
1124 spin_unlock(&current->fs->lock);
1125 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001126}
1127
1128static inline void io_req_work_drop_env(struct io_kiocb *req)
1129{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001130 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1131 return;
1132
Jens Axboecccf0ee2020-01-27 16:34:48 -07001133 if (req->work.mm) {
1134 mmdrop(req->work.mm);
1135 req->work.mm = NULL;
1136 }
1137 if (req->work.creds) {
1138 put_cred(req->work.creds);
1139 req->work.creds = NULL;
1140 }
Jens Axboeff002b32020-02-07 16:05:21 -07001141 if (req->work.fs) {
1142 struct fs_struct *fs = req->work.fs;
1143
1144 spin_lock(&req->work.fs->lock);
1145 if (--fs->users)
1146 fs = NULL;
1147 spin_unlock(&req->work.fs->lock);
1148 if (fs)
1149 free_fs_struct(fs);
1150 }
Jens Axboe561fb042019-10-24 07:25:42 -06001151}
1152
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001153static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001154{
Jens Axboed3656342019-12-18 09:50:26 -07001155 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001156
Jens Axboed3656342019-12-18 09:50:26 -07001157 if (req->flags & REQ_F_ISREG) {
1158 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001159 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001160 } else {
1161 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001162 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001163 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001164
Pavel Begunkov351fd532020-06-29 19:18:40 +03001165 io_req_work_grab_env(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001166}
1167
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001168static void io_prep_async_link(struct io_kiocb *req)
1169{
1170 struct io_kiocb *cur;
1171
1172 io_prep_async_work(req);
1173 if (req->flags & REQ_F_LINK_HEAD)
1174 list_for_each_entry(cur, &req->link_list, link_list)
1175 io_prep_async_work(cur);
1176}
1177
1178static void __io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001179{
Jackie Liua197f662019-11-08 08:09:12 -07001180 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001181 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001182
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001183 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1184 &req->work, req->flags);
1185 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001186
1187 if (link)
1188 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001189}
1190
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001191static void io_queue_async_work(struct io_kiocb *req)
1192{
1193 /* init ->work of the whole link before punting */
1194 io_prep_async_link(req);
1195 __io_queue_async_work(req);
1196}
1197
Jens Axboe5262f562019-09-17 12:26:57 -06001198static void io_kill_timeout(struct io_kiocb *req)
1199{
1200 int ret;
1201
Jens Axboe2d283902019-12-04 11:08:05 -07001202 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001203 if (ret != -1) {
1204 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001205 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001206 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001207 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001208 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001209 }
1210}
1211
1212static void io_kill_timeouts(struct io_ring_ctx *ctx)
1213{
1214 struct io_kiocb *req, *tmp;
1215
1216 spin_lock_irq(&ctx->completion_lock);
1217 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1218 io_kill_timeout(req);
1219 spin_unlock_irq(&ctx->completion_lock);
1220}
1221
Pavel Begunkov04518942020-05-26 20:34:05 +03001222static void __io_queue_deferred(struct io_ring_ctx *ctx)
1223{
1224 do {
1225 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1226 struct io_kiocb, list);
1227
1228 if (req_need_defer(req))
1229 break;
1230 list_del_init(&req->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001231 /* punt-init is done before queueing for defer */
1232 __io_queue_async_work(req);
Pavel Begunkov04518942020-05-26 20:34:05 +03001233 } while (!list_empty(&ctx->defer_list));
1234}
1235
Pavel Begunkov360428f2020-05-30 14:54:17 +03001236static void io_flush_timeouts(struct io_ring_ctx *ctx)
1237{
1238 while (!list_empty(&ctx->timeout_list)) {
1239 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1240 struct io_kiocb, list);
1241
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001242 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001243 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001244 if (req->timeout.target_seq != ctx->cached_cq_tail
1245 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001246 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001247
Pavel Begunkov360428f2020-05-30 14:54:17 +03001248 list_del_init(&req->list);
1249 io_kill_timeout(req);
1250 }
1251}
1252
Jens Axboede0617e2019-04-06 21:51:27 -06001253static void io_commit_cqring(struct io_ring_ctx *ctx)
1254{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001255 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001256 __io_commit_cqring(ctx);
1257
Pavel Begunkov04518942020-05-26 20:34:05 +03001258 if (unlikely(!list_empty(&ctx->defer_list)))
1259 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001260}
1261
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1263{
Hristo Venev75b28af2019-08-26 17:23:46 +00001264 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265 unsigned tail;
1266
1267 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001268 /*
1269 * writes to the cq entry need to come after reading head; the
1270 * control dependency is enough as we're using WRITE_ONCE to
1271 * fill the cq entry
1272 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001273 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274 return NULL;
1275
1276 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001277 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278}
1279
Jens Axboef2842ab2020-01-08 11:04:00 -07001280static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1281{
Jens Axboef0b493e2020-02-01 21:30:11 -07001282 if (!ctx->cq_ev_fd)
1283 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001284 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1285 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001286 if (!ctx->eventfd_async)
1287 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001288 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001289}
1290
Jens Axboeb41e9852020-02-17 09:52:41 -07001291static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001292{
1293 if (waitqueue_active(&ctx->wait))
1294 wake_up(&ctx->wait);
1295 if (waitqueue_active(&ctx->sqo_wait))
1296 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001297 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001298 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001299}
1300
Jens Axboec4a2ed72019-11-21 21:01:26 -07001301/* Returns true if there are no backlogged entries after the flush */
1302static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001303{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001304 struct io_rings *rings = ctx->rings;
1305 struct io_uring_cqe *cqe;
1306 struct io_kiocb *req;
1307 unsigned long flags;
1308 LIST_HEAD(list);
1309
1310 if (!force) {
1311 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001312 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001313 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1314 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001315 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001316 }
1317
1318 spin_lock_irqsave(&ctx->completion_lock, flags);
1319
1320 /* if force is set, the ring is going away. always drop after that */
1321 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001322 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001323
Jens Axboec4a2ed72019-11-21 21:01:26 -07001324 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001325 while (!list_empty(&ctx->cq_overflow_list)) {
1326 cqe = io_get_cqring(ctx);
1327 if (!cqe && !force)
1328 break;
1329
1330 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1331 list);
1332 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001333 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001334 if (cqe) {
1335 WRITE_ONCE(cqe->user_data, req->user_data);
1336 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001337 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001338 } else {
1339 WRITE_ONCE(ctx->rings->cq_overflow,
1340 atomic_inc_return(&ctx->cached_cq_overflow));
1341 }
1342 }
1343
1344 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001345 if (cqe) {
1346 clear_bit(0, &ctx->sq_check_overflow);
1347 clear_bit(0, &ctx->cq_check_overflow);
1348 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001349 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1350 io_cqring_ev_posted(ctx);
1351
1352 while (!list_empty(&list)) {
1353 req = list_first_entry(&list, struct io_kiocb, list);
1354 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001355 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001356 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001357
1358 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001359}
1360
Jens Axboebcda7ba2020-02-23 16:42:51 -07001361static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001362{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001363 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364 struct io_uring_cqe *cqe;
1365
Jens Axboe78e19bb2019-11-06 15:21:34 -07001366 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001367
Jens Axboe2b188cc2019-01-07 10:46:33 -07001368 /*
1369 * If we can't get a cq entry, userspace overflowed the
1370 * submission (by quite a lot). Increment the overflow count in
1371 * the ring.
1372 */
1373 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001374 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001375 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001376 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001377 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001378 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379 WRITE_ONCE(ctx->rings->cq_overflow,
1380 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001381 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001382 if (list_empty(&ctx->cq_overflow_list)) {
1383 set_bit(0, &ctx->sq_check_overflow);
1384 set_bit(0, &ctx->cq_check_overflow);
1385 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001386 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001387 refcount_inc(&req->refs);
1388 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001389 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001390 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001391 }
1392}
1393
Jens Axboebcda7ba2020-02-23 16:42:51 -07001394static void io_cqring_fill_event(struct io_kiocb *req, long res)
1395{
1396 __io_cqring_fill_event(req, res, 0);
1397}
1398
Jens Axboee1e16092020-06-22 09:17:17 -06001399static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001401 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001402 unsigned long flags;
1403
1404 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001405 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406 io_commit_cqring(ctx);
1407 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1408
Jens Axboe8c838782019-03-12 15:48:16 -06001409 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410}
1411
Jens Axboe229a7b62020-06-22 10:13:11 -06001412static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001413{
Jens Axboe229a7b62020-06-22 10:13:11 -06001414 struct io_ring_ctx *ctx = cs->ctx;
1415
1416 spin_lock_irq(&ctx->completion_lock);
1417 while (!list_empty(&cs->list)) {
1418 struct io_kiocb *req;
1419
1420 req = list_first_entry(&cs->list, struct io_kiocb, list);
1421 list_del(&req->list);
1422 io_cqring_fill_event(req, req->result);
1423 if (!(req->flags & REQ_F_LINK_HEAD)) {
1424 req->flags |= REQ_F_COMP_LOCKED;
1425 io_put_req(req);
1426 } else {
1427 spin_unlock_irq(&ctx->completion_lock);
1428 io_put_req(req);
1429 spin_lock_irq(&ctx->completion_lock);
1430 }
1431 }
1432 io_commit_cqring(ctx);
1433 spin_unlock_irq(&ctx->completion_lock);
1434
1435 io_cqring_ev_posted(ctx);
1436 cs->nr = 0;
1437}
1438
1439static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1440 struct io_comp_state *cs)
1441{
1442 if (!cs) {
1443 io_cqring_add_event(req, res, cflags);
1444 io_put_req(req);
1445 } else {
1446 req->result = res;
1447 list_add_tail(&req->list, &cs->list);
1448 if (++cs->nr >= 32)
1449 io_submit_flush_completions(cs);
1450 }
Jens Axboee1e16092020-06-22 09:17:17 -06001451}
1452
1453static void io_req_complete(struct io_kiocb *req, long res)
1454{
Jens Axboe229a7b62020-06-22 10:13:11 -06001455 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001456}
1457
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001458static inline bool io_is_fallback_req(struct io_kiocb *req)
1459{
1460 return req == (struct io_kiocb *)
1461 ((unsigned long) req->ctx->fallback_req & ~1UL);
1462}
1463
1464static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1465{
1466 struct io_kiocb *req;
1467
1468 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001469 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001470 return req;
1471
1472 return NULL;
1473}
1474
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001475static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1476 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001477{
Jens Axboefd6fab22019-03-14 16:30:06 -06001478 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479 struct io_kiocb *req;
1480
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001481 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001482 size_t sz;
1483 int ret;
1484
1485 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001486 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1487
1488 /*
1489 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1490 * retry single alloc to be on the safe side.
1491 */
1492 if (unlikely(ret <= 0)) {
1493 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1494 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001495 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001496 ret = 1;
1497 }
Jens Axboe2579f912019-01-09 09:10:43 -07001498 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001499 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001500 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001501 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001502 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001503 }
1504
Jens Axboe2579f912019-01-09 09:10:43 -07001505 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001506fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001507 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001508}
1509
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001510static inline void io_put_file(struct io_kiocb *req, struct file *file,
1511 bool fixed)
1512{
1513 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001514 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001515 else
1516 fput(file);
1517}
1518
Pavel Begunkove6543a82020-06-28 12:52:30 +03001519static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001520{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001521 if (req->flags & REQ_F_NEED_CLEANUP)
1522 io_cleanup_req(req);
1523
YueHaibing96fd84d2020-01-07 22:22:44 +08001524 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001525 if (req->file)
1526 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001527 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001528 io_req_work_drop_env(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001529
Jens Axboefcb323c2019-10-24 12:39:47 -06001530 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001531 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001532 unsigned long flags;
1533
1534 spin_lock_irqsave(&ctx->inflight_lock, flags);
1535 list_del(&req->inflight_entry);
1536 if (waitqueue_active(&ctx->inflight_wait))
1537 wake_up(&ctx->inflight_wait);
1538 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1539 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001540}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001541
Pavel Begunkove6543a82020-06-28 12:52:30 +03001542static void __io_free_req(struct io_kiocb *req)
1543{
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001544 struct io_ring_ctx *ctx;
1545
Pavel Begunkove6543a82020-06-28 12:52:30 +03001546 io_dismantle_req(req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001547 ctx = req->ctx;
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001548 if (likely(!io_is_fallback_req(req)))
1549 kmem_cache_free(req_cachep, req);
1550 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001551 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1552 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001553}
1554
Jackie Liua197f662019-11-08 08:09:12 -07001555static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001556{
Jackie Liua197f662019-11-08 08:09:12 -07001557 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001558 int ret;
1559
Jens Axboe2d283902019-12-04 11:08:05 -07001560 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001561 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001562 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001563 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001564 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001565 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001566 return true;
1567 }
1568
1569 return false;
1570}
1571
Jens Axboeab0b6452020-06-30 08:43:15 -06001572static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001573{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001574 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001575 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001576
1577 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001578 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001579 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1580 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001581 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001582
1583 list_del_init(&link->link_list);
1584 wake_ev = io_link_cancel_timeout(link);
1585 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001586 return wake_ev;
1587}
1588
1589static void io_kill_linked_timeout(struct io_kiocb *req)
1590{
1591 struct io_ring_ctx *ctx = req->ctx;
1592 bool wake_ev;
1593
1594 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1595 unsigned long flags;
1596
1597 spin_lock_irqsave(&ctx->completion_lock, flags);
1598 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001599 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001600 } else {
1601 wake_ev = __io_kill_linked_timeout(req);
1602 }
1603
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001604 if (wake_ev)
1605 io_cqring_ev_posted(ctx);
1606}
1607
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001608static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001609{
1610 struct io_kiocb *nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001611
1612 /*
1613 * The list should never be empty when we are called here. But could
1614 * potentially happen if the chain is messed up, check to be on the
1615 * safe side.
1616 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001617 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001618 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001619
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001620 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1621 list_del_init(&req->link_list);
1622 if (!list_empty(&nxt->link_list))
1623 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001624 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001625}
1626
1627/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001628 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001629 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001630static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001631{
Jens Axboe2665abf2019-11-05 12:40:47 -07001632 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001633
1634 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001635 struct io_kiocb *link = list_first_entry(&req->link_list,
1636 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001637
Pavel Begunkov44932332019-12-05 16:16:35 +03001638 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001639 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001640
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001641 io_cqring_fill_event(link, -ECANCELED);
1642 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001643 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001644 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001645
1646 io_commit_cqring(ctx);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001647 io_cqring_ev_posted(ctx);
1648}
1649
1650static void io_fail_links(struct io_kiocb *req)
1651{
1652 struct io_ring_ctx *ctx = req->ctx;
1653
1654 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1655 unsigned long flags;
1656
1657 spin_lock_irqsave(&ctx->completion_lock, flags);
1658 __io_fail_links(req);
1659 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1660 } else {
1661 __io_fail_links(req);
1662 }
1663
Jens Axboe2665abf2019-11-05 12:40:47 -07001664 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001665}
1666
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001667static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001668{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001669 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001670 return NULL;
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001671 req->flags &= ~REQ_F_LINK_HEAD;
Jens Axboe2665abf2019-11-05 12:40:47 -07001672
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001673 if (req->flags & REQ_F_LINK_TIMEOUT)
1674 io_kill_linked_timeout(req);
1675
Jens Axboe9e645e112019-05-10 16:07:28 -06001676 /*
1677 * If LINK is set, we have dependent requests in this chain. If we
1678 * didn't fail this request, queue the first one up, moving any other
1679 * dependencies to the next request. In case of failure, fail the rest
1680 * of the chain.
1681 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001682 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1683 return io_req_link_next(req);
1684 io_fail_links(req);
1685 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001686}
Jens Axboe9e645e112019-05-10 16:07:28 -06001687
Jens Axboec40f6372020-06-25 15:39:59 -06001688static void __io_req_task_cancel(struct io_kiocb *req, int error)
1689{
1690 struct io_ring_ctx *ctx = req->ctx;
1691
1692 spin_lock_irq(&ctx->completion_lock);
1693 io_cqring_fill_event(req, error);
1694 io_commit_cqring(ctx);
1695 spin_unlock_irq(&ctx->completion_lock);
1696
1697 io_cqring_ev_posted(ctx);
1698 req_set_fail_links(req);
1699 io_double_put_req(req);
1700}
1701
1702static void io_req_task_cancel(struct callback_head *cb)
1703{
1704 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1705
1706 __io_req_task_cancel(req, -ECANCELED);
1707}
1708
1709static void __io_req_task_submit(struct io_kiocb *req)
1710{
1711 struct io_ring_ctx *ctx = req->ctx;
1712
1713 __set_current_state(TASK_RUNNING);
1714 if (!__io_sq_thread_acquire_mm(ctx)) {
1715 mutex_lock(&ctx->uring_lock);
1716 __io_queue_sqe(req, NULL, NULL);
1717 mutex_unlock(&ctx->uring_lock);
1718 } else {
1719 __io_req_task_cancel(req, -EFAULT);
1720 }
1721}
1722
1723static void io_req_task_submit(struct callback_head *cb)
1724{
1725 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1726
1727 __io_req_task_submit(req);
1728}
1729
1730static void io_req_task_queue(struct io_kiocb *req)
1731{
1732 struct task_struct *tsk = req->task;
1733 int ret;
1734
1735 init_task_work(&req->task_work, io_req_task_submit);
1736
1737 ret = task_work_add(tsk, &req->task_work, true);
1738 if (unlikely(ret)) {
1739 init_task_work(&req->task_work, io_req_task_cancel);
1740 tsk = io_wq_get_task(req->ctx->io_wq);
1741 task_work_add(tsk, &req->task_work, true);
1742 }
1743 wake_up_process(tsk);
1744}
1745
Pavel Begunkovc3524382020-06-28 12:52:32 +03001746static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001747{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001748 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001749
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001750 if (nxt)
1751 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001752}
1753
Pavel Begunkovc3524382020-06-28 12:52:32 +03001754static void io_free_req(struct io_kiocb *req)
1755{
1756 io_queue_next(req);
1757 __io_free_req(req);
1758}
1759
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001760struct req_batch {
1761 void *reqs[IO_IOPOLL_BATCH];
1762 int to_free;
1763};
1764
1765static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1766 struct req_batch *rb)
1767{
1768 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1769 percpu_ref_put_many(&ctx->refs, rb->to_free);
1770 rb->to_free = 0;
1771}
1772
1773static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1774 struct req_batch *rb)
1775{
1776 if (rb->to_free)
1777 __io_req_free_batch_flush(ctx, rb);
1778}
1779
1780static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1781{
1782 if (unlikely(io_is_fallback_req(req))) {
1783 io_free_req(req);
1784 return;
1785 }
1786 if (req->flags & REQ_F_LINK_HEAD)
1787 io_queue_next(req);
1788
1789 io_dismantle_req(req);
1790 rb->reqs[rb->to_free++] = req;
1791 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1792 __io_req_free_batch_flush(req->ctx, rb);
1793}
1794
Jens Axboeba816ad2019-09-28 11:36:45 -06001795/*
1796 * Drop reference to request, return next in chain (if there is one) if this
1797 * was the last reference to this request.
1798 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001799static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001800{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001801 struct io_kiocb *nxt = NULL;
1802
Jens Axboe2a44f462020-02-25 13:25:41 -07001803 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001804 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001805 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001806 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001807 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001808}
1809
Jens Axboe2b188cc2019-01-07 10:46:33 -07001810static void io_put_req(struct io_kiocb *req)
1811{
Jens Axboedef596e2019-01-09 08:59:42 -07001812 if (refcount_dec_and_test(&req->refs))
1813 io_free_req(req);
1814}
1815
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001816static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001817{
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001818 struct io_kiocb *timeout, *nxt = NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001819
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001820 /*
1821 * A ref is owned by io-wq in which context we're. So, if that's the
1822 * last one, it's safe to steal next work. False negatives are Ok,
1823 * it just will be re-punted async in io_put_work()
1824 */
1825 if (refcount_read(&req->refs) != 1)
1826 return NULL;
1827
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001828 nxt = io_req_find_next(req);
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001829 if (!nxt)
1830 return NULL;
1831
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001832 timeout = io_prep_linked_timeout(nxt);
1833 if (timeout)
1834 nxt->flags |= REQ_F_QUEUE_TIMEOUT;
1835 return &nxt->work;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001836}
1837
Jens Axboe978db572019-11-14 22:39:04 -07001838/*
1839 * Must only be used if we don't need to care about links, usually from
1840 * within the completion handling itself.
1841 */
1842static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001843{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001844 /* drop both submit and complete references */
1845 if (refcount_sub_and_test(2, &req->refs))
1846 __io_free_req(req);
1847}
1848
Jens Axboe978db572019-11-14 22:39:04 -07001849static void io_double_put_req(struct io_kiocb *req)
1850{
1851 /* drop both submit and complete references */
1852 if (refcount_sub_and_test(2, &req->refs))
1853 io_free_req(req);
1854}
1855
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001856static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001857{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001858 struct io_rings *rings = ctx->rings;
1859
Jens Axboead3eb2c2019-12-18 17:12:20 -07001860 if (test_bit(0, &ctx->cq_check_overflow)) {
1861 /*
1862 * noflush == true is from the waitqueue handler, just ensure
1863 * we wake up the task, and the next invocation will flush the
1864 * entries. We cannot safely to it from here.
1865 */
1866 if (noflush && !list_empty(&ctx->cq_overflow_list))
1867 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001868
Jens Axboead3eb2c2019-12-18 17:12:20 -07001869 io_cqring_overflow_flush(ctx, false);
1870 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001871
Jens Axboea3a0e432019-08-20 11:03:11 -06001872 /* See comment at the top of this file */
1873 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001874 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001875}
1876
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001877static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1878{
1879 struct io_rings *rings = ctx->rings;
1880
1881 /* make sure SQ entry isn't read before tail */
1882 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1883}
1884
Jens Axboebcda7ba2020-02-23 16:42:51 -07001885static int io_put_kbuf(struct io_kiocb *req)
1886{
Jens Axboe4d954c22020-02-27 07:31:19 -07001887 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001888 int cflags;
1889
Jens Axboe4d954c22020-02-27 07:31:19 -07001890 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001891 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1892 cflags |= IORING_CQE_F_BUFFER;
1893 req->rw.addr = 0;
1894 kfree(kbuf);
1895 return cflags;
1896}
1897
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001898static void io_iopoll_queue(struct list_head *again)
1899{
1900 struct io_kiocb *req;
1901
1902 do {
1903 req = list_first_entry(again, struct io_kiocb, list);
1904 list_del(&req->list);
Pavel Begunkovcf2f5422020-06-30 15:20:40 +03001905 if (!io_rw_reissue(req, -EAGAIN))
Jens Axboe2237d762020-06-26 13:44:16 -06001906 io_complete_rw_common(&req->rw.kiocb, -EAGAIN, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001907 } while (!list_empty(again));
1908}
1909
Jens Axboedef596e2019-01-09 08:59:42 -07001910/*
1911 * Find and free completed poll iocbs
1912 */
1913static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1914 struct list_head *done)
1915{
Jens Axboe8237e042019-12-28 10:48:22 -07001916 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001917 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001918 LIST_HEAD(again);
1919
1920 /* order with ->result store in io_complete_rw_iopoll() */
1921 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001922
Pavel Begunkov2757a232020-06-28 12:52:31 +03001923 rb.to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001924 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001925 int cflags = 0;
1926
Jens Axboedef596e2019-01-09 08:59:42 -07001927 req = list_first_entry(done, struct io_kiocb, list);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001928 if (READ_ONCE(req->result) == -EAGAIN) {
1929 req->iopoll_completed = 0;
1930 list_move_tail(&req->list, &again);
1931 continue;
1932 }
Jens Axboedef596e2019-01-09 08:59:42 -07001933 list_del(&req->list);
1934
Jens Axboebcda7ba2020-02-23 16:42:51 -07001935 if (req->flags & REQ_F_BUFFER_SELECTED)
1936 cflags = io_put_kbuf(req);
1937
1938 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001939 (*nr_events)++;
1940
Pavel Begunkovc3524382020-06-28 12:52:32 +03001941 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001942 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07001943 }
Jens Axboedef596e2019-01-09 08:59:42 -07001944
Jens Axboe09bb8392019-03-13 12:39:28 -06001945 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001946 if (ctx->flags & IORING_SETUP_SQPOLL)
1947 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001948 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001949
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001950 if (!list_empty(&again))
1951 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001952}
1953
Jens Axboedef596e2019-01-09 08:59:42 -07001954static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1955 long min)
1956{
1957 struct io_kiocb *req, *tmp;
1958 LIST_HEAD(done);
1959 bool spin;
1960 int ret;
1961
1962 /*
1963 * Only spin for completions if we don't have multiple devices hanging
1964 * off our complete list, and we're under the requested amount.
1965 */
1966 spin = !ctx->poll_multi_file && *nr_events < min;
1967
1968 ret = 0;
1969 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001970 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001971
1972 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001973 * Move completed and retryable entries to our local lists.
1974 * If we find a request that requires polling, break out
1975 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001976 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08001977 if (READ_ONCE(req->iopoll_completed)) {
Jens Axboedef596e2019-01-09 08:59:42 -07001978 list_move_tail(&req->list, &done);
1979 continue;
1980 }
1981 if (!list_empty(&done))
1982 break;
1983
1984 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1985 if (ret < 0)
1986 break;
1987
1988 if (ret && spin)
1989 spin = false;
1990 ret = 0;
1991 }
1992
1993 if (!list_empty(&done))
1994 io_iopoll_complete(ctx, nr_events, &done);
1995
1996 return ret;
1997}
1998
1999/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002000 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002001 * non-spinning poll check - we'll still enter the driver poll loop, but only
2002 * as a non-spinning completion check.
2003 */
2004static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2005 long min)
2006{
Jens Axboe08f54392019-08-21 22:19:11 -06002007 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002008 int ret;
2009
2010 ret = io_do_iopoll(ctx, nr_events, min);
2011 if (ret < 0)
2012 return ret;
2013 if (!min || *nr_events >= min)
2014 return 0;
2015 }
2016
2017 return 1;
2018}
2019
2020/*
2021 * We can't just wait for polled events to come to us, we have to actively
2022 * find and complete them.
2023 */
2024static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
2025{
2026 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2027 return;
2028
2029 mutex_lock(&ctx->uring_lock);
2030 while (!list_empty(&ctx->poll_list)) {
2031 unsigned int nr_events = 0;
2032
2033 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06002034
2035 /*
2036 * Ensure we allow local-to-the-cpu processing to take place,
2037 * in this case we need to ensure that we reap all events.
2038 */
2039 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07002040 }
2041 mutex_unlock(&ctx->uring_lock);
2042}
2043
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002044static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
2045 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002046{
Jens Axboe2b2ed972019-10-25 10:06:15 -06002047 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002048
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002049 /*
2050 * We disallow the app entering submit/complete with polling, but we
2051 * still need to lock the ring to prevent racing with polled issue
2052 * that got punted to a workqueue.
2053 */
2054 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002055 do {
2056 int tmin = 0;
2057
Jens Axboe500f9fb2019-08-19 12:15:59 -06002058 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002059 * Don't enter poll loop if we already have events pending.
2060 * If we do, we can potentially be spinning for commands that
2061 * already triggered a CQE (eg in error).
2062 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002063 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002064 break;
2065
2066 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002067 * If a submit got punted to a workqueue, we can have the
2068 * application entering polling for a command before it gets
2069 * issued. That app will hold the uring_lock for the duration
2070 * of the poll right here, so we need to take a breather every
2071 * now and then to ensure that the issue has a chance to add
2072 * the poll to the issued list. Otherwise we can spin here
2073 * forever, while the workqueue is stuck trying to acquire the
2074 * very same mutex.
2075 */
2076 if (!(++iters & 7)) {
2077 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov3adfeca2020-06-28 12:52:37 +03002078 if (current->task_works)
2079 task_work_run();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002080 mutex_lock(&ctx->uring_lock);
2081 }
2082
Jens Axboedef596e2019-01-09 08:59:42 -07002083 if (*nr_events < min)
2084 tmin = min - *nr_events;
2085
2086 ret = io_iopoll_getevents(ctx, nr_events, tmin);
2087 if (ret <= 0)
2088 break;
2089 ret = 0;
2090 } while (min && !*nr_events && !need_resched());
2091
Jens Axboe500f9fb2019-08-19 12:15:59 -06002092 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002093 return ret;
2094}
2095
Jens Axboe491381ce2019-10-17 09:20:46 -06002096static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002097{
Jens Axboe491381ce2019-10-17 09:20:46 -06002098 /*
2099 * Tell lockdep we inherited freeze protection from submission
2100 * thread.
2101 */
2102 if (req->flags & REQ_F_ISREG) {
2103 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002104
Jens Axboe491381ce2019-10-17 09:20:46 -06002105 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002106 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002107 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002108}
2109
Jens Axboea1d7c392020-06-22 11:09:46 -06002110static void io_complete_rw_common(struct kiocb *kiocb, long res,
2111 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002112{
Jens Axboe9adbd452019-12-20 08:45:55 -07002113 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002114 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002115
Jens Axboe491381ce2019-10-17 09:20:46 -06002116 if (kiocb->ki_flags & IOCB_WRITE)
2117 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002118
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002119 if (res != req->result)
2120 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002121 if (req->flags & REQ_F_BUFFER_SELECTED)
2122 cflags = io_put_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002123 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002124}
2125
Jens Axboeb63534c2020-06-04 11:28:00 -06002126#ifdef CONFIG_BLOCK
2127static bool io_resubmit_prep(struct io_kiocb *req, int error)
2128{
2129 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2130 ssize_t ret = -ECANCELED;
2131 struct iov_iter iter;
2132 int rw;
2133
2134 if (error) {
2135 ret = error;
2136 goto end_req;
2137 }
2138
2139 switch (req->opcode) {
2140 case IORING_OP_READV:
2141 case IORING_OP_READ_FIXED:
2142 case IORING_OP_READ:
2143 rw = READ;
2144 break;
2145 case IORING_OP_WRITEV:
2146 case IORING_OP_WRITE_FIXED:
2147 case IORING_OP_WRITE:
2148 rw = WRITE;
2149 break;
2150 default:
2151 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2152 req->opcode);
2153 goto end_req;
2154 }
2155
2156 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2157 if (ret < 0)
2158 goto end_req;
2159 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2160 if (!ret)
2161 return true;
2162 kfree(iovec);
2163end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002164 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002165 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002166 return false;
2167}
2168
2169static void io_rw_resubmit(struct callback_head *cb)
2170{
2171 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2172 struct io_ring_ctx *ctx = req->ctx;
2173 int err;
2174
2175 __set_current_state(TASK_RUNNING);
2176
2177 err = io_sq_thread_acquire_mm(ctx, req);
2178
2179 if (io_resubmit_prep(req, err)) {
2180 refcount_inc(&req->refs);
2181 io_queue_async_work(req);
2182 }
2183}
2184#endif
2185
2186static bool io_rw_reissue(struct io_kiocb *req, long res)
2187{
2188#ifdef CONFIG_BLOCK
2189 struct task_struct *tsk;
2190 int ret;
2191
2192 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2193 return false;
2194
2195 tsk = req->task;
2196 init_task_work(&req->task_work, io_rw_resubmit);
2197 ret = task_work_add(tsk, &req->task_work, true);
Pavel Begunkovfb492782020-06-29 12:59:48 +03002198 if (!ret) {
2199 wake_up_process(tsk);
Jens Axboeb63534c2020-06-04 11:28:00 -06002200 return true;
Pavel Begunkovfb492782020-06-29 12:59:48 +03002201 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002202#endif
2203 return false;
2204}
2205
Jens Axboea1d7c392020-06-22 11:09:46 -06002206static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2207 struct io_comp_state *cs)
2208{
2209 if (!io_rw_reissue(req, res))
2210 io_complete_rw_common(&req->rw.kiocb, res, cs);
2211}
2212
Jens Axboeba816ad2019-09-28 11:36:45 -06002213static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2214{
Jens Axboe9adbd452019-12-20 08:45:55 -07002215 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002216
Jens Axboea1d7c392020-06-22 11:09:46 -06002217 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002218}
2219
Jens Axboedef596e2019-01-09 08:59:42 -07002220static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2221{
Jens Axboe9adbd452019-12-20 08:45:55 -07002222 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002223
Jens Axboe491381ce2019-10-17 09:20:46 -06002224 if (kiocb->ki_flags & IOCB_WRITE)
2225 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002226
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002227 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002228 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002229
2230 WRITE_ONCE(req->result, res);
2231 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002232 smp_wmb();
2233 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002234}
2235
2236/*
2237 * After the iocb has been issued, it's safe to be found on the poll list.
2238 * Adding the kiocb to the list AFTER submission ensures that we don't
2239 * find it from a io_iopoll_getevents() thread before the issuer is done
2240 * accessing the kiocb cookie.
2241 */
2242static void io_iopoll_req_issued(struct io_kiocb *req)
2243{
2244 struct io_ring_ctx *ctx = req->ctx;
2245
2246 /*
2247 * Track whether we have multiple files in our lists. This will impact
2248 * how we do polling eventually, not spinning if we're on potentially
2249 * different devices.
2250 */
2251 if (list_empty(&ctx->poll_list)) {
2252 ctx->poll_multi_file = false;
2253 } else if (!ctx->poll_multi_file) {
2254 struct io_kiocb *list_req;
2255
2256 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2257 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002258 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002259 ctx->poll_multi_file = true;
2260 }
2261
2262 /*
2263 * For fast devices, IO may have already completed. If it has, add
2264 * it to the front so we find it first.
2265 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002266 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002267 list_add(&req->list, &ctx->poll_list);
2268 else
2269 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002270
2271 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2272 wq_has_sleeper(&ctx->sqo_wait))
2273 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002274}
2275
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002276static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002277{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002278 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002279
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002280 if (diff)
2281 fput_many(state->file, diff);
2282 state->file = NULL;
2283}
2284
2285static inline void io_state_file_put(struct io_submit_state *state)
2286{
2287 if (state->file)
2288 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002289}
2290
2291/*
2292 * Get as many references to a file as we have IOs left in this submission,
2293 * assuming most submissions are for one file, or at least that each file
2294 * has more than one submission.
2295 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002296static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002297{
2298 if (!state)
2299 return fget(fd);
2300
2301 if (state->file) {
2302 if (state->fd == fd) {
2303 state->used_refs++;
2304 state->ios_left--;
2305 return state->file;
2306 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002307 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002308 }
2309 state->file = fget_many(fd, state->ios_left);
2310 if (!state->file)
2311 return NULL;
2312
2313 state->fd = fd;
2314 state->has_refs = state->ios_left;
2315 state->used_refs = 1;
2316 state->ios_left--;
2317 return state->file;
2318}
2319
Jens Axboe4503b762020-06-01 10:00:27 -06002320static bool io_bdev_nowait(struct block_device *bdev)
2321{
2322#ifdef CONFIG_BLOCK
2323 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2324#else
2325 return true;
2326#endif
2327}
2328
Jens Axboe2b188cc2019-01-07 10:46:33 -07002329/*
2330 * If we tracked the file through the SCM inflight mechanism, we could support
2331 * any file. For now, just ensure that anything potentially problematic is done
2332 * inline.
2333 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002334static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002335{
2336 umode_t mode = file_inode(file)->i_mode;
2337
Jens Axboe4503b762020-06-01 10:00:27 -06002338 if (S_ISBLK(mode)) {
2339 if (io_bdev_nowait(file->f_inode->i_bdev))
2340 return true;
2341 return false;
2342 }
2343 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002344 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002345 if (S_ISREG(mode)) {
2346 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2347 file->f_op != &io_uring_fops)
2348 return true;
2349 return false;
2350 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351
Jens Axboec5b85622020-06-09 19:23:05 -06002352 /* any ->read/write should understand O_NONBLOCK */
2353 if (file->f_flags & O_NONBLOCK)
2354 return true;
2355
Jens Axboeaf197f52020-04-28 13:15:06 -06002356 if (!(file->f_mode & FMODE_NOWAIT))
2357 return false;
2358
2359 if (rw == READ)
2360 return file->f_op->read_iter != NULL;
2361
2362 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002363}
2364
Jens Axboe3529d8c2019-12-19 18:24:38 -07002365static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2366 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367{
Jens Axboedef596e2019-01-09 08:59:42 -07002368 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002369 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002370 unsigned ioprio;
2371 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002372
Jens Axboe491381ce2019-10-17 09:20:46 -06002373 if (S_ISREG(file_inode(req->file)->i_mode))
2374 req->flags |= REQ_F_ISREG;
2375
Jens Axboe2b188cc2019-01-07 10:46:33 -07002376 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002377 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2378 req->flags |= REQ_F_CUR_POS;
2379 kiocb->ki_pos = req->file->f_pos;
2380 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002381 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002382 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2383 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2384 if (unlikely(ret))
2385 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002386
2387 ioprio = READ_ONCE(sqe->ioprio);
2388 if (ioprio) {
2389 ret = ioprio_check_cap(ioprio);
2390 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002391 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392
2393 kiocb->ki_ioprio = ioprio;
2394 } else
2395 kiocb->ki_ioprio = get_current_ioprio();
2396
Stefan Bühler8449eed2019-04-27 20:34:19 +02002397 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002398 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002399 req->flags |= REQ_F_NOWAIT;
2400
Jens Axboeb63534c2020-06-04 11:28:00 -06002401 if (kiocb->ki_flags & IOCB_DIRECT)
2402 io_get_req_task(req);
2403
Stefan Bühler8449eed2019-04-27 20:34:19 +02002404 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002405 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002406
Jens Axboedef596e2019-01-09 08:59:42 -07002407 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002408 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2409 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002410 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002411
Jens Axboedef596e2019-01-09 08:59:42 -07002412 kiocb->ki_flags |= IOCB_HIPRI;
2413 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002414 req->iopoll_completed = 0;
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +03002415 io_get_req_task(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002416 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002417 if (kiocb->ki_flags & IOCB_HIPRI)
2418 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002419 kiocb->ki_complete = io_complete_rw;
2420 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002421
Jens Axboe3529d8c2019-12-19 18:24:38 -07002422 req->rw.addr = READ_ONCE(sqe->addr);
2423 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002424 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002425 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002426}
2427
2428static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2429{
2430 switch (ret) {
2431 case -EIOCBQUEUED:
2432 break;
2433 case -ERESTARTSYS:
2434 case -ERESTARTNOINTR:
2435 case -ERESTARTNOHAND:
2436 case -ERESTART_RESTARTBLOCK:
2437 /*
2438 * We can't just restart the syscall, since previously
2439 * submitted sqes may already be in progress. Just fail this
2440 * IO with EINTR.
2441 */
2442 ret = -EINTR;
2443 /* fall through */
2444 default:
2445 kiocb->ki_complete(kiocb, ret, 0);
2446 }
2447}
2448
Jens Axboea1d7c392020-06-22 11:09:46 -06002449static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2450 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002451{
Jens Axboeba042912019-12-25 16:33:42 -07002452 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2453
2454 if (req->flags & REQ_F_CUR_POS)
2455 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002456 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002457 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002458 else
2459 io_rw_done(kiocb, ret);
2460}
2461
Jens Axboe9adbd452019-12-20 08:45:55 -07002462static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002463 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002464{
Jens Axboe9adbd452019-12-20 08:45:55 -07002465 struct io_ring_ctx *ctx = req->ctx;
2466 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002467 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002468 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002469 size_t offset;
2470 u64 buf_addr;
2471
2472 /* attempt to use fixed buffers without having provided iovecs */
2473 if (unlikely(!ctx->user_bufs))
2474 return -EFAULT;
2475
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002476 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002477 if (unlikely(buf_index >= ctx->nr_user_bufs))
2478 return -EFAULT;
2479
2480 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2481 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002482 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002483
2484 /* overflow */
2485 if (buf_addr + len < buf_addr)
2486 return -EFAULT;
2487 /* not inside the mapped region */
2488 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2489 return -EFAULT;
2490
2491 /*
2492 * May not be a start of buffer, set size appropriately
2493 * and advance us to the beginning.
2494 */
2495 offset = buf_addr - imu->ubuf;
2496 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002497
2498 if (offset) {
2499 /*
2500 * Don't use iov_iter_advance() here, as it's really slow for
2501 * using the latter parts of a big fixed buffer - it iterates
2502 * over each segment manually. We can cheat a bit here, because
2503 * we know that:
2504 *
2505 * 1) it's a BVEC iter, we set it up
2506 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2507 * first and last bvec
2508 *
2509 * So just find our index, and adjust the iterator afterwards.
2510 * If the offset is within the first bvec (or the whole first
2511 * bvec, just use iov_iter_advance(). This makes it easier
2512 * since we can just skip the first segment, which may not
2513 * be PAGE_SIZE aligned.
2514 */
2515 const struct bio_vec *bvec = imu->bvec;
2516
2517 if (offset <= bvec->bv_len) {
2518 iov_iter_advance(iter, offset);
2519 } else {
2520 unsigned long seg_skip;
2521
2522 /* skip first vec */
2523 offset -= bvec->bv_len;
2524 seg_skip = 1 + (offset >> PAGE_SHIFT);
2525
2526 iter->bvec = bvec + seg_skip;
2527 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002528 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002529 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002530 }
2531 }
2532
Jens Axboe5e559562019-11-13 16:12:46 -07002533 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002534}
2535
Jens Axboebcda7ba2020-02-23 16:42:51 -07002536static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2537{
2538 if (needs_lock)
2539 mutex_unlock(&ctx->uring_lock);
2540}
2541
2542static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2543{
2544 /*
2545 * "Normal" inline submissions always hold the uring_lock, since we
2546 * grab it from the system call. Same is true for the SQPOLL offload.
2547 * The only exception is when we've detached the request and issue it
2548 * from an async worker thread, grab the lock for that case.
2549 */
2550 if (needs_lock)
2551 mutex_lock(&ctx->uring_lock);
2552}
2553
2554static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2555 int bgid, struct io_buffer *kbuf,
2556 bool needs_lock)
2557{
2558 struct io_buffer *head;
2559
2560 if (req->flags & REQ_F_BUFFER_SELECTED)
2561 return kbuf;
2562
2563 io_ring_submit_lock(req->ctx, needs_lock);
2564
2565 lockdep_assert_held(&req->ctx->uring_lock);
2566
2567 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2568 if (head) {
2569 if (!list_empty(&head->list)) {
2570 kbuf = list_last_entry(&head->list, struct io_buffer,
2571 list);
2572 list_del(&kbuf->list);
2573 } else {
2574 kbuf = head;
2575 idr_remove(&req->ctx->io_buffer_idr, bgid);
2576 }
2577 if (*len > kbuf->len)
2578 *len = kbuf->len;
2579 } else {
2580 kbuf = ERR_PTR(-ENOBUFS);
2581 }
2582
2583 io_ring_submit_unlock(req->ctx, needs_lock);
2584
2585 return kbuf;
2586}
2587
Jens Axboe4d954c22020-02-27 07:31:19 -07002588static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2589 bool needs_lock)
2590{
2591 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002592 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002593
2594 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002595 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002596 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2597 if (IS_ERR(kbuf))
2598 return kbuf;
2599 req->rw.addr = (u64) (unsigned long) kbuf;
2600 req->flags |= REQ_F_BUFFER_SELECTED;
2601 return u64_to_user_ptr(kbuf->addr);
2602}
2603
2604#ifdef CONFIG_COMPAT
2605static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2606 bool needs_lock)
2607{
2608 struct compat_iovec __user *uiov;
2609 compat_ssize_t clen;
2610 void __user *buf;
2611 ssize_t len;
2612
2613 uiov = u64_to_user_ptr(req->rw.addr);
2614 if (!access_ok(uiov, sizeof(*uiov)))
2615 return -EFAULT;
2616 if (__get_user(clen, &uiov->iov_len))
2617 return -EFAULT;
2618 if (clen < 0)
2619 return -EINVAL;
2620
2621 len = clen;
2622 buf = io_rw_buffer_select(req, &len, needs_lock);
2623 if (IS_ERR(buf))
2624 return PTR_ERR(buf);
2625 iov[0].iov_base = buf;
2626 iov[0].iov_len = (compat_size_t) len;
2627 return 0;
2628}
2629#endif
2630
2631static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2632 bool needs_lock)
2633{
2634 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2635 void __user *buf;
2636 ssize_t len;
2637
2638 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2639 return -EFAULT;
2640
2641 len = iov[0].iov_len;
2642 if (len < 0)
2643 return -EINVAL;
2644 buf = io_rw_buffer_select(req, &len, needs_lock);
2645 if (IS_ERR(buf))
2646 return PTR_ERR(buf);
2647 iov[0].iov_base = buf;
2648 iov[0].iov_len = len;
2649 return 0;
2650}
2651
2652static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2653 bool needs_lock)
2654{
Jens Axboedddb3e22020-06-04 11:27:01 -06002655 if (req->flags & REQ_F_BUFFER_SELECTED) {
2656 struct io_buffer *kbuf;
2657
2658 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2659 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2660 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002661 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002662 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002663 if (!req->rw.len)
2664 return 0;
2665 else if (req->rw.len > 1)
2666 return -EINVAL;
2667
2668#ifdef CONFIG_COMPAT
2669 if (req->ctx->compat)
2670 return io_compat_import(req, iov, needs_lock);
2671#endif
2672
2673 return __io_iov_buffer_select(req, iov, needs_lock);
2674}
2675
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002676static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002677 struct iovec **iovec, struct iov_iter *iter,
2678 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679{
Jens Axboe9adbd452019-12-20 08:45:55 -07002680 void __user *buf = u64_to_user_ptr(req->rw.addr);
2681 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002682 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002683 u8 opcode;
2684
Jens Axboed625c6e2019-12-17 19:53:05 -07002685 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002686 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002687 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002688 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002689 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002690
Jens Axboebcda7ba2020-02-23 16:42:51 -07002691 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002692 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002693 return -EINVAL;
2694
Jens Axboe3a6820f2019-12-22 15:19:35 -07002695 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002696 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002697 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2698 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002699 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002700 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002701 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002702 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002703 }
2704
Jens Axboe3a6820f2019-12-22 15:19:35 -07002705 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2706 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002707 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002708 }
2709
Jens Axboef67676d2019-12-02 11:03:47 -07002710 if (req->io) {
2711 struct io_async_rw *iorw = &req->io->rw;
2712
2713 *iovec = iorw->iov;
2714 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2715 if (iorw->iov == iorw->fast_iov)
2716 *iovec = NULL;
2717 return iorw->size;
2718 }
2719
Jens Axboe4d954c22020-02-27 07:31:19 -07002720 if (req->flags & REQ_F_BUFFER_SELECT) {
2721 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002722 if (!ret) {
2723 ret = (*iovec)->iov_len;
2724 iov_iter_init(iter, rw, *iovec, 1, ret);
2725 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002726 *iovec = NULL;
2727 return ret;
2728 }
2729
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002731 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002732 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2733 iovec, iter);
2734#endif
2735
2736 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2737}
2738
Jens Axboe32960612019-09-23 11:05:34 -06002739/*
2740 * For files that don't have ->read_iter() and ->write_iter(), handle them
2741 * by looping over ->read() or ->write() manually.
2742 */
2743static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2744 struct iov_iter *iter)
2745{
2746 ssize_t ret = 0;
2747
2748 /*
2749 * Don't support polled IO through this interface, and we can't
2750 * support non-blocking either. For the latter, this just causes
2751 * the kiocb to be handled from an async context.
2752 */
2753 if (kiocb->ki_flags & IOCB_HIPRI)
2754 return -EOPNOTSUPP;
2755 if (kiocb->ki_flags & IOCB_NOWAIT)
2756 return -EAGAIN;
2757
2758 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002759 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002760 ssize_t nr;
2761
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002762 if (!iov_iter_is_bvec(iter)) {
2763 iovec = iov_iter_iovec(iter);
2764 } else {
2765 /* fixed buffers import bvec */
2766 iovec.iov_base = kmap(iter->bvec->bv_page)
2767 + iter->iov_offset;
2768 iovec.iov_len = min(iter->count,
2769 iter->bvec->bv_len - iter->iov_offset);
2770 }
2771
Jens Axboe32960612019-09-23 11:05:34 -06002772 if (rw == READ) {
2773 nr = file->f_op->read(file, iovec.iov_base,
2774 iovec.iov_len, &kiocb->ki_pos);
2775 } else {
2776 nr = file->f_op->write(file, iovec.iov_base,
2777 iovec.iov_len, &kiocb->ki_pos);
2778 }
2779
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002780 if (iov_iter_is_bvec(iter))
2781 kunmap(iter->bvec->bv_page);
2782
Jens Axboe32960612019-09-23 11:05:34 -06002783 if (nr < 0) {
2784 if (!ret)
2785 ret = nr;
2786 break;
2787 }
2788 ret += nr;
2789 if (nr != iovec.iov_len)
2790 break;
2791 iov_iter_advance(iter, nr);
2792 }
2793
2794 return ret;
2795}
2796
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002797static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002798 struct iovec *iovec, struct iovec *fast_iov,
2799 struct iov_iter *iter)
2800{
2801 req->io->rw.nr_segs = iter->nr_segs;
2802 req->io->rw.size = io_size;
2803 req->io->rw.iov = iovec;
2804 if (!req->io->rw.iov) {
2805 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002806 if (req->io->rw.iov != fast_iov)
2807 memcpy(req->io->rw.iov, fast_iov,
2808 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002809 } else {
2810 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002811 }
2812}
2813
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002814static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2815{
2816 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2817 return req->io == NULL;
2818}
2819
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002820static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002821{
Jens Axboed3656342019-12-18 09:50:26 -07002822 if (!io_op_defs[req->opcode].async_ctx)
2823 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002824
2825 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002826}
2827
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002828static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2829 struct iovec *iovec, struct iovec *fast_iov,
2830 struct iov_iter *iter)
2831{
Jens Axboe980ad262020-01-24 23:08:54 -07002832 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002833 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002834 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002835 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002836 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002837
Jens Axboe5d204bc2020-01-31 12:06:52 -07002838 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2839 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002840 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002841}
2842
Jens Axboe3529d8c2019-12-19 18:24:38 -07002843static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2844 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002845{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002846 struct io_async_ctx *io;
2847 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002848 ssize_t ret;
2849
Jens Axboe3529d8c2019-12-19 18:24:38 -07002850 ret = io_prep_rw(req, sqe, force_nonblock);
2851 if (ret)
2852 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002853
Jens Axboe3529d8c2019-12-19 18:24:38 -07002854 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2855 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002856
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002857 /* either don't need iovec imported or already have it */
2858 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002859 return 0;
2860
2861 io = req->io;
2862 io->rw.iov = io->rw.fast_iov;
2863 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002864 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002865 req->io = io;
2866 if (ret < 0)
2867 return ret;
2868
2869 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2870 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002871}
2872
Jens Axboebcf5a062020-05-22 09:24:42 -06002873static void io_async_buf_cancel(struct callback_head *cb)
2874{
2875 struct io_async_rw *rw;
2876 struct io_kiocb *req;
2877
2878 rw = container_of(cb, struct io_async_rw, task_work);
2879 req = rw->wpq.wait.private;
Jens Axboec40f6372020-06-25 15:39:59 -06002880 __io_req_task_cancel(req, -ECANCELED);
Jens Axboebcf5a062020-05-22 09:24:42 -06002881}
2882
2883static void io_async_buf_retry(struct callback_head *cb)
2884{
2885 struct io_async_rw *rw;
Jens Axboebcf5a062020-05-22 09:24:42 -06002886 struct io_kiocb *req;
2887
2888 rw = container_of(cb, struct io_async_rw, task_work);
2889 req = rw->wpq.wait.private;
Jens Axboebcf5a062020-05-22 09:24:42 -06002890
Jens Axboec40f6372020-06-25 15:39:59 -06002891 __io_req_task_submit(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06002892}
2893
2894static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2895 int sync, void *arg)
2896{
2897 struct wait_page_queue *wpq;
2898 struct io_kiocb *req = wait->private;
2899 struct io_async_rw *rw = &req->io->rw;
2900 struct wait_page_key *key = arg;
2901 struct task_struct *tsk;
2902 int ret;
2903
2904 wpq = container_of(wait, struct wait_page_queue, wait);
2905
2906 ret = wake_page_match(wpq, key);
2907 if (ret != 1)
2908 return ret;
2909
2910 list_del_init(&wait->entry);
2911
2912 init_task_work(&rw->task_work, io_async_buf_retry);
2913 /* submit ref gets dropped, acquire a new one */
2914 refcount_inc(&req->refs);
2915 tsk = req->task;
2916 ret = task_work_add(tsk, &rw->task_work, true);
2917 if (unlikely(ret)) {
2918 /* queue just for cancelation */
2919 init_task_work(&rw->task_work, io_async_buf_cancel);
2920 tsk = io_wq_get_task(req->ctx->io_wq);
2921 task_work_add(tsk, &rw->task_work, true);
2922 }
2923 wake_up_process(tsk);
2924 return 1;
2925}
2926
2927static bool io_rw_should_retry(struct io_kiocb *req)
2928{
2929 struct kiocb *kiocb = &req->rw.kiocb;
2930 int ret;
2931
2932 /* never retry for NOWAIT, we just complete with -EAGAIN */
2933 if (req->flags & REQ_F_NOWAIT)
2934 return false;
2935
2936 /* already tried, or we're doing O_DIRECT */
2937 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
2938 return false;
2939 /*
2940 * just use poll if we can, and don't attempt if the fs doesn't
2941 * support callback based unlocks
2942 */
2943 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
2944 return false;
2945
2946 /*
2947 * If request type doesn't require req->io to defer in general,
2948 * we need to allocate it here
2949 */
2950 if (!req->io && __io_alloc_async_ctx(req))
2951 return false;
2952
2953 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
2954 io_async_buf_func, req);
2955 if (!ret) {
2956 io_get_req_task(req);
2957 return true;
2958 }
2959
2960 return false;
2961}
2962
2963static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
2964{
2965 if (req->file->f_op->read_iter)
2966 return call_read_iter(req->file, &req->rw.kiocb, iter);
2967 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
2968}
2969
Jens Axboea1d7c392020-06-22 11:09:46 -06002970static int io_read(struct io_kiocb *req, bool force_nonblock,
2971 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002972{
2973 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002974 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002975 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002976 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002977 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002978
Jens Axboebcda7ba2020-02-23 16:42:51 -07002979 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002980 if (ret < 0)
2981 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002982
Jens Axboefd6c2e42019-12-18 12:19:41 -07002983 /* Ensure we clear previously set non-block flag */
2984 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002985 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002986
Jens Axboef67676d2019-12-02 11:03:47 -07002987 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03002988 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07002989
Pavel Begunkov24c74672020-06-21 13:09:51 +03002990 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06002991 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002992 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002993
Jens Axboe31b51512019-01-18 22:56:34 -07002994 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002995 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002996 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06002997 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06002998 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002999
Jens Axboebcf5a062020-05-22 09:24:42 -06003000 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003001
Jens Axboe9d93a3f2019-05-15 13:53:07 -06003002 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06003003 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003004 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003005 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003006 iter.count = iov_count;
3007 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003008copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003009 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003010 inline_vecs, &iter);
3011 if (ret)
3012 goto out_free;
Jens Axboebcf5a062020-05-22 09:24:42 -06003013 /* if we can retry, do so with the callbacks armed */
3014 if (io_rw_should_retry(req)) {
3015 ret2 = io_iter_do_read(req, &iter);
3016 if (ret2 == -EIOCBQUEUED) {
3017 goto out_free;
3018 } else if (ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003019 kiocb_done(kiocb, ret2, cs);
Jens Axboebcf5a062020-05-22 09:24:42 -06003020 goto out_free;
3021 }
3022 }
3023 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07003024 return -EAGAIN;
3025 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003026 }
Jens Axboef67676d2019-12-02 11:03:47 -07003027out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003028 if (!(req->flags & REQ_F_NEED_CLEANUP))
3029 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003030 return ret;
3031}
3032
Jens Axboe3529d8c2019-12-19 18:24:38 -07003033static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3034 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003035{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003036 struct io_async_ctx *io;
3037 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07003038 ssize_t ret;
3039
Jens Axboe3529d8c2019-12-19 18:24:38 -07003040 ret = io_prep_rw(req, sqe, force_nonblock);
3041 if (ret)
3042 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003043
Jens Axboe3529d8c2019-12-19 18:24:38 -07003044 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3045 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003046
Jens Axboe4ed734b2020-03-20 11:23:41 -06003047 req->fsize = rlimit(RLIMIT_FSIZE);
3048
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003049 /* either don't need iovec imported or already have it */
3050 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003051 return 0;
3052
3053 io = req->io;
3054 io->rw.iov = io->rw.fast_iov;
3055 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003056 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003057 req->io = io;
3058 if (ret < 0)
3059 return ret;
3060
3061 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
3062 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003063}
3064
Jens Axboea1d7c392020-06-22 11:09:46 -06003065static int io_write(struct io_kiocb *req, bool force_nonblock,
3066 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003067{
3068 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003069 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003070 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003071 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003072 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003073
Jens Axboebcda7ba2020-02-23 16:42:51 -07003074 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003075 if (ret < 0)
3076 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003077
Jens Axboefd6c2e42019-12-18 12:19:41 -07003078 /* Ensure we clear previously set non-block flag */
3079 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003080 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003081
Jens Axboef67676d2019-12-02 11:03:47 -07003082 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03003083 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07003084
Pavel Begunkov24c74672020-06-21 13:09:51 +03003085 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003086 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003087 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003088
Jens Axboe10d59342019-12-09 20:16:22 -07003089 /* file path doesn't support NOWAIT for non-direct_IO */
3090 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3091 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003092 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003093
Jens Axboe31b51512019-01-18 22:56:34 -07003094 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003095 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003096 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003097 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003098 ssize_t ret2;
3099
Jens Axboe2b188cc2019-01-07 10:46:33 -07003100 /*
3101 * Open-code file_start_write here to grab freeze protection,
3102 * which will be released by another thread in
3103 * io_complete_rw(). Fool lockdep by telling it the lock got
3104 * released so that it doesn't complain about the held lock when
3105 * we return to userspace.
3106 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003107 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003108 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003109 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003110 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003111 SB_FREEZE_WRITE);
3112 }
3113 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003114
Jens Axboe4ed734b2020-03-20 11:23:41 -06003115 if (!force_nonblock)
3116 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3117
Jens Axboe9adbd452019-12-20 08:45:55 -07003118 if (req->file->f_op->write_iter)
3119 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003120 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003121 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003122
3123 if (!force_nonblock)
3124 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3125
Jens Axboefaac9962020-02-07 15:45:22 -07003126 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003127 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003128 * retry them without IOCB_NOWAIT.
3129 */
3130 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3131 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003132 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003133 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003134 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003135 iter.count = iov_count;
3136 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003137copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003138 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003139 inline_vecs, &iter);
3140 if (ret)
3141 goto out_free;
3142 return -EAGAIN;
3143 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003144 }
Jens Axboe31b51512019-01-18 22:56:34 -07003145out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003146 if (!(req->flags & REQ_F_NEED_CLEANUP))
3147 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148 return ret;
3149}
3150
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003151static int __io_splice_prep(struct io_kiocb *req,
3152 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003153{
3154 struct io_splice* sp = &req->splice;
3155 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3156 int ret;
3157
3158 if (req->flags & REQ_F_NEED_CLEANUP)
3159 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003160 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3161 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003162
3163 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003164 sp->len = READ_ONCE(sqe->len);
3165 sp->flags = READ_ONCE(sqe->splice_flags);
3166
3167 if (unlikely(sp->flags & ~valid_flags))
3168 return -EINVAL;
3169
3170 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3171 (sp->flags & SPLICE_F_FD_IN_FIXED));
3172 if (ret)
3173 return ret;
3174 req->flags |= REQ_F_NEED_CLEANUP;
3175
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003176 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3177 /*
3178 * Splice operation will be punted aync, and here need to
3179 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3180 */
3181 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003182 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003183 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003184
3185 return 0;
3186}
3187
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003188static int io_tee_prep(struct io_kiocb *req,
3189 const struct io_uring_sqe *sqe)
3190{
3191 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3192 return -EINVAL;
3193 return __io_splice_prep(req, sqe);
3194}
3195
3196static int io_tee(struct io_kiocb *req, bool force_nonblock)
3197{
3198 struct io_splice *sp = &req->splice;
3199 struct file *in = sp->file_in;
3200 struct file *out = sp->file_out;
3201 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3202 long ret = 0;
3203
3204 if (force_nonblock)
3205 return -EAGAIN;
3206 if (sp->len)
3207 ret = do_tee(in, out, sp->len, flags);
3208
3209 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3210 req->flags &= ~REQ_F_NEED_CLEANUP;
3211
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003212 if (ret != sp->len)
3213 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003214 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003215 return 0;
3216}
3217
3218static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3219{
3220 struct io_splice* sp = &req->splice;
3221
3222 sp->off_in = READ_ONCE(sqe->splice_off_in);
3223 sp->off_out = READ_ONCE(sqe->off);
3224 return __io_splice_prep(req, sqe);
3225}
3226
Pavel Begunkov014db002020-03-03 21:33:12 +03003227static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003228{
3229 struct io_splice *sp = &req->splice;
3230 struct file *in = sp->file_in;
3231 struct file *out = sp->file_out;
3232 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3233 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003234 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003235
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003236 if (force_nonblock)
3237 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003238
3239 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3240 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003241
Jens Axboe948a7742020-05-17 14:21:38 -06003242 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003243 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003244
3245 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3246 req->flags &= ~REQ_F_NEED_CLEANUP;
3247
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003248 if (ret != sp->len)
3249 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003250 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003251 return 0;
3252}
3253
Jens Axboe2b188cc2019-01-07 10:46:33 -07003254/*
3255 * IORING_OP_NOP just posts a completion event, nothing else.
3256 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003257static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003258{
3259 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003260
Jens Axboedef596e2019-01-09 08:59:42 -07003261 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3262 return -EINVAL;
3263
Jens Axboe229a7b62020-06-22 10:13:11 -06003264 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003265 return 0;
3266}
3267
Jens Axboe3529d8c2019-12-19 18:24:38 -07003268static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003269{
Jens Axboe6b063142019-01-10 22:13:58 -07003270 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003271
Jens Axboe09bb8392019-03-13 12:39:28 -06003272 if (!req->file)
3273 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003274
Jens Axboe6b063142019-01-10 22:13:58 -07003275 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003276 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003277 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003278 return -EINVAL;
3279
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003280 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3281 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3282 return -EINVAL;
3283
3284 req->sync.off = READ_ONCE(sqe->off);
3285 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003286 return 0;
3287}
3288
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003289static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003290{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003291 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003292 int ret;
3293
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003294 /* fsync always requires a blocking context */
3295 if (force_nonblock)
3296 return -EAGAIN;
3297
Jens Axboe9adbd452019-12-20 08:45:55 -07003298 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003299 end > 0 ? end : LLONG_MAX,
3300 req->sync.flags & IORING_FSYNC_DATASYNC);
3301 if (ret < 0)
3302 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003303 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003304 return 0;
3305}
3306
Jens Axboed63d1b52019-12-10 10:38:56 -07003307static int io_fallocate_prep(struct io_kiocb *req,
3308 const struct io_uring_sqe *sqe)
3309{
3310 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3311 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003312 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3313 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003314
3315 req->sync.off = READ_ONCE(sqe->off);
3316 req->sync.len = READ_ONCE(sqe->addr);
3317 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003318 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07003319 return 0;
3320}
3321
Pavel Begunkov014db002020-03-03 21:33:12 +03003322static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003323{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003324 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003325
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003326 /* fallocate always requiring blocking context */
3327 if (force_nonblock)
3328 return -EAGAIN;
3329
3330 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3331 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3332 req->sync.len);
3333 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3334 if (ret < 0)
3335 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003336 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003337 return 0;
3338}
3339
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003340static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003341{
Jens Axboef8748882020-01-08 17:47:02 -07003342 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003343 int ret;
3344
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003345 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003346 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003347 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003348 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003349 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003350 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003351
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003352 /* open.how should be already initialised */
3353 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003354 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003355
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003356 req->open.dfd = READ_ONCE(sqe->fd);
3357 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003358 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003359 if (IS_ERR(req->open.filename)) {
3360 ret = PTR_ERR(req->open.filename);
3361 req->open.filename = NULL;
3362 return ret;
3363 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003364 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003365 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003366 return 0;
3367}
3368
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003369static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3370{
3371 u64 flags, mode;
3372
3373 if (req->flags & REQ_F_NEED_CLEANUP)
3374 return 0;
3375 mode = READ_ONCE(sqe->len);
3376 flags = READ_ONCE(sqe->open_flags);
3377 req->open.how = build_open_how(flags, mode);
3378 return __io_openat_prep(req, sqe);
3379}
3380
Jens Axboecebdb982020-01-08 17:59:24 -07003381static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3382{
3383 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003384 size_t len;
3385 int ret;
3386
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003387 if (req->flags & REQ_F_NEED_CLEANUP)
3388 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003389 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3390 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003391 if (len < OPEN_HOW_SIZE_VER0)
3392 return -EINVAL;
3393
3394 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3395 len);
3396 if (ret)
3397 return ret;
3398
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003399 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003400}
3401
Pavel Begunkov014db002020-03-03 21:33:12 +03003402static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003403{
3404 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003405 struct file *file;
3406 int ret;
3407
Jens Axboef86cd202020-01-29 13:46:44 -07003408 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003409 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003410
Jens Axboecebdb982020-01-08 17:59:24 -07003411 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003412 if (ret)
3413 goto err;
3414
Jens Axboe4022e7a2020-03-19 19:23:18 -06003415 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003416 if (ret < 0)
3417 goto err;
3418
3419 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3420 if (IS_ERR(file)) {
3421 put_unused_fd(ret);
3422 ret = PTR_ERR(file);
3423 } else {
3424 fsnotify_open(file);
3425 fd_install(ret, file);
3426 }
3427err:
3428 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003429 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003430 if (ret < 0)
3431 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003432 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003433 return 0;
3434}
3435
Pavel Begunkov014db002020-03-03 21:33:12 +03003436static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003437{
Pavel Begunkov014db002020-03-03 21:33:12 +03003438 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003439}
3440
Jens Axboe067524e2020-03-02 16:32:28 -07003441static int io_remove_buffers_prep(struct io_kiocb *req,
3442 const struct io_uring_sqe *sqe)
3443{
3444 struct io_provide_buf *p = &req->pbuf;
3445 u64 tmp;
3446
3447 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3448 return -EINVAL;
3449
3450 tmp = READ_ONCE(sqe->fd);
3451 if (!tmp || tmp > USHRT_MAX)
3452 return -EINVAL;
3453
3454 memset(p, 0, sizeof(*p));
3455 p->nbufs = tmp;
3456 p->bgid = READ_ONCE(sqe->buf_group);
3457 return 0;
3458}
3459
3460static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3461 int bgid, unsigned nbufs)
3462{
3463 unsigned i = 0;
3464
3465 /* shouldn't happen */
3466 if (!nbufs)
3467 return 0;
3468
3469 /* the head kbuf is the list itself */
3470 while (!list_empty(&buf->list)) {
3471 struct io_buffer *nxt;
3472
3473 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3474 list_del(&nxt->list);
3475 kfree(nxt);
3476 if (++i == nbufs)
3477 return i;
3478 }
3479 i++;
3480 kfree(buf);
3481 idr_remove(&ctx->io_buffer_idr, bgid);
3482
3483 return i;
3484}
3485
Jens Axboe229a7b62020-06-22 10:13:11 -06003486static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3487 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003488{
3489 struct io_provide_buf *p = &req->pbuf;
3490 struct io_ring_ctx *ctx = req->ctx;
3491 struct io_buffer *head;
3492 int ret = 0;
3493
3494 io_ring_submit_lock(ctx, !force_nonblock);
3495
3496 lockdep_assert_held(&ctx->uring_lock);
3497
3498 ret = -ENOENT;
3499 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3500 if (head)
3501 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3502
3503 io_ring_submit_lock(ctx, !force_nonblock);
3504 if (ret < 0)
3505 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003506 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003507 return 0;
3508}
3509
Jens Axboeddf0322d2020-02-23 16:41:33 -07003510static int io_provide_buffers_prep(struct io_kiocb *req,
3511 const struct io_uring_sqe *sqe)
3512{
3513 struct io_provide_buf *p = &req->pbuf;
3514 u64 tmp;
3515
3516 if (sqe->ioprio || sqe->rw_flags)
3517 return -EINVAL;
3518
3519 tmp = READ_ONCE(sqe->fd);
3520 if (!tmp || tmp > USHRT_MAX)
3521 return -E2BIG;
3522 p->nbufs = tmp;
3523 p->addr = READ_ONCE(sqe->addr);
3524 p->len = READ_ONCE(sqe->len);
3525
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003526 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003527 return -EFAULT;
3528
3529 p->bgid = READ_ONCE(sqe->buf_group);
3530 tmp = READ_ONCE(sqe->off);
3531 if (tmp > USHRT_MAX)
3532 return -E2BIG;
3533 p->bid = tmp;
3534 return 0;
3535}
3536
3537static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3538{
3539 struct io_buffer *buf;
3540 u64 addr = pbuf->addr;
3541 int i, bid = pbuf->bid;
3542
3543 for (i = 0; i < pbuf->nbufs; i++) {
3544 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3545 if (!buf)
3546 break;
3547
3548 buf->addr = addr;
3549 buf->len = pbuf->len;
3550 buf->bid = bid;
3551 addr += pbuf->len;
3552 bid++;
3553 if (!*head) {
3554 INIT_LIST_HEAD(&buf->list);
3555 *head = buf;
3556 } else {
3557 list_add_tail(&buf->list, &(*head)->list);
3558 }
3559 }
3560
3561 return i ? i : -ENOMEM;
3562}
3563
Jens Axboe229a7b62020-06-22 10:13:11 -06003564static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3565 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003566{
3567 struct io_provide_buf *p = &req->pbuf;
3568 struct io_ring_ctx *ctx = req->ctx;
3569 struct io_buffer *head, *list;
3570 int ret = 0;
3571
3572 io_ring_submit_lock(ctx, !force_nonblock);
3573
3574 lockdep_assert_held(&ctx->uring_lock);
3575
3576 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3577
3578 ret = io_add_buffers(p, &head);
3579 if (ret < 0)
3580 goto out;
3581
3582 if (!list) {
3583 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3584 GFP_KERNEL);
3585 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003586 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003587 goto out;
3588 }
3589 }
3590out:
3591 io_ring_submit_unlock(ctx, !force_nonblock);
3592 if (ret < 0)
3593 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003594 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003595 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003596}
3597
Jens Axboe3e4827b2020-01-08 15:18:09 -07003598static int io_epoll_ctl_prep(struct io_kiocb *req,
3599 const struct io_uring_sqe *sqe)
3600{
3601#if defined(CONFIG_EPOLL)
3602 if (sqe->ioprio || sqe->buf_index)
3603 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003604 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3605 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003606
3607 req->epoll.epfd = READ_ONCE(sqe->fd);
3608 req->epoll.op = READ_ONCE(sqe->len);
3609 req->epoll.fd = READ_ONCE(sqe->off);
3610
3611 if (ep_op_has_event(req->epoll.op)) {
3612 struct epoll_event __user *ev;
3613
3614 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3615 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3616 return -EFAULT;
3617 }
3618
3619 return 0;
3620#else
3621 return -EOPNOTSUPP;
3622#endif
3623}
3624
Jens Axboe229a7b62020-06-22 10:13:11 -06003625static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3626 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003627{
3628#if defined(CONFIG_EPOLL)
3629 struct io_epoll *ie = &req->epoll;
3630 int ret;
3631
3632 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3633 if (force_nonblock && ret == -EAGAIN)
3634 return -EAGAIN;
3635
3636 if (ret < 0)
3637 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003638 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003639 return 0;
3640#else
3641 return -EOPNOTSUPP;
3642#endif
3643}
3644
Jens Axboec1ca7572019-12-25 22:18:28 -07003645static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3646{
3647#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3648 if (sqe->ioprio || sqe->buf_index || sqe->off)
3649 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003650 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3651 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003652
3653 req->madvise.addr = READ_ONCE(sqe->addr);
3654 req->madvise.len = READ_ONCE(sqe->len);
3655 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3656 return 0;
3657#else
3658 return -EOPNOTSUPP;
3659#endif
3660}
3661
Pavel Begunkov014db002020-03-03 21:33:12 +03003662static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003663{
3664#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3665 struct io_madvise *ma = &req->madvise;
3666 int ret;
3667
3668 if (force_nonblock)
3669 return -EAGAIN;
3670
3671 ret = do_madvise(ma->addr, ma->len, ma->advice);
3672 if (ret < 0)
3673 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003674 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003675 return 0;
3676#else
3677 return -EOPNOTSUPP;
3678#endif
3679}
3680
Jens Axboe4840e412019-12-25 22:03:45 -07003681static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3682{
3683 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3684 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003685 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3686 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003687
3688 req->fadvise.offset = READ_ONCE(sqe->off);
3689 req->fadvise.len = READ_ONCE(sqe->len);
3690 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3691 return 0;
3692}
3693
Pavel Begunkov014db002020-03-03 21:33:12 +03003694static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003695{
3696 struct io_fadvise *fa = &req->fadvise;
3697 int ret;
3698
Jens Axboe3e694262020-02-01 09:22:49 -07003699 if (force_nonblock) {
3700 switch (fa->advice) {
3701 case POSIX_FADV_NORMAL:
3702 case POSIX_FADV_RANDOM:
3703 case POSIX_FADV_SEQUENTIAL:
3704 break;
3705 default:
3706 return -EAGAIN;
3707 }
3708 }
Jens Axboe4840e412019-12-25 22:03:45 -07003709
3710 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3711 if (ret < 0)
3712 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003713 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003714 return 0;
3715}
3716
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003717static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3718{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003719 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3720 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003721 if (sqe->ioprio || sqe->buf_index)
3722 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003723 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003724 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003725
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003726 req->statx.dfd = READ_ONCE(sqe->fd);
3727 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003728 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003729 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3730 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003731
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003732 return 0;
3733}
3734
Pavel Begunkov014db002020-03-03 21:33:12 +03003735static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003736{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003737 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003738 int ret;
3739
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003740 if (force_nonblock) {
3741 /* only need file table for an actual valid fd */
3742 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3743 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003744 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003745 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003746
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003747 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3748 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003749
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003750 if (ret < 0)
3751 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003752 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003753 return 0;
3754}
3755
Jens Axboeb5dba592019-12-11 14:02:38 -07003756static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3757{
3758 /*
3759 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003760 * leave the 'file' in an undeterminate state, and here need to modify
3761 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003762 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003763 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003764 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3765
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003766 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3767 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003768 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3769 sqe->rw_flags || sqe->buf_index)
3770 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003771 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003772 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003773
3774 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003775 if ((req->file && req->file->f_op == &io_uring_fops) ||
3776 req->close.fd == req->ctx->ring_fd)
3777 return -EBADF;
3778
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003779 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003780 return 0;
3781}
3782
Jens Axboe229a7b62020-06-22 10:13:11 -06003783static int io_close(struct io_kiocb *req, bool force_nonblock,
3784 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003785{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003786 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003787 int ret;
3788
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003789 /* might be already done during nonblock submission */
3790 if (!close->put_file) {
3791 ret = __close_fd_get_file(close->fd, &close->put_file);
3792 if (ret < 0)
3793 return (ret == -ENOENT) ? -EBADF : ret;
3794 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003795
3796 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003797 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003798 /* was never set, but play safe */
3799 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003800 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003801 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003802 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003803 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003804
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003805 /* No ->flush() or already async, safely close from here */
3806 ret = filp_close(close->put_file, req->work.files);
3807 if (ret < 0)
3808 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003809 fput(close->put_file);
3810 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003811 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003812 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003813}
3814
Jens Axboe3529d8c2019-12-19 18:24:38 -07003815static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003816{
3817 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003818
3819 if (!req->file)
3820 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003821
3822 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3823 return -EINVAL;
3824 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3825 return -EINVAL;
3826
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003827 req->sync.off = READ_ONCE(sqe->off);
3828 req->sync.len = READ_ONCE(sqe->len);
3829 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003830 return 0;
3831}
3832
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003833static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003834{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003835 int ret;
3836
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003837 /* sync_file_range always requires a blocking context */
3838 if (force_nonblock)
3839 return -EAGAIN;
3840
Jens Axboe9adbd452019-12-20 08:45:55 -07003841 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003842 req->sync.flags);
3843 if (ret < 0)
3844 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003845 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003846 return 0;
3847}
3848
YueHaibing469956e2020-03-04 15:53:52 +08003849#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003850static int io_setup_async_msg(struct io_kiocb *req,
3851 struct io_async_msghdr *kmsg)
3852{
3853 if (req->io)
3854 return -EAGAIN;
3855 if (io_alloc_async_ctx(req)) {
3856 if (kmsg->iov != kmsg->fast_iov)
3857 kfree(kmsg->iov);
3858 return -ENOMEM;
3859 }
3860 req->flags |= REQ_F_NEED_CLEANUP;
3861 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3862 return -EAGAIN;
3863}
3864
Jens Axboe3529d8c2019-12-19 18:24:38 -07003865static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003866{
Jens Axboee47293f2019-12-20 08:58:21 -07003867 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003868 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003869 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003870
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003871 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3872 return -EINVAL;
3873
Jens Axboee47293f2019-12-20 08:58:21 -07003874 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3875 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003876 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003877
Jens Axboed8768362020-02-27 14:17:49 -07003878#ifdef CONFIG_COMPAT
3879 if (req->ctx->compat)
3880 sr->msg_flags |= MSG_CMSG_COMPAT;
3881#endif
3882
Jens Axboefddafac2020-01-04 20:19:44 -07003883 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003884 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003885 /* iovec is already imported */
3886 if (req->flags & REQ_F_NEED_CLEANUP)
3887 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003888
Jens Axboed9688562019-12-09 19:35:20 -07003889 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003890 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003891 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003892 if (!ret)
3893 req->flags |= REQ_F_NEED_CLEANUP;
3894 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003895}
3896
Jens Axboe229a7b62020-06-22 10:13:11 -06003897static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3898 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07003899{
Jens Axboe0b416c32019-12-15 10:57:46 -07003900 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003901 struct socket *sock;
3902 int ret;
3903
Jens Axboe03b12302019-12-02 18:50:25 -07003904 sock = sock_from_file(req->file, &ret);
3905 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003906 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003907 unsigned flags;
3908
Jens Axboe03b12302019-12-02 18:50:25 -07003909 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003910 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003911 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003912 /* if iov is set, it's allocated already */
3913 if (!kmsg->iov)
3914 kmsg->iov = kmsg->fast_iov;
3915 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003916 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003917 struct io_sr_msg *sr = &req->sr_msg;
3918
Jens Axboe0b416c32019-12-15 10:57:46 -07003919 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003920 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003921
3922 io.msg.iov = io.msg.fast_iov;
3923 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3924 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003925 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003926 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003927 }
3928
Jens Axboee47293f2019-12-20 08:58:21 -07003929 flags = req->sr_msg.msg_flags;
3930 if (flags & MSG_DONTWAIT)
3931 req->flags |= REQ_F_NOWAIT;
3932 else if (force_nonblock)
3933 flags |= MSG_DONTWAIT;
3934
Jens Axboe0b416c32019-12-15 10:57:46 -07003935 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003936 if (force_nonblock && ret == -EAGAIN)
3937 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003938 if (ret == -ERESTARTSYS)
3939 ret = -EINTR;
3940 }
3941
Pavel Begunkov1e950812020-02-06 19:51:16 +03003942 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003943 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003944 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003945 if (ret < 0)
3946 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003947 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07003948 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003949}
3950
Jens Axboe229a7b62020-06-22 10:13:11 -06003951static int io_send(struct io_kiocb *req, bool force_nonblock,
3952 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07003953{
Jens Axboefddafac2020-01-04 20:19:44 -07003954 struct socket *sock;
3955 int ret;
3956
Jens Axboefddafac2020-01-04 20:19:44 -07003957 sock = sock_from_file(req->file, &ret);
3958 if (sock) {
3959 struct io_sr_msg *sr = &req->sr_msg;
3960 struct msghdr msg;
3961 struct iovec iov;
3962 unsigned flags;
3963
3964 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3965 &msg.msg_iter);
3966 if (ret)
3967 return ret;
3968
3969 msg.msg_name = NULL;
3970 msg.msg_control = NULL;
3971 msg.msg_controllen = 0;
3972 msg.msg_namelen = 0;
3973
3974 flags = req->sr_msg.msg_flags;
3975 if (flags & MSG_DONTWAIT)
3976 req->flags |= REQ_F_NOWAIT;
3977 else if (force_nonblock)
3978 flags |= MSG_DONTWAIT;
3979
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003980 msg.msg_flags = flags;
3981 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003982 if (force_nonblock && ret == -EAGAIN)
3983 return -EAGAIN;
3984 if (ret == -ERESTARTSYS)
3985 ret = -EINTR;
3986 }
3987
Jens Axboefddafac2020-01-04 20:19:44 -07003988 if (ret < 0)
3989 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003990 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07003991 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003992}
3993
Jens Axboe52de1fe2020-02-27 10:15:42 -07003994static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3995{
3996 struct io_sr_msg *sr = &req->sr_msg;
3997 struct iovec __user *uiov;
3998 size_t iov_len;
3999 int ret;
4000
4001 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
4002 &uiov, &iov_len);
4003 if (ret)
4004 return ret;
4005
4006 if (req->flags & REQ_F_BUFFER_SELECT) {
4007 if (iov_len > 1)
4008 return -EINVAL;
4009 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
4010 return -EFAULT;
4011 sr->len = io->msg.iov[0].iov_len;
4012 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
4013 sr->len);
4014 io->msg.iov = NULL;
4015 } else {
4016 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4017 &io->msg.iov, &io->msg.msg.msg_iter);
4018 if (ret > 0)
4019 ret = 0;
4020 }
4021
4022 return ret;
4023}
4024
4025#ifdef CONFIG_COMPAT
4026static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
4027 struct io_async_ctx *io)
4028{
4029 struct compat_msghdr __user *msg_compat;
4030 struct io_sr_msg *sr = &req->sr_msg;
4031 struct compat_iovec __user *uiov;
4032 compat_uptr_t ptr;
4033 compat_size_t len;
4034 int ret;
4035
4036 msg_compat = (struct compat_msghdr __user *) sr->msg;
4037 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
4038 &ptr, &len);
4039 if (ret)
4040 return ret;
4041
4042 uiov = compat_ptr(ptr);
4043 if (req->flags & REQ_F_BUFFER_SELECT) {
4044 compat_ssize_t clen;
4045
4046 if (len > 1)
4047 return -EINVAL;
4048 if (!access_ok(uiov, sizeof(*uiov)))
4049 return -EFAULT;
4050 if (__get_user(clen, &uiov->iov_len))
4051 return -EFAULT;
4052 if (clen < 0)
4053 return -EINVAL;
4054 sr->len = io->msg.iov[0].iov_len;
4055 io->msg.iov = NULL;
4056 } else {
4057 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
4058 &io->msg.iov,
4059 &io->msg.msg.msg_iter);
4060 if (ret < 0)
4061 return ret;
4062 }
4063
4064 return 0;
4065}
Jens Axboe03b12302019-12-02 18:50:25 -07004066#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004067
4068static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
4069{
4070 io->msg.iov = io->msg.fast_iov;
4071
4072#ifdef CONFIG_COMPAT
4073 if (req->ctx->compat)
4074 return __io_compat_recvmsg_copy_hdr(req, io);
4075#endif
4076
4077 return __io_recvmsg_copy_hdr(req, io);
4078}
4079
Jens Axboebcda7ba2020-02-23 16:42:51 -07004080static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
4081 int *cflags, bool needs_lock)
4082{
4083 struct io_sr_msg *sr = &req->sr_msg;
4084 struct io_buffer *kbuf;
4085
4086 if (!(req->flags & REQ_F_BUFFER_SELECT))
4087 return NULL;
4088
4089 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4090 if (IS_ERR(kbuf))
4091 return kbuf;
4092
4093 sr->kbuf = kbuf;
4094 req->flags |= REQ_F_BUFFER_SELECTED;
4095
4096 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
4097 *cflags |= IORING_CQE_F_BUFFER;
4098 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004099}
4100
Jens Axboe3529d8c2019-12-19 18:24:38 -07004101static int io_recvmsg_prep(struct io_kiocb *req,
4102 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004103{
Jens Axboee47293f2019-12-20 08:58:21 -07004104 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004105 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004106 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004107
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004108 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4109 return -EINVAL;
4110
Jens Axboe3529d8c2019-12-19 18:24:38 -07004111 sr->msg_flags = READ_ONCE(sqe->msg_flags);
4112 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004113 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004114 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004115
Jens Axboed8768362020-02-27 14:17:49 -07004116#ifdef CONFIG_COMPAT
4117 if (req->ctx->compat)
4118 sr->msg_flags |= MSG_CMSG_COMPAT;
4119#endif
4120
Jens Axboefddafac2020-01-04 20:19:44 -07004121 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004122 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004123 /* iovec is already imported */
4124 if (req->flags & REQ_F_NEED_CLEANUP)
4125 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004126
Jens Axboe52de1fe2020-02-27 10:15:42 -07004127 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004128 if (!ret)
4129 req->flags |= REQ_F_NEED_CLEANUP;
4130 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004131}
4132
Jens Axboe229a7b62020-06-22 10:13:11 -06004133static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4134 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004135{
Jens Axboe0b416c32019-12-15 10:57:46 -07004136 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004137 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004138 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004139
Jens Axboe0fa03c62019-04-19 13:34:07 -06004140 sock = sock_from_file(req->file, &ret);
4141 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07004142 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004143 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004144 unsigned flags;
4145
Jens Axboe03b12302019-12-02 18:50:25 -07004146 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07004147 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004148 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07004149 /* if iov is set, it's allocated already */
4150 if (!kmsg->iov)
4151 kmsg->iov = kmsg->fast_iov;
4152 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004153 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07004154 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004155 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156
Jens Axboe52de1fe2020-02-27 10:15:42 -07004157 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07004158 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004159 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004160 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06004161
Jens Axboe52de1fe2020-02-27 10:15:42 -07004162 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4163 if (IS_ERR(kbuf)) {
4164 return PTR_ERR(kbuf);
4165 } else if (kbuf) {
4166 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4167 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4168 1, req->sr_msg.len);
4169 }
4170
Jens Axboee47293f2019-12-20 08:58:21 -07004171 flags = req->sr_msg.msg_flags;
4172 if (flags & MSG_DONTWAIT)
4173 req->flags |= REQ_F_NOWAIT;
4174 else if (force_nonblock)
4175 flags |= MSG_DONTWAIT;
4176
4177 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
4178 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004179 if (force_nonblock && ret == -EAGAIN)
4180 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07004181 if (ret == -ERESTARTSYS)
4182 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004183 }
4184
Pavel Begunkov1e950812020-02-06 19:51:16 +03004185 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004186 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004187 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004188 if (ret < 0)
4189 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004190 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004191 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004192}
4193
Jens Axboe229a7b62020-06-22 10:13:11 -06004194static int io_recv(struct io_kiocb *req, bool force_nonblock,
4195 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004196{
Jens Axboebcda7ba2020-02-23 16:42:51 -07004197 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07004198 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004199 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004200
Jens Axboefddafac2020-01-04 20:19:44 -07004201 sock = sock_from_file(req->file, &ret);
4202 if (sock) {
4203 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004204 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004205 struct msghdr msg;
4206 struct iovec iov;
4207 unsigned flags;
4208
Jens Axboebcda7ba2020-02-23 16:42:51 -07004209 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4210 if (IS_ERR(kbuf))
4211 return PTR_ERR(kbuf);
4212 else if (kbuf)
4213 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004214
Jens Axboebcda7ba2020-02-23 16:42:51 -07004215 ret = import_single_range(READ, buf, sr->len, &iov,
4216 &msg.msg_iter);
4217 if (ret) {
4218 kfree(kbuf);
4219 return ret;
4220 }
4221
4222 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004223 msg.msg_name = NULL;
4224 msg.msg_control = NULL;
4225 msg.msg_controllen = 0;
4226 msg.msg_namelen = 0;
4227 msg.msg_iocb = NULL;
4228 msg.msg_flags = 0;
4229
4230 flags = req->sr_msg.msg_flags;
4231 if (flags & MSG_DONTWAIT)
4232 req->flags |= REQ_F_NOWAIT;
4233 else if (force_nonblock)
4234 flags |= MSG_DONTWAIT;
4235
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004236 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07004237 if (force_nonblock && ret == -EAGAIN)
4238 return -EAGAIN;
4239 if (ret == -ERESTARTSYS)
4240 ret = -EINTR;
4241 }
4242
Jens Axboebcda7ba2020-02-23 16:42:51 -07004243 kfree(kbuf);
4244 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004245 if (ret < 0)
4246 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004247 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004248 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004249}
4250
Jens Axboe3529d8c2019-12-19 18:24:38 -07004251static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004252{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004253 struct io_accept *accept = &req->accept;
4254
Jens Axboe17f2fe32019-10-17 14:42:58 -06004255 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4256 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004257 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004258 return -EINVAL;
4259
Jens Axboed55e5f52019-12-11 16:12:15 -07004260 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4261 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004262 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004263 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004264 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004265}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004266
Jens Axboe229a7b62020-06-22 10:13:11 -06004267static int io_accept(struct io_kiocb *req, bool force_nonblock,
4268 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004269{
4270 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004271 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004272 int ret;
4273
Jiufei Xuee697dee2020-06-10 13:41:59 +08004274 if (req->file->f_flags & O_NONBLOCK)
4275 req->flags |= REQ_F_NOWAIT;
4276
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004277 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004278 accept->addr_len, accept->flags,
4279 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004280 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004281 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004282 if (ret < 0) {
4283 if (ret == -ERESTARTSYS)
4284 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004285 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004286 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004287 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004288 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004289}
4290
Jens Axboe3529d8c2019-12-19 18:24:38 -07004291static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004292{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004293 struct io_connect *conn = &req->connect;
4294 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004295
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004296 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4297 return -EINVAL;
4298 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4299 return -EINVAL;
4300
Jens Axboe3529d8c2019-12-19 18:24:38 -07004301 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4302 conn->addr_len = READ_ONCE(sqe->addr2);
4303
4304 if (!io)
4305 return 0;
4306
4307 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004308 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004309}
4310
Jens Axboe229a7b62020-06-22 10:13:11 -06004311static int io_connect(struct io_kiocb *req, bool force_nonblock,
4312 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004313{
Jens Axboef499a022019-12-02 16:28:46 -07004314 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004315 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004316 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004317
Jens Axboef499a022019-12-02 16:28:46 -07004318 if (req->io) {
4319 io = req->io;
4320 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004321 ret = move_addr_to_kernel(req->connect.addr,
4322 req->connect.addr_len,
4323 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004324 if (ret)
4325 goto out;
4326 io = &__io;
4327 }
4328
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004329 file_flags = force_nonblock ? O_NONBLOCK : 0;
4330
4331 ret = __sys_connect_file(req->file, &io->connect.address,
4332 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004333 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004334 if (req->io)
4335 return -EAGAIN;
4336 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004337 ret = -ENOMEM;
4338 goto out;
4339 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004340 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004341 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004342 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004343 if (ret == -ERESTARTSYS)
4344 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004345out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004346 if (ret < 0)
4347 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004348 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004349 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004350}
YueHaibing469956e2020-03-04 15:53:52 +08004351#else /* !CONFIG_NET */
4352static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4353{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004354 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004355}
4356
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004357static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4358 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004359{
YueHaibing469956e2020-03-04 15:53:52 +08004360 return -EOPNOTSUPP;
4361}
4362
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004363static int io_send(struct io_kiocb *req, bool force_nonblock,
4364 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004365{
4366 return -EOPNOTSUPP;
4367}
4368
4369static int io_recvmsg_prep(struct io_kiocb *req,
4370 const struct io_uring_sqe *sqe)
4371{
4372 return -EOPNOTSUPP;
4373}
4374
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004375static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4376 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004377{
4378 return -EOPNOTSUPP;
4379}
4380
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004381static int io_recv(struct io_kiocb *req, bool force_nonblock,
4382 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004383{
4384 return -EOPNOTSUPP;
4385}
4386
4387static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4388{
4389 return -EOPNOTSUPP;
4390}
4391
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004392static int io_accept(struct io_kiocb *req, bool force_nonblock,
4393 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004394{
4395 return -EOPNOTSUPP;
4396}
4397
4398static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4399{
4400 return -EOPNOTSUPP;
4401}
4402
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004403static int io_connect(struct io_kiocb *req, bool force_nonblock,
4404 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004405{
4406 return -EOPNOTSUPP;
4407}
4408#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004409
Jens Axboed7718a92020-02-14 22:23:12 -07004410struct io_poll_table {
4411 struct poll_table_struct pt;
4412 struct io_kiocb *req;
4413 int error;
4414};
4415
Jens Axboed7718a92020-02-14 22:23:12 -07004416static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4417 __poll_t mask, task_work_func_t func)
4418{
4419 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004420 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004421
4422 /* for instances that support it check for an event match first: */
4423 if (mask && !(mask & poll->events))
4424 return 0;
4425
4426 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4427
4428 list_del_init(&poll->wait.entry);
4429
4430 tsk = req->task;
4431 req->result = mask;
4432 init_task_work(&req->task_work, func);
4433 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004434 * If this fails, then the task is exiting. When a task exits, the
4435 * work gets canceled, so just cancel this request as well instead
4436 * of executing it. We can't safely execute it anyway, as we may not
4437 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004438 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004439 ret = task_work_add(tsk, &req->task_work, true);
4440 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004441 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004442 tsk = io_wq_get_task(req->ctx->io_wq);
4443 task_work_add(tsk, &req->task_work, true);
4444 }
Jens Axboed7718a92020-02-14 22:23:12 -07004445 wake_up_process(tsk);
4446 return 1;
4447}
4448
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004449static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4450 __acquires(&req->ctx->completion_lock)
4451{
4452 struct io_ring_ctx *ctx = req->ctx;
4453
4454 if (!req->result && !READ_ONCE(poll->canceled)) {
4455 struct poll_table_struct pt = { ._key = poll->events };
4456
4457 req->result = vfs_poll(req->file, &pt) & poll->events;
4458 }
4459
4460 spin_lock_irq(&ctx->completion_lock);
4461 if (!req->result && !READ_ONCE(poll->canceled)) {
4462 add_wait_queue(poll->head, &poll->wait);
4463 return true;
4464 }
4465
4466 return false;
4467}
4468
Jens Axboe18bceab2020-05-15 11:56:54 -06004469static void io_poll_remove_double(struct io_kiocb *req)
4470{
4471 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4472
4473 lockdep_assert_held(&req->ctx->completion_lock);
4474
4475 if (poll && poll->head) {
4476 struct wait_queue_head *head = poll->head;
4477
4478 spin_lock(&head->lock);
4479 list_del_init(&poll->wait.entry);
4480 if (poll->wait.private)
4481 refcount_dec(&req->refs);
4482 poll->head = NULL;
4483 spin_unlock(&head->lock);
4484 }
4485}
4486
4487static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4488{
4489 struct io_ring_ctx *ctx = req->ctx;
4490
4491 io_poll_remove_double(req);
4492 req->poll.done = true;
4493 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4494 io_commit_cqring(ctx);
4495}
4496
4497static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4498{
4499 struct io_ring_ctx *ctx = req->ctx;
4500
4501 if (io_poll_rewait(req, &req->poll)) {
4502 spin_unlock_irq(&ctx->completion_lock);
4503 return;
4504 }
4505
4506 hash_del(&req->hash_node);
4507 io_poll_complete(req, req->result, 0);
4508 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03004509 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004510 spin_unlock_irq(&ctx->completion_lock);
4511
4512 io_cqring_ev_posted(ctx);
4513}
4514
4515static void io_poll_task_func(struct callback_head *cb)
4516{
4517 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4518 struct io_kiocb *nxt = NULL;
4519
4520 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004521 if (nxt)
4522 __io_req_task_submit(nxt);
Jens Axboe18bceab2020-05-15 11:56:54 -06004523}
4524
4525static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4526 int sync, void *key)
4527{
4528 struct io_kiocb *req = wait->private;
4529 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4530 __poll_t mask = key_to_poll(key);
4531
4532 /* for instances that support it check for an event match first: */
4533 if (mask && !(mask & poll->events))
4534 return 0;
4535
4536 if (req->poll.head) {
4537 bool done;
4538
4539 spin_lock(&req->poll.head->lock);
4540 done = list_empty(&req->poll.wait.entry);
4541 if (!done)
4542 list_del_init(&req->poll.wait.entry);
4543 spin_unlock(&req->poll.head->lock);
4544 if (!done)
4545 __io_async_wake(req, poll, mask, io_poll_task_func);
4546 }
4547 refcount_dec(&req->refs);
4548 return 1;
4549}
4550
4551static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4552 wait_queue_func_t wake_func)
4553{
4554 poll->head = NULL;
4555 poll->done = false;
4556 poll->canceled = false;
4557 poll->events = events;
4558 INIT_LIST_HEAD(&poll->wait.entry);
4559 init_waitqueue_func_entry(&poll->wait, wake_func);
4560}
4561
4562static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4563 struct wait_queue_head *head)
4564{
4565 struct io_kiocb *req = pt->req;
4566
4567 /*
4568 * If poll->head is already set, it's because the file being polled
4569 * uses multiple waitqueues for poll handling (eg one for read, one
4570 * for write). Setup a separate io_poll_iocb if this happens.
4571 */
4572 if (unlikely(poll->head)) {
4573 /* already have a 2nd entry, fail a third attempt */
4574 if (req->io) {
4575 pt->error = -EINVAL;
4576 return;
4577 }
4578 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4579 if (!poll) {
4580 pt->error = -ENOMEM;
4581 return;
4582 }
4583 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4584 refcount_inc(&req->refs);
4585 poll->wait.private = req;
4586 req->io = (void *) poll;
4587 }
4588
4589 pt->error = 0;
4590 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004591
4592 if (poll->events & EPOLLEXCLUSIVE)
4593 add_wait_queue_exclusive(head, &poll->wait);
4594 else
4595 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004596}
4597
4598static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4599 struct poll_table_struct *p)
4600{
4601 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4602
4603 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4604}
4605
Jens Axboed7718a92020-02-14 22:23:12 -07004606static void io_async_task_func(struct callback_head *cb)
4607{
4608 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4609 struct async_poll *apoll = req->apoll;
4610 struct io_ring_ctx *ctx = req->ctx;
4611
4612 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4613
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004614 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004615 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004616 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004617 }
4618
Jens Axboe31067252020-05-17 17:43:31 -06004619 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004620 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004621 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004622
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004623 spin_unlock_irq(&ctx->completion_lock);
4624
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004625 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004626 if (req->flags & REQ_F_WORK_INITIALIZED)
4627 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004628 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004629
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004630 if (!READ_ONCE(apoll->poll.canceled))
4631 __io_req_task_submit(req);
4632 else
4633 __io_req_task_cancel(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07004634}
4635
4636static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4637 void *key)
4638{
4639 struct io_kiocb *req = wait->private;
4640 struct io_poll_iocb *poll = &req->apoll->poll;
4641
4642 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4643 key_to_poll(key));
4644
4645 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4646}
4647
4648static void io_poll_req_insert(struct io_kiocb *req)
4649{
4650 struct io_ring_ctx *ctx = req->ctx;
4651 struct hlist_head *list;
4652
4653 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4654 hlist_add_head(&req->hash_node, list);
4655}
4656
4657static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4658 struct io_poll_iocb *poll,
4659 struct io_poll_table *ipt, __poll_t mask,
4660 wait_queue_func_t wake_func)
4661 __acquires(&ctx->completion_lock)
4662{
4663 struct io_ring_ctx *ctx = req->ctx;
4664 bool cancel = false;
4665
Jens Axboe18bceab2020-05-15 11:56:54 -06004666 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004667 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004668 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004669
4670 ipt->pt._key = mask;
4671 ipt->req = req;
4672 ipt->error = -EINVAL;
4673
Jens Axboed7718a92020-02-14 22:23:12 -07004674 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4675
4676 spin_lock_irq(&ctx->completion_lock);
4677 if (likely(poll->head)) {
4678 spin_lock(&poll->head->lock);
4679 if (unlikely(list_empty(&poll->wait.entry))) {
4680 if (ipt->error)
4681 cancel = true;
4682 ipt->error = 0;
4683 mask = 0;
4684 }
4685 if (mask || ipt->error)
4686 list_del_init(&poll->wait.entry);
4687 else if (cancel)
4688 WRITE_ONCE(poll->canceled, true);
4689 else if (!poll->done) /* actually waiting for an event */
4690 io_poll_req_insert(req);
4691 spin_unlock(&poll->head->lock);
4692 }
4693
4694 return mask;
4695}
4696
4697static bool io_arm_poll_handler(struct io_kiocb *req)
4698{
4699 const struct io_op_def *def = &io_op_defs[req->opcode];
4700 struct io_ring_ctx *ctx = req->ctx;
4701 struct async_poll *apoll;
4702 struct io_poll_table ipt;
4703 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004704 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004705
4706 if (!req->file || !file_can_poll(req->file))
4707 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004708 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004709 return false;
4710 if (!def->pollin && !def->pollout)
4711 return false;
4712
4713 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4714 if (unlikely(!apoll))
4715 return false;
4716
4717 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004718 if (req->flags & REQ_F_WORK_INITIALIZED)
4719 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004720 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004721
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004722 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004723 req->apoll = apoll;
4724 INIT_HLIST_NODE(&req->hash_node);
4725
Nathan Chancellor8755d972020-03-02 16:01:19 -07004726 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004727 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004728 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004729 if (def->pollout)
4730 mask |= POLLOUT | POLLWRNORM;
4731 mask |= POLLERR | POLLPRI;
4732
4733 ipt.pt._qproc = io_async_queue_proc;
4734
4735 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4736 io_async_wake);
4737 if (ret) {
4738 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004739 /* only remove double add if we did it here */
4740 if (!had_io)
4741 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004742 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004743 if (req->flags & REQ_F_WORK_INITIALIZED)
4744 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004745 kfree(apoll);
4746 return false;
4747 }
4748 spin_unlock_irq(&ctx->completion_lock);
4749 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4750 apoll->poll.events);
4751 return true;
4752}
4753
4754static bool __io_poll_remove_one(struct io_kiocb *req,
4755 struct io_poll_iocb *poll)
4756{
Jens Axboeb41e9852020-02-17 09:52:41 -07004757 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004758
4759 spin_lock(&poll->head->lock);
4760 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004761 if (!list_empty(&poll->wait.entry)) {
4762 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004763 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004764 }
4765 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004766 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004767 return do_complete;
4768}
4769
4770static bool io_poll_remove_one(struct io_kiocb *req)
4771{
4772 bool do_complete;
4773
4774 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004775 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004776 do_complete = __io_poll_remove_one(req, &req->poll);
4777 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004778 struct async_poll *apoll = req->apoll;
4779
Jens Axboed7718a92020-02-14 22:23:12 -07004780 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004781 do_complete = __io_poll_remove_one(req, &apoll->poll);
4782 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004783 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004784 /*
4785 * restore ->work because we will call
4786 * io_req_work_drop_env below when dropping the
4787 * final reference.
4788 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004789 if (req->flags & REQ_F_WORK_INITIALIZED)
4790 memcpy(&req->work, &apoll->work,
4791 sizeof(req->work));
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004792 kfree(apoll);
4793 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004794 }
4795
Jens Axboeb41e9852020-02-17 09:52:41 -07004796 if (do_complete) {
4797 io_cqring_fill_event(req, -ECANCELED);
4798 io_commit_cqring(req->ctx);
4799 req->flags |= REQ_F_COMP_LOCKED;
4800 io_put_req(req);
4801 }
4802
4803 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004804}
4805
4806static void io_poll_remove_all(struct io_ring_ctx *ctx)
4807{
Jens Axboe78076bb2019-12-04 19:56:40 -07004808 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004809 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004810 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004811
4812 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004813 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4814 struct hlist_head *list;
4815
4816 list = &ctx->cancel_hash[i];
4817 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004818 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004819 }
4820 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004821
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004822 if (posted)
4823 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004824}
4825
Jens Axboe47f46762019-11-09 17:43:02 -07004826static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4827{
Jens Axboe78076bb2019-12-04 19:56:40 -07004828 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004829 struct io_kiocb *req;
4830
Jens Axboe78076bb2019-12-04 19:56:40 -07004831 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4832 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004833 if (sqe_addr != req->user_data)
4834 continue;
4835 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004836 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004837 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004838 }
4839
4840 return -ENOENT;
4841}
4842
Jens Axboe3529d8c2019-12-19 18:24:38 -07004843static int io_poll_remove_prep(struct io_kiocb *req,
4844 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004845{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004846 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4847 return -EINVAL;
4848 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4849 sqe->poll_events)
4850 return -EINVAL;
4851
Jens Axboe0969e782019-12-17 18:40:57 -07004852 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004853 return 0;
4854}
4855
4856/*
4857 * Find a running poll command that matches one specified in sqe->addr,
4858 * and remove it if found.
4859 */
4860static int io_poll_remove(struct io_kiocb *req)
4861{
4862 struct io_ring_ctx *ctx = req->ctx;
4863 u64 addr;
4864 int ret;
4865
Jens Axboe0969e782019-12-17 18:40:57 -07004866 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004867 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004868 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004869 spin_unlock_irq(&ctx->completion_lock);
4870
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004871 if (ret < 0)
4872 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004873 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004874 return 0;
4875}
4876
Jens Axboe221c5eb2019-01-17 09:41:58 -07004877static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4878 void *key)
4879{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004880 struct io_kiocb *req = wait->private;
4881 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004882
Jens Axboed7718a92020-02-14 22:23:12 -07004883 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004884}
4885
Jens Axboe221c5eb2019-01-17 09:41:58 -07004886static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4887 struct poll_table_struct *p)
4888{
4889 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4890
Jens Axboed7718a92020-02-14 22:23:12 -07004891 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004892}
4893
Jens Axboe3529d8c2019-12-19 18:24:38 -07004894static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004895{
4896 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004897 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004898
4899 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4900 return -EINVAL;
4901 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4902 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004903 if (!poll->file)
4904 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004905
Jiufei Xue5769a352020-06-17 17:53:55 +08004906 events = READ_ONCE(sqe->poll32_events);
4907#ifdef __BIG_ENDIAN
4908 events = swahw32(events);
4909#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004910 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4911 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004912
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004913 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004914 return 0;
4915}
4916
Pavel Begunkov014db002020-03-03 21:33:12 +03004917static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004918{
4919 struct io_poll_iocb *poll = &req->poll;
4920 struct io_ring_ctx *ctx = req->ctx;
4921 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004922 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004923
Jens Axboe78076bb2019-12-04 19:56:40 -07004924 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004925 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004926 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004927
Jens Axboed7718a92020-02-14 22:23:12 -07004928 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4929 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004930
Jens Axboe8c838782019-03-12 15:48:16 -06004931 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004932 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004933 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004934 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004935 spin_unlock_irq(&ctx->completion_lock);
4936
Jens Axboe8c838782019-03-12 15:48:16 -06004937 if (mask) {
4938 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004939 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004940 }
Jens Axboe8c838782019-03-12 15:48:16 -06004941 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004942}
4943
Jens Axboe5262f562019-09-17 12:26:57 -06004944static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4945{
Jens Axboead8a48a2019-11-15 08:49:11 -07004946 struct io_timeout_data *data = container_of(timer,
4947 struct io_timeout_data, timer);
4948 struct io_kiocb *req = data->req;
4949 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004950 unsigned long flags;
4951
Jens Axboe5262f562019-09-17 12:26:57 -06004952 atomic_inc(&ctx->cq_timeouts);
4953
4954 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004955 /*
Jens Axboe11365042019-10-16 09:08:32 -06004956 * We could be racing with timeout deletion. If the list is empty,
4957 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004958 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004959 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004960 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004961
Jens Axboe78e19bb2019-11-06 15:21:34 -07004962 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004963 io_commit_cqring(ctx);
4964 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4965
4966 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004967 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004968 io_put_req(req);
4969 return HRTIMER_NORESTART;
4970}
4971
Jens Axboe47f46762019-11-09 17:43:02 -07004972static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4973{
4974 struct io_kiocb *req;
4975 int ret = -ENOENT;
4976
4977 list_for_each_entry(req, &ctx->timeout_list, list) {
4978 if (user_data == req->user_data) {
4979 list_del_init(&req->list);
4980 ret = 0;
4981 break;
4982 }
4983 }
4984
4985 if (ret == -ENOENT)
4986 return ret;
4987
Jens Axboe2d283902019-12-04 11:08:05 -07004988 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004989 if (ret == -1)
4990 return -EALREADY;
4991
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004992 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004993 io_cqring_fill_event(req, -ECANCELED);
4994 io_put_req(req);
4995 return 0;
4996}
4997
Jens Axboe3529d8c2019-12-19 18:24:38 -07004998static int io_timeout_remove_prep(struct io_kiocb *req,
4999 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005000{
Jens Axboeb29472e2019-12-17 18:50:29 -07005001 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5002 return -EINVAL;
5003 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
5004 return -EINVAL;
5005
5006 req->timeout.addr = READ_ONCE(sqe->addr);
5007 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5008 if (req->timeout.flags)
5009 return -EINVAL;
5010
Jens Axboeb29472e2019-12-17 18:50:29 -07005011 return 0;
5012}
5013
Jens Axboe11365042019-10-16 09:08:32 -06005014/*
5015 * Remove or update an existing timeout command
5016 */
Jens Axboefc4df992019-12-10 14:38:45 -07005017static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005018{
5019 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005020 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005021
Jens Axboe11365042019-10-16 09:08:32 -06005022 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005023 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005024
Jens Axboe47f46762019-11-09 17:43:02 -07005025 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005026 io_commit_cqring(ctx);
5027 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005028 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005029 if (ret < 0)
5030 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005031 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005032 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005033}
5034
Jens Axboe3529d8c2019-12-19 18:24:38 -07005035static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005036 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005037{
Jens Axboead8a48a2019-11-15 08:49:11 -07005038 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005039 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005040 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005041
Jens Axboead8a48a2019-11-15 08:49:11 -07005042 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005043 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005044 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005045 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005046 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005047 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005048 flags = READ_ONCE(sqe->timeout_flags);
5049 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005050 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005051
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005052 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005053
Jens Axboe3529d8c2019-12-19 18:24:38 -07005054 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005055 return -ENOMEM;
5056
5057 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005058 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005059
5060 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005061 return -EFAULT;
5062
Jens Axboe11365042019-10-16 09:08:32 -06005063 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005064 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005065 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005066 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005067
Jens Axboead8a48a2019-11-15 08:49:11 -07005068 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5069 return 0;
5070}
5071
Jens Axboefc4df992019-12-10 14:38:45 -07005072static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005073{
Jens Axboead8a48a2019-11-15 08:49:11 -07005074 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005075 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005076 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005077 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005078
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005079 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005080
Jens Axboe5262f562019-09-17 12:26:57 -06005081 /*
5082 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005083 * timeout event to be satisfied. If it isn't set, then this is
5084 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005085 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005086 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005087 entry = ctx->timeout_list.prev;
5088 goto add;
5089 }
Jens Axboe5262f562019-09-17 12:26:57 -06005090
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005091 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5092 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005093
5094 /*
5095 * Insertion sort, ensuring the first entry in the list is always
5096 * the one we need first.
5097 */
Jens Axboe5262f562019-09-17 12:26:57 -06005098 list_for_each_prev(entry, &ctx->timeout_list) {
5099 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06005100
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005101 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005102 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005103 /* nxt.seq is behind @tail, otherwise would've been completed */
5104 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005105 break;
5106 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005107add:
Jens Axboe5262f562019-09-17 12:26:57 -06005108 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005109 data->timer.function = io_timeout_fn;
5110 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005111 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005112 return 0;
5113}
5114
Jens Axboe62755e32019-10-28 21:49:21 -06005115static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005116{
Jens Axboe62755e32019-10-28 21:49:21 -06005117 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005118
Jens Axboe62755e32019-10-28 21:49:21 -06005119 return req->user_data == (unsigned long) data;
5120}
5121
Jens Axboee977d6d2019-11-05 12:39:45 -07005122static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005123{
Jens Axboe62755e32019-10-28 21:49:21 -06005124 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005125 int ret = 0;
5126
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005127 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005128 switch (cancel_ret) {
5129 case IO_WQ_CANCEL_OK:
5130 ret = 0;
5131 break;
5132 case IO_WQ_CANCEL_RUNNING:
5133 ret = -EALREADY;
5134 break;
5135 case IO_WQ_CANCEL_NOTFOUND:
5136 ret = -ENOENT;
5137 break;
5138 }
5139
Jens Axboee977d6d2019-11-05 12:39:45 -07005140 return ret;
5141}
5142
Jens Axboe47f46762019-11-09 17:43:02 -07005143static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5144 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005145 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005146{
5147 unsigned long flags;
5148 int ret;
5149
5150 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5151 if (ret != -ENOENT) {
5152 spin_lock_irqsave(&ctx->completion_lock, flags);
5153 goto done;
5154 }
5155
5156 spin_lock_irqsave(&ctx->completion_lock, flags);
5157 ret = io_timeout_cancel(ctx, sqe_addr);
5158 if (ret != -ENOENT)
5159 goto done;
5160 ret = io_poll_cancel(ctx, sqe_addr);
5161done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005162 if (!ret)
5163 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005164 io_cqring_fill_event(req, ret);
5165 io_commit_cqring(ctx);
5166 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5167 io_cqring_ev_posted(ctx);
5168
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005169 if (ret < 0)
5170 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005171 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005172}
5173
Jens Axboe3529d8c2019-12-19 18:24:38 -07005174static int io_async_cancel_prep(struct io_kiocb *req,
5175 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005176{
Jens Axboefbf23842019-12-17 18:45:56 -07005177 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005178 return -EINVAL;
5179 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
5180 sqe->cancel_flags)
5181 return -EINVAL;
5182
Jens Axboefbf23842019-12-17 18:45:56 -07005183 req->cancel.addr = READ_ONCE(sqe->addr);
5184 return 0;
5185}
5186
Pavel Begunkov014db002020-03-03 21:33:12 +03005187static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005188{
5189 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005190
Pavel Begunkov014db002020-03-03 21:33:12 +03005191 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005192 return 0;
5193}
5194
Jens Axboe05f3fb32019-12-09 11:22:50 -07005195static int io_files_update_prep(struct io_kiocb *req,
5196 const struct io_uring_sqe *sqe)
5197{
5198 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
5199 return -EINVAL;
5200
5201 req->files_update.offset = READ_ONCE(sqe->off);
5202 req->files_update.nr_args = READ_ONCE(sqe->len);
5203 if (!req->files_update.nr_args)
5204 return -EINVAL;
5205 req->files_update.arg = READ_ONCE(sqe->addr);
5206 return 0;
5207}
5208
Jens Axboe229a7b62020-06-22 10:13:11 -06005209static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5210 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005211{
5212 struct io_ring_ctx *ctx = req->ctx;
5213 struct io_uring_files_update up;
5214 int ret;
5215
Jens Axboef86cd202020-01-29 13:46:44 -07005216 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005217 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005218
5219 up.offset = req->files_update.offset;
5220 up.fds = req->files_update.arg;
5221
5222 mutex_lock(&ctx->uring_lock);
5223 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5224 mutex_unlock(&ctx->uring_lock);
5225
5226 if (ret < 0)
5227 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005228 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005229 return 0;
5230}
5231
Jens Axboe3529d8c2019-12-19 18:24:38 -07005232static int io_req_defer_prep(struct io_kiocb *req,
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005233 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005234{
Jens Axboee7815732019-12-17 19:45:06 -07005235 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005236
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005237 if (!sqe)
5238 return 0;
5239
Pavel Begunkov710c2bf2020-06-27 14:04:58 +03005240 if (io_op_defs[req->opcode].file_table) {
5241 io_req_init_async(req);
5242 ret = io_grab_files(req);
5243 if (unlikely(ret))
5244 return ret;
5245 }
5246
Jens Axboed625c6e2019-12-17 19:53:05 -07005247 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005248 case IORING_OP_NOP:
5249 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005250 case IORING_OP_READV:
5251 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005252 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005253 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005254 break;
5255 case IORING_OP_WRITEV:
5256 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005257 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005258 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005259 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005260 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005261 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005262 break;
5263 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005264 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005265 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005266 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005267 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005268 break;
5269 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005270 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005271 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005272 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005273 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005274 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005275 break;
5276 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005277 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005278 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005279 break;
Jens Axboef499a022019-12-02 16:28:46 -07005280 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005281 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005282 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005283 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005284 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005285 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005286 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005287 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005288 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005289 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005290 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005291 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005292 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005293 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005294 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005295 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005296 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005297 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005298 case IORING_OP_FALLOCATE:
5299 ret = io_fallocate_prep(req, sqe);
5300 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005301 case IORING_OP_OPENAT:
5302 ret = io_openat_prep(req, sqe);
5303 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005304 case IORING_OP_CLOSE:
5305 ret = io_close_prep(req, sqe);
5306 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005307 case IORING_OP_FILES_UPDATE:
5308 ret = io_files_update_prep(req, sqe);
5309 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005310 case IORING_OP_STATX:
5311 ret = io_statx_prep(req, sqe);
5312 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005313 case IORING_OP_FADVISE:
5314 ret = io_fadvise_prep(req, sqe);
5315 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005316 case IORING_OP_MADVISE:
5317 ret = io_madvise_prep(req, sqe);
5318 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005319 case IORING_OP_OPENAT2:
5320 ret = io_openat2_prep(req, sqe);
5321 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005322 case IORING_OP_EPOLL_CTL:
5323 ret = io_epoll_ctl_prep(req, sqe);
5324 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005325 case IORING_OP_SPLICE:
5326 ret = io_splice_prep(req, sqe);
5327 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005328 case IORING_OP_PROVIDE_BUFFERS:
5329 ret = io_provide_buffers_prep(req, sqe);
5330 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005331 case IORING_OP_REMOVE_BUFFERS:
5332 ret = io_remove_buffers_prep(req, sqe);
5333 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005334 case IORING_OP_TEE:
5335 ret = io_tee_prep(req, sqe);
5336 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005337 default:
Jens Axboee7815732019-12-17 19:45:06 -07005338 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5339 req->opcode);
5340 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005341 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005342 }
5343
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005344 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005345}
5346
Jens Axboe3529d8c2019-12-19 18:24:38 -07005347static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005348{
Jackie Liua197f662019-11-08 08:09:12 -07005349 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005350 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005351
Bob Liu9d858b22019-11-13 18:06:25 +08005352 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005353 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005354 return 0;
5355
Pavel Begunkov650b5482020-05-17 14:02:11 +03005356 if (!req->io) {
5357 if (io_alloc_async_ctx(req))
5358 return -EAGAIN;
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005359 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov650b5482020-05-17 14:02:11 +03005360 if (ret < 0)
5361 return ret;
5362 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005363 io_prep_async_link(req);
Jens Axboe2d283902019-12-04 11:08:05 -07005364
Jens Axboede0617e2019-04-06 21:51:27 -06005365 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005366 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005367 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005368 return 0;
5369 }
5370
Jens Axboe915967f2019-11-21 09:01:20 -07005371 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005372 list_add_tail(&req->list, &ctx->defer_list);
5373 spin_unlock_irq(&ctx->completion_lock);
5374 return -EIOCBQUEUED;
5375}
5376
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005377static void io_cleanup_req(struct io_kiocb *req)
5378{
5379 struct io_async_ctx *io = req->io;
5380
5381 switch (req->opcode) {
5382 case IORING_OP_READV:
5383 case IORING_OP_READ_FIXED:
5384 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005385 if (req->flags & REQ_F_BUFFER_SELECTED)
5386 kfree((void *)(unsigned long)req->rw.addr);
5387 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005388 case IORING_OP_WRITEV:
5389 case IORING_OP_WRITE_FIXED:
5390 case IORING_OP_WRITE:
5391 if (io->rw.iov != io->rw.fast_iov)
5392 kfree(io->rw.iov);
5393 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005394 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005395 if (req->flags & REQ_F_BUFFER_SELECTED)
5396 kfree(req->sr_msg.kbuf);
5397 /* fallthrough */
5398 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005399 if (io->msg.iov != io->msg.fast_iov)
5400 kfree(io->msg.iov);
5401 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005402 case IORING_OP_RECV:
5403 if (req->flags & REQ_F_BUFFER_SELECTED)
5404 kfree(req->sr_msg.kbuf);
5405 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005406 case IORING_OP_OPENAT:
5407 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005408 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005409 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005410 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005411 io_put_file(req, req->splice.file_in,
5412 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5413 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005414 }
5415
5416 req->flags &= ~REQ_F_NEED_CLEANUP;
5417}
5418
Jens Axboe3529d8c2019-12-19 18:24:38 -07005419static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005420 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005421{
Jackie Liua197f662019-11-08 08:09:12 -07005422 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005423 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005424
Jens Axboed625c6e2019-12-17 19:53:05 -07005425 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005426 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005427 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005428 break;
5429 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005430 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005431 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005432 if (sqe) {
5433 ret = io_read_prep(req, sqe, force_nonblock);
5434 if (ret < 0)
5435 break;
5436 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005437 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005438 break;
5439 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005440 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005441 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005442 if (sqe) {
5443 ret = io_write_prep(req, sqe, force_nonblock);
5444 if (ret < 0)
5445 break;
5446 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005447 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005448 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005449 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005450 if (sqe) {
5451 ret = io_prep_fsync(req, sqe);
5452 if (ret < 0)
5453 break;
5454 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005455 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005456 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005457 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005458 if (sqe) {
5459 ret = io_poll_add_prep(req, sqe);
5460 if (ret)
5461 break;
5462 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005463 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005464 break;
5465 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005466 if (sqe) {
5467 ret = io_poll_remove_prep(req, sqe);
5468 if (ret < 0)
5469 break;
5470 }
Jens Axboefc4df992019-12-10 14:38:45 -07005471 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005472 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005473 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005474 if (sqe) {
5475 ret = io_prep_sfr(req, sqe);
5476 if (ret < 0)
5477 break;
5478 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005479 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005480 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005481 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005482 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005483 if (sqe) {
5484 ret = io_sendmsg_prep(req, sqe);
5485 if (ret < 0)
5486 break;
5487 }
Jens Axboefddafac2020-01-04 20:19:44 -07005488 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005489 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005490 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005491 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005492 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005493 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005494 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005495 if (sqe) {
5496 ret = io_recvmsg_prep(req, sqe);
5497 if (ret)
5498 break;
5499 }
Jens Axboefddafac2020-01-04 20:19:44 -07005500 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005501 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005502 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005503 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005504 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005505 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005506 if (sqe) {
5507 ret = io_timeout_prep(req, sqe, false);
5508 if (ret)
5509 break;
5510 }
Jens Axboefc4df992019-12-10 14:38:45 -07005511 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005512 break;
Jens Axboe11365042019-10-16 09:08:32 -06005513 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005514 if (sqe) {
5515 ret = io_timeout_remove_prep(req, sqe);
5516 if (ret)
5517 break;
5518 }
Jens Axboefc4df992019-12-10 14:38:45 -07005519 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005520 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005521 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005522 if (sqe) {
5523 ret = io_accept_prep(req, sqe);
5524 if (ret)
5525 break;
5526 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005527 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005528 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005529 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005530 if (sqe) {
5531 ret = io_connect_prep(req, sqe);
5532 if (ret)
5533 break;
5534 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005535 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005536 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005537 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005538 if (sqe) {
5539 ret = io_async_cancel_prep(req, sqe);
5540 if (ret)
5541 break;
5542 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005543 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005544 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005545 case IORING_OP_FALLOCATE:
5546 if (sqe) {
5547 ret = io_fallocate_prep(req, sqe);
5548 if (ret)
5549 break;
5550 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005551 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005552 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005553 case IORING_OP_OPENAT:
5554 if (sqe) {
5555 ret = io_openat_prep(req, sqe);
5556 if (ret)
5557 break;
5558 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005559 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005560 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005561 case IORING_OP_CLOSE:
5562 if (sqe) {
5563 ret = io_close_prep(req, sqe);
5564 if (ret)
5565 break;
5566 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005567 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005568 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005569 case IORING_OP_FILES_UPDATE:
5570 if (sqe) {
5571 ret = io_files_update_prep(req, sqe);
5572 if (ret)
5573 break;
5574 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005575 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005576 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005577 case IORING_OP_STATX:
5578 if (sqe) {
5579 ret = io_statx_prep(req, sqe);
5580 if (ret)
5581 break;
5582 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005583 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005584 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005585 case IORING_OP_FADVISE:
5586 if (sqe) {
5587 ret = io_fadvise_prep(req, sqe);
5588 if (ret)
5589 break;
5590 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005591 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005592 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005593 case IORING_OP_MADVISE:
5594 if (sqe) {
5595 ret = io_madvise_prep(req, sqe);
5596 if (ret)
5597 break;
5598 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005599 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005600 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005601 case IORING_OP_OPENAT2:
5602 if (sqe) {
5603 ret = io_openat2_prep(req, sqe);
5604 if (ret)
5605 break;
5606 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005607 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005608 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005609 case IORING_OP_EPOLL_CTL:
5610 if (sqe) {
5611 ret = io_epoll_ctl_prep(req, sqe);
5612 if (ret)
5613 break;
5614 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005615 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005616 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005617 case IORING_OP_SPLICE:
5618 if (sqe) {
5619 ret = io_splice_prep(req, sqe);
5620 if (ret < 0)
5621 break;
5622 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005623 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005624 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005625 case IORING_OP_PROVIDE_BUFFERS:
5626 if (sqe) {
5627 ret = io_provide_buffers_prep(req, sqe);
5628 if (ret)
5629 break;
5630 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005631 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005632 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005633 case IORING_OP_REMOVE_BUFFERS:
5634 if (sqe) {
5635 ret = io_remove_buffers_prep(req, sqe);
5636 if (ret)
5637 break;
5638 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005639 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005640 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005641 case IORING_OP_TEE:
5642 if (sqe) {
5643 ret = io_tee_prep(req, sqe);
5644 if (ret < 0)
5645 break;
5646 }
5647 ret = io_tee(req, force_nonblock);
5648 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005649 default:
5650 ret = -EINVAL;
5651 break;
5652 }
5653
5654 if (ret)
5655 return ret;
5656
Jens Axboeb5325762020-05-19 21:20:27 -06005657 /* If the op doesn't have a file, we're not polling for it */
5658 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005659 const bool in_async = io_wq_current_is_worker();
5660
Jens Axboe11ba8202020-01-15 21:51:17 -07005661 /* workqueue context doesn't hold uring_lock, grab it now */
5662 if (in_async)
5663 mutex_lock(&ctx->uring_lock);
5664
Jens Axboe2b188cc2019-01-07 10:46:33 -07005665 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005666
5667 if (in_async)
5668 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005669 }
5670
5671 return 0;
5672}
5673
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005674static void io_arm_async_linked_timeout(struct io_kiocb *req)
5675{
5676 struct io_kiocb *link;
5677
5678 /* link head's timeout is queued in io_queue_async_work() */
5679 if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5680 return;
5681
5682 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5683 io_queue_linked_timeout(link);
5684}
5685
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005686static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Jens Axboedef596e2019-01-09 08:59:42 -07005687{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005688 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005689 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005690
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005691 io_arm_async_linked_timeout(req);
5692
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005693 /* if NO_CANCEL is set, we must still run the work */
5694 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5695 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005696 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005697 }
Jens Axboe31b51512019-01-18 22:56:34 -07005698
Jens Axboe561fb042019-10-24 07:25:42 -06005699 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005700 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005701 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005702 /*
5703 * We can get EAGAIN for polled IO even though we're
5704 * forcing a sync submission from here, since we can't
5705 * wait for request slots on the block side.
5706 */
5707 if (ret != -EAGAIN)
5708 break;
5709 cond_resched();
5710 } while (1);
5711 }
Jens Axboe31b51512019-01-18 22:56:34 -07005712
Jens Axboe561fb042019-10-24 07:25:42 -06005713 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005714 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005715 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005716 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005717
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005718 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005719}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005720
Jens Axboe65e19f52019-10-26 07:20:21 -06005721static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5722 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005723{
Jens Axboe65e19f52019-10-26 07:20:21 -06005724 struct fixed_file_table *table;
5725
Jens Axboe05f3fb32019-12-09 11:22:50 -07005726 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005727 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005728}
5729
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005730static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5731 int fd, struct file **out_file, bool fixed)
5732{
5733 struct io_ring_ctx *ctx = req->ctx;
5734 struct file *file;
5735
5736 if (fixed) {
5737 if (unlikely(!ctx->file_data ||
5738 (unsigned) fd >= ctx->nr_user_files))
5739 return -EBADF;
5740 fd = array_index_nospec(fd, ctx->nr_user_files);
5741 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005742 if (file) {
5743 req->fixed_file_refs = ctx->file_data->cur_refs;
5744 percpu_ref_get(req->fixed_file_refs);
5745 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005746 } else {
5747 trace_io_uring_file_get(ctx, fd);
5748 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005749 }
5750
Jens Axboefd2206e2020-06-02 16:40:47 -06005751 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5752 *out_file = file;
5753 return 0;
5754 }
5755 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005756}
5757
Jens Axboe3529d8c2019-12-19 18:24:38 -07005758static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005759 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005760{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005761 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005762
Jens Axboe63ff8222020-05-07 14:56:15 -06005763 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005764 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005765 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005766
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005767 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005768}
5769
Jackie Liua197f662019-11-08 08:09:12 -07005770static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005771{
Jens Axboefcb323c2019-10-24 12:39:47 -06005772 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005773 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005774
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005775 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005776 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005777 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005778 return -EBADF;
5779
Jens Axboefcb323c2019-10-24 12:39:47 -06005780 rcu_read_lock();
5781 spin_lock_irq(&ctx->inflight_lock);
5782 /*
5783 * We use the f_ops->flush() handler to ensure that we can flush
5784 * out work accessing these files if the fd is closed. Check if
5785 * the fd has changed since we started down this path, and disallow
5786 * this operation if it has.
5787 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005788 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005789 list_add(&req->inflight_entry, &ctx->inflight_list);
5790 req->flags |= REQ_F_INFLIGHT;
5791 req->work.files = current->files;
5792 ret = 0;
5793 }
5794 spin_unlock_irq(&ctx->inflight_lock);
5795 rcu_read_unlock();
5796
5797 return ret;
5798}
5799
Jens Axboe2665abf2019-11-05 12:40:47 -07005800static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5801{
Jens Axboead8a48a2019-11-15 08:49:11 -07005802 struct io_timeout_data *data = container_of(timer,
5803 struct io_timeout_data, timer);
5804 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005805 struct io_ring_ctx *ctx = req->ctx;
5806 struct io_kiocb *prev = NULL;
5807 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005808
5809 spin_lock_irqsave(&ctx->completion_lock, flags);
5810
5811 /*
5812 * We don't expect the list to be empty, that will only happen if we
5813 * race with the completion of the linked work.
5814 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005815 if (!list_empty(&req->link_list)) {
5816 prev = list_entry(req->link_list.prev, struct io_kiocb,
5817 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005818 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005819 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005820 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5821 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005822 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005823 }
5824
5825 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5826
5827 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005828 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005829 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005830 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005831 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005832 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005833 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005834 return HRTIMER_NORESTART;
5835}
5836
Jens Axboead8a48a2019-11-15 08:49:11 -07005837static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005838{
Jens Axboe76a46e02019-11-10 23:34:16 -07005839 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005840
Jens Axboe76a46e02019-11-10 23:34:16 -07005841 /*
5842 * If the list is now empty, then our linked request finished before
5843 * we got a chance to setup the timer
5844 */
5845 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005846 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005847 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005848
Jens Axboead8a48a2019-11-15 08:49:11 -07005849 data->timer.function = io_link_timeout_fn;
5850 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5851 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005852 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005853 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005854
Jens Axboe2665abf2019-11-05 12:40:47 -07005855 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005856 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005857}
5858
Jens Axboead8a48a2019-11-15 08:49:11 -07005859static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005860{
5861 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005862
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005863 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005864 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005865 /* for polled retry, if flag is set, we already went through here */
5866 if (req->flags & REQ_F_POLLED)
5867 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005868
Pavel Begunkov44932332019-12-05 16:16:35 +03005869 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5870 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005871 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005872 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005873
Jens Axboe76a46e02019-11-10 23:34:16 -07005874 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005875 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005876}
5877
Jens Axboef13fad72020-06-22 09:34:30 -06005878static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5879 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005880{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005881 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005882 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005883 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005884 int ret;
5885
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005886again:
5887 linked_timeout = io_prep_linked_timeout(req);
5888
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005889 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5890 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005891 if (old_creds)
5892 revert_creds(old_creds);
5893 if (old_creds == req->work.creds)
5894 old_creds = NULL; /* restored original creds */
5895 else
5896 old_creds = override_creds(req->work.creds);
5897 }
5898
Jens Axboef13fad72020-06-22 09:34:30 -06005899 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06005900
5901 /*
5902 * We async punt it if the file wasn't marked NOWAIT, or if the file
5903 * doesn't support non-blocking read/write attempts
5904 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005905 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005906 if (io_arm_poll_handler(req)) {
5907 if (linked_timeout)
5908 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005909 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005910 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005911punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005912 io_req_init_async(req);
5913
Jens Axboef86cd202020-01-29 13:46:44 -07005914 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005915 ret = io_grab_files(req);
5916 if (ret)
5917 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005918 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005919
5920 /*
5921 * Queued up for async execution, worker will release
5922 * submit reference when the iocb is actually submitted.
5923 */
5924 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005925 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005926 }
Jens Axboee65ef562019-03-12 10:16:44 -06005927
Jens Axboefcb323c2019-10-24 12:39:47 -06005928err:
Jens Axboee65ef562019-03-12 10:16:44 -06005929 /* drop submission reference */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03005930 nxt = io_put_req_find_next(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005931
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005932 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005933 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005934 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005935 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005936 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005937 }
5938
Jens Axboee65ef562019-03-12 10:16:44 -06005939 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005940 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005941 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005942 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06005943 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005944 if (nxt) {
5945 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005946
5947 if (req->flags & REQ_F_FORCE_ASYNC)
5948 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005949 goto again;
5950 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005951exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005952 if (old_creds)
5953 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005954}
5955
Jens Axboef13fad72020-06-22 09:34:30 -06005956static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5957 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005958{
5959 int ret;
5960
Jens Axboe3529d8c2019-12-19 18:24:38 -07005961 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005962 if (ret) {
5963 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005964fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005965 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005966 io_put_req(req);
5967 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005968 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005969 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005970 if (!req->io) {
5971 ret = -EAGAIN;
5972 if (io_alloc_async_ctx(req))
5973 goto fail_req;
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005974 ret = io_req_defer_prep(req, sqe);
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005975 if (unlikely(ret < 0))
5976 goto fail_req;
5977 }
5978
Jens Axboece35a472019-12-17 08:04:44 -07005979 /*
5980 * Never try inline submit of IOSQE_ASYNC is set, go straight
5981 * to async execution.
5982 */
5983 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5984 io_queue_async_work(req);
5985 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06005986 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07005987 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005988}
5989
Jens Axboef13fad72020-06-22 09:34:30 -06005990static inline void io_queue_link_head(struct io_kiocb *req,
5991 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005992{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005993 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06005994 io_put_req(req);
5995 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005996 } else
Jens Axboef13fad72020-06-22 09:34:30 -06005997 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005998}
5999
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006000static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006001 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006002{
Jackie Liua197f662019-11-08 08:09:12 -07006003 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006004 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006005
Jens Axboe9e645e112019-05-10 16:07:28 -06006006 /*
6007 * If we already have a head request, queue this one for async
6008 * submittal once the head completes. If we don't have a head but
6009 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6010 * submitted sync once the chain is complete. If none of those
6011 * conditions are true (normal request), then just queue it.
6012 */
6013 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006014 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006015
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006016 /*
6017 * Taking sequential execution of a link, draining both sides
6018 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6019 * requests in the link. So, it drains the head and the
6020 * next after the link request. The last one is done via
6021 * drain_next flag to persist the effect across calls.
6022 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006023 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006024 head->flags |= REQ_F_IO_DRAIN;
6025 ctx->drain_next = 1;
6026 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006027 if (io_alloc_async_ctx(req))
6028 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06006029
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006030 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006031 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006032 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006033 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006034 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006035 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006036 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006037 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006038 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006039
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006040 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006041 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006042 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006043 *link = NULL;
6044 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006045 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006046 if (unlikely(ctx->drain_next)) {
6047 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006048 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006049 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006050 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006051 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006052 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006053
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006054 if (io_alloc_async_ctx(req))
6055 return -EAGAIN;
6056
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006057 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov711be032020-01-17 03:57:59 +03006058 if (ret)
6059 req->flags |= REQ_F_FAIL_LINK;
6060 *link = req;
6061 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006062 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006063 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006064 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006065
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006066 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006067}
6068
Jens Axboe9a56a232019-01-09 09:06:50 -07006069/*
6070 * Batched submission is done, ensure local IO is flushed out.
6071 */
6072static void io_submit_state_end(struct io_submit_state *state)
6073{
Jens Axboef13fad72020-06-22 09:34:30 -06006074 if (!list_empty(&state->comp.list))
6075 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006076 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006077 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006078 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006079 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006080}
6081
6082/*
6083 * Start submission side cache.
6084 */
6085static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006086 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006087{
6088 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006089#ifdef CONFIG_BLOCK
6090 state->plug.nowait = true;
6091#endif
Jens Axboe013538b2020-06-22 09:29:15 -06006092 state->comp.nr = 0;
6093 INIT_LIST_HEAD(&state->comp.list);
6094 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006095 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006096 state->file = NULL;
6097 state->ios_left = max_ios;
6098}
6099
Jens Axboe2b188cc2019-01-07 10:46:33 -07006100static void io_commit_sqring(struct io_ring_ctx *ctx)
6101{
Hristo Venev75b28af2019-08-26 17:23:46 +00006102 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006103
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006104 /*
6105 * Ensure any loads from the SQEs are done at this point,
6106 * since once we write the new head, the application could
6107 * write new data to them.
6108 */
6109 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110}
6111
6112/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006113 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006114 * that is mapped by userspace. This means that care needs to be taken to
6115 * ensure that reads are stable, as we cannot rely on userspace always
6116 * being a good citizen. If members of the sqe are validated and then later
6117 * used, it's important that those reads are done through READ_ONCE() to
6118 * prevent a re-load down the line.
6119 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006120static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006121{
Hristo Venev75b28af2019-08-26 17:23:46 +00006122 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006123 unsigned head;
6124
6125 /*
6126 * The cached sq head (or cq tail) serves two purposes:
6127 *
6128 * 1) allows us to batch the cost of updating the user visible
6129 * head updates.
6130 * 2) allows the kernel side to track the head on its own, even
6131 * though the application is the one updating it.
6132 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006133 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006134 if (likely(head < ctx->sq_entries))
6135 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006136
6137 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006138 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006139 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006140 return NULL;
6141}
6142
6143static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6144{
6145 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006146}
6147
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006148#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6149 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6150 IOSQE_BUFFER_SELECT)
6151
6152static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6153 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006154 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006155{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006156 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006157 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006158
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006159 /*
6160 * All io need record the previous position, if LINK vs DARIN,
6161 * it can be used to mark the position of the first IO in the
6162 * link list.
6163 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03006164 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006165 req->opcode = READ_ONCE(sqe->opcode);
6166 req->user_data = READ_ONCE(sqe->user_data);
6167 req->io = NULL;
6168 req->file = NULL;
6169 req->ctx = ctx;
6170 req->flags = 0;
6171 /* one is dropped after submission, the other at completion */
6172 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006173 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006174 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006175
6176 if (unlikely(req->opcode >= IORING_OP_LAST))
6177 return -EINVAL;
6178
Jens Axboe9d8426a2020-06-16 18:42:49 -06006179 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6180 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006181
6182 sqe_flags = READ_ONCE(sqe->flags);
6183 /* enforce forwards compatibility on users */
6184 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6185 return -EINVAL;
6186
6187 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6188 !io_op_defs[req->opcode].buffer_select)
6189 return -EOPNOTSUPP;
6190
6191 id = READ_ONCE(sqe->personality);
6192 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006193 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006194 req->work.creds = idr_find(&ctx->personality_idr, id);
6195 if (unlikely(!req->work.creds))
6196 return -EINVAL;
6197 get_cred(req->work.creds);
6198 }
6199
6200 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006201 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006202
Jens Axboe63ff8222020-05-07 14:56:15 -06006203 if (!io_op_defs[req->opcode].needs_file)
6204 return 0;
6205
6206 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006207}
6208
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006209static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006210 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006211{
Jens Axboeac8691c2020-06-01 08:30:41 -06006212 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006213 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006214 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006215
Jens Axboec4a2ed72019-11-21 21:01:26 -07006216 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006217 if (test_bit(0, &ctx->sq_check_overflow)) {
6218 if (!list_empty(&ctx->cq_overflow_list) &&
6219 !io_cqring_overflow_flush(ctx, false))
6220 return -EBUSY;
6221 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006222
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006223 /* make sure SQ entry isn't read before tail */
6224 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006225
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006226 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6227 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006228
Jens Axboe013538b2020-06-22 09:29:15 -06006229 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006230
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006231 ctx->ring_fd = ring_fd;
6232 ctx->ring_file = ring_file;
6233
Jens Axboe6c271ce2019-01-10 11:22:30 -07006234 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006235 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006236 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006237 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006238
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006239 sqe = io_get_sqe(ctx);
6240 if (unlikely(!sqe)) {
6241 io_consume_sqe(ctx);
6242 break;
6243 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006244 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006245 if (unlikely(!req)) {
6246 if (!submitted)
6247 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006248 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006249 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006250
Jens Axboeac8691c2020-06-01 08:30:41 -06006251 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006252 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006253 /* will complete beyond this point, count as submitted */
6254 submitted++;
6255
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006256 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006257fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006258 io_put_req(req);
6259 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006260 break;
6261 }
6262
Jens Axboe354420f2020-01-08 18:55:15 -07006263 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006264 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006265 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006266 if (err)
6267 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006268 }
6269
Pavel Begunkov9466f432020-01-25 22:34:01 +03006270 if (unlikely(submitted != nr)) {
6271 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6272
6273 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6274 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006275 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006276 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006277 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006278
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006279 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6280 io_commit_sqring(ctx);
6281
Jens Axboe6c271ce2019-01-10 11:22:30 -07006282 return submitted;
6283}
6284
6285static int io_sq_thread(void *data)
6286{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006287 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006288 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006289 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006290 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006291 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006292
Jens Axboe0f158b42020-05-14 17:18:39 -06006293 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006294
Jens Axboe181e4482019-11-25 08:52:30 -07006295 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006296
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006297 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006298 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006299 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006300
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006301 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006302 unsigned nr_events = 0;
6303
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006304 mutex_lock(&ctx->uring_lock);
6305 if (!list_empty(&ctx->poll_list))
6306 io_iopoll_getevents(ctx, &nr_events, 0);
6307 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006308 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006309 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006310 }
6311
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006312 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006313
6314 /*
6315 * If submit got -EBUSY, flag us as needing the application
6316 * to enter the kernel to reap and flush events.
6317 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006318 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006319 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006320 * Drop cur_mm before scheduling, we can't hold it for
6321 * long periods (or over schedule()). Do this before
6322 * adding ourselves to the waitqueue, as the unuse/drop
6323 * may sleep.
6324 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006325 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006326
6327 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006328 * We're polling. If we're within the defined idle
6329 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006330 * to sleep. The exception is if we got EBUSY doing
6331 * more IO, we should wait for the application to
6332 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006333 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006334 if (!list_empty(&ctx->poll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006335 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6336 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006337 if (current->task_works)
6338 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006339 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006340 continue;
6341 }
6342
Jens Axboe6c271ce2019-01-10 11:22:30 -07006343 prepare_to_wait(&ctx->sqo_wait, &wait,
6344 TASK_INTERRUPTIBLE);
6345
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006346 /*
6347 * While doing polled IO, before going to sleep, we need
6348 * to check if there are new reqs added to poll_list, it
6349 * is because reqs may have been punted to io worker and
6350 * will be added to poll_list later, hence check the
6351 * poll_list again.
6352 */
6353 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6354 !list_empty_careful(&ctx->poll_list)) {
6355 finish_wait(&ctx->sqo_wait, &wait);
6356 continue;
6357 }
6358
Jens Axboe6c271ce2019-01-10 11:22:30 -07006359 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006360 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006361 /* make sure to read SQ tail after writing flags */
6362 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006363
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006364 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006365 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006366 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006367 finish_wait(&ctx->sqo_wait, &wait);
6368 break;
6369 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006370 if (current->task_works) {
6371 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006372 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006373 continue;
6374 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006375 if (signal_pending(current))
6376 flush_signals(current);
6377 schedule();
6378 finish_wait(&ctx->sqo_wait, &wait);
6379
Hristo Venev75b28af2019-08-26 17:23:46 +00006380 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006381 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006382 continue;
6383 }
6384 finish_wait(&ctx->sqo_wait, &wait);
6385
Hristo Venev75b28af2019-08-26 17:23:46 +00006386 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006387 }
6388
Jens Axboe8a4955f2019-12-09 14:52:35 -07006389 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006390 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6391 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006392 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006393 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006394 }
6395
Jens Axboeb41e9852020-02-17 09:52:41 -07006396 if (current->task_works)
6397 task_work_run();
6398
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006399 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006400 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006401
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006402 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006403
Jens Axboe6c271ce2019-01-10 11:22:30 -07006404 return 0;
6405}
6406
Jens Axboebda52162019-09-24 13:47:15 -06006407struct io_wait_queue {
6408 struct wait_queue_entry wq;
6409 struct io_ring_ctx *ctx;
6410 unsigned to_wait;
6411 unsigned nr_timeouts;
6412};
6413
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006414static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006415{
6416 struct io_ring_ctx *ctx = iowq->ctx;
6417
6418 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006419 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006420 * started waiting. For timeouts, we always want to return to userspace,
6421 * regardless of event count.
6422 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006423 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006424 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6425}
6426
6427static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6428 int wake_flags, void *key)
6429{
6430 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6431 wq);
6432
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006433 /* use noflush == true, as we can't safely rely on locking context */
6434 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006435 return -1;
6436
6437 return autoremove_wake_function(curr, mode, wake_flags, key);
6438}
6439
Jens Axboe2b188cc2019-01-07 10:46:33 -07006440/*
6441 * Wait until events become available, if we don't already have some. The
6442 * application must reap them itself, as they reside on the shared cq ring.
6443 */
6444static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6445 const sigset_t __user *sig, size_t sigsz)
6446{
Jens Axboebda52162019-09-24 13:47:15 -06006447 struct io_wait_queue iowq = {
6448 .wq = {
6449 .private = current,
6450 .func = io_wake_function,
6451 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6452 },
6453 .ctx = ctx,
6454 .to_wait = min_events,
6455 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006456 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006457 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006458
Jens Axboeb41e9852020-02-17 09:52:41 -07006459 do {
6460 if (io_cqring_events(ctx, false) >= min_events)
6461 return 0;
6462 if (!current->task_works)
6463 break;
6464 task_work_run();
6465 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006466
6467 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006468#ifdef CONFIG_COMPAT
6469 if (in_compat_syscall())
6470 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006471 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006472 else
6473#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006474 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006475
Jens Axboe2b188cc2019-01-07 10:46:33 -07006476 if (ret)
6477 return ret;
6478 }
6479
Jens Axboebda52162019-09-24 13:47:15 -06006480 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006481 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006482 do {
6483 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6484 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006485 if (current->task_works)
6486 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006487 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006488 break;
6489 schedule();
6490 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006491 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006492 break;
6493 }
6494 } while (1);
6495 finish_wait(&ctx->wait, &iowq.wq);
6496
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006497 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006498
Hristo Venev75b28af2019-08-26 17:23:46 +00006499 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006500}
6501
Jens Axboe6b063142019-01-10 22:13:58 -07006502static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6503{
6504#if defined(CONFIG_UNIX)
6505 if (ctx->ring_sock) {
6506 struct sock *sock = ctx->ring_sock->sk;
6507 struct sk_buff *skb;
6508
6509 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6510 kfree_skb(skb);
6511 }
6512#else
6513 int i;
6514
Jens Axboe65e19f52019-10-26 07:20:21 -06006515 for (i = 0; i < ctx->nr_user_files; i++) {
6516 struct file *file;
6517
6518 file = io_file_from_index(ctx, i);
6519 if (file)
6520 fput(file);
6521 }
Jens Axboe6b063142019-01-10 22:13:58 -07006522#endif
6523}
6524
Jens Axboe05f3fb32019-12-09 11:22:50 -07006525static void io_file_ref_kill(struct percpu_ref *ref)
6526{
6527 struct fixed_file_data *data;
6528
6529 data = container_of(ref, struct fixed_file_data, refs);
6530 complete(&data->done);
6531}
6532
Jens Axboe6b063142019-01-10 22:13:58 -07006533static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6534{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006535 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006536 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006537 unsigned nr_tables, i;
6538
Jens Axboe05f3fb32019-12-09 11:22:50 -07006539 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006540 return -ENXIO;
6541
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006542 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006543 if (!list_empty(&data->ref_list))
6544 ref_node = list_first_entry(&data->ref_list,
6545 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006546 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006547 if (ref_node)
6548 percpu_ref_kill(&ref_node->refs);
6549
6550 percpu_ref_kill(&data->refs);
6551
6552 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006553 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006554 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006555
Jens Axboe6b063142019-01-10 22:13:58 -07006556 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006557 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6558 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006559 kfree(data->table[i].files);
6560 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006561 percpu_ref_exit(&data->refs);
6562 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006563 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006564 ctx->nr_user_files = 0;
6565 return 0;
6566}
6567
Jens Axboe6c271ce2019-01-10 11:22:30 -07006568static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6569{
6570 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006571 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006572 /*
6573 * The park is a bit of a work-around, without it we get
6574 * warning spews on shutdown with SQPOLL set and affinity
6575 * set to a single CPU.
6576 */
Jens Axboe06058632019-04-13 09:26:03 -06006577 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006578 kthread_stop(ctx->sqo_thread);
6579 ctx->sqo_thread = NULL;
6580 }
6581}
6582
Jens Axboe6b063142019-01-10 22:13:58 -07006583static void io_finish_async(struct io_ring_ctx *ctx)
6584{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006585 io_sq_thread_stop(ctx);
6586
Jens Axboe561fb042019-10-24 07:25:42 -06006587 if (ctx->io_wq) {
6588 io_wq_destroy(ctx->io_wq);
6589 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006590 }
6591}
6592
6593#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006594/*
6595 * Ensure the UNIX gc is aware of our file set, so we are certain that
6596 * the io_uring can be safely unregistered on process exit, even if we have
6597 * loops in the file referencing.
6598 */
6599static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6600{
6601 struct sock *sk = ctx->ring_sock->sk;
6602 struct scm_fp_list *fpl;
6603 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006604 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006605
Jens Axboe6b063142019-01-10 22:13:58 -07006606 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6607 if (!fpl)
6608 return -ENOMEM;
6609
6610 skb = alloc_skb(0, GFP_KERNEL);
6611 if (!skb) {
6612 kfree(fpl);
6613 return -ENOMEM;
6614 }
6615
6616 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006617
Jens Axboe08a45172019-10-03 08:11:03 -06006618 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006619 fpl->user = get_uid(ctx->user);
6620 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006621 struct file *file = io_file_from_index(ctx, i + offset);
6622
6623 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006624 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006625 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006626 unix_inflight(fpl->user, fpl->fp[nr_files]);
6627 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006628 }
6629
Jens Axboe08a45172019-10-03 08:11:03 -06006630 if (nr_files) {
6631 fpl->max = SCM_MAX_FD;
6632 fpl->count = nr_files;
6633 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006634 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006635 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6636 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006637
Jens Axboe08a45172019-10-03 08:11:03 -06006638 for (i = 0; i < nr_files; i++)
6639 fput(fpl->fp[i]);
6640 } else {
6641 kfree_skb(skb);
6642 kfree(fpl);
6643 }
Jens Axboe6b063142019-01-10 22:13:58 -07006644
6645 return 0;
6646}
6647
6648/*
6649 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6650 * causes regular reference counting to break down. We rely on the UNIX
6651 * garbage collection to take care of this problem for us.
6652 */
6653static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6654{
6655 unsigned left, total;
6656 int ret = 0;
6657
6658 total = 0;
6659 left = ctx->nr_user_files;
6660 while (left) {
6661 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006662
6663 ret = __io_sqe_files_scm(ctx, this_files, total);
6664 if (ret)
6665 break;
6666 left -= this_files;
6667 total += this_files;
6668 }
6669
6670 if (!ret)
6671 return 0;
6672
6673 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006674 struct file *file = io_file_from_index(ctx, total);
6675
6676 if (file)
6677 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006678 total++;
6679 }
6680
6681 return ret;
6682}
6683#else
6684static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6685{
6686 return 0;
6687}
6688#endif
6689
Jens Axboe65e19f52019-10-26 07:20:21 -06006690static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6691 unsigned nr_files)
6692{
6693 int i;
6694
6695 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006696 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006697 unsigned this_files;
6698
6699 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6700 table->files = kcalloc(this_files, sizeof(struct file *),
6701 GFP_KERNEL);
6702 if (!table->files)
6703 break;
6704 nr_files -= this_files;
6705 }
6706
6707 if (i == nr_tables)
6708 return 0;
6709
6710 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006711 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006712 kfree(table->files);
6713 }
6714 return 1;
6715}
6716
Jens Axboe05f3fb32019-12-09 11:22:50 -07006717static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006718{
6719#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006720 struct sock *sock = ctx->ring_sock->sk;
6721 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6722 struct sk_buff *skb;
6723 int i;
6724
6725 __skb_queue_head_init(&list);
6726
6727 /*
6728 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6729 * remove this entry and rearrange the file array.
6730 */
6731 skb = skb_dequeue(head);
6732 while (skb) {
6733 struct scm_fp_list *fp;
6734
6735 fp = UNIXCB(skb).fp;
6736 for (i = 0; i < fp->count; i++) {
6737 int left;
6738
6739 if (fp->fp[i] != file)
6740 continue;
6741
6742 unix_notinflight(fp->user, fp->fp[i]);
6743 left = fp->count - 1 - i;
6744 if (left) {
6745 memmove(&fp->fp[i], &fp->fp[i + 1],
6746 left * sizeof(struct file *));
6747 }
6748 fp->count--;
6749 if (!fp->count) {
6750 kfree_skb(skb);
6751 skb = NULL;
6752 } else {
6753 __skb_queue_tail(&list, skb);
6754 }
6755 fput(file);
6756 file = NULL;
6757 break;
6758 }
6759
6760 if (!file)
6761 break;
6762
6763 __skb_queue_tail(&list, skb);
6764
6765 skb = skb_dequeue(head);
6766 }
6767
6768 if (skb_peek(&list)) {
6769 spin_lock_irq(&head->lock);
6770 while ((skb = __skb_dequeue(&list)) != NULL)
6771 __skb_queue_tail(head, skb);
6772 spin_unlock_irq(&head->lock);
6773 }
6774#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006775 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006776#endif
6777}
6778
Jens Axboe05f3fb32019-12-09 11:22:50 -07006779struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006780 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006781 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006782};
6783
Jens Axboe4a38aed22020-05-14 17:21:15 -06006784static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006785{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006786 struct fixed_file_data *file_data = ref_node->file_data;
6787 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006788 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006789
6790 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006791 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006792 io_ring_file_put(ctx, pfile->file);
6793 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006794 }
6795
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006796 spin_lock(&file_data->lock);
6797 list_del(&ref_node->node);
6798 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006799
Xiaoguang Wang05589552020-03-31 14:05:18 +08006800 percpu_ref_exit(&ref_node->refs);
6801 kfree(ref_node);
6802 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006803}
6804
Jens Axboe4a38aed22020-05-14 17:21:15 -06006805static void io_file_put_work(struct work_struct *work)
6806{
6807 struct io_ring_ctx *ctx;
6808 struct llist_node *node;
6809
6810 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6811 node = llist_del_all(&ctx->file_put_llist);
6812
6813 while (node) {
6814 struct fixed_file_ref_node *ref_node;
6815 struct llist_node *next = node->next;
6816
6817 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6818 __io_file_put_work(ref_node);
6819 node = next;
6820 }
6821}
6822
Jens Axboe05f3fb32019-12-09 11:22:50 -07006823static void io_file_data_ref_zero(struct percpu_ref *ref)
6824{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006825 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006826 struct io_ring_ctx *ctx;
6827 bool first_add;
6828 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006829
Xiaoguang Wang05589552020-03-31 14:05:18 +08006830 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006831 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006832
Jens Axboe4a38aed22020-05-14 17:21:15 -06006833 if (percpu_ref_is_dying(&ctx->file_data->refs))
6834 delay = 0;
6835
6836 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6837 if (!delay)
6838 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6839 else if (first_add)
6840 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006841}
6842
6843static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6844 struct io_ring_ctx *ctx)
6845{
6846 struct fixed_file_ref_node *ref_node;
6847
6848 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6849 if (!ref_node)
6850 return ERR_PTR(-ENOMEM);
6851
6852 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6853 0, GFP_KERNEL)) {
6854 kfree(ref_node);
6855 return ERR_PTR(-ENOMEM);
6856 }
6857 INIT_LIST_HEAD(&ref_node->node);
6858 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006859 ref_node->file_data = ctx->file_data;
6860 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006861}
6862
6863static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6864{
6865 percpu_ref_exit(&ref_node->refs);
6866 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006867}
6868
6869static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6870 unsigned nr_args)
6871{
6872 __s32 __user *fds = (__s32 __user *) arg;
6873 unsigned nr_tables;
6874 struct file *file;
6875 int fd, ret = 0;
6876 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006877 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006878
6879 if (ctx->file_data)
6880 return -EBUSY;
6881 if (!nr_args)
6882 return -EINVAL;
6883 if (nr_args > IORING_MAX_FIXED_FILES)
6884 return -EMFILE;
6885
6886 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6887 if (!ctx->file_data)
6888 return -ENOMEM;
6889 ctx->file_data->ctx = ctx;
6890 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006891 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006892 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006893
6894 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6895 ctx->file_data->table = kcalloc(nr_tables,
6896 sizeof(struct fixed_file_table),
6897 GFP_KERNEL);
6898 if (!ctx->file_data->table) {
6899 kfree(ctx->file_data);
6900 ctx->file_data = NULL;
6901 return -ENOMEM;
6902 }
6903
Xiaoguang Wang05589552020-03-31 14:05:18 +08006904 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006905 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6906 kfree(ctx->file_data->table);
6907 kfree(ctx->file_data);
6908 ctx->file_data = NULL;
6909 return -ENOMEM;
6910 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006911
6912 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6913 percpu_ref_exit(&ctx->file_data->refs);
6914 kfree(ctx->file_data->table);
6915 kfree(ctx->file_data);
6916 ctx->file_data = NULL;
6917 return -ENOMEM;
6918 }
6919
6920 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6921 struct fixed_file_table *table;
6922 unsigned index;
6923
6924 ret = -EFAULT;
6925 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6926 break;
6927 /* allow sparse sets */
6928 if (fd == -1) {
6929 ret = 0;
6930 continue;
6931 }
6932
6933 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6934 index = i & IORING_FILE_TABLE_MASK;
6935 file = fget(fd);
6936
6937 ret = -EBADF;
6938 if (!file)
6939 break;
6940
6941 /*
6942 * Don't allow io_uring instances to be registered. If UNIX
6943 * isn't enabled, then this causes a reference cycle and this
6944 * instance can never get freed. If UNIX is enabled we'll
6945 * handle it just fine, but there's still no point in allowing
6946 * a ring fd as it doesn't support regular read/write anyway.
6947 */
6948 if (file->f_op == &io_uring_fops) {
6949 fput(file);
6950 break;
6951 }
6952 ret = 0;
6953 table->files[index] = file;
6954 }
6955
6956 if (ret) {
6957 for (i = 0; i < ctx->nr_user_files; i++) {
6958 file = io_file_from_index(ctx, i);
6959 if (file)
6960 fput(file);
6961 }
6962 for (i = 0; i < nr_tables; i++)
6963 kfree(ctx->file_data->table[i].files);
6964
6965 kfree(ctx->file_data->table);
6966 kfree(ctx->file_data);
6967 ctx->file_data = NULL;
6968 ctx->nr_user_files = 0;
6969 return ret;
6970 }
6971
6972 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006973 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006974 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006975 return ret;
6976 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006977
Xiaoguang Wang05589552020-03-31 14:05:18 +08006978 ref_node = alloc_fixed_file_ref_node(ctx);
6979 if (IS_ERR(ref_node)) {
6980 io_sqe_files_unregister(ctx);
6981 return PTR_ERR(ref_node);
6982 }
6983
6984 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006985 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006986 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006987 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006988 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006989 return ret;
6990}
6991
Jens Axboec3a31e62019-10-03 13:59:56 -06006992static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6993 int index)
6994{
6995#if defined(CONFIG_UNIX)
6996 struct sock *sock = ctx->ring_sock->sk;
6997 struct sk_buff_head *head = &sock->sk_receive_queue;
6998 struct sk_buff *skb;
6999
7000 /*
7001 * See if we can merge this file into an existing skb SCM_RIGHTS
7002 * file set. If there's no room, fall back to allocating a new skb
7003 * and filling it in.
7004 */
7005 spin_lock_irq(&head->lock);
7006 skb = skb_peek(head);
7007 if (skb) {
7008 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7009
7010 if (fpl->count < SCM_MAX_FD) {
7011 __skb_unlink(skb, head);
7012 spin_unlock_irq(&head->lock);
7013 fpl->fp[fpl->count] = get_file(file);
7014 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7015 fpl->count++;
7016 spin_lock_irq(&head->lock);
7017 __skb_queue_head(head, skb);
7018 } else {
7019 skb = NULL;
7020 }
7021 }
7022 spin_unlock_irq(&head->lock);
7023
7024 if (skb) {
7025 fput(file);
7026 return 0;
7027 }
7028
7029 return __io_sqe_files_scm(ctx, 1, index);
7030#else
7031 return 0;
7032#endif
7033}
7034
Hillf Dantona5318d32020-03-23 17:47:15 +08007035static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007036 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007037{
Hillf Dantona5318d32020-03-23 17:47:15 +08007038 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007039 struct percpu_ref *refs = data->cur_refs;
7040 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007041
Jens Axboe05f3fb32019-12-09 11:22:50 -07007042 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007043 if (!pfile)
7044 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007045
Xiaoguang Wang05589552020-03-31 14:05:18 +08007046 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007047 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007048 list_add(&pfile->list, &ref_node->file_list);
7049
Hillf Dantona5318d32020-03-23 17:47:15 +08007050 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007051}
7052
7053static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7054 struct io_uring_files_update *up,
7055 unsigned nr_args)
7056{
7057 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007058 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007059 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007060 __s32 __user *fds;
7061 int fd, i, err;
7062 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007063 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007064
Jens Axboe05f3fb32019-12-09 11:22:50 -07007065 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007066 return -EOVERFLOW;
7067 if (done > ctx->nr_user_files)
7068 return -EINVAL;
7069
Xiaoguang Wang05589552020-03-31 14:05:18 +08007070 ref_node = alloc_fixed_file_ref_node(ctx);
7071 if (IS_ERR(ref_node))
7072 return PTR_ERR(ref_node);
7073
Jens Axboec3a31e62019-10-03 13:59:56 -06007074 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007075 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007076 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007077 struct fixed_file_table *table;
7078 unsigned index;
7079
Jens Axboec3a31e62019-10-03 13:59:56 -06007080 err = 0;
7081 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7082 err = -EFAULT;
7083 break;
7084 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007085 i = array_index_nospec(up->offset, ctx->nr_user_files);
7086 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007087 index = i & IORING_FILE_TABLE_MASK;
7088 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007089 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007090 err = io_queue_file_removal(data, file);
7091 if (err)
7092 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007093 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007094 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007095 }
7096 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007097 file = fget(fd);
7098 if (!file) {
7099 err = -EBADF;
7100 break;
7101 }
7102 /*
7103 * Don't allow io_uring instances to be registered. If
7104 * UNIX isn't enabled, then this causes a reference
7105 * cycle and this instance can never get freed. If UNIX
7106 * is enabled we'll handle it just fine, but there's
7107 * still no point in allowing a ring fd as it doesn't
7108 * support regular read/write anyway.
7109 */
7110 if (file->f_op == &io_uring_fops) {
7111 fput(file);
7112 err = -EBADF;
7113 break;
7114 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007115 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007116 err = io_sqe_file_register(ctx, file, i);
7117 if (err)
7118 break;
7119 }
7120 nr_args--;
7121 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007122 up->offset++;
7123 }
7124
Xiaoguang Wang05589552020-03-31 14:05:18 +08007125 if (needs_switch) {
7126 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007127 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007128 list_add(&ref_node->node, &data->ref_list);
7129 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007130 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007131 percpu_ref_get(&ctx->file_data->refs);
7132 } else
7133 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007134
7135 return done ? done : err;
7136}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007137
Jens Axboe05f3fb32019-12-09 11:22:50 -07007138static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7139 unsigned nr_args)
7140{
7141 struct io_uring_files_update up;
7142
7143 if (!ctx->file_data)
7144 return -ENXIO;
7145 if (!nr_args)
7146 return -EINVAL;
7147 if (copy_from_user(&up, arg, sizeof(up)))
7148 return -EFAULT;
7149 if (up.resv)
7150 return -EINVAL;
7151
7152 return __io_sqe_files_update(ctx, &up, nr_args);
7153}
Jens Axboec3a31e62019-10-03 13:59:56 -06007154
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007155static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007156{
7157 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7158
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007159 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007160 io_put_req(req);
7161}
7162
Pavel Begunkov24369c22020-01-28 03:15:48 +03007163static int io_init_wq_offload(struct io_ring_ctx *ctx,
7164 struct io_uring_params *p)
7165{
7166 struct io_wq_data data;
7167 struct fd f;
7168 struct io_ring_ctx *ctx_attach;
7169 unsigned int concurrency;
7170 int ret = 0;
7171
7172 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007173 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007174 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007175
7176 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7177 /* Do QD, or 4 * CPUS, whatever is smallest */
7178 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7179
7180 ctx->io_wq = io_wq_create(concurrency, &data);
7181 if (IS_ERR(ctx->io_wq)) {
7182 ret = PTR_ERR(ctx->io_wq);
7183 ctx->io_wq = NULL;
7184 }
7185 return ret;
7186 }
7187
7188 f = fdget(p->wq_fd);
7189 if (!f.file)
7190 return -EBADF;
7191
7192 if (f.file->f_op != &io_uring_fops) {
7193 ret = -EINVAL;
7194 goto out_fput;
7195 }
7196
7197 ctx_attach = f.file->private_data;
7198 /* @io_wq is protected by holding the fd */
7199 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7200 ret = -EINVAL;
7201 goto out_fput;
7202 }
7203
7204 ctx->io_wq = ctx_attach->io_wq;
7205out_fput:
7206 fdput(f);
7207 return ret;
7208}
7209
Jens Axboe6c271ce2019-01-10 11:22:30 -07007210static int io_sq_offload_start(struct io_ring_ctx *ctx,
7211 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007212{
7213 int ret;
7214
7215 mmgrab(current->mm);
7216 ctx->sqo_mm = current->mm;
7217
Jens Axboe6c271ce2019-01-10 11:22:30 -07007218 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007219 ret = -EPERM;
7220 if (!capable(CAP_SYS_ADMIN))
7221 goto err;
7222
Jens Axboe917257d2019-04-13 09:28:55 -06007223 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7224 if (!ctx->sq_thread_idle)
7225 ctx->sq_thread_idle = HZ;
7226
Jens Axboe6c271ce2019-01-10 11:22:30 -07007227 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007228 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007229
Jens Axboe917257d2019-04-13 09:28:55 -06007230 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007231 if (cpu >= nr_cpu_ids)
7232 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007233 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007234 goto err;
7235
Jens Axboe6c271ce2019-01-10 11:22:30 -07007236 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7237 ctx, cpu,
7238 "io_uring-sq");
7239 } else {
7240 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7241 "io_uring-sq");
7242 }
7243 if (IS_ERR(ctx->sqo_thread)) {
7244 ret = PTR_ERR(ctx->sqo_thread);
7245 ctx->sqo_thread = NULL;
7246 goto err;
7247 }
7248 wake_up_process(ctx->sqo_thread);
7249 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7250 /* Can't have SQ_AFF without SQPOLL */
7251 ret = -EINVAL;
7252 goto err;
7253 }
7254
Pavel Begunkov24369c22020-01-28 03:15:48 +03007255 ret = io_init_wq_offload(ctx, p);
7256 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007257 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007258
7259 return 0;
7260err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007261 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007262 mmdrop(ctx->sqo_mm);
7263 ctx->sqo_mm = NULL;
7264 return ret;
7265}
7266
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007267static inline void __io_unaccount_mem(struct user_struct *user,
7268 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007269{
7270 atomic_long_sub(nr_pages, &user->locked_vm);
7271}
7272
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007273static inline int __io_account_mem(struct user_struct *user,
7274 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007275{
7276 unsigned long page_limit, cur_pages, new_pages;
7277
7278 /* Don't allow more pages than we can safely lock */
7279 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7280
7281 do {
7282 cur_pages = atomic_long_read(&user->locked_vm);
7283 new_pages = cur_pages + nr_pages;
7284 if (new_pages > page_limit)
7285 return -ENOMEM;
7286 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7287 new_pages) != cur_pages);
7288
7289 return 0;
7290}
7291
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007292static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7293 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007294{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007295 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007296 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007297
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007298 if (ctx->sqo_mm) {
7299 if (acct == ACCT_LOCKED)
7300 ctx->sqo_mm->locked_vm -= nr_pages;
7301 else if (acct == ACCT_PINNED)
7302 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7303 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007304}
7305
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007306static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7307 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007308{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007309 int ret;
7310
7311 if (ctx->limit_mem) {
7312 ret = __io_account_mem(ctx->user, nr_pages);
7313 if (ret)
7314 return ret;
7315 }
7316
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007317 if (ctx->sqo_mm) {
7318 if (acct == ACCT_LOCKED)
7319 ctx->sqo_mm->locked_vm += nr_pages;
7320 else if (acct == ACCT_PINNED)
7321 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7322 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007323
7324 return 0;
7325}
7326
Jens Axboe2b188cc2019-01-07 10:46:33 -07007327static void io_mem_free(void *ptr)
7328{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007329 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007330
Mark Rutland52e04ef2019-04-30 17:30:21 +01007331 if (!ptr)
7332 return;
7333
7334 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007335 if (put_page_testzero(page))
7336 free_compound_page(page);
7337}
7338
7339static void *io_mem_alloc(size_t size)
7340{
7341 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7342 __GFP_NORETRY;
7343
7344 return (void *) __get_free_pages(gfp_flags, get_order(size));
7345}
7346
Hristo Venev75b28af2019-08-26 17:23:46 +00007347static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7348 size_t *sq_offset)
7349{
7350 struct io_rings *rings;
7351 size_t off, sq_array_size;
7352
7353 off = struct_size(rings, cqes, cq_entries);
7354 if (off == SIZE_MAX)
7355 return SIZE_MAX;
7356
7357#ifdef CONFIG_SMP
7358 off = ALIGN(off, SMP_CACHE_BYTES);
7359 if (off == 0)
7360 return SIZE_MAX;
7361#endif
7362
7363 sq_array_size = array_size(sizeof(u32), sq_entries);
7364 if (sq_array_size == SIZE_MAX)
7365 return SIZE_MAX;
7366
7367 if (check_add_overflow(off, sq_array_size, &off))
7368 return SIZE_MAX;
7369
7370 if (sq_offset)
7371 *sq_offset = off;
7372
7373 return off;
7374}
7375
Jens Axboe2b188cc2019-01-07 10:46:33 -07007376static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7377{
Hristo Venev75b28af2019-08-26 17:23:46 +00007378 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007379
Hristo Venev75b28af2019-08-26 17:23:46 +00007380 pages = (size_t)1 << get_order(
7381 rings_size(sq_entries, cq_entries, NULL));
7382 pages += (size_t)1 << get_order(
7383 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007384
Hristo Venev75b28af2019-08-26 17:23:46 +00007385 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007386}
7387
Jens Axboeedafcce2019-01-09 09:16:05 -07007388static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7389{
7390 int i, j;
7391
7392 if (!ctx->user_bufs)
7393 return -ENXIO;
7394
7395 for (i = 0; i < ctx->nr_user_bufs; i++) {
7396 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7397
7398 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007399 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007400
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007401 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007402 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007403 imu->nr_bvecs = 0;
7404 }
7405
7406 kfree(ctx->user_bufs);
7407 ctx->user_bufs = NULL;
7408 ctx->nr_user_bufs = 0;
7409 return 0;
7410}
7411
7412static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7413 void __user *arg, unsigned index)
7414{
7415 struct iovec __user *src;
7416
7417#ifdef CONFIG_COMPAT
7418 if (ctx->compat) {
7419 struct compat_iovec __user *ciovs;
7420 struct compat_iovec ciov;
7421
7422 ciovs = (struct compat_iovec __user *) arg;
7423 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7424 return -EFAULT;
7425
Jens Axboed55e5f52019-12-11 16:12:15 -07007426 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007427 dst->iov_len = ciov.iov_len;
7428 return 0;
7429 }
7430#endif
7431 src = (struct iovec __user *) arg;
7432 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7433 return -EFAULT;
7434 return 0;
7435}
7436
7437static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7438 unsigned nr_args)
7439{
7440 struct vm_area_struct **vmas = NULL;
7441 struct page **pages = NULL;
7442 int i, j, got_pages = 0;
7443 int ret = -EINVAL;
7444
7445 if (ctx->user_bufs)
7446 return -EBUSY;
7447 if (!nr_args || nr_args > UIO_MAXIOV)
7448 return -EINVAL;
7449
7450 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7451 GFP_KERNEL);
7452 if (!ctx->user_bufs)
7453 return -ENOMEM;
7454
7455 for (i = 0; i < nr_args; i++) {
7456 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7457 unsigned long off, start, end, ubuf;
7458 int pret, nr_pages;
7459 struct iovec iov;
7460 size_t size;
7461
7462 ret = io_copy_iov(ctx, &iov, arg, i);
7463 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007464 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007465
7466 /*
7467 * Don't impose further limits on the size and buffer
7468 * constraints here, we'll -EINVAL later when IO is
7469 * submitted if they are wrong.
7470 */
7471 ret = -EFAULT;
7472 if (!iov.iov_base || !iov.iov_len)
7473 goto err;
7474
7475 /* arbitrary limit, but we need something */
7476 if (iov.iov_len > SZ_1G)
7477 goto err;
7478
7479 ubuf = (unsigned long) iov.iov_base;
7480 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7481 start = ubuf >> PAGE_SHIFT;
7482 nr_pages = end - start;
7483
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007484 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007485 if (ret)
7486 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007487
7488 ret = 0;
7489 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007490 kvfree(vmas);
7491 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007492 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007493 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007494 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007495 sizeof(struct vm_area_struct *),
7496 GFP_KERNEL);
7497 if (!pages || !vmas) {
7498 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007499 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007500 goto err;
7501 }
7502 got_pages = nr_pages;
7503 }
7504
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007505 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007506 GFP_KERNEL);
7507 ret = -ENOMEM;
7508 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007509 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007510 goto err;
7511 }
7512
7513 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007514 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007515 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007516 FOLL_WRITE | FOLL_LONGTERM,
7517 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007518 if (pret == nr_pages) {
7519 /* don't support file backed memory */
7520 for (j = 0; j < nr_pages; j++) {
7521 struct vm_area_struct *vma = vmas[j];
7522
7523 if (vma->vm_file &&
7524 !is_file_hugepages(vma->vm_file)) {
7525 ret = -EOPNOTSUPP;
7526 break;
7527 }
7528 }
7529 } else {
7530 ret = pret < 0 ? pret : -EFAULT;
7531 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007532 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007533 if (ret) {
7534 /*
7535 * if we did partial map, or found file backed vmas,
7536 * release any pages we did get
7537 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007538 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007539 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007540 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007541 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007542 goto err;
7543 }
7544
7545 off = ubuf & ~PAGE_MASK;
7546 size = iov.iov_len;
7547 for (j = 0; j < nr_pages; j++) {
7548 size_t vec_len;
7549
7550 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7551 imu->bvec[j].bv_page = pages[j];
7552 imu->bvec[j].bv_len = vec_len;
7553 imu->bvec[j].bv_offset = off;
7554 off = 0;
7555 size -= vec_len;
7556 }
7557 /* store original address for later verification */
7558 imu->ubuf = ubuf;
7559 imu->len = iov.iov_len;
7560 imu->nr_bvecs = nr_pages;
7561
7562 ctx->nr_user_bufs++;
7563 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007564 kvfree(pages);
7565 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007566 return 0;
7567err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007568 kvfree(pages);
7569 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007570 io_sqe_buffer_unregister(ctx);
7571 return ret;
7572}
7573
Jens Axboe9b402842019-04-11 11:45:41 -06007574static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7575{
7576 __s32 __user *fds = arg;
7577 int fd;
7578
7579 if (ctx->cq_ev_fd)
7580 return -EBUSY;
7581
7582 if (copy_from_user(&fd, fds, sizeof(*fds)))
7583 return -EFAULT;
7584
7585 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7586 if (IS_ERR(ctx->cq_ev_fd)) {
7587 int ret = PTR_ERR(ctx->cq_ev_fd);
7588 ctx->cq_ev_fd = NULL;
7589 return ret;
7590 }
7591
7592 return 0;
7593}
7594
7595static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7596{
7597 if (ctx->cq_ev_fd) {
7598 eventfd_ctx_put(ctx->cq_ev_fd);
7599 ctx->cq_ev_fd = NULL;
7600 return 0;
7601 }
7602
7603 return -ENXIO;
7604}
7605
Jens Axboe5a2e7452020-02-23 16:23:11 -07007606static int __io_destroy_buffers(int id, void *p, void *data)
7607{
7608 struct io_ring_ctx *ctx = data;
7609 struct io_buffer *buf = p;
7610
Jens Axboe067524e2020-03-02 16:32:28 -07007611 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007612 return 0;
7613}
7614
7615static void io_destroy_buffers(struct io_ring_ctx *ctx)
7616{
7617 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7618 idr_destroy(&ctx->io_buffer_idr);
7619}
7620
Jens Axboe2b188cc2019-01-07 10:46:33 -07007621static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7622{
Jens Axboe6b063142019-01-10 22:13:58 -07007623 io_finish_async(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007624 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007625 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007626 ctx->sqo_mm = NULL;
7627 }
Jens Axboedef596e2019-01-09 08:59:42 -07007628
7629 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007630 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007631 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007632 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007633 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007634 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007635
Jens Axboe2b188cc2019-01-07 10:46:33 -07007636#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007637 if (ctx->ring_sock) {
7638 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007639 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007640 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007641#endif
7642
Hristo Venev75b28af2019-08-26 17:23:46 +00007643 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007644 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007645
7646 percpu_ref_exit(&ctx->refs);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007647 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7648 ACCT_LOCKED);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007649 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007650 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007651 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007652 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007653 kfree(ctx);
7654}
7655
7656static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7657{
7658 struct io_ring_ctx *ctx = file->private_data;
7659 __poll_t mask = 0;
7660
7661 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007662 /*
7663 * synchronizes with barrier from wq_has_sleeper call in
7664 * io_commit_cqring
7665 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007666 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007667 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7668 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007669 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007670 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007671 mask |= EPOLLIN | EPOLLRDNORM;
7672
7673 return mask;
7674}
7675
7676static int io_uring_fasync(int fd, struct file *file, int on)
7677{
7678 struct io_ring_ctx *ctx = file->private_data;
7679
7680 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7681}
7682
Jens Axboe071698e2020-01-28 10:04:42 -07007683static int io_remove_personalities(int id, void *p, void *data)
7684{
7685 struct io_ring_ctx *ctx = data;
7686 const struct cred *cred;
7687
7688 cred = idr_remove(&ctx->personality_idr, id);
7689 if (cred)
7690 put_cred(cred);
7691 return 0;
7692}
7693
Jens Axboe85faa7b2020-04-09 18:14:00 -06007694static void io_ring_exit_work(struct work_struct *work)
7695{
7696 struct io_ring_ctx *ctx;
7697
7698 ctx = container_of(work, struct io_ring_ctx, exit_work);
7699 if (ctx->rings)
7700 io_cqring_overflow_flush(ctx, true);
7701
Jens Axboe56952e92020-06-17 15:00:04 -06007702 /*
7703 * If we're doing polled IO and end up having requests being
7704 * submitted async (out-of-line), then completions can come in while
7705 * we're waiting for refs to drop. We need to reap these manually,
7706 * as nobody else will be looking for them.
7707 */
7708 while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7709 io_iopoll_reap_events(ctx);
7710 if (ctx->rings)
7711 io_cqring_overflow_flush(ctx, true);
7712 }
Jens Axboe85faa7b2020-04-09 18:14:00 -06007713 io_ring_ctx_free(ctx);
7714}
7715
Jens Axboe2b188cc2019-01-07 10:46:33 -07007716static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7717{
7718 mutex_lock(&ctx->uring_lock);
7719 percpu_ref_kill(&ctx->refs);
7720 mutex_unlock(&ctx->uring_lock);
7721
Jens Axboe5262f562019-09-17 12:26:57 -06007722 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007723 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007724
7725 if (ctx->io_wq)
7726 io_wq_cancel_all(ctx->io_wq);
7727
Jens Axboedef596e2019-01-09 08:59:42 -07007728 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007729 /* if we failed setting up the ctx, we might not have any rings */
7730 if (ctx->rings)
7731 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007732 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007733 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7734 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007735}
7736
7737static int io_uring_release(struct inode *inode, struct file *file)
7738{
7739 struct io_ring_ctx *ctx = file->private_data;
7740
7741 file->private_data = NULL;
7742 io_ring_ctx_wait_and_kill(ctx);
7743 return 0;
7744}
7745
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007746static bool io_wq_files_match(struct io_wq_work *work, void *data)
7747{
7748 struct files_struct *files = data;
7749
7750 return work->files == files;
7751}
7752
Jens Axboefcb323c2019-10-24 12:39:47 -06007753static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7754 struct files_struct *files)
7755{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007756 if (list_empty_careful(&ctx->inflight_list))
7757 return;
7758
7759 /* cancel all at once, should be faster than doing it one by one*/
7760 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7761
Jens Axboefcb323c2019-10-24 12:39:47 -06007762 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007763 struct io_kiocb *cancel_req = NULL, *req;
7764 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007765
7766 spin_lock_irq(&ctx->inflight_lock);
7767 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007768 if (req->work.files != files)
7769 continue;
7770 /* req is being completed, ignore */
7771 if (!refcount_inc_not_zero(&req->refs))
7772 continue;
7773 cancel_req = req;
7774 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007775 }
Jens Axboe768134d2019-11-10 20:30:53 -07007776 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007777 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007778 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007779 spin_unlock_irq(&ctx->inflight_lock);
7780
Jens Axboe768134d2019-11-10 20:30:53 -07007781 /* We need to keep going until we don't find a matching req */
7782 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007783 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007784
Jens Axboe2ca10252020-02-13 17:17:35 -07007785 if (cancel_req->flags & REQ_F_OVERFLOW) {
7786 spin_lock_irq(&ctx->completion_lock);
7787 list_del(&cancel_req->list);
7788 cancel_req->flags &= ~REQ_F_OVERFLOW;
7789 if (list_empty(&ctx->cq_overflow_list)) {
7790 clear_bit(0, &ctx->sq_check_overflow);
7791 clear_bit(0, &ctx->cq_check_overflow);
7792 }
7793 spin_unlock_irq(&ctx->completion_lock);
7794
7795 WRITE_ONCE(ctx->rings->cq_overflow,
7796 atomic_inc_return(&ctx->cached_cq_overflow));
7797
7798 /*
7799 * Put inflight ref and overflow ref. If that's
7800 * all we had, then we're done with this request.
7801 */
7802 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007803 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007804 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007805 continue;
7806 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007807 } else {
7808 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7809 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007810 }
7811
Jens Axboefcb323c2019-10-24 12:39:47 -06007812 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007813 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007814 }
7815}
7816
Pavel Begunkov801dd572020-06-15 10:33:14 +03007817static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007818{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007819 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7820 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007821
Pavel Begunkov801dd572020-06-15 10:33:14 +03007822 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007823}
7824
Jens Axboefcb323c2019-10-24 12:39:47 -06007825static int io_uring_flush(struct file *file, void *data)
7826{
7827 struct io_ring_ctx *ctx = file->private_data;
7828
7829 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007830
7831 /*
7832 * If the task is going away, cancel work it may have pending
7833 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007834 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7835 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007836
Jens Axboefcb323c2019-10-24 12:39:47 -06007837 return 0;
7838}
7839
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007840static void *io_uring_validate_mmap_request(struct file *file,
7841 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007842{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007843 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007844 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007845 struct page *page;
7846 void *ptr;
7847
7848 switch (offset) {
7849 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007850 case IORING_OFF_CQ_RING:
7851 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007852 break;
7853 case IORING_OFF_SQES:
7854 ptr = ctx->sq_sqes;
7855 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007856 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007857 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007858 }
7859
7860 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007861 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007862 return ERR_PTR(-EINVAL);
7863
7864 return ptr;
7865}
7866
7867#ifdef CONFIG_MMU
7868
7869static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7870{
7871 size_t sz = vma->vm_end - vma->vm_start;
7872 unsigned long pfn;
7873 void *ptr;
7874
7875 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7876 if (IS_ERR(ptr))
7877 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007878
7879 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7880 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7881}
7882
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007883#else /* !CONFIG_MMU */
7884
7885static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7886{
7887 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7888}
7889
7890static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7891{
7892 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7893}
7894
7895static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7896 unsigned long addr, unsigned long len,
7897 unsigned long pgoff, unsigned long flags)
7898{
7899 void *ptr;
7900
7901 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7902 if (IS_ERR(ptr))
7903 return PTR_ERR(ptr);
7904
7905 return (unsigned long) ptr;
7906}
7907
7908#endif /* !CONFIG_MMU */
7909
Jens Axboe2b188cc2019-01-07 10:46:33 -07007910SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7911 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7912 size_t, sigsz)
7913{
7914 struct io_ring_ctx *ctx;
7915 long ret = -EBADF;
7916 int submitted = 0;
7917 struct fd f;
7918
Jens Axboeb41e9852020-02-17 09:52:41 -07007919 if (current->task_works)
7920 task_work_run();
7921
Jens Axboe6c271ce2019-01-10 11:22:30 -07007922 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007923 return -EINVAL;
7924
7925 f = fdget(fd);
7926 if (!f.file)
7927 return -EBADF;
7928
7929 ret = -EOPNOTSUPP;
7930 if (f.file->f_op != &io_uring_fops)
7931 goto out_fput;
7932
7933 ret = -ENXIO;
7934 ctx = f.file->private_data;
7935 if (!percpu_ref_tryget(&ctx->refs))
7936 goto out_fput;
7937
Jens Axboe6c271ce2019-01-10 11:22:30 -07007938 /*
7939 * For SQ polling, the thread will do all submissions and completions.
7940 * Just return the requested submit count, and wake the thread if
7941 * we were asked to.
7942 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007943 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007944 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007945 if (!list_empty_careful(&ctx->cq_overflow_list))
7946 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007947 if (flags & IORING_ENTER_SQ_WAKEUP)
7948 wake_up(&ctx->sqo_wait);
7949 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007950 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007951 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007952 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007953 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007954
7955 if (submitted != to_submit)
7956 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007957 }
7958 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007959 unsigned nr_events = 0;
7960
Jens Axboe2b188cc2019-01-07 10:46:33 -07007961 min_complete = min(min_complete, ctx->cq_entries);
7962
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007963 /*
7964 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7965 * space applications don't need to do io completion events
7966 * polling again, they can rely on io_sq_thread to do polling
7967 * work, which can reduce cpu usage and uring_lock contention.
7968 */
7969 if (ctx->flags & IORING_SETUP_IOPOLL &&
7970 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007971 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007972 } else {
7973 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7974 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007975 }
7976
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007977out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007978 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007979out_fput:
7980 fdput(f);
7981 return submitted ? submitted : ret;
7982}
7983
Tobias Klauserbebdb652020-02-26 18:38:32 +01007984#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007985static int io_uring_show_cred(int id, void *p, void *data)
7986{
7987 const struct cred *cred = p;
7988 struct seq_file *m = data;
7989 struct user_namespace *uns = seq_user_ns(m);
7990 struct group_info *gi;
7991 kernel_cap_t cap;
7992 unsigned __capi;
7993 int g;
7994
7995 seq_printf(m, "%5d\n", id);
7996 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7997 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7998 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7999 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8000 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8001 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8002 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8003 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8004 seq_puts(m, "\n\tGroups:\t");
8005 gi = cred->group_info;
8006 for (g = 0; g < gi->ngroups; g++) {
8007 seq_put_decimal_ull(m, g ? " " : "",
8008 from_kgid_munged(uns, gi->gid[g]));
8009 }
8010 seq_puts(m, "\n\tCapEff:\t");
8011 cap = cred->cap_effective;
8012 CAP_FOR_EACH_U32(__capi)
8013 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8014 seq_putc(m, '\n');
8015 return 0;
8016}
8017
8018static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8019{
8020 int i;
8021
8022 mutex_lock(&ctx->uring_lock);
8023 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
8024 for (i = 0; i < ctx->nr_user_files; i++) {
8025 struct fixed_file_table *table;
8026 struct file *f;
8027
8028 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8029 f = table->files[i & IORING_FILE_TABLE_MASK];
8030 if (f)
8031 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8032 else
8033 seq_printf(m, "%5u: <none>\n", i);
8034 }
8035 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
8036 for (i = 0; i < ctx->nr_user_bufs; i++) {
8037 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8038
8039 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8040 (unsigned int) buf->len);
8041 }
8042 if (!idr_is_empty(&ctx->personality_idr)) {
8043 seq_printf(m, "Personalities:\n");
8044 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8045 }
Jens Axboed7718a92020-02-14 22:23:12 -07008046 seq_printf(m, "PollList:\n");
8047 spin_lock_irq(&ctx->completion_lock);
8048 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8049 struct hlist_head *list = &ctx->cancel_hash[i];
8050 struct io_kiocb *req;
8051
8052 hlist_for_each_entry(req, list, hash_node)
8053 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8054 req->task->task_works != NULL);
8055 }
8056 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008057 mutex_unlock(&ctx->uring_lock);
8058}
8059
8060static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8061{
8062 struct io_ring_ctx *ctx = f->private_data;
8063
8064 if (percpu_ref_tryget(&ctx->refs)) {
8065 __io_uring_show_fdinfo(ctx, m);
8066 percpu_ref_put(&ctx->refs);
8067 }
8068}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008069#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008070
Jens Axboe2b188cc2019-01-07 10:46:33 -07008071static const struct file_operations io_uring_fops = {
8072 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008073 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008074 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008075#ifndef CONFIG_MMU
8076 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8077 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8078#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008079 .poll = io_uring_poll,
8080 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008081#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008082 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008083#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008084};
8085
8086static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8087 struct io_uring_params *p)
8088{
Hristo Venev75b28af2019-08-26 17:23:46 +00008089 struct io_rings *rings;
8090 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008091
Hristo Venev75b28af2019-08-26 17:23:46 +00008092 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8093 if (size == SIZE_MAX)
8094 return -EOVERFLOW;
8095
8096 rings = io_mem_alloc(size);
8097 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008098 return -ENOMEM;
8099
Hristo Venev75b28af2019-08-26 17:23:46 +00008100 ctx->rings = rings;
8101 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8102 rings->sq_ring_mask = p->sq_entries - 1;
8103 rings->cq_ring_mask = p->cq_entries - 1;
8104 rings->sq_ring_entries = p->sq_entries;
8105 rings->cq_ring_entries = p->cq_entries;
8106 ctx->sq_mask = rings->sq_ring_mask;
8107 ctx->cq_mask = rings->cq_ring_mask;
8108 ctx->sq_entries = rings->sq_ring_entries;
8109 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008110
8111 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008112 if (size == SIZE_MAX) {
8113 io_mem_free(ctx->rings);
8114 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008115 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008116 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008117
8118 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008119 if (!ctx->sq_sqes) {
8120 io_mem_free(ctx->rings);
8121 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008122 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008123 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008124
Jens Axboe2b188cc2019-01-07 10:46:33 -07008125 return 0;
8126}
8127
8128/*
8129 * Allocate an anonymous fd, this is what constitutes the application
8130 * visible backing of an io_uring instance. The application mmaps this
8131 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8132 * we have to tie this fd to a socket for file garbage collection purposes.
8133 */
8134static int io_uring_get_fd(struct io_ring_ctx *ctx)
8135{
8136 struct file *file;
8137 int ret;
8138
8139#if defined(CONFIG_UNIX)
8140 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8141 &ctx->ring_sock);
8142 if (ret)
8143 return ret;
8144#endif
8145
8146 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8147 if (ret < 0)
8148 goto err;
8149
8150 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8151 O_RDWR | O_CLOEXEC);
8152 if (IS_ERR(file)) {
8153 put_unused_fd(ret);
8154 ret = PTR_ERR(file);
8155 goto err;
8156 }
8157
8158#if defined(CONFIG_UNIX)
8159 ctx->ring_sock->file = file;
8160#endif
8161 fd_install(ret, file);
8162 return ret;
8163err:
8164#if defined(CONFIG_UNIX)
8165 sock_release(ctx->ring_sock);
8166 ctx->ring_sock = NULL;
8167#endif
8168 return ret;
8169}
8170
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008171static int io_uring_create(unsigned entries, struct io_uring_params *p,
8172 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008173{
8174 struct user_struct *user = NULL;
8175 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008176 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008177 int ret;
8178
Jens Axboe8110c1a2019-12-28 15:39:54 -07008179 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008180 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008181 if (entries > IORING_MAX_ENTRIES) {
8182 if (!(p->flags & IORING_SETUP_CLAMP))
8183 return -EINVAL;
8184 entries = IORING_MAX_ENTRIES;
8185 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008186
8187 /*
8188 * Use twice as many entries for the CQ ring. It's possible for the
8189 * application to drive a higher depth than the size of the SQ ring,
8190 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008191 * some flexibility in overcommitting a bit. If the application has
8192 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8193 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008194 */
8195 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008196 if (p->flags & IORING_SETUP_CQSIZE) {
8197 /*
8198 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8199 * to a power-of-two, if it isn't already. We do NOT impose
8200 * any cq vs sq ring sizing.
8201 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008202 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008203 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008204 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8205 if (!(p->flags & IORING_SETUP_CLAMP))
8206 return -EINVAL;
8207 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8208 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008209 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8210 } else {
8211 p->cq_entries = 2 * p->sq_entries;
8212 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008213
8214 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008215 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008216
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008217 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008218 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008219 ring_pages(p->sq_entries, p->cq_entries));
8220 if (ret) {
8221 free_uid(user);
8222 return ret;
8223 }
8224 }
8225
8226 ctx = io_ring_ctx_alloc(p);
8227 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008228 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008229 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008230 p->cq_entries));
8231 free_uid(user);
8232 return -ENOMEM;
8233 }
8234 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008235 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008236 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008237
8238 ret = io_allocate_scq_urings(ctx, p);
8239 if (ret)
8240 goto err;
8241
Jens Axboe6c271ce2019-01-10 11:22:30 -07008242 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008243 if (ret)
8244 goto err;
8245
Jens Axboe2b188cc2019-01-07 10:46:33 -07008246 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008247 p->sq_off.head = offsetof(struct io_rings, sq.head);
8248 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8249 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8250 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8251 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8252 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8253 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008254
8255 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008256 p->cq_off.head = offsetof(struct io_rings, cq.head);
8257 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8258 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8259 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8260 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8261 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008262 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008263
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008264 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8265 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008266 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8267 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008268
8269 if (copy_to_user(params, p, sizeof(*p))) {
8270 ret = -EFAULT;
8271 goto err;
8272 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008273 /*
8274 * Install ring fd as the very last thing, so we don't risk someone
8275 * having closed it before we finish setup
8276 */
8277 ret = io_uring_get_fd(ctx);
8278 if (ret < 0)
8279 goto err;
8280
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008281 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008282 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8283 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008284 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008285 return ret;
8286err:
8287 io_ring_ctx_wait_and_kill(ctx);
8288 return ret;
8289}
8290
8291/*
8292 * Sets up an aio uring context, and returns the fd. Applications asks for a
8293 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8294 * params structure passed in.
8295 */
8296static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8297{
8298 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008299 int i;
8300
8301 if (copy_from_user(&p, params, sizeof(p)))
8302 return -EFAULT;
8303 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8304 if (p.resv[i])
8305 return -EINVAL;
8306 }
8307
Jens Axboe6c271ce2019-01-10 11:22:30 -07008308 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008309 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008310 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008311 return -EINVAL;
8312
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008313 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008314}
8315
8316SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8317 struct io_uring_params __user *, params)
8318{
8319 return io_uring_setup(entries, params);
8320}
8321
Jens Axboe66f4af92020-01-16 15:36:52 -07008322static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8323{
8324 struct io_uring_probe *p;
8325 size_t size;
8326 int i, ret;
8327
8328 size = struct_size(p, ops, nr_args);
8329 if (size == SIZE_MAX)
8330 return -EOVERFLOW;
8331 p = kzalloc(size, GFP_KERNEL);
8332 if (!p)
8333 return -ENOMEM;
8334
8335 ret = -EFAULT;
8336 if (copy_from_user(p, arg, size))
8337 goto out;
8338 ret = -EINVAL;
8339 if (memchr_inv(p, 0, size))
8340 goto out;
8341
8342 p->last_op = IORING_OP_LAST - 1;
8343 if (nr_args > IORING_OP_LAST)
8344 nr_args = IORING_OP_LAST;
8345
8346 for (i = 0; i < nr_args; i++) {
8347 p->ops[i].op = i;
8348 if (!io_op_defs[i].not_supported)
8349 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8350 }
8351 p->ops_len = i;
8352
8353 ret = 0;
8354 if (copy_to_user(arg, p, size))
8355 ret = -EFAULT;
8356out:
8357 kfree(p);
8358 return ret;
8359}
8360
Jens Axboe071698e2020-01-28 10:04:42 -07008361static int io_register_personality(struct io_ring_ctx *ctx)
8362{
8363 const struct cred *creds = get_current_cred();
8364 int id;
8365
8366 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8367 USHRT_MAX, GFP_KERNEL);
8368 if (id < 0)
8369 put_cred(creds);
8370 return id;
8371}
8372
8373static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8374{
8375 const struct cred *old_creds;
8376
8377 old_creds = idr_remove(&ctx->personality_idr, id);
8378 if (old_creds) {
8379 put_cred(old_creds);
8380 return 0;
8381 }
8382
8383 return -EINVAL;
8384}
8385
8386static bool io_register_op_must_quiesce(int op)
8387{
8388 switch (op) {
8389 case IORING_UNREGISTER_FILES:
8390 case IORING_REGISTER_FILES_UPDATE:
8391 case IORING_REGISTER_PROBE:
8392 case IORING_REGISTER_PERSONALITY:
8393 case IORING_UNREGISTER_PERSONALITY:
8394 return false;
8395 default:
8396 return true;
8397 }
8398}
8399
Jens Axboeedafcce2019-01-09 09:16:05 -07008400static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8401 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008402 __releases(ctx->uring_lock)
8403 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008404{
8405 int ret;
8406
Jens Axboe35fa71a2019-04-22 10:23:23 -06008407 /*
8408 * We're inside the ring mutex, if the ref is already dying, then
8409 * someone else killed the ctx or is already going through
8410 * io_uring_register().
8411 */
8412 if (percpu_ref_is_dying(&ctx->refs))
8413 return -ENXIO;
8414
Jens Axboe071698e2020-01-28 10:04:42 -07008415 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008416 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008417
Jens Axboe05f3fb32019-12-09 11:22:50 -07008418 /*
8419 * Drop uring mutex before waiting for references to exit. If
8420 * another thread is currently inside io_uring_enter() it might
8421 * need to grab the uring_lock to make progress. If we hold it
8422 * here across the drain wait, then we can deadlock. It's safe
8423 * to drop the mutex here, since no new references will come in
8424 * after we've killed the percpu ref.
8425 */
8426 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008427 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008428 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008429 if (ret) {
8430 percpu_ref_resurrect(&ctx->refs);
8431 ret = -EINTR;
8432 goto out;
8433 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008434 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008435
8436 switch (opcode) {
8437 case IORING_REGISTER_BUFFERS:
8438 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8439 break;
8440 case IORING_UNREGISTER_BUFFERS:
8441 ret = -EINVAL;
8442 if (arg || nr_args)
8443 break;
8444 ret = io_sqe_buffer_unregister(ctx);
8445 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008446 case IORING_REGISTER_FILES:
8447 ret = io_sqe_files_register(ctx, arg, nr_args);
8448 break;
8449 case IORING_UNREGISTER_FILES:
8450 ret = -EINVAL;
8451 if (arg || nr_args)
8452 break;
8453 ret = io_sqe_files_unregister(ctx);
8454 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008455 case IORING_REGISTER_FILES_UPDATE:
8456 ret = io_sqe_files_update(ctx, arg, nr_args);
8457 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008458 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008459 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008460 ret = -EINVAL;
8461 if (nr_args != 1)
8462 break;
8463 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008464 if (ret)
8465 break;
8466 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8467 ctx->eventfd_async = 1;
8468 else
8469 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008470 break;
8471 case IORING_UNREGISTER_EVENTFD:
8472 ret = -EINVAL;
8473 if (arg || nr_args)
8474 break;
8475 ret = io_eventfd_unregister(ctx);
8476 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008477 case IORING_REGISTER_PROBE:
8478 ret = -EINVAL;
8479 if (!arg || nr_args > 256)
8480 break;
8481 ret = io_probe(ctx, arg, nr_args);
8482 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008483 case IORING_REGISTER_PERSONALITY:
8484 ret = -EINVAL;
8485 if (arg || nr_args)
8486 break;
8487 ret = io_register_personality(ctx);
8488 break;
8489 case IORING_UNREGISTER_PERSONALITY:
8490 ret = -EINVAL;
8491 if (arg)
8492 break;
8493 ret = io_unregister_personality(ctx, nr_args);
8494 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008495 default:
8496 ret = -EINVAL;
8497 break;
8498 }
8499
Jens Axboe071698e2020-01-28 10:04:42 -07008500 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008501 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008502 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008503out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008504 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008505 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008506 return ret;
8507}
8508
8509SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8510 void __user *, arg, unsigned int, nr_args)
8511{
8512 struct io_ring_ctx *ctx;
8513 long ret = -EBADF;
8514 struct fd f;
8515
8516 f = fdget(fd);
8517 if (!f.file)
8518 return -EBADF;
8519
8520 ret = -EOPNOTSUPP;
8521 if (f.file->f_op != &io_uring_fops)
8522 goto out_fput;
8523
8524 ctx = f.file->private_data;
8525
8526 mutex_lock(&ctx->uring_lock);
8527 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8528 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008529 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8530 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008531out_fput:
8532 fdput(f);
8533 return ret;
8534}
8535
Jens Axboe2b188cc2019-01-07 10:46:33 -07008536static int __init io_uring_init(void)
8537{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008538#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8539 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8540 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8541} while (0)
8542
8543#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8544 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8545 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8546 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8547 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8548 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8549 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8550 BUILD_BUG_SQE_ELEM(8, __u64, off);
8551 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8552 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008553 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008554 BUILD_BUG_SQE_ELEM(24, __u32, len);
8555 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8556 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8557 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8558 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008559 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8560 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008561 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8562 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8563 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8564 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8565 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8566 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8567 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8568 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008569 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008570 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8571 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8572 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008573 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008574
Jens Axboed3656342019-12-18 09:50:26 -07008575 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008576 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008577 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8578 return 0;
8579};
8580__initcall(io_uring_init);