blob: f283d111666b768c0fe498162e46959b1257a2f1 [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,
534 REQ_F_TIMEOUT_BIT,
535 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300536 REQ_F_TIMEOUT_NOSEQ_BIT,
537 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300538 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700539 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700540 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700541 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600542 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300543 REQ_F_QUEUE_TIMEOUT_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800544 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300545 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700546
547 /* not a real bit, just to check we're not overflowing the space */
548 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300549};
550
551enum {
552 /* ctx owns file */
553 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
554 /* drain existing IO first */
555 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
556 /* linked sqes */
557 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
558 /* doesn't sever on completion < 0 */
559 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
560 /* IOSQE_ASYNC */
561 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700562 /* IOSQE_BUFFER_SELECT */
563 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300564
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300565 /* head of a link */
566 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300567 /* fail rest of links */
568 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
569 /* on inflight list */
570 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
571 /* read/write uses file position */
572 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
573 /* must not punt to workers */
574 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575 /* has linked timeout */
576 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
577 /* timeout request */
578 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
579 /* regular file */
580 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300581 /* no timeout sequence */
582 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
583 /* completion under lock */
584 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300585 /* needs cleanup */
586 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700587 /* in overflow list */
588 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700589 /* already went through poll handler */
590 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700591 /* buffer already selected */
592 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600593 /* doesn't need file table for this request */
594 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300595 /* needs to queue linked timeout */
596 REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800597 /* io_wq_work is initialized */
598 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300599 /* req->task is refcounted */
600 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700601};
602
603struct async_poll {
604 struct io_poll_iocb poll;
605 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300606};
607
Jens Axboe09bb8392019-03-13 12:39:28 -0600608/*
609 * NOTE! Each of the iocb union members has the file pointer
610 * as the first entry in their struct definition. So you can
611 * access the file pointer through any of the sub-structs,
612 * or directly as just 'ki_filp' in this struct.
613 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700614struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700615 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600616 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700617 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700618 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700619 struct io_accept accept;
620 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700621 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700622 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700623 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700624 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700625 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700626 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700627 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700628 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700629 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700630 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300631 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700632 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700633 struct io_statx statx;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700634 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700635
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700636 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300637 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700638 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800639 /* polled IO has completed */
640 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700642 u16 buf_index;
643
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700645 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700646 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700647 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600648 struct task_struct *task;
649 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700650 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600651 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600652 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700653
Jens Axboed7718a92020-02-14 22:23:12 -0700654 struct list_head link_list;
655
Jens Axboefcb323c2019-10-24 12:39:47 -0600656 struct list_head inflight_entry;
657
Xiaoguang Wang05589552020-03-31 14:05:18 +0800658 struct percpu_ref *fixed_file_refs;
659
Jens Axboeb41e9852020-02-17 09:52:41 -0700660 union {
661 /*
662 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700663 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
664 * async armed poll handlers for regular commands. The latter
665 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700666 */
667 struct {
Jens Axboed7718a92020-02-14 22:23:12 -0700668 struct hlist_node hash_node;
669 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700670 };
671 struct io_wq_work work;
672 };
Pavel Begunkov8ef77762020-06-27 14:04:59 +0300673 struct callback_head task_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700674};
675
Jens Axboedef596e2019-01-09 08:59:42 -0700676#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700677
Jens Axboe013538b2020-06-22 09:29:15 -0600678struct io_comp_state {
679 unsigned int nr;
680 struct list_head list;
681 struct io_ring_ctx *ctx;
682};
683
Jens Axboe9a56a232019-01-09 09:06:50 -0700684struct io_submit_state {
685 struct blk_plug plug;
686
687 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700688 * io_kiocb alloc cache
689 */
690 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300691 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700692
693 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600694 * Batch completion logic
695 */
696 struct io_comp_state comp;
697
698 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700699 * File reference cache
700 */
701 struct file *file;
702 unsigned int fd;
703 unsigned int has_refs;
704 unsigned int used_refs;
705 unsigned int ios_left;
706};
707
Jens Axboed3656342019-12-18 09:50:26 -0700708struct io_op_def {
709 /* needs req->io allocated for deferral/async */
710 unsigned async_ctx : 1;
711 /* needs current->mm setup, does mm access */
712 unsigned needs_mm : 1;
713 /* needs req->file assigned */
714 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600715 /* don't fail if file grab fails */
716 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700717 /* hash wq insertion if file is a regular file */
718 unsigned hash_reg_file : 1;
719 /* unbound wq insertion if file is a non-regular file */
720 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700721 /* opcode is not supported by this kernel */
722 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700723 /* needs file table */
724 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700725 /* needs ->fs */
726 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700727 /* set if opcode supports polled "wait" */
728 unsigned pollin : 1;
729 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700730 /* op supports buffer selection */
731 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700732};
733
734static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_NOP] = {},
736 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700737 .async_ctx = 1,
738 .needs_mm = 1,
739 .needs_file = 1,
740 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700741 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700742 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700743 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300744 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700745 .async_ctx = 1,
746 .needs_mm = 1,
747 .needs_file = 1,
748 .hash_reg_file = 1,
749 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700750 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700751 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300752 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .needs_file = 1,
754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700756 .needs_file = 1,
757 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700758 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700759 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700761 .needs_file = 1,
762 .hash_reg_file = 1,
763 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700764 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700765 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300766 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700767 .needs_file = 1,
768 .unbound_nonreg_file = 1,
769 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300770 [IORING_OP_POLL_REMOVE] = {},
771 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700772 .needs_file = 1,
773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .async_ctx = 1,
776 .needs_mm = 1,
777 .needs_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700779 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700780 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700783 .async_ctx = 1,
784 .needs_mm = 1,
785 .needs_file = 1,
786 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700787 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700788 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700789 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700790 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300791 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700792 .async_ctx = 1,
793 .needs_mm = 1,
794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_TIMEOUT_REMOVE] = {},
796 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_mm = 1,
798 .needs_file = 1,
799 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700800 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700801 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_ASYNC_CANCEL] = {},
804 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700805 .async_ctx = 1,
806 .needs_mm = 1,
807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700809 .async_ctx = 1,
810 .needs_mm = 1,
811 .needs_file = 1,
812 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700813 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700814 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300815 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700816 .needs_file = 1,
817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700819 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700820 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600823 .needs_file = 1,
824 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700825 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700826 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300827 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700828 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700829 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700830 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300831 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700832 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700833 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600834 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700835 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300836 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700837 .needs_mm = 1,
838 .needs_file = 1,
839 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700840 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700841 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700842 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300843 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700844 .needs_mm = 1,
845 .needs_file = 1,
846 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700847 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700848 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300849 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700850 .needs_file = 1,
851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700853 .needs_mm = 1,
854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_SEND] = {
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 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700860 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300861 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700862 .needs_mm = 1,
863 .needs_file = 1,
864 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700865 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700866 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700867 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300868 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700869 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700870 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700871 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700872 [IORING_OP_EPOLL_CTL] = {
873 .unbound_nonreg_file = 1,
874 .file_table = 1,
875 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300876 [IORING_OP_SPLICE] = {
877 .needs_file = 1,
878 .hash_reg_file = 1,
879 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700880 },
881 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700882 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300883 [IORING_OP_TEE] = {
884 .needs_file = 1,
885 .hash_reg_file = 1,
886 .unbound_nonreg_file = 1,
887 },
Jens Axboed3656342019-12-18 09:50:26 -0700888};
889
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700890enum io_mem_account {
891 ACCT_LOCKED,
892 ACCT_PINNED,
893};
894
Jens Axboe78e19bb2019-11-06 15:21:34 -0700895static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800896static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600897static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700898static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700899static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
900static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700901static int __io_sqe_files_update(struct io_ring_ctx *ctx,
902 struct io_uring_files_update *ip,
903 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700904static int io_grab_files(struct io_kiocb *req);
Jens Axboe2237d762020-06-26 13:44:16 -0600905static void io_complete_rw_common(struct kiocb *kiocb, long res,
906 struct io_comp_state *cs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300907static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700908static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
909 int fd, struct file **out_file, bool fixed);
910static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600911 const struct io_uring_sqe *sqe,
912 struct io_comp_state *cs);
Jens Axboede0617e2019-04-06 21:51:27 -0600913
Jens Axboeb63534c2020-06-04 11:28:00 -0600914static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
915 struct iovec **iovec, struct iov_iter *iter,
916 bool needs_lock);
917static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
918 struct iovec *iovec, struct iovec *fast_iov,
919 struct iov_iter *iter);
920
Jens Axboe2b188cc2019-01-07 10:46:33 -0700921static struct kmem_cache *req_cachep;
922
923static const struct file_operations io_uring_fops;
924
925struct sock *io_uring_get_socket(struct file *file)
926{
927#if defined(CONFIG_UNIX)
928 if (file->f_op == &io_uring_fops) {
929 struct io_ring_ctx *ctx = file->private_data;
930
931 return ctx->ring_sock->sk;
932 }
933#endif
934 return NULL;
935}
936EXPORT_SYMBOL(io_uring_get_socket);
937
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300938static void io_get_req_task(struct io_kiocb *req)
939{
940 if (req->flags & REQ_F_TASK_PINNED)
941 return;
942 get_task_struct(req->task);
943 req->flags |= REQ_F_TASK_PINNED;
944}
945
946/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
947static void __io_put_req_task(struct io_kiocb *req)
948{
949 if (req->flags & REQ_F_TASK_PINNED)
950 put_task_struct(req->task);
951}
952
Jens Axboec40f6372020-06-25 15:39:59 -0600953static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
954{
955 struct mm_struct *mm = current->mm;
956
957 if (mm) {
958 kthread_unuse_mm(mm);
959 mmput(mm);
960 }
961}
962
963static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
964{
965 if (!current->mm) {
966 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
967 return -EFAULT;
968 kthread_use_mm(ctx->sqo_mm);
969 }
970
971 return 0;
972}
973
974static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
975 struct io_kiocb *req)
976{
977 if (!io_op_defs[req->opcode].needs_mm)
978 return 0;
979 return __io_sq_thread_acquire_mm(ctx);
980}
981
982static inline void req_set_fail_links(struct io_kiocb *req)
983{
984 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
985 req->flags |= REQ_F_FAIL_LINK;
986}
987
Jens Axboe4a38aed22020-05-14 17:21:15 -0600988static void io_file_put_work(struct work_struct *work);
989
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800990/*
991 * Note: must call io_req_init_async() for the first time you
992 * touch any members of io_wq_work.
993 */
994static inline void io_req_init_async(struct io_kiocb *req)
995{
996 if (req->flags & REQ_F_WORK_INITIALIZED)
997 return;
998
999 memset(&req->work, 0, sizeof(req->work));
1000 req->flags |= REQ_F_WORK_INITIALIZED;
1001}
1002
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001003static inline bool io_async_submit(struct io_ring_ctx *ctx)
1004{
1005 return ctx->flags & IORING_SETUP_SQPOLL;
1006}
1007
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1009{
1010 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1011
Jens Axboe0f158b42020-05-14 17:18:39 -06001012 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001013}
1014
1015static 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
Jens Axboecccf0ee2020-01-27 16:34:48 -07001104static inline void io_req_work_grab_env(struct io_kiocb *req,
1105 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001106{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001107 if (!req->work.mm && def->needs_mm) {
1108 mmgrab(current->mm);
1109 req->work.mm = current->mm;
1110 }
1111 if (!req->work.creds)
1112 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001113 if (!req->work.fs && def->needs_fs) {
1114 spin_lock(&current->fs->lock);
1115 if (!current->fs->in_exec) {
1116 req->work.fs = current->fs;
1117 req->work.fs->users++;
1118 } else {
1119 req->work.flags |= IO_WQ_WORK_CANCEL;
1120 }
1121 spin_unlock(&current->fs->lock);
1122 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001123}
1124
1125static inline void io_req_work_drop_env(struct io_kiocb *req)
1126{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001127 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1128 return;
1129
Jens Axboecccf0ee2020-01-27 16:34:48 -07001130 if (req->work.mm) {
1131 mmdrop(req->work.mm);
1132 req->work.mm = NULL;
1133 }
1134 if (req->work.creds) {
1135 put_cred(req->work.creds);
1136 req->work.creds = NULL;
1137 }
Jens Axboeff002b32020-02-07 16:05:21 -07001138 if (req->work.fs) {
1139 struct fs_struct *fs = req->work.fs;
1140
1141 spin_lock(&req->work.fs->lock);
1142 if (--fs->users)
1143 fs = NULL;
1144 spin_unlock(&req->work.fs->lock);
1145 if (fs)
1146 free_fs_struct(fs);
1147 }
Jens Axboe561fb042019-10-24 07:25:42 -06001148}
1149
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001150static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001151 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001152{
Jens Axboed3656342019-12-18 09:50:26 -07001153 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001154
Jens Axboed3656342019-12-18 09:50:26 -07001155 if (req->flags & REQ_F_ISREG) {
1156 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001157 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001158 } else {
1159 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001160 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001161 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001162
Pavel Begunkov59960b92020-06-15 16:36:30 +03001163 io_req_init_async(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001164 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001165
Jens Axboe94ae5e72019-11-14 19:39:52 -07001166 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001167}
1168
Jackie Liua197f662019-11-08 08:09:12 -07001169static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001170{
Jackie Liua197f662019-11-08 08:09:12 -07001171 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001172 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001173
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001174 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001175
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001176 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1177 &req->work, req->flags);
1178 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001179
1180 if (link)
1181 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001182}
1183
Jens Axboe5262f562019-09-17 12:26:57 -06001184static void io_kill_timeout(struct io_kiocb *req)
1185{
1186 int ret;
1187
Jens Axboe2d283902019-12-04 11:08:05 -07001188 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001189 if (ret != -1) {
1190 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001191 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001192 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001193 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001194 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001195 }
1196}
1197
1198static void io_kill_timeouts(struct io_ring_ctx *ctx)
1199{
1200 struct io_kiocb *req, *tmp;
1201
1202 spin_lock_irq(&ctx->completion_lock);
1203 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1204 io_kill_timeout(req);
1205 spin_unlock_irq(&ctx->completion_lock);
1206}
1207
Pavel Begunkov04518942020-05-26 20:34:05 +03001208static void __io_queue_deferred(struct io_ring_ctx *ctx)
1209{
1210 do {
1211 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1212 struct io_kiocb, list);
1213
1214 if (req_need_defer(req))
1215 break;
1216 list_del_init(&req->list);
1217 io_queue_async_work(req);
1218 } while (!list_empty(&ctx->defer_list));
1219}
1220
Pavel Begunkov360428f2020-05-30 14:54:17 +03001221static void io_flush_timeouts(struct io_ring_ctx *ctx)
1222{
1223 while (!list_empty(&ctx->timeout_list)) {
1224 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1225 struct io_kiocb, list);
1226
1227 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1228 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001229 if (req->timeout.target_seq != ctx->cached_cq_tail
1230 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001231 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001232
Pavel Begunkov360428f2020-05-30 14:54:17 +03001233 list_del_init(&req->list);
1234 io_kill_timeout(req);
1235 }
1236}
1237
Jens Axboede0617e2019-04-06 21:51:27 -06001238static void io_commit_cqring(struct io_ring_ctx *ctx)
1239{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001240 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001241 __io_commit_cqring(ctx);
1242
Pavel Begunkov04518942020-05-26 20:34:05 +03001243 if (unlikely(!list_empty(&ctx->defer_list)))
1244 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001245}
1246
Jens Axboe2b188cc2019-01-07 10:46:33 -07001247static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1248{
Hristo Venev75b28af2019-08-26 17:23:46 +00001249 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250 unsigned tail;
1251
1252 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001253 /*
1254 * writes to the cq entry need to come after reading head; the
1255 * control dependency is enough as we're using WRITE_ONCE to
1256 * fill the cq entry
1257 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001258 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259 return NULL;
1260
1261 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001262 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001263}
1264
Jens Axboef2842ab2020-01-08 11:04:00 -07001265static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1266{
Jens Axboef0b493e2020-02-01 21:30:11 -07001267 if (!ctx->cq_ev_fd)
1268 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001269 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1270 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001271 if (!ctx->eventfd_async)
1272 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001273 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001274}
1275
Jens Axboeb41e9852020-02-17 09:52:41 -07001276static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001277{
1278 if (waitqueue_active(&ctx->wait))
1279 wake_up(&ctx->wait);
1280 if (waitqueue_active(&ctx->sqo_wait))
1281 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001282 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001283 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001284}
1285
Jens Axboec4a2ed72019-11-21 21:01:26 -07001286/* Returns true if there are no backlogged entries after the flush */
1287static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001289 struct io_rings *rings = ctx->rings;
1290 struct io_uring_cqe *cqe;
1291 struct io_kiocb *req;
1292 unsigned long flags;
1293 LIST_HEAD(list);
1294
1295 if (!force) {
1296 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001297 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001298 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1299 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001300 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001301 }
1302
1303 spin_lock_irqsave(&ctx->completion_lock, flags);
1304
1305 /* if force is set, the ring is going away. always drop after that */
1306 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001307 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001308
Jens Axboec4a2ed72019-11-21 21:01:26 -07001309 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001310 while (!list_empty(&ctx->cq_overflow_list)) {
1311 cqe = io_get_cqring(ctx);
1312 if (!cqe && !force)
1313 break;
1314
1315 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1316 list);
1317 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001318 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001319 if (cqe) {
1320 WRITE_ONCE(cqe->user_data, req->user_data);
1321 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001322 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001323 } else {
1324 WRITE_ONCE(ctx->rings->cq_overflow,
1325 atomic_inc_return(&ctx->cached_cq_overflow));
1326 }
1327 }
1328
1329 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001330 if (cqe) {
1331 clear_bit(0, &ctx->sq_check_overflow);
1332 clear_bit(0, &ctx->cq_check_overflow);
1333 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001334 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1335 io_cqring_ev_posted(ctx);
1336
1337 while (!list_empty(&list)) {
1338 req = list_first_entry(&list, struct io_kiocb, list);
1339 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001340 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001341 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001342
1343 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001344}
1345
Jens Axboebcda7ba2020-02-23 16:42:51 -07001346static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001347{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001348 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349 struct io_uring_cqe *cqe;
1350
Jens Axboe78e19bb2019-11-06 15:21:34 -07001351 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001352
Jens Axboe2b188cc2019-01-07 10:46:33 -07001353 /*
1354 * If we can't get a cq entry, userspace overflowed the
1355 * submission (by quite a lot). Increment the overflow count in
1356 * the ring.
1357 */
1358 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001359 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001360 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001362 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001363 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364 WRITE_ONCE(ctx->rings->cq_overflow,
1365 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001366 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001367 if (list_empty(&ctx->cq_overflow_list)) {
1368 set_bit(0, &ctx->sq_check_overflow);
1369 set_bit(0, &ctx->cq_check_overflow);
1370 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001371 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001372 refcount_inc(&req->refs);
1373 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001374 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001375 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001376 }
1377}
1378
Jens Axboebcda7ba2020-02-23 16:42:51 -07001379static void io_cqring_fill_event(struct io_kiocb *req, long res)
1380{
1381 __io_cqring_fill_event(req, res, 0);
1382}
1383
Jens Axboee1e16092020-06-22 09:17:17 -06001384static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001385{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001386 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001387 unsigned long flags;
1388
1389 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001390 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001391 io_commit_cqring(ctx);
1392 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1393
Jens Axboe8c838782019-03-12 15:48:16 -06001394 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001395}
1396
Jens Axboe229a7b62020-06-22 10:13:11 -06001397static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001398{
Jens Axboe229a7b62020-06-22 10:13:11 -06001399 struct io_ring_ctx *ctx = cs->ctx;
1400
1401 spin_lock_irq(&ctx->completion_lock);
1402 while (!list_empty(&cs->list)) {
1403 struct io_kiocb *req;
1404
1405 req = list_first_entry(&cs->list, struct io_kiocb, list);
1406 list_del(&req->list);
1407 io_cqring_fill_event(req, req->result);
1408 if (!(req->flags & REQ_F_LINK_HEAD)) {
1409 req->flags |= REQ_F_COMP_LOCKED;
1410 io_put_req(req);
1411 } else {
1412 spin_unlock_irq(&ctx->completion_lock);
1413 io_put_req(req);
1414 spin_lock_irq(&ctx->completion_lock);
1415 }
1416 }
1417 io_commit_cqring(ctx);
1418 spin_unlock_irq(&ctx->completion_lock);
1419
1420 io_cqring_ev_posted(ctx);
1421 cs->nr = 0;
1422}
1423
1424static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1425 struct io_comp_state *cs)
1426{
1427 if (!cs) {
1428 io_cqring_add_event(req, res, cflags);
1429 io_put_req(req);
1430 } else {
1431 req->result = res;
1432 list_add_tail(&req->list, &cs->list);
1433 if (++cs->nr >= 32)
1434 io_submit_flush_completions(cs);
1435 }
Jens Axboee1e16092020-06-22 09:17:17 -06001436}
1437
1438static void io_req_complete(struct io_kiocb *req, long res)
1439{
Jens Axboe229a7b62020-06-22 10:13:11 -06001440 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001441}
1442
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001443static inline bool io_is_fallback_req(struct io_kiocb *req)
1444{
1445 return req == (struct io_kiocb *)
1446 ((unsigned long) req->ctx->fallback_req & ~1UL);
1447}
1448
1449static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1450{
1451 struct io_kiocb *req;
1452
1453 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001454 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001455 return req;
1456
1457 return NULL;
1458}
1459
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001460static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1461 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001462{
Jens Axboefd6fab22019-03-14 16:30:06 -06001463 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001464 struct io_kiocb *req;
1465
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001466 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001467 size_t sz;
1468 int ret;
1469
1470 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001471 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1472
1473 /*
1474 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1475 * retry single alloc to be on the safe side.
1476 */
1477 if (unlikely(ret <= 0)) {
1478 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1479 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001480 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001481 ret = 1;
1482 }
Jens Axboe2579f912019-01-09 09:10:43 -07001483 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001484 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001485 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001486 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001487 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488 }
1489
Jens Axboe2579f912019-01-09 09:10:43 -07001490 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001491fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001492 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001493}
1494
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001495static inline void io_put_file(struct io_kiocb *req, struct file *file,
1496 bool fixed)
1497{
1498 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001499 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001500 else
1501 fput(file);
1502}
1503
Pavel Begunkove6543a82020-06-28 12:52:30 +03001504static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001505{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001506 if (req->flags & REQ_F_NEED_CLEANUP)
1507 io_cleanup_req(req);
1508
YueHaibing96fd84d2020-01-07 22:22:44 +08001509 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001510 if (req->file)
1511 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001512 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001513 io_req_work_drop_env(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001514
Jens Axboefcb323c2019-10-24 12:39:47 -06001515 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001516 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001517 unsigned long flags;
1518
1519 spin_lock_irqsave(&ctx->inflight_lock, flags);
1520 list_del(&req->inflight_entry);
1521 if (waitqueue_active(&ctx->inflight_wait))
1522 wake_up(&ctx->inflight_wait);
1523 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1524 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001525}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001526
Pavel Begunkove6543a82020-06-28 12:52:30 +03001527static void __io_free_req(struct io_kiocb *req)
1528{
1529 io_dismantle_req(req);
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001530 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001531 if (likely(!io_is_fallback_req(req)))
1532 kmem_cache_free(req_cachep, req);
1533 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001534 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001535}
1536
Jackie Liua197f662019-11-08 08:09:12 -07001537static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001538{
Jackie Liua197f662019-11-08 08:09:12 -07001539 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001540 int ret;
1541
Jens Axboe2d283902019-12-04 11:08:05 -07001542 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001543 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001544 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001545 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001546 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001547 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001548 return true;
1549 }
1550
1551 return false;
1552}
1553
Jens Axboeba816ad2019-09-28 11:36:45 -06001554static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001555{
Jens Axboe2665abf2019-11-05 12:40:47 -07001556 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001557 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001558
1559 /*
1560 * The list should never be empty when we are called here. But could
1561 * potentially happen if the chain is messed up, check to be on the
1562 * safe side.
1563 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001564 while (!list_empty(&req->link_list)) {
1565 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1566 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001567
Pavel Begunkov44932332019-12-05 16:16:35 +03001568 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1569 (nxt->flags & REQ_F_TIMEOUT))) {
1570 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001571 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001572 req->flags &= ~REQ_F_LINK_TIMEOUT;
1573 continue;
1574 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001575
Pavel Begunkov44932332019-12-05 16:16:35 +03001576 list_del_init(&req->link_list);
1577 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001578 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001579 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001580 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001581 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001582
1583 if (wake_ev)
1584 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001585}
1586
1587/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001588 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001589 */
1590static void io_fail_links(struct io_kiocb *req)
1591{
Jens Axboe2665abf2019-11-05 12:40:47 -07001592 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001593 unsigned long flags;
1594
1595 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001596
1597 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001598 struct io_kiocb *link = list_first_entry(&req->link_list,
1599 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001600
Pavel Begunkov44932332019-12-05 16:16:35 +03001601 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001602 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001603
1604 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001605 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001606 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001607 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001608 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001609 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001610 }
Jens Axboe5d960722019-11-19 15:31:28 -07001611 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001612 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001613
1614 io_commit_cqring(ctx);
1615 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1616 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001617}
1618
Jens Axboe4d7dd462019-11-20 13:03:52 -07001619static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001620{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001621 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001622 return;
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001623 req->flags &= ~REQ_F_LINK_HEAD;
Jens Axboe2665abf2019-11-05 12:40:47 -07001624
Jens Axboe9e645e112019-05-10 16:07:28 -06001625 /*
1626 * If LINK is set, we have dependent requests in this chain. If we
1627 * didn't fail this request, queue the first one up, moving any other
1628 * dependencies to the next request. In case of failure, fail the rest
1629 * of the chain.
1630 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001631 if (req->flags & REQ_F_FAIL_LINK) {
1632 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001633 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1634 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001635 struct io_ring_ctx *ctx = req->ctx;
1636 unsigned long flags;
1637
1638 /*
1639 * If this is a timeout link, we could be racing with the
1640 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001641 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001642 */
1643 spin_lock_irqsave(&ctx->completion_lock, flags);
1644 io_req_link_next(req, nxt);
1645 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1646 } else {
1647 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001648 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001649}
Jens Axboe9e645e112019-05-10 16:07:28 -06001650
Jens Axboec40f6372020-06-25 15:39:59 -06001651static void __io_req_task_cancel(struct io_kiocb *req, int error)
1652{
1653 struct io_ring_ctx *ctx = req->ctx;
1654
1655 spin_lock_irq(&ctx->completion_lock);
1656 io_cqring_fill_event(req, error);
1657 io_commit_cqring(ctx);
1658 spin_unlock_irq(&ctx->completion_lock);
1659
1660 io_cqring_ev_posted(ctx);
1661 req_set_fail_links(req);
1662 io_double_put_req(req);
1663}
1664
1665static void io_req_task_cancel(struct callback_head *cb)
1666{
1667 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1668
1669 __io_req_task_cancel(req, -ECANCELED);
1670}
1671
1672static void __io_req_task_submit(struct io_kiocb *req)
1673{
1674 struct io_ring_ctx *ctx = req->ctx;
1675
1676 __set_current_state(TASK_RUNNING);
1677 if (!__io_sq_thread_acquire_mm(ctx)) {
1678 mutex_lock(&ctx->uring_lock);
1679 __io_queue_sqe(req, NULL, NULL);
1680 mutex_unlock(&ctx->uring_lock);
1681 } else {
1682 __io_req_task_cancel(req, -EFAULT);
1683 }
1684}
1685
1686static void io_req_task_submit(struct callback_head *cb)
1687{
1688 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1689
1690 __io_req_task_submit(req);
1691}
1692
1693static void io_req_task_queue(struct io_kiocb *req)
1694{
1695 struct task_struct *tsk = req->task;
1696 int ret;
1697
1698 init_task_work(&req->task_work, io_req_task_submit);
1699
1700 ret = task_work_add(tsk, &req->task_work, true);
1701 if (unlikely(ret)) {
1702 init_task_work(&req->task_work, io_req_task_cancel);
1703 tsk = io_wq_get_task(req->ctx->io_wq);
1704 task_work_add(tsk, &req->task_work, true);
1705 }
1706 wake_up_process(tsk);
1707}
1708
Pavel Begunkovc3524382020-06-28 12:52:32 +03001709static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001710{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001711 struct io_kiocb *nxt = NULL;
1712
1713 io_req_find_next(req, &nxt);
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001714 if (nxt)
1715 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001716}
1717
Pavel Begunkovc3524382020-06-28 12:52:32 +03001718static void io_free_req(struct io_kiocb *req)
1719{
1720 io_queue_next(req);
1721 __io_free_req(req);
1722}
1723
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001724struct req_batch {
1725 void *reqs[IO_IOPOLL_BATCH];
1726 int to_free;
1727};
1728
1729static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1730 struct req_batch *rb)
1731{
1732 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1733 percpu_ref_put_many(&ctx->refs, rb->to_free);
1734 rb->to_free = 0;
1735}
1736
1737static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1738 struct req_batch *rb)
1739{
1740 if (rb->to_free)
1741 __io_req_free_batch_flush(ctx, rb);
1742}
1743
1744static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1745{
1746 if (unlikely(io_is_fallback_req(req))) {
1747 io_free_req(req);
1748 return;
1749 }
1750 if (req->flags & REQ_F_LINK_HEAD)
1751 io_queue_next(req);
1752
1753 io_dismantle_req(req);
1754 rb->reqs[rb->to_free++] = req;
1755 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1756 __io_req_free_batch_flush(req->ctx, rb);
1757}
1758
Jens Axboeba816ad2019-09-28 11:36:45 -06001759/*
1760 * Drop reference to request, return next in chain (if there is one) if this
1761 * was the last reference to this request.
1762 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001763__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001764static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001765{
Jens Axboe2a44f462020-02-25 13:25:41 -07001766 if (refcount_dec_and_test(&req->refs)) {
1767 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001768 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001769 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001770}
1771
Jens Axboe2b188cc2019-01-07 10:46:33 -07001772static void io_put_req(struct io_kiocb *req)
1773{
Jens Axboedef596e2019-01-09 08:59:42 -07001774 if (refcount_dec_and_test(&req->refs))
1775 io_free_req(req);
1776}
1777
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001778static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001779{
Pavel Begunkov1bcb8c5d2020-06-27 14:04:56 +03001780 struct io_kiocb *nxt = NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001781
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001782 /*
1783 * A ref is owned by io-wq in which context we're. So, if that's the
1784 * last one, it's safe to steal next work. False negatives are Ok,
1785 * it just will be re-punted async in io_put_work()
1786 */
1787 if (refcount_read(&req->refs) != 1)
1788 return NULL;
1789
1790 io_req_find_next(req, &nxt);
1791 if (!nxt)
1792 return NULL;
1793
1794 if ((nxt->flags & REQ_F_ISREG) && io_op_defs[nxt->opcode].hash_reg_file)
1795 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
1796
Pavel Begunkov1bcb8c5d2020-06-27 14:04:56 +03001797 io_req_task_queue(nxt);
1798 /*
1799 * If we're going to return actual work, here should be timeout prep:
1800 *
1801 * link = io_prep_linked_timeout(nxt);
1802 * if (link)
1803 * nxt->flags |= REQ_F_QUEUE_TIMEOUT;
1804 */
1805 return NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001806}
1807
Jens Axboe978db572019-11-14 22:39:04 -07001808/*
1809 * Must only be used if we don't need to care about links, usually from
1810 * within the completion handling itself.
1811 */
1812static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001813{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001814 /* drop both submit and complete references */
1815 if (refcount_sub_and_test(2, &req->refs))
1816 __io_free_req(req);
1817}
1818
Jens Axboe978db572019-11-14 22:39:04 -07001819static void io_double_put_req(struct io_kiocb *req)
1820{
1821 /* drop both submit and complete references */
1822 if (refcount_sub_and_test(2, &req->refs))
1823 io_free_req(req);
1824}
1825
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001826static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001827{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001828 struct io_rings *rings = ctx->rings;
1829
Jens Axboead3eb2c2019-12-18 17:12:20 -07001830 if (test_bit(0, &ctx->cq_check_overflow)) {
1831 /*
1832 * noflush == true is from the waitqueue handler, just ensure
1833 * we wake up the task, and the next invocation will flush the
1834 * entries. We cannot safely to it from here.
1835 */
1836 if (noflush && !list_empty(&ctx->cq_overflow_list))
1837 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001838
Jens Axboead3eb2c2019-12-18 17:12:20 -07001839 io_cqring_overflow_flush(ctx, false);
1840 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001841
Jens Axboea3a0e432019-08-20 11:03:11 -06001842 /* See comment at the top of this file */
1843 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001844 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001845}
1846
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001847static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1848{
1849 struct io_rings *rings = ctx->rings;
1850
1851 /* make sure SQ entry isn't read before tail */
1852 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1853}
1854
Jens Axboebcda7ba2020-02-23 16:42:51 -07001855static int io_put_kbuf(struct io_kiocb *req)
1856{
Jens Axboe4d954c22020-02-27 07:31:19 -07001857 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001858 int cflags;
1859
Jens Axboe4d954c22020-02-27 07:31:19 -07001860 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001861 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1862 cflags |= IORING_CQE_F_BUFFER;
1863 req->rw.addr = 0;
1864 kfree(kbuf);
1865 return cflags;
1866}
1867
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001868static void io_iopoll_queue(struct list_head *again)
1869{
1870 struct io_kiocb *req;
1871
1872 do {
1873 req = list_first_entry(again, struct io_kiocb, list);
1874 list_del(&req->list);
Pavel Begunkovd60b5fb2020-06-25 12:37:11 +03001875
1876 /* shouldn't happen unless io_uring is dying, cancel reqs */
1877 if (unlikely(!current->mm)) {
Jens Axboe2237d762020-06-26 13:44:16 -06001878 io_complete_rw_common(&req->rw.kiocb, -EAGAIN, NULL);
Pavel Begunkovd60b5fb2020-06-25 12:37:11 +03001879 continue;
1880 }
1881
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001882 refcount_inc(&req->refs);
1883 io_queue_async_work(req);
1884 } while (!list_empty(again));
1885}
1886
Jens Axboedef596e2019-01-09 08:59:42 -07001887/*
1888 * Find and free completed poll iocbs
1889 */
1890static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1891 struct list_head *done)
1892{
Jens Axboe8237e042019-12-28 10:48:22 -07001893 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001894 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001895 LIST_HEAD(again);
1896
1897 /* order with ->result store in io_complete_rw_iopoll() */
1898 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001899
Pavel Begunkov2757a232020-06-28 12:52:31 +03001900 rb.to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001901 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001902 int cflags = 0;
1903
Jens Axboedef596e2019-01-09 08:59:42 -07001904 req = list_first_entry(done, struct io_kiocb, list);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001905 if (READ_ONCE(req->result) == -EAGAIN) {
1906 req->iopoll_completed = 0;
1907 list_move_tail(&req->list, &again);
1908 continue;
1909 }
Jens Axboedef596e2019-01-09 08:59:42 -07001910 list_del(&req->list);
1911
Jens Axboebcda7ba2020-02-23 16:42:51 -07001912 if (req->flags & REQ_F_BUFFER_SELECTED)
1913 cflags = io_put_kbuf(req);
1914
1915 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001916 (*nr_events)++;
1917
Pavel Begunkovc3524382020-06-28 12:52:32 +03001918 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001919 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07001920 }
Jens Axboedef596e2019-01-09 08:59:42 -07001921
Jens Axboe09bb8392019-03-13 12:39:28 -06001922 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001923 if (ctx->flags & IORING_SETUP_SQPOLL)
1924 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001925 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001926
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001927 if (!list_empty(&again))
1928 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001929}
1930
Jens Axboedef596e2019-01-09 08:59:42 -07001931static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1932 long min)
1933{
1934 struct io_kiocb *req, *tmp;
1935 LIST_HEAD(done);
1936 bool spin;
1937 int ret;
1938
1939 /*
1940 * Only spin for completions if we don't have multiple devices hanging
1941 * off our complete list, and we're under the requested amount.
1942 */
1943 spin = !ctx->poll_multi_file && *nr_events < min;
1944
1945 ret = 0;
1946 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001947 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001948
1949 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001950 * Move completed and retryable entries to our local lists.
1951 * If we find a request that requires polling, break out
1952 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001953 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08001954 if (READ_ONCE(req->iopoll_completed)) {
Jens Axboedef596e2019-01-09 08:59:42 -07001955 list_move_tail(&req->list, &done);
1956 continue;
1957 }
1958 if (!list_empty(&done))
1959 break;
1960
1961 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1962 if (ret < 0)
1963 break;
1964
1965 if (ret && spin)
1966 spin = false;
1967 ret = 0;
1968 }
1969
1970 if (!list_empty(&done))
1971 io_iopoll_complete(ctx, nr_events, &done);
1972
1973 return ret;
1974}
1975
1976/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001977 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001978 * non-spinning poll check - we'll still enter the driver poll loop, but only
1979 * as a non-spinning completion check.
1980 */
1981static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1982 long min)
1983{
Jens Axboe08f54392019-08-21 22:19:11 -06001984 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001985 int ret;
1986
1987 ret = io_do_iopoll(ctx, nr_events, min);
1988 if (ret < 0)
1989 return ret;
1990 if (!min || *nr_events >= min)
1991 return 0;
1992 }
1993
1994 return 1;
1995}
1996
1997/*
1998 * We can't just wait for polled events to come to us, we have to actively
1999 * find and complete them.
2000 */
2001static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
2002{
2003 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2004 return;
2005
2006 mutex_lock(&ctx->uring_lock);
2007 while (!list_empty(&ctx->poll_list)) {
2008 unsigned int nr_events = 0;
2009
2010 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06002011
2012 /*
2013 * Ensure we allow local-to-the-cpu processing to take place,
2014 * in this case we need to ensure that we reap all events.
2015 */
2016 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07002017 }
2018 mutex_unlock(&ctx->uring_lock);
2019}
2020
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002021static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
2022 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002023{
Jens Axboe2b2ed972019-10-25 10:06:15 -06002024 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002025
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002026 /*
2027 * We disallow the app entering submit/complete with polling, but we
2028 * still need to lock the ring to prevent racing with polled issue
2029 * that got punted to a workqueue.
2030 */
2031 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002032 do {
2033 int tmin = 0;
2034
Jens Axboe500f9fb2019-08-19 12:15:59 -06002035 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002036 * Don't enter poll loop if we already have events pending.
2037 * If we do, we can potentially be spinning for commands that
2038 * already triggered a CQE (eg in error).
2039 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002040 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002041 break;
2042
2043 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002044 * If a submit got punted to a workqueue, we can have the
2045 * application entering polling for a command before it gets
2046 * issued. That app will hold the uring_lock for the duration
2047 * of the poll right here, so we need to take a breather every
2048 * now and then to ensure that the issue has a chance to add
2049 * the poll to the issued list. Otherwise we can spin here
2050 * forever, while the workqueue is stuck trying to acquire the
2051 * very same mutex.
2052 */
2053 if (!(++iters & 7)) {
2054 mutex_unlock(&ctx->uring_lock);
2055 mutex_lock(&ctx->uring_lock);
2056 }
2057
Jens Axboedef596e2019-01-09 08:59:42 -07002058 if (*nr_events < min)
2059 tmin = min - *nr_events;
2060
2061 ret = io_iopoll_getevents(ctx, nr_events, tmin);
2062 if (ret <= 0)
2063 break;
2064 ret = 0;
2065 } while (min && !*nr_events && !need_resched());
2066
Jens Axboe500f9fb2019-08-19 12:15:59 -06002067 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002068 return ret;
2069}
2070
Jens Axboe491381ce2019-10-17 09:20:46 -06002071static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002072{
Jens Axboe491381ce2019-10-17 09:20:46 -06002073 /*
2074 * Tell lockdep we inherited freeze protection from submission
2075 * thread.
2076 */
2077 if (req->flags & REQ_F_ISREG) {
2078 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002079
Jens Axboe491381ce2019-10-17 09:20:46 -06002080 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002081 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002082 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002083}
2084
Jens Axboea1d7c392020-06-22 11:09:46 -06002085static void io_complete_rw_common(struct kiocb *kiocb, long res,
2086 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002087{
Jens Axboe9adbd452019-12-20 08:45:55 -07002088 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002089 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002090
Jens Axboe491381ce2019-10-17 09:20:46 -06002091 if (kiocb->ki_flags & IOCB_WRITE)
2092 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002093
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002094 if (res != req->result)
2095 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002096 if (req->flags & REQ_F_BUFFER_SELECTED)
2097 cflags = io_put_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002098 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002099}
2100
Jens Axboeb63534c2020-06-04 11:28:00 -06002101#ifdef CONFIG_BLOCK
2102static bool io_resubmit_prep(struct io_kiocb *req, int error)
2103{
2104 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2105 ssize_t ret = -ECANCELED;
2106 struct iov_iter iter;
2107 int rw;
2108
2109 if (error) {
2110 ret = error;
2111 goto end_req;
2112 }
2113
2114 switch (req->opcode) {
2115 case IORING_OP_READV:
2116 case IORING_OP_READ_FIXED:
2117 case IORING_OP_READ:
2118 rw = READ;
2119 break;
2120 case IORING_OP_WRITEV:
2121 case IORING_OP_WRITE_FIXED:
2122 case IORING_OP_WRITE:
2123 rw = WRITE;
2124 break;
2125 default:
2126 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2127 req->opcode);
2128 goto end_req;
2129 }
2130
2131 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2132 if (ret < 0)
2133 goto end_req;
2134 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2135 if (!ret)
2136 return true;
2137 kfree(iovec);
2138end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002139 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002140 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002141 return false;
2142}
2143
2144static void io_rw_resubmit(struct callback_head *cb)
2145{
2146 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2147 struct io_ring_ctx *ctx = req->ctx;
2148 int err;
2149
2150 __set_current_state(TASK_RUNNING);
2151
2152 err = io_sq_thread_acquire_mm(ctx, req);
2153
2154 if (io_resubmit_prep(req, err)) {
2155 refcount_inc(&req->refs);
2156 io_queue_async_work(req);
2157 }
2158}
2159#endif
2160
2161static bool io_rw_reissue(struct io_kiocb *req, long res)
2162{
2163#ifdef CONFIG_BLOCK
2164 struct task_struct *tsk;
2165 int ret;
2166
2167 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2168 return false;
2169
2170 tsk = req->task;
2171 init_task_work(&req->task_work, io_rw_resubmit);
2172 ret = task_work_add(tsk, &req->task_work, true);
2173 if (!ret)
2174 return true;
2175#endif
2176 return false;
2177}
2178
Jens Axboea1d7c392020-06-22 11:09:46 -06002179static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2180 struct io_comp_state *cs)
2181{
2182 if (!io_rw_reissue(req, res))
2183 io_complete_rw_common(&req->rw.kiocb, res, cs);
2184}
2185
Jens Axboeba816ad2019-09-28 11:36:45 -06002186static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2187{
Jens Axboe9adbd452019-12-20 08:45:55 -07002188 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002189
Jens Axboea1d7c392020-06-22 11:09:46 -06002190 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002191}
2192
Jens Axboedef596e2019-01-09 08:59:42 -07002193static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2194{
Jens Axboe9adbd452019-12-20 08:45:55 -07002195 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002196
Jens Axboe491381ce2019-10-17 09:20:46 -06002197 if (kiocb->ki_flags & IOCB_WRITE)
2198 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002199
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002200 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002201 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002202
2203 WRITE_ONCE(req->result, res);
2204 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002205 smp_wmb();
2206 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002207}
2208
2209/*
2210 * After the iocb has been issued, it's safe to be found on the poll list.
2211 * Adding the kiocb to the list AFTER submission ensures that we don't
2212 * find it from a io_iopoll_getevents() thread before the issuer is done
2213 * accessing the kiocb cookie.
2214 */
2215static void io_iopoll_req_issued(struct io_kiocb *req)
2216{
2217 struct io_ring_ctx *ctx = req->ctx;
2218
2219 /*
2220 * Track whether we have multiple files in our lists. This will impact
2221 * how we do polling eventually, not spinning if we're on potentially
2222 * different devices.
2223 */
2224 if (list_empty(&ctx->poll_list)) {
2225 ctx->poll_multi_file = false;
2226 } else if (!ctx->poll_multi_file) {
2227 struct io_kiocb *list_req;
2228
2229 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2230 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002231 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002232 ctx->poll_multi_file = true;
2233 }
2234
2235 /*
2236 * For fast devices, IO may have already completed. If it has, add
2237 * it to the front so we find it first.
2238 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002239 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002240 list_add(&req->list, &ctx->poll_list);
2241 else
2242 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002243
2244 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2245 wq_has_sleeper(&ctx->sqo_wait))
2246 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002247}
2248
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002249static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002250{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002251 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002252
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002253 if (diff)
2254 fput_many(state->file, diff);
2255 state->file = NULL;
2256}
2257
2258static inline void io_state_file_put(struct io_submit_state *state)
2259{
2260 if (state->file)
2261 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002262}
2263
2264/*
2265 * Get as many references to a file as we have IOs left in this submission,
2266 * assuming most submissions are for one file, or at least that each file
2267 * has more than one submission.
2268 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002269static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002270{
2271 if (!state)
2272 return fget(fd);
2273
2274 if (state->file) {
2275 if (state->fd == fd) {
2276 state->used_refs++;
2277 state->ios_left--;
2278 return state->file;
2279 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002280 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002281 }
2282 state->file = fget_many(fd, state->ios_left);
2283 if (!state->file)
2284 return NULL;
2285
2286 state->fd = fd;
2287 state->has_refs = state->ios_left;
2288 state->used_refs = 1;
2289 state->ios_left--;
2290 return state->file;
2291}
2292
Jens Axboe4503b762020-06-01 10:00:27 -06002293static bool io_bdev_nowait(struct block_device *bdev)
2294{
2295#ifdef CONFIG_BLOCK
2296 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2297#else
2298 return true;
2299#endif
2300}
2301
Jens Axboe2b188cc2019-01-07 10:46:33 -07002302/*
2303 * If we tracked the file through the SCM inflight mechanism, we could support
2304 * any file. For now, just ensure that anything potentially problematic is done
2305 * inline.
2306 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002307static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002308{
2309 umode_t mode = file_inode(file)->i_mode;
2310
Jens Axboe4503b762020-06-01 10:00:27 -06002311 if (S_ISBLK(mode)) {
2312 if (io_bdev_nowait(file->f_inode->i_bdev))
2313 return true;
2314 return false;
2315 }
2316 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002317 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002318 if (S_ISREG(mode)) {
2319 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2320 file->f_op != &io_uring_fops)
2321 return true;
2322 return false;
2323 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324
Jens Axboec5b85622020-06-09 19:23:05 -06002325 /* any ->read/write should understand O_NONBLOCK */
2326 if (file->f_flags & O_NONBLOCK)
2327 return true;
2328
Jens Axboeaf197f52020-04-28 13:15:06 -06002329 if (!(file->f_mode & FMODE_NOWAIT))
2330 return false;
2331
2332 if (rw == READ)
2333 return file->f_op->read_iter != NULL;
2334
2335 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002336}
2337
Jens Axboe3529d8c2019-12-19 18:24:38 -07002338static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2339 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002340{
Jens Axboedef596e2019-01-09 08:59:42 -07002341 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002342 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002343 unsigned ioprio;
2344 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002345
Jens Axboe491381ce2019-10-17 09:20:46 -06002346 if (S_ISREG(file_inode(req->file)->i_mode))
2347 req->flags |= REQ_F_ISREG;
2348
Jens Axboe2b188cc2019-01-07 10:46:33 -07002349 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002350 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2351 req->flags |= REQ_F_CUR_POS;
2352 kiocb->ki_pos = req->file->f_pos;
2353 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002354 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002355 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2356 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2357 if (unlikely(ret))
2358 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002359
2360 ioprio = READ_ONCE(sqe->ioprio);
2361 if (ioprio) {
2362 ret = ioprio_check_cap(ioprio);
2363 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002364 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002365
2366 kiocb->ki_ioprio = ioprio;
2367 } else
2368 kiocb->ki_ioprio = get_current_ioprio();
2369
Stefan Bühler8449eed2019-04-27 20:34:19 +02002370 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002371 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002372 req->flags |= REQ_F_NOWAIT;
2373
Jens Axboeb63534c2020-06-04 11:28:00 -06002374 if (kiocb->ki_flags & IOCB_DIRECT)
2375 io_get_req_task(req);
2376
Stefan Bühler8449eed2019-04-27 20:34:19 +02002377 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002378 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002379
Jens Axboedef596e2019-01-09 08:59:42 -07002380 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002381 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2382 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002383 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002384
Jens Axboedef596e2019-01-09 08:59:42 -07002385 kiocb->ki_flags |= IOCB_HIPRI;
2386 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002387 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002388 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002389 if (kiocb->ki_flags & IOCB_HIPRI)
2390 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002391 kiocb->ki_complete = io_complete_rw;
2392 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002393
Jens Axboe3529d8c2019-12-19 18:24:38 -07002394 req->rw.addr = READ_ONCE(sqe->addr);
2395 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002396 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002397 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002398}
2399
2400static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2401{
2402 switch (ret) {
2403 case -EIOCBQUEUED:
2404 break;
2405 case -ERESTARTSYS:
2406 case -ERESTARTNOINTR:
2407 case -ERESTARTNOHAND:
2408 case -ERESTART_RESTARTBLOCK:
2409 /*
2410 * We can't just restart the syscall, since previously
2411 * submitted sqes may already be in progress. Just fail this
2412 * IO with EINTR.
2413 */
2414 ret = -EINTR;
2415 /* fall through */
2416 default:
2417 kiocb->ki_complete(kiocb, ret, 0);
2418 }
2419}
2420
Jens Axboea1d7c392020-06-22 11:09:46 -06002421static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2422 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002423{
Jens Axboeba042912019-12-25 16:33:42 -07002424 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2425
2426 if (req->flags & REQ_F_CUR_POS)
2427 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002428 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002429 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002430 else
2431 io_rw_done(kiocb, ret);
2432}
2433
Jens Axboe9adbd452019-12-20 08:45:55 -07002434static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002435 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002436{
Jens Axboe9adbd452019-12-20 08:45:55 -07002437 struct io_ring_ctx *ctx = req->ctx;
2438 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002439 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002440 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002441 size_t offset;
2442 u64 buf_addr;
2443
2444 /* attempt to use fixed buffers without having provided iovecs */
2445 if (unlikely(!ctx->user_bufs))
2446 return -EFAULT;
2447
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002448 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002449 if (unlikely(buf_index >= ctx->nr_user_bufs))
2450 return -EFAULT;
2451
2452 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2453 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002454 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002455
2456 /* overflow */
2457 if (buf_addr + len < buf_addr)
2458 return -EFAULT;
2459 /* not inside the mapped region */
2460 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2461 return -EFAULT;
2462
2463 /*
2464 * May not be a start of buffer, set size appropriately
2465 * and advance us to the beginning.
2466 */
2467 offset = buf_addr - imu->ubuf;
2468 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002469
2470 if (offset) {
2471 /*
2472 * Don't use iov_iter_advance() here, as it's really slow for
2473 * using the latter parts of a big fixed buffer - it iterates
2474 * over each segment manually. We can cheat a bit here, because
2475 * we know that:
2476 *
2477 * 1) it's a BVEC iter, we set it up
2478 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2479 * first and last bvec
2480 *
2481 * So just find our index, and adjust the iterator afterwards.
2482 * If the offset is within the first bvec (or the whole first
2483 * bvec, just use iov_iter_advance(). This makes it easier
2484 * since we can just skip the first segment, which may not
2485 * be PAGE_SIZE aligned.
2486 */
2487 const struct bio_vec *bvec = imu->bvec;
2488
2489 if (offset <= bvec->bv_len) {
2490 iov_iter_advance(iter, offset);
2491 } else {
2492 unsigned long seg_skip;
2493
2494 /* skip first vec */
2495 offset -= bvec->bv_len;
2496 seg_skip = 1 + (offset >> PAGE_SHIFT);
2497
2498 iter->bvec = bvec + seg_skip;
2499 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002500 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002501 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002502 }
2503 }
2504
Jens Axboe5e559562019-11-13 16:12:46 -07002505 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002506}
2507
Jens Axboebcda7ba2020-02-23 16:42:51 -07002508static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2509{
2510 if (needs_lock)
2511 mutex_unlock(&ctx->uring_lock);
2512}
2513
2514static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2515{
2516 /*
2517 * "Normal" inline submissions always hold the uring_lock, since we
2518 * grab it from the system call. Same is true for the SQPOLL offload.
2519 * The only exception is when we've detached the request and issue it
2520 * from an async worker thread, grab the lock for that case.
2521 */
2522 if (needs_lock)
2523 mutex_lock(&ctx->uring_lock);
2524}
2525
2526static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2527 int bgid, struct io_buffer *kbuf,
2528 bool needs_lock)
2529{
2530 struct io_buffer *head;
2531
2532 if (req->flags & REQ_F_BUFFER_SELECTED)
2533 return kbuf;
2534
2535 io_ring_submit_lock(req->ctx, needs_lock);
2536
2537 lockdep_assert_held(&req->ctx->uring_lock);
2538
2539 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2540 if (head) {
2541 if (!list_empty(&head->list)) {
2542 kbuf = list_last_entry(&head->list, struct io_buffer,
2543 list);
2544 list_del(&kbuf->list);
2545 } else {
2546 kbuf = head;
2547 idr_remove(&req->ctx->io_buffer_idr, bgid);
2548 }
2549 if (*len > kbuf->len)
2550 *len = kbuf->len;
2551 } else {
2552 kbuf = ERR_PTR(-ENOBUFS);
2553 }
2554
2555 io_ring_submit_unlock(req->ctx, needs_lock);
2556
2557 return kbuf;
2558}
2559
Jens Axboe4d954c22020-02-27 07:31:19 -07002560static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2561 bool needs_lock)
2562{
2563 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002564 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002565
2566 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002567 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002568 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2569 if (IS_ERR(kbuf))
2570 return kbuf;
2571 req->rw.addr = (u64) (unsigned long) kbuf;
2572 req->flags |= REQ_F_BUFFER_SELECTED;
2573 return u64_to_user_ptr(kbuf->addr);
2574}
2575
2576#ifdef CONFIG_COMPAT
2577static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2578 bool needs_lock)
2579{
2580 struct compat_iovec __user *uiov;
2581 compat_ssize_t clen;
2582 void __user *buf;
2583 ssize_t len;
2584
2585 uiov = u64_to_user_ptr(req->rw.addr);
2586 if (!access_ok(uiov, sizeof(*uiov)))
2587 return -EFAULT;
2588 if (__get_user(clen, &uiov->iov_len))
2589 return -EFAULT;
2590 if (clen < 0)
2591 return -EINVAL;
2592
2593 len = clen;
2594 buf = io_rw_buffer_select(req, &len, needs_lock);
2595 if (IS_ERR(buf))
2596 return PTR_ERR(buf);
2597 iov[0].iov_base = buf;
2598 iov[0].iov_len = (compat_size_t) len;
2599 return 0;
2600}
2601#endif
2602
2603static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2604 bool needs_lock)
2605{
2606 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2607 void __user *buf;
2608 ssize_t len;
2609
2610 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2611 return -EFAULT;
2612
2613 len = iov[0].iov_len;
2614 if (len < 0)
2615 return -EINVAL;
2616 buf = io_rw_buffer_select(req, &len, needs_lock);
2617 if (IS_ERR(buf))
2618 return PTR_ERR(buf);
2619 iov[0].iov_base = buf;
2620 iov[0].iov_len = len;
2621 return 0;
2622}
2623
2624static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2625 bool needs_lock)
2626{
Jens Axboedddb3e22020-06-04 11:27:01 -06002627 if (req->flags & REQ_F_BUFFER_SELECTED) {
2628 struct io_buffer *kbuf;
2629
2630 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2631 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2632 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002633 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002634 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002635 if (!req->rw.len)
2636 return 0;
2637 else if (req->rw.len > 1)
2638 return -EINVAL;
2639
2640#ifdef CONFIG_COMPAT
2641 if (req->ctx->compat)
2642 return io_compat_import(req, iov, needs_lock);
2643#endif
2644
2645 return __io_iov_buffer_select(req, iov, needs_lock);
2646}
2647
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002648static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002649 struct iovec **iovec, struct iov_iter *iter,
2650 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002651{
Jens Axboe9adbd452019-12-20 08:45:55 -07002652 void __user *buf = u64_to_user_ptr(req->rw.addr);
2653 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002654 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002655 u8 opcode;
2656
Jens Axboed625c6e2019-12-17 19:53:05 -07002657 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002658 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002659 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002660 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002661 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002662
Jens Axboebcda7ba2020-02-23 16:42:51 -07002663 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002664 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002665 return -EINVAL;
2666
Jens Axboe3a6820f2019-12-22 15:19:35 -07002667 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002668 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002669 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2670 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002671 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002672 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002673 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002674 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002675 }
2676
Jens Axboe3a6820f2019-12-22 15:19:35 -07002677 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2678 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002679 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002680 }
2681
Jens Axboef67676d2019-12-02 11:03:47 -07002682 if (req->io) {
2683 struct io_async_rw *iorw = &req->io->rw;
2684
2685 *iovec = iorw->iov;
2686 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2687 if (iorw->iov == iorw->fast_iov)
2688 *iovec = NULL;
2689 return iorw->size;
2690 }
2691
Jens Axboe4d954c22020-02-27 07:31:19 -07002692 if (req->flags & REQ_F_BUFFER_SELECT) {
2693 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002694 if (!ret) {
2695 ret = (*iovec)->iov_len;
2696 iov_iter_init(iter, rw, *iovec, 1, ret);
2697 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002698 *iovec = NULL;
2699 return ret;
2700 }
2701
Jens Axboe2b188cc2019-01-07 10:46:33 -07002702#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002703 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002704 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2705 iovec, iter);
2706#endif
2707
2708 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2709}
2710
Jens Axboe32960612019-09-23 11:05:34 -06002711/*
2712 * For files that don't have ->read_iter() and ->write_iter(), handle them
2713 * by looping over ->read() or ->write() manually.
2714 */
2715static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2716 struct iov_iter *iter)
2717{
2718 ssize_t ret = 0;
2719
2720 /*
2721 * Don't support polled IO through this interface, and we can't
2722 * support non-blocking either. For the latter, this just causes
2723 * the kiocb to be handled from an async context.
2724 */
2725 if (kiocb->ki_flags & IOCB_HIPRI)
2726 return -EOPNOTSUPP;
2727 if (kiocb->ki_flags & IOCB_NOWAIT)
2728 return -EAGAIN;
2729
2730 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002731 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002732 ssize_t nr;
2733
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002734 if (!iov_iter_is_bvec(iter)) {
2735 iovec = iov_iter_iovec(iter);
2736 } else {
2737 /* fixed buffers import bvec */
2738 iovec.iov_base = kmap(iter->bvec->bv_page)
2739 + iter->iov_offset;
2740 iovec.iov_len = min(iter->count,
2741 iter->bvec->bv_len - iter->iov_offset);
2742 }
2743
Jens Axboe32960612019-09-23 11:05:34 -06002744 if (rw == READ) {
2745 nr = file->f_op->read(file, iovec.iov_base,
2746 iovec.iov_len, &kiocb->ki_pos);
2747 } else {
2748 nr = file->f_op->write(file, iovec.iov_base,
2749 iovec.iov_len, &kiocb->ki_pos);
2750 }
2751
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002752 if (iov_iter_is_bvec(iter))
2753 kunmap(iter->bvec->bv_page);
2754
Jens Axboe32960612019-09-23 11:05:34 -06002755 if (nr < 0) {
2756 if (!ret)
2757 ret = nr;
2758 break;
2759 }
2760 ret += nr;
2761 if (nr != iovec.iov_len)
2762 break;
2763 iov_iter_advance(iter, nr);
2764 }
2765
2766 return ret;
2767}
2768
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002769static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002770 struct iovec *iovec, struct iovec *fast_iov,
2771 struct iov_iter *iter)
2772{
2773 req->io->rw.nr_segs = iter->nr_segs;
2774 req->io->rw.size = io_size;
2775 req->io->rw.iov = iovec;
2776 if (!req->io->rw.iov) {
2777 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002778 if (req->io->rw.iov != fast_iov)
2779 memcpy(req->io->rw.iov, fast_iov,
2780 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002781 } else {
2782 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002783 }
2784}
2785
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002786static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2787{
2788 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2789 return req->io == NULL;
2790}
2791
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002792static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002793{
Jens Axboed3656342019-12-18 09:50:26 -07002794 if (!io_op_defs[req->opcode].async_ctx)
2795 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002796
2797 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002798}
2799
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002800static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2801 struct iovec *iovec, struct iovec *fast_iov,
2802 struct iov_iter *iter)
2803{
Jens Axboe980ad262020-01-24 23:08:54 -07002804 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002805 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002806 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002807 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002808 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002809
Jens Axboe5d204bc2020-01-31 12:06:52 -07002810 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2811 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002812 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002813}
2814
Jens Axboe3529d8c2019-12-19 18:24:38 -07002815static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2816 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002817{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002818 struct io_async_ctx *io;
2819 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002820 ssize_t ret;
2821
Jens Axboe3529d8c2019-12-19 18:24:38 -07002822 ret = io_prep_rw(req, sqe, force_nonblock);
2823 if (ret)
2824 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002825
Jens Axboe3529d8c2019-12-19 18:24:38 -07002826 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2827 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002828
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002829 /* either don't need iovec imported or already have it */
2830 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002831 return 0;
2832
2833 io = req->io;
2834 io->rw.iov = io->rw.fast_iov;
2835 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002836 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002837 req->io = io;
2838 if (ret < 0)
2839 return ret;
2840
2841 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2842 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002843}
2844
Jens Axboebcf5a062020-05-22 09:24:42 -06002845static void io_async_buf_cancel(struct callback_head *cb)
2846{
2847 struct io_async_rw *rw;
2848 struct io_kiocb *req;
2849
2850 rw = container_of(cb, struct io_async_rw, task_work);
2851 req = rw->wpq.wait.private;
Jens Axboec40f6372020-06-25 15:39:59 -06002852 __io_req_task_cancel(req, -ECANCELED);
Jens Axboebcf5a062020-05-22 09:24:42 -06002853}
2854
2855static void io_async_buf_retry(struct callback_head *cb)
2856{
2857 struct io_async_rw *rw;
Jens Axboebcf5a062020-05-22 09:24:42 -06002858 struct io_kiocb *req;
2859
2860 rw = container_of(cb, struct io_async_rw, task_work);
2861 req = rw->wpq.wait.private;
Jens Axboebcf5a062020-05-22 09:24:42 -06002862
Jens Axboec40f6372020-06-25 15:39:59 -06002863 __io_req_task_submit(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06002864}
2865
2866static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2867 int sync, void *arg)
2868{
2869 struct wait_page_queue *wpq;
2870 struct io_kiocb *req = wait->private;
2871 struct io_async_rw *rw = &req->io->rw;
2872 struct wait_page_key *key = arg;
2873 struct task_struct *tsk;
2874 int ret;
2875
2876 wpq = container_of(wait, struct wait_page_queue, wait);
2877
2878 ret = wake_page_match(wpq, key);
2879 if (ret != 1)
2880 return ret;
2881
2882 list_del_init(&wait->entry);
2883
2884 init_task_work(&rw->task_work, io_async_buf_retry);
2885 /* submit ref gets dropped, acquire a new one */
2886 refcount_inc(&req->refs);
2887 tsk = req->task;
2888 ret = task_work_add(tsk, &rw->task_work, true);
2889 if (unlikely(ret)) {
2890 /* queue just for cancelation */
2891 init_task_work(&rw->task_work, io_async_buf_cancel);
2892 tsk = io_wq_get_task(req->ctx->io_wq);
2893 task_work_add(tsk, &rw->task_work, true);
2894 }
2895 wake_up_process(tsk);
2896 return 1;
2897}
2898
2899static bool io_rw_should_retry(struct io_kiocb *req)
2900{
2901 struct kiocb *kiocb = &req->rw.kiocb;
2902 int ret;
2903
2904 /* never retry for NOWAIT, we just complete with -EAGAIN */
2905 if (req->flags & REQ_F_NOWAIT)
2906 return false;
2907
2908 /* already tried, or we're doing O_DIRECT */
2909 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
2910 return false;
2911 /*
2912 * just use poll if we can, and don't attempt if the fs doesn't
2913 * support callback based unlocks
2914 */
2915 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
2916 return false;
2917
2918 /*
2919 * If request type doesn't require req->io to defer in general,
2920 * we need to allocate it here
2921 */
2922 if (!req->io && __io_alloc_async_ctx(req))
2923 return false;
2924
2925 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
2926 io_async_buf_func, req);
2927 if (!ret) {
2928 io_get_req_task(req);
2929 return true;
2930 }
2931
2932 return false;
2933}
2934
2935static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
2936{
2937 if (req->file->f_op->read_iter)
2938 return call_read_iter(req->file, &req->rw.kiocb, iter);
2939 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
2940}
2941
Jens Axboea1d7c392020-06-22 11:09:46 -06002942static int io_read(struct io_kiocb *req, bool force_nonblock,
2943 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002944{
2945 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002946 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002947 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002948 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002949 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950
Jens Axboebcda7ba2020-02-23 16:42:51 -07002951 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002952 if (ret < 0)
2953 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002954
Jens Axboefd6c2e42019-12-18 12:19:41 -07002955 /* Ensure we clear previously set non-block flag */
2956 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002957 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002958
Jens Axboef67676d2019-12-02 11:03:47 -07002959 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03002960 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07002961
Pavel Begunkov24c74672020-06-21 13:09:51 +03002962 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06002963 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002964 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002965
Jens Axboe31b51512019-01-18 22:56:34 -07002966 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002967 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002968 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06002969 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06002970 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002971
Jens Axboebcf5a062020-05-22 09:24:42 -06002972 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002973
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002974 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06002975 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Jens Axboea1d7c392020-06-22 11:09:46 -06002976 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07002977 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002978 iter.count = iov_count;
2979 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07002980copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002981 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002982 inline_vecs, &iter);
2983 if (ret)
2984 goto out_free;
Jens Axboebcf5a062020-05-22 09:24:42 -06002985 /* if we can retry, do so with the callbacks armed */
2986 if (io_rw_should_retry(req)) {
2987 ret2 = io_iter_do_read(req, &iter);
2988 if (ret2 == -EIOCBQUEUED) {
2989 goto out_free;
2990 } else if (ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06002991 kiocb_done(kiocb, ret2, cs);
Jens Axboebcf5a062020-05-22 09:24:42 -06002992 goto out_free;
2993 }
2994 }
2995 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07002996 return -EAGAIN;
2997 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002998 }
Jens Axboef67676d2019-12-02 11:03:47 -07002999out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003000 if (!(req->flags & REQ_F_NEED_CLEANUP))
3001 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003002 return ret;
3003}
3004
Jens Axboe3529d8c2019-12-19 18:24:38 -07003005static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3006 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003007{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003008 struct io_async_ctx *io;
3009 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07003010 ssize_t ret;
3011
Jens Axboe3529d8c2019-12-19 18:24:38 -07003012 ret = io_prep_rw(req, sqe, force_nonblock);
3013 if (ret)
3014 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003015
Jens Axboe3529d8c2019-12-19 18:24:38 -07003016 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3017 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003018
Jens Axboe4ed734b2020-03-20 11:23:41 -06003019 req->fsize = rlimit(RLIMIT_FSIZE);
3020
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003021 /* either don't need iovec imported or already have it */
3022 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003023 return 0;
3024
3025 io = req->io;
3026 io->rw.iov = io->rw.fast_iov;
3027 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003028 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003029 req->io = io;
3030 if (ret < 0)
3031 return ret;
3032
3033 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
3034 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003035}
3036
Jens Axboea1d7c392020-06-22 11:09:46 -06003037static int io_write(struct io_kiocb *req, bool force_nonblock,
3038 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003039{
3040 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003041 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003042 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003043 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003044 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003045
Jens Axboebcda7ba2020-02-23 16:42:51 -07003046 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003047 if (ret < 0)
3048 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003049
Jens Axboefd6c2e42019-12-18 12:19:41 -07003050 /* Ensure we clear previously set non-block flag */
3051 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003052 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003053
Jens Axboef67676d2019-12-02 11:03:47 -07003054 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03003055 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07003056
Pavel Begunkov24c74672020-06-21 13:09:51 +03003057 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003058 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003059 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003060
Jens Axboe10d59342019-12-09 20:16:22 -07003061 /* file path doesn't support NOWAIT for non-direct_IO */
3062 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3063 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003064 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003065
Jens Axboe31b51512019-01-18 22:56:34 -07003066 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003067 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003068 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003069 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003070 ssize_t ret2;
3071
Jens Axboe2b188cc2019-01-07 10:46:33 -07003072 /*
3073 * Open-code file_start_write here to grab freeze protection,
3074 * which will be released by another thread in
3075 * io_complete_rw(). Fool lockdep by telling it the lock got
3076 * released so that it doesn't complain about the held lock when
3077 * we return to userspace.
3078 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003079 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003080 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003081 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003082 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003083 SB_FREEZE_WRITE);
3084 }
3085 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003086
Jens Axboe4ed734b2020-03-20 11:23:41 -06003087 if (!force_nonblock)
3088 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3089
Jens Axboe9adbd452019-12-20 08:45:55 -07003090 if (req->file->f_op->write_iter)
3091 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003092 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003093 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003094
3095 if (!force_nonblock)
3096 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3097
Jens Axboefaac9962020-02-07 15:45:22 -07003098 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003099 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003100 * retry them without IOCB_NOWAIT.
3101 */
3102 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3103 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003104 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003105 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003106 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003107 iter.count = iov_count;
3108 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003109copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003110 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003111 inline_vecs, &iter);
3112 if (ret)
3113 goto out_free;
3114 return -EAGAIN;
3115 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003116 }
Jens Axboe31b51512019-01-18 22:56:34 -07003117out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003118 if (!(req->flags & REQ_F_NEED_CLEANUP))
3119 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003120 return ret;
3121}
3122
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003123static int __io_splice_prep(struct io_kiocb *req,
3124 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003125{
3126 struct io_splice* sp = &req->splice;
3127 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3128 int ret;
3129
3130 if (req->flags & REQ_F_NEED_CLEANUP)
3131 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003132 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3133 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003134
3135 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003136 sp->len = READ_ONCE(sqe->len);
3137 sp->flags = READ_ONCE(sqe->splice_flags);
3138
3139 if (unlikely(sp->flags & ~valid_flags))
3140 return -EINVAL;
3141
3142 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3143 (sp->flags & SPLICE_F_FD_IN_FIXED));
3144 if (ret)
3145 return ret;
3146 req->flags |= REQ_F_NEED_CLEANUP;
3147
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003148 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3149 /*
3150 * Splice operation will be punted aync, and here need to
3151 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3152 */
3153 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003154 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003155 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003156
3157 return 0;
3158}
3159
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003160static int io_tee_prep(struct io_kiocb *req,
3161 const struct io_uring_sqe *sqe)
3162{
3163 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3164 return -EINVAL;
3165 return __io_splice_prep(req, sqe);
3166}
3167
3168static int io_tee(struct io_kiocb *req, bool force_nonblock)
3169{
3170 struct io_splice *sp = &req->splice;
3171 struct file *in = sp->file_in;
3172 struct file *out = sp->file_out;
3173 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3174 long ret = 0;
3175
3176 if (force_nonblock)
3177 return -EAGAIN;
3178 if (sp->len)
3179 ret = do_tee(in, out, sp->len, flags);
3180
3181 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3182 req->flags &= ~REQ_F_NEED_CLEANUP;
3183
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003184 if (ret != sp->len)
3185 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003186 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003187 return 0;
3188}
3189
3190static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3191{
3192 struct io_splice* sp = &req->splice;
3193
3194 sp->off_in = READ_ONCE(sqe->splice_off_in);
3195 sp->off_out = READ_ONCE(sqe->off);
3196 return __io_splice_prep(req, sqe);
3197}
3198
Pavel Begunkov014db002020-03-03 21:33:12 +03003199static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003200{
3201 struct io_splice *sp = &req->splice;
3202 struct file *in = sp->file_in;
3203 struct file *out = sp->file_out;
3204 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3205 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003206 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003207
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003208 if (force_nonblock)
3209 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003210
3211 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3212 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003213
Jens Axboe948a7742020-05-17 14:21:38 -06003214 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003215 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003216
3217 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3218 req->flags &= ~REQ_F_NEED_CLEANUP;
3219
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003220 if (ret != sp->len)
3221 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003222 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003223 return 0;
3224}
3225
Jens Axboe2b188cc2019-01-07 10:46:33 -07003226/*
3227 * IORING_OP_NOP just posts a completion event, nothing else.
3228 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003229static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003230{
3231 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003232
Jens Axboedef596e2019-01-09 08:59:42 -07003233 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3234 return -EINVAL;
3235
Jens Axboe229a7b62020-06-22 10:13:11 -06003236 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003237 return 0;
3238}
3239
Jens Axboe3529d8c2019-12-19 18:24:38 -07003240static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003241{
Jens Axboe6b063142019-01-10 22:13:58 -07003242 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003243
Jens Axboe09bb8392019-03-13 12:39:28 -06003244 if (!req->file)
3245 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003246
Jens Axboe6b063142019-01-10 22:13:58 -07003247 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003248 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003249 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003250 return -EINVAL;
3251
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003252 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3253 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3254 return -EINVAL;
3255
3256 req->sync.off = READ_ONCE(sqe->off);
3257 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003258 return 0;
3259}
3260
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003261static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003262{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003263 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003264 int ret;
3265
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003266 /* fsync always requires a blocking context */
3267 if (force_nonblock)
3268 return -EAGAIN;
3269
Jens Axboe9adbd452019-12-20 08:45:55 -07003270 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003271 end > 0 ? end : LLONG_MAX,
3272 req->sync.flags & IORING_FSYNC_DATASYNC);
3273 if (ret < 0)
3274 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003275 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003276 return 0;
3277}
3278
Jens Axboed63d1b52019-12-10 10:38:56 -07003279static int io_fallocate_prep(struct io_kiocb *req,
3280 const struct io_uring_sqe *sqe)
3281{
3282 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3283 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003284 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3285 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003286
3287 req->sync.off = READ_ONCE(sqe->off);
3288 req->sync.len = READ_ONCE(sqe->addr);
3289 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003290 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07003291 return 0;
3292}
3293
Pavel Begunkov014db002020-03-03 21:33:12 +03003294static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003295{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003296 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003297
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003298 /* fallocate always requiring blocking context */
3299 if (force_nonblock)
3300 return -EAGAIN;
3301
3302 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3303 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3304 req->sync.len);
3305 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3306 if (ret < 0)
3307 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003308 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003309 return 0;
3310}
3311
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003312static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003313{
Jens Axboef8748882020-01-08 17:47:02 -07003314 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003315 int ret;
3316
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003317 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003318 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003319 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003320 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003321 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003322 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003323
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003324 /* open.how should be already initialised */
3325 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003326 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003327
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003328 req->open.dfd = READ_ONCE(sqe->fd);
3329 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003330 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003331 if (IS_ERR(req->open.filename)) {
3332 ret = PTR_ERR(req->open.filename);
3333 req->open.filename = NULL;
3334 return ret;
3335 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003336 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003337 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003338 return 0;
3339}
3340
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003341static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3342{
3343 u64 flags, mode;
3344
3345 if (req->flags & REQ_F_NEED_CLEANUP)
3346 return 0;
3347 mode = READ_ONCE(sqe->len);
3348 flags = READ_ONCE(sqe->open_flags);
3349 req->open.how = build_open_how(flags, mode);
3350 return __io_openat_prep(req, sqe);
3351}
3352
Jens Axboecebdb982020-01-08 17:59:24 -07003353static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3354{
3355 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003356 size_t len;
3357 int ret;
3358
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003359 if (req->flags & REQ_F_NEED_CLEANUP)
3360 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003361 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3362 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003363 if (len < OPEN_HOW_SIZE_VER0)
3364 return -EINVAL;
3365
3366 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3367 len);
3368 if (ret)
3369 return ret;
3370
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003371 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003372}
3373
Pavel Begunkov014db002020-03-03 21:33:12 +03003374static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003375{
3376 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003377 struct file *file;
3378 int ret;
3379
Jens Axboef86cd202020-01-29 13:46:44 -07003380 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003381 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003382
Jens Axboecebdb982020-01-08 17:59:24 -07003383 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003384 if (ret)
3385 goto err;
3386
Jens Axboe4022e7a2020-03-19 19:23:18 -06003387 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003388 if (ret < 0)
3389 goto err;
3390
3391 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3392 if (IS_ERR(file)) {
3393 put_unused_fd(ret);
3394 ret = PTR_ERR(file);
3395 } else {
3396 fsnotify_open(file);
3397 fd_install(ret, file);
3398 }
3399err:
3400 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003401 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003402 if (ret < 0)
3403 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003404 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003405 return 0;
3406}
3407
Pavel Begunkov014db002020-03-03 21:33:12 +03003408static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003409{
Pavel Begunkov014db002020-03-03 21:33:12 +03003410 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003411}
3412
Jens Axboe067524e2020-03-02 16:32:28 -07003413static int io_remove_buffers_prep(struct io_kiocb *req,
3414 const struct io_uring_sqe *sqe)
3415{
3416 struct io_provide_buf *p = &req->pbuf;
3417 u64 tmp;
3418
3419 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3420 return -EINVAL;
3421
3422 tmp = READ_ONCE(sqe->fd);
3423 if (!tmp || tmp > USHRT_MAX)
3424 return -EINVAL;
3425
3426 memset(p, 0, sizeof(*p));
3427 p->nbufs = tmp;
3428 p->bgid = READ_ONCE(sqe->buf_group);
3429 return 0;
3430}
3431
3432static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3433 int bgid, unsigned nbufs)
3434{
3435 unsigned i = 0;
3436
3437 /* shouldn't happen */
3438 if (!nbufs)
3439 return 0;
3440
3441 /* the head kbuf is the list itself */
3442 while (!list_empty(&buf->list)) {
3443 struct io_buffer *nxt;
3444
3445 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3446 list_del(&nxt->list);
3447 kfree(nxt);
3448 if (++i == nbufs)
3449 return i;
3450 }
3451 i++;
3452 kfree(buf);
3453 idr_remove(&ctx->io_buffer_idr, bgid);
3454
3455 return i;
3456}
3457
Jens Axboe229a7b62020-06-22 10:13:11 -06003458static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3459 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003460{
3461 struct io_provide_buf *p = &req->pbuf;
3462 struct io_ring_ctx *ctx = req->ctx;
3463 struct io_buffer *head;
3464 int ret = 0;
3465
3466 io_ring_submit_lock(ctx, !force_nonblock);
3467
3468 lockdep_assert_held(&ctx->uring_lock);
3469
3470 ret = -ENOENT;
3471 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3472 if (head)
3473 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3474
3475 io_ring_submit_lock(ctx, !force_nonblock);
3476 if (ret < 0)
3477 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003478 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003479 return 0;
3480}
3481
Jens Axboeddf0322d2020-02-23 16:41:33 -07003482static int io_provide_buffers_prep(struct io_kiocb *req,
3483 const struct io_uring_sqe *sqe)
3484{
3485 struct io_provide_buf *p = &req->pbuf;
3486 u64 tmp;
3487
3488 if (sqe->ioprio || sqe->rw_flags)
3489 return -EINVAL;
3490
3491 tmp = READ_ONCE(sqe->fd);
3492 if (!tmp || tmp > USHRT_MAX)
3493 return -E2BIG;
3494 p->nbufs = tmp;
3495 p->addr = READ_ONCE(sqe->addr);
3496 p->len = READ_ONCE(sqe->len);
3497
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003498 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003499 return -EFAULT;
3500
3501 p->bgid = READ_ONCE(sqe->buf_group);
3502 tmp = READ_ONCE(sqe->off);
3503 if (tmp > USHRT_MAX)
3504 return -E2BIG;
3505 p->bid = tmp;
3506 return 0;
3507}
3508
3509static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3510{
3511 struct io_buffer *buf;
3512 u64 addr = pbuf->addr;
3513 int i, bid = pbuf->bid;
3514
3515 for (i = 0; i < pbuf->nbufs; i++) {
3516 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3517 if (!buf)
3518 break;
3519
3520 buf->addr = addr;
3521 buf->len = pbuf->len;
3522 buf->bid = bid;
3523 addr += pbuf->len;
3524 bid++;
3525 if (!*head) {
3526 INIT_LIST_HEAD(&buf->list);
3527 *head = buf;
3528 } else {
3529 list_add_tail(&buf->list, &(*head)->list);
3530 }
3531 }
3532
3533 return i ? i : -ENOMEM;
3534}
3535
Jens Axboe229a7b62020-06-22 10:13:11 -06003536static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3537 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003538{
3539 struct io_provide_buf *p = &req->pbuf;
3540 struct io_ring_ctx *ctx = req->ctx;
3541 struct io_buffer *head, *list;
3542 int ret = 0;
3543
3544 io_ring_submit_lock(ctx, !force_nonblock);
3545
3546 lockdep_assert_held(&ctx->uring_lock);
3547
3548 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3549
3550 ret = io_add_buffers(p, &head);
3551 if (ret < 0)
3552 goto out;
3553
3554 if (!list) {
3555 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3556 GFP_KERNEL);
3557 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003558 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003559 goto out;
3560 }
3561 }
3562out:
3563 io_ring_submit_unlock(ctx, !force_nonblock);
3564 if (ret < 0)
3565 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003566 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003567 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003568}
3569
Jens Axboe3e4827b2020-01-08 15:18:09 -07003570static int io_epoll_ctl_prep(struct io_kiocb *req,
3571 const struct io_uring_sqe *sqe)
3572{
3573#if defined(CONFIG_EPOLL)
3574 if (sqe->ioprio || sqe->buf_index)
3575 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003576 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3577 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003578
3579 req->epoll.epfd = READ_ONCE(sqe->fd);
3580 req->epoll.op = READ_ONCE(sqe->len);
3581 req->epoll.fd = READ_ONCE(sqe->off);
3582
3583 if (ep_op_has_event(req->epoll.op)) {
3584 struct epoll_event __user *ev;
3585
3586 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3587 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3588 return -EFAULT;
3589 }
3590
3591 return 0;
3592#else
3593 return -EOPNOTSUPP;
3594#endif
3595}
3596
Jens Axboe229a7b62020-06-22 10:13:11 -06003597static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3598 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003599{
3600#if defined(CONFIG_EPOLL)
3601 struct io_epoll *ie = &req->epoll;
3602 int ret;
3603
3604 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3605 if (force_nonblock && ret == -EAGAIN)
3606 return -EAGAIN;
3607
3608 if (ret < 0)
3609 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003610 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003611 return 0;
3612#else
3613 return -EOPNOTSUPP;
3614#endif
3615}
3616
Jens Axboec1ca7572019-12-25 22:18:28 -07003617static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3618{
3619#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3620 if (sqe->ioprio || sqe->buf_index || sqe->off)
3621 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003622 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3623 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003624
3625 req->madvise.addr = READ_ONCE(sqe->addr);
3626 req->madvise.len = READ_ONCE(sqe->len);
3627 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3628 return 0;
3629#else
3630 return -EOPNOTSUPP;
3631#endif
3632}
3633
Pavel Begunkov014db002020-03-03 21:33:12 +03003634static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003635{
3636#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3637 struct io_madvise *ma = &req->madvise;
3638 int ret;
3639
3640 if (force_nonblock)
3641 return -EAGAIN;
3642
3643 ret = do_madvise(ma->addr, ma->len, ma->advice);
3644 if (ret < 0)
3645 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003646 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003647 return 0;
3648#else
3649 return -EOPNOTSUPP;
3650#endif
3651}
3652
Jens Axboe4840e412019-12-25 22:03:45 -07003653static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3654{
3655 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3656 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003657 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3658 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003659
3660 req->fadvise.offset = READ_ONCE(sqe->off);
3661 req->fadvise.len = READ_ONCE(sqe->len);
3662 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3663 return 0;
3664}
3665
Pavel Begunkov014db002020-03-03 21:33:12 +03003666static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003667{
3668 struct io_fadvise *fa = &req->fadvise;
3669 int ret;
3670
Jens Axboe3e694262020-02-01 09:22:49 -07003671 if (force_nonblock) {
3672 switch (fa->advice) {
3673 case POSIX_FADV_NORMAL:
3674 case POSIX_FADV_RANDOM:
3675 case POSIX_FADV_SEQUENTIAL:
3676 break;
3677 default:
3678 return -EAGAIN;
3679 }
3680 }
Jens Axboe4840e412019-12-25 22:03:45 -07003681
3682 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3683 if (ret < 0)
3684 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003685 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003686 return 0;
3687}
3688
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003689static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3690{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003691 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3692 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003693 if (sqe->ioprio || sqe->buf_index)
3694 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003695 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003696 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003697
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003698 req->statx.dfd = READ_ONCE(sqe->fd);
3699 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003700 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003701 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3702 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003703
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003704 return 0;
3705}
3706
Pavel Begunkov014db002020-03-03 21:33:12 +03003707static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003708{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003709 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003710 int ret;
3711
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003712 if (force_nonblock) {
3713 /* only need file table for an actual valid fd */
3714 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3715 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003716 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003717 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003718
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003719 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3720 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003721
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003722 if (ret < 0)
3723 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003724 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003725 return 0;
3726}
3727
Jens Axboeb5dba592019-12-11 14:02:38 -07003728static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3729{
3730 /*
3731 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003732 * leave the 'file' in an undeterminate state, and here need to modify
3733 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003734 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003735 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003736 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3737
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003738 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3739 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003740 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3741 sqe->rw_flags || sqe->buf_index)
3742 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003743 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003744 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003745
3746 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003747 if ((req->file && req->file->f_op == &io_uring_fops) ||
3748 req->close.fd == req->ctx->ring_fd)
3749 return -EBADF;
3750
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003751 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003752 return 0;
3753}
3754
Jens Axboe229a7b62020-06-22 10:13:11 -06003755static int io_close(struct io_kiocb *req, bool force_nonblock,
3756 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003757{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003758 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003759 int ret;
3760
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003761 /* might be already done during nonblock submission */
3762 if (!close->put_file) {
3763 ret = __close_fd_get_file(close->fd, &close->put_file);
3764 if (ret < 0)
3765 return (ret == -ENOENT) ? -EBADF : ret;
3766 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003767
3768 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003769 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003770 /* was never set, but play safe */
3771 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003772 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003773 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003774 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003775 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003776
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003777 /* No ->flush() or already async, safely close from here */
3778 ret = filp_close(close->put_file, req->work.files);
3779 if (ret < 0)
3780 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003781 fput(close->put_file);
3782 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003783 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003784 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003785}
3786
Jens Axboe3529d8c2019-12-19 18:24:38 -07003787static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003788{
3789 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003790
3791 if (!req->file)
3792 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003793
3794 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3795 return -EINVAL;
3796 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3797 return -EINVAL;
3798
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003799 req->sync.off = READ_ONCE(sqe->off);
3800 req->sync.len = READ_ONCE(sqe->len);
3801 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003802 return 0;
3803}
3804
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003805static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003806{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003807 int ret;
3808
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003809 /* sync_file_range always requires a blocking context */
3810 if (force_nonblock)
3811 return -EAGAIN;
3812
Jens Axboe9adbd452019-12-20 08:45:55 -07003813 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003814 req->sync.flags);
3815 if (ret < 0)
3816 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003817 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003818 return 0;
3819}
3820
YueHaibing469956e2020-03-04 15:53:52 +08003821#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003822static int io_setup_async_msg(struct io_kiocb *req,
3823 struct io_async_msghdr *kmsg)
3824{
3825 if (req->io)
3826 return -EAGAIN;
3827 if (io_alloc_async_ctx(req)) {
3828 if (kmsg->iov != kmsg->fast_iov)
3829 kfree(kmsg->iov);
3830 return -ENOMEM;
3831 }
3832 req->flags |= REQ_F_NEED_CLEANUP;
3833 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3834 return -EAGAIN;
3835}
3836
Jens Axboe3529d8c2019-12-19 18:24:38 -07003837static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003838{
Jens Axboee47293f2019-12-20 08:58:21 -07003839 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003840 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003841 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003842
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003843 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3844 return -EINVAL;
3845
Jens Axboee47293f2019-12-20 08:58:21 -07003846 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3847 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003848 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003849
Jens Axboed8768362020-02-27 14:17:49 -07003850#ifdef CONFIG_COMPAT
3851 if (req->ctx->compat)
3852 sr->msg_flags |= MSG_CMSG_COMPAT;
3853#endif
3854
Jens Axboefddafac2020-01-04 20:19:44 -07003855 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003856 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003857 /* iovec is already imported */
3858 if (req->flags & REQ_F_NEED_CLEANUP)
3859 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003860
Jens Axboed9688562019-12-09 19:35:20 -07003861 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003862 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003863 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003864 if (!ret)
3865 req->flags |= REQ_F_NEED_CLEANUP;
3866 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003867}
3868
Jens Axboe229a7b62020-06-22 10:13:11 -06003869static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3870 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07003871{
Jens Axboe0b416c32019-12-15 10:57:46 -07003872 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003873 struct socket *sock;
3874 int ret;
3875
Jens Axboe03b12302019-12-02 18:50:25 -07003876 sock = sock_from_file(req->file, &ret);
3877 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003878 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003879 unsigned flags;
3880
Jens Axboe03b12302019-12-02 18:50:25 -07003881 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003882 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003883 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003884 /* if iov is set, it's allocated already */
3885 if (!kmsg->iov)
3886 kmsg->iov = kmsg->fast_iov;
3887 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003888 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003889 struct io_sr_msg *sr = &req->sr_msg;
3890
Jens Axboe0b416c32019-12-15 10:57:46 -07003891 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003892 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003893
3894 io.msg.iov = io.msg.fast_iov;
3895 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3896 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003897 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003898 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003899 }
3900
Jens Axboee47293f2019-12-20 08:58:21 -07003901 flags = req->sr_msg.msg_flags;
3902 if (flags & MSG_DONTWAIT)
3903 req->flags |= REQ_F_NOWAIT;
3904 else if (force_nonblock)
3905 flags |= MSG_DONTWAIT;
3906
Jens Axboe0b416c32019-12-15 10:57:46 -07003907 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003908 if (force_nonblock && ret == -EAGAIN)
3909 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003910 if (ret == -ERESTARTSYS)
3911 ret = -EINTR;
3912 }
3913
Pavel Begunkov1e950812020-02-06 19:51:16 +03003914 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003915 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003916 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003917 if (ret < 0)
3918 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003919 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07003920 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003921}
3922
Jens Axboe229a7b62020-06-22 10:13:11 -06003923static int io_send(struct io_kiocb *req, bool force_nonblock,
3924 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07003925{
Jens Axboefddafac2020-01-04 20:19:44 -07003926 struct socket *sock;
3927 int ret;
3928
Jens Axboefddafac2020-01-04 20:19:44 -07003929 sock = sock_from_file(req->file, &ret);
3930 if (sock) {
3931 struct io_sr_msg *sr = &req->sr_msg;
3932 struct msghdr msg;
3933 struct iovec iov;
3934 unsigned flags;
3935
3936 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3937 &msg.msg_iter);
3938 if (ret)
3939 return ret;
3940
3941 msg.msg_name = NULL;
3942 msg.msg_control = NULL;
3943 msg.msg_controllen = 0;
3944 msg.msg_namelen = 0;
3945
3946 flags = req->sr_msg.msg_flags;
3947 if (flags & MSG_DONTWAIT)
3948 req->flags |= REQ_F_NOWAIT;
3949 else if (force_nonblock)
3950 flags |= MSG_DONTWAIT;
3951
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003952 msg.msg_flags = flags;
3953 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003954 if (force_nonblock && ret == -EAGAIN)
3955 return -EAGAIN;
3956 if (ret == -ERESTARTSYS)
3957 ret = -EINTR;
3958 }
3959
Jens Axboefddafac2020-01-04 20:19:44 -07003960 if (ret < 0)
3961 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003962 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07003963 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003964}
3965
Jens Axboe52de1fe2020-02-27 10:15:42 -07003966static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3967{
3968 struct io_sr_msg *sr = &req->sr_msg;
3969 struct iovec __user *uiov;
3970 size_t iov_len;
3971 int ret;
3972
3973 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3974 &uiov, &iov_len);
3975 if (ret)
3976 return ret;
3977
3978 if (req->flags & REQ_F_BUFFER_SELECT) {
3979 if (iov_len > 1)
3980 return -EINVAL;
3981 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3982 return -EFAULT;
3983 sr->len = io->msg.iov[0].iov_len;
3984 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3985 sr->len);
3986 io->msg.iov = NULL;
3987 } else {
3988 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3989 &io->msg.iov, &io->msg.msg.msg_iter);
3990 if (ret > 0)
3991 ret = 0;
3992 }
3993
3994 return ret;
3995}
3996
3997#ifdef CONFIG_COMPAT
3998static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3999 struct io_async_ctx *io)
4000{
4001 struct compat_msghdr __user *msg_compat;
4002 struct io_sr_msg *sr = &req->sr_msg;
4003 struct compat_iovec __user *uiov;
4004 compat_uptr_t ptr;
4005 compat_size_t len;
4006 int ret;
4007
4008 msg_compat = (struct compat_msghdr __user *) sr->msg;
4009 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
4010 &ptr, &len);
4011 if (ret)
4012 return ret;
4013
4014 uiov = compat_ptr(ptr);
4015 if (req->flags & REQ_F_BUFFER_SELECT) {
4016 compat_ssize_t clen;
4017
4018 if (len > 1)
4019 return -EINVAL;
4020 if (!access_ok(uiov, sizeof(*uiov)))
4021 return -EFAULT;
4022 if (__get_user(clen, &uiov->iov_len))
4023 return -EFAULT;
4024 if (clen < 0)
4025 return -EINVAL;
4026 sr->len = io->msg.iov[0].iov_len;
4027 io->msg.iov = NULL;
4028 } else {
4029 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
4030 &io->msg.iov,
4031 &io->msg.msg.msg_iter);
4032 if (ret < 0)
4033 return ret;
4034 }
4035
4036 return 0;
4037}
Jens Axboe03b12302019-12-02 18:50:25 -07004038#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004039
4040static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
4041{
4042 io->msg.iov = io->msg.fast_iov;
4043
4044#ifdef CONFIG_COMPAT
4045 if (req->ctx->compat)
4046 return __io_compat_recvmsg_copy_hdr(req, io);
4047#endif
4048
4049 return __io_recvmsg_copy_hdr(req, io);
4050}
4051
Jens Axboebcda7ba2020-02-23 16:42:51 -07004052static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
4053 int *cflags, bool needs_lock)
4054{
4055 struct io_sr_msg *sr = &req->sr_msg;
4056 struct io_buffer *kbuf;
4057
4058 if (!(req->flags & REQ_F_BUFFER_SELECT))
4059 return NULL;
4060
4061 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4062 if (IS_ERR(kbuf))
4063 return kbuf;
4064
4065 sr->kbuf = kbuf;
4066 req->flags |= REQ_F_BUFFER_SELECTED;
4067
4068 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
4069 *cflags |= IORING_CQE_F_BUFFER;
4070 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004071}
4072
Jens Axboe3529d8c2019-12-19 18:24:38 -07004073static int io_recvmsg_prep(struct io_kiocb *req,
4074 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004075{
Jens Axboee47293f2019-12-20 08:58:21 -07004076 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004077 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004078 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004079
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004080 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4081 return -EINVAL;
4082
Jens Axboe3529d8c2019-12-19 18:24:38 -07004083 sr->msg_flags = READ_ONCE(sqe->msg_flags);
4084 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004085 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004086 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004087
Jens Axboed8768362020-02-27 14:17:49 -07004088#ifdef CONFIG_COMPAT
4089 if (req->ctx->compat)
4090 sr->msg_flags |= MSG_CMSG_COMPAT;
4091#endif
4092
Jens Axboefddafac2020-01-04 20:19:44 -07004093 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004094 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004095 /* iovec is already imported */
4096 if (req->flags & REQ_F_NEED_CLEANUP)
4097 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004098
Jens Axboe52de1fe2020-02-27 10:15:42 -07004099 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004100 if (!ret)
4101 req->flags |= REQ_F_NEED_CLEANUP;
4102 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004103}
4104
Jens Axboe229a7b62020-06-22 10:13:11 -06004105static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4106 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004107{
Jens Axboe0b416c32019-12-15 10:57:46 -07004108 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004109 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004110 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004111
Jens Axboe0fa03c62019-04-19 13:34:07 -06004112 sock = sock_from_file(req->file, &ret);
4113 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07004114 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004115 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004116 unsigned flags;
4117
Jens Axboe03b12302019-12-02 18:50:25 -07004118 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07004119 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004120 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07004121 /* if iov is set, it's allocated already */
4122 if (!kmsg->iov)
4123 kmsg->iov = kmsg->fast_iov;
4124 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004125 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07004126 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004127 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004128
Jens Axboe52de1fe2020-02-27 10:15:42 -07004129 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07004130 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004131 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004132 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06004133
Jens Axboe52de1fe2020-02-27 10:15:42 -07004134 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4135 if (IS_ERR(kbuf)) {
4136 return PTR_ERR(kbuf);
4137 } else if (kbuf) {
4138 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4139 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4140 1, req->sr_msg.len);
4141 }
4142
Jens Axboee47293f2019-12-20 08:58:21 -07004143 flags = req->sr_msg.msg_flags;
4144 if (flags & MSG_DONTWAIT)
4145 req->flags |= REQ_F_NOWAIT;
4146 else if (force_nonblock)
4147 flags |= MSG_DONTWAIT;
4148
4149 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
4150 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004151 if (force_nonblock && ret == -EAGAIN)
4152 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07004153 if (ret == -ERESTARTSYS)
4154 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004155 }
4156
Pavel Begunkov1e950812020-02-06 19:51:16 +03004157 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004158 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004159 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004160 if (ret < 0)
4161 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004162 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004163 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004164}
4165
Jens Axboe229a7b62020-06-22 10:13:11 -06004166static int io_recv(struct io_kiocb *req, bool force_nonblock,
4167 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004168{
Jens Axboebcda7ba2020-02-23 16:42:51 -07004169 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07004170 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004171 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004172
Jens Axboefddafac2020-01-04 20:19:44 -07004173 sock = sock_from_file(req->file, &ret);
4174 if (sock) {
4175 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004176 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004177 struct msghdr msg;
4178 struct iovec iov;
4179 unsigned flags;
4180
Jens Axboebcda7ba2020-02-23 16:42:51 -07004181 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4182 if (IS_ERR(kbuf))
4183 return PTR_ERR(kbuf);
4184 else if (kbuf)
4185 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004186
Jens Axboebcda7ba2020-02-23 16:42:51 -07004187 ret = import_single_range(READ, buf, sr->len, &iov,
4188 &msg.msg_iter);
4189 if (ret) {
4190 kfree(kbuf);
4191 return ret;
4192 }
4193
4194 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004195 msg.msg_name = NULL;
4196 msg.msg_control = NULL;
4197 msg.msg_controllen = 0;
4198 msg.msg_namelen = 0;
4199 msg.msg_iocb = NULL;
4200 msg.msg_flags = 0;
4201
4202 flags = req->sr_msg.msg_flags;
4203 if (flags & MSG_DONTWAIT)
4204 req->flags |= REQ_F_NOWAIT;
4205 else if (force_nonblock)
4206 flags |= MSG_DONTWAIT;
4207
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004208 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07004209 if (force_nonblock && ret == -EAGAIN)
4210 return -EAGAIN;
4211 if (ret == -ERESTARTSYS)
4212 ret = -EINTR;
4213 }
4214
Jens Axboebcda7ba2020-02-23 16:42:51 -07004215 kfree(kbuf);
4216 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004217 if (ret < 0)
4218 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004219 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004220 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004221}
4222
Jens Axboe3529d8c2019-12-19 18:24:38 -07004223static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004224{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004225 struct io_accept *accept = &req->accept;
4226
Jens Axboe17f2fe32019-10-17 14:42:58 -06004227 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4228 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004229 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004230 return -EINVAL;
4231
Jens Axboed55e5f52019-12-11 16:12:15 -07004232 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4233 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004234 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004235 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004236 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004237}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004238
Jens Axboe229a7b62020-06-22 10:13:11 -06004239static int io_accept(struct io_kiocb *req, bool force_nonblock,
4240 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004241{
4242 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004243 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004244 int ret;
4245
Jiufei Xuee697dee2020-06-10 13:41:59 +08004246 if (req->file->f_flags & O_NONBLOCK)
4247 req->flags |= REQ_F_NOWAIT;
4248
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004249 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004250 accept->addr_len, accept->flags,
4251 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004252 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004253 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004254 if (ret < 0) {
4255 if (ret == -ERESTARTSYS)
4256 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004257 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004258 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004259 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004260 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004261}
4262
Jens Axboe3529d8c2019-12-19 18:24:38 -07004263static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004264{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004265 struct io_connect *conn = &req->connect;
4266 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004267
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004268 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4269 return -EINVAL;
4270 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4271 return -EINVAL;
4272
Jens Axboe3529d8c2019-12-19 18:24:38 -07004273 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4274 conn->addr_len = READ_ONCE(sqe->addr2);
4275
4276 if (!io)
4277 return 0;
4278
4279 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004280 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004281}
4282
Jens Axboe229a7b62020-06-22 10:13:11 -06004283static int io_connect(struct io_kiocb *req, bool force_nonblock,
4284 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004285{
Jens Axboef499a022019-12-02 16:28:46 -07004286 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004287 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004288 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004289
Jens Axboef499a022019-12-02 16:28:46 -07004290 if (req->io) {
4291 io = req->io;
4292 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004293 ret = move_addr_to_kernel(req->connect.addr,
4294 req->connect.addr_len,
4295 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004296 if (ret)
4297 goto out;
4298 io = &__io;
4299 }
4300
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004301 file_flags = force_nonblock ? O_NONBLOCK : 0;
4302
4303 ret = __sys_connect_file(req->file, &io->connect.address,
4304 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004305 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004306 if (req->io)
4307 return -EAGAIN;
4308 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004309 ret = -ENOMEM;
4310 goto out;
4311 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004312 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004313 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004314 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004315 if (ret == -ERESTARTSYS)
4316 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004317out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004318 if (ret < 0)
4319 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004320 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004321 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004322}
YueHaibing469956e2020-03-04 15:53:52 +08004323#else /* !CONFIG_NET */
4324static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4325{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004326 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004327}
4328
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004329static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4330 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004331{
YueHaibing469956e2020-03-04 15:53:52 +08004332 return -EOPNOTSUPP;
4333}
4334
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004335static int io_send(struct io_kiocb *req, bool force_nonblock,
4336 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004337{
4338 return -EOPNOTSUPP;
4339}
4340
4341static int io_recvmsg_prep(struct io_kiocb *req,
4342 const struct io_uring_sqe *sqe)
4343{
4344 return -EOPNOTSUPP;
4345}
4346
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004347static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4348 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004349{
4350 return -EOPNOTSUPP;
4351}
4352
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004353static int io_recv(struct io_kiocb *req, bool force_nonblock,
4354 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004355{
4356 return -EOPNOTSUPP;
4357}
4358
4359static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4360{
4361 return -EOPNOTSUPP;
4362}
4363
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004364static int io_accept(struct io_kiocb *req, bool force_nonblock,
4365 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004366{
4367 return -EOPNOTSUPP;
4368}
4369
4370static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4371{
4372 return -EOPNOTSUPP;
4373}
4374
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004375static int io_connect(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#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004381
Jens Axboed7718a92020-02-14 22:23:12 -07004382struct io_poll_table {
4383 struct poll_table_struct pt;
4384 struct io_kiocb *req;
4385 int error;
4386};
4387
Jens Axboed7718a92020-02-14 22:23:12 -07004388static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4389 __poll_t mask, task_work_func_t func)
4390{
4391 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004392 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004393
4394 /* for instances that support it check for an event match first: */
4395 if (mask && !(mask & poll->events))
4396 return 0;
4397
4398 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4399
4400 list_del_init(&poll->wait.entry);
4401
4402 tsk = req->task;
4403 req->result = mask;
4404 init_task_work(&req->task_work, func);
4405 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004406 * If this fails, then the task is exiting. When a task exits, the
4407 * work gets canceled, so just cancel this request as well instead
4408 * of executing it. We can't safely execute it anyway, as we may not
4409 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004410 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004411 ret = task_work_add(tsk, &req->task_work, true);
4412 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004413 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004414 tsk = io_wq_get_task(req->ctx->io_wq);
4415 task_work_add(tsk, &req->task_work, true);
4416 }
Jens Axboed7718a92020-02-14 22:23:12 -07004417 wake_up_process(tsk);
4418 return 1;
4419}
4420
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004421static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4422 __acquires(&req->ctx->completion_lock)
4423{
4424 struct io_ring_ctx *ctx = req->ctx;
4425
4426 if (!req->result && !READ_ONCE(poll->canceled)) {
4427 struct poll_table_struct pt = { ._key = poll->events };
4428
4429 req->result = vfs_poll(req->file, &pt) & poll->events;
4430 }
4431
4432 spin_lock_irq(&ctx->completion_lock);
4433 if (!req->result && !READ_ONCE(poll->canceled)) {
4434 add_wait_queue(poll->head, &poll->wait);
4435 return true;
4436 }
4437
4438 return false;
4439}
4440
Jens Axboe18bceab2020-05-15 11:56:54 -06004441static void io_poll_remove_double(struct io_kiocb *req)
4442{
4443 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4444
4445 lockdep_assert_held(&req->ctx->completion_lock);
4446
4447 if (poll && poll->head) {
4448 struct wait_queue_head *head = poll->head;
4449
4450 spin_lock(&head->lock);
4451 list_del_init(&poll->wait.entry);
4452 if (poll->wait.private)
4453 refcount_dec(&req->refs);
4454 poll->head = NULL;
4455 spin_unlock(&head->lock);
4456 }
4457}
4458
4459static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4460{
4461 struct io_ring_ctx *ctx = req->ctx;
4462
4463 io_poll_remove_double(req);
4464 req->poll.done = true;
4465 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4466 io_commit_cqring(ctx);
4467}
4468
4469static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4470{
4471 struct io_ring_ctx *ctx = req->ctx;
4472
4473 if (io_poll_rewait(req, &req->poll)) {
4474 spin_unlock_irq(&ctx->completion_lock);
4475 return;
4476 }
4477
4478 hash_del(&req->hash_node);
4479 io_poll_complete(req, req->result, 0);
4480 req->flags |= REQ_F_COMP_LOCKED;
4481 io_put_req_find_next(req, nxt);
4482 spin_unlock_irq(&ctx->completion_lock);
4483
4484 io_cqring_ev_posted(ctx);
4485}
4486
4487static void io_poll_task_func(struct callback_head *cb)
4488{
4489 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4490 struct io_kiocb *nxt = NULL;
4491
4492 io_poll_task_handler(req, &nxt);
4493 if (nxt) {
4494 struct io_ring_ctx *ctx = nxt->ctx;
4495
4496 mutex_lock(&ctx->uring_lock);
Jens Axboef13fad72020-06-22 09:34:30 -06004497 __io_queue_sqe(nxt, NULL, NULL);
Jens Axboe18bceab2020-05-15 11:56:54 -06004498 mutex_unlock(&ctx->uring_lock);
4499 }
4500}
4501
4502static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4503 int sync, void *key)
4504{
4505 struct io_kiocb *req = wait->private;
4506 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4507 __poll_t mask = key_to_poll(key);
4508
4509 /* for instances that support it check for an event match first: */
4510 if (mask && !(mask & poll->events))
4511 return 0;
4512
4513 if (req->poll.head) {
4514 bool done;
4515
4516 spin_lock(&req->poll.head->lock);
4517 done = list_empty(&req->poll.wait.entry);
4518 if (!done)
4519 list_del_init(&req->poll.wait.entry);
4520 spin_unlock(&req->poll.head->lock);
4521 if (!done)
4522 __io_async_wake(req, poll, mask, io_poll_task_func);
4523 }
4524 refcount_dec(&req->refs);
4525 return 1;
4526}
4527
4528static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4529 wait_queue_func_t wake_func)
4530{
4531 poll->head = NULL;
4532 poll->done = false;
4533 poll->canceled = false;
4534 poll->events = events;
4535 INIT_LIST_HEAD(&poll->wait.entry);
4536 init_waitqueue_func_entry(&poll->wait, wake_func);
4537}
4538
4539static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4540 struct wait_queue_head *head)
4541{
4542 struct io_kiocb *req = pt->req;
4543
4544 /*
4545 * If poll->head is already set, it's because the file being polled
4546 * uses multiple waitqueues for poll handling (eg one for read, one
4547 * for write). Setup a separate io_poll_iocb if this happens.
4548 */
4549 if (unlikely(poll->head)) {
4550 /* already have a 2nd entry, fail a third attempt */
4551 if (req->io) {
4552 pt->error = -EINVAL;
4553 return;
4554 }
4555 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4556 if (!poll) {
4557 pt->error = -ENOMEM;
4558 return;
4559 }
4560 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4561 refcount_inc(&req->refs);
4562 poll->wait.private = req;
4563 req->io = (void *) poll;
4564 }
4565
4566 pt->error = 0;
4567 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004568
4569 if (poll->events & EPOLLEXCLUSIVE)
4570 add_wait_queue_exclusive(head, &poll->wait);
4571 else
4572 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004573}
4574
4575static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4576 struct poll_table_struct *p)
4577{
4578 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4579
4580 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4581}
4582
Jens Axboed7718a92020-02-14 22:23:12 -07004583static void io_async_task_func(struct callback_head *cb)
4584{
4585 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4586 struct async_poll *apoll = req->apoll;
4587 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004588 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004589
4590 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4591
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004592 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004593 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004594 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004595 }
4596
Jens Axboe31067252020-05-17 17:43:31 -06004597 /* If req is still hashed, it cannot have been canceled. Don't check. */
4598 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004599 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004600 } else {
4601 canceled = READ_ONCE(apoll->poll.canceled);
4602 if (canceled) {
4603 io_cqring_fill_event(req, -ECANCELED);
4604 io_commit_cqring(ctx);
4605 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004606 }
4607
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004608 spin_unlock_irq(&ctx->completion_lock);
4609
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004610 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004611 if (req->flags & REQ_F_WORK_INITIALIZED)
4612 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004613 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004614
Jens Axboe31067252020-05-17 17:43:31 -06004615 if (!canceled) {
4616 __set_current_state(TASK_RUNNING);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004617 if (io_sq_thread_acquire_mm(ctx, req)) {
Jens Axboee1e16092020-06-22 09:17:17 -06004618 io_cqring_add_event(req, -EFAULT, 0);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004619 goto end_req;
4620 }
Jens Axboe31067252020-05-17 17:43:31 -06004621 mutex_lock(&ctx->uring_lock);
Jens Axboef13fad72020-06-22 09:34:30 -06004622 __io_queue_sqe(req, NULL, NULL);
Jens Axboe31067252020-05-17 17:43:31 -06004623 mutex_unlock(&ctx->uring_lock);
4624 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004625 io_cqring_ev_posted(ctx);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004626end_req:
Jens Axboe2bae0472020-04-13 11:16:34 -06004627 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004628 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004629 }
Jens Axboed7718a92020-02-14 22:23:12 -07004630}
4631
4632static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4633 void *key)
4634{
4635 struct io_kiocb *req = wait->private;
4636 struct io_poll_iocb *poll = &req->apoll->poll;
4637
4638 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4639 key_to_poll(key));
4640
4641 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4642}
4643
4644static void io_poll_req_insert(struct io_kiocb *req)
4645{
4646 struct io_ring_ctx *ctx = req->ctx;
4647 struct hlist_head *list;
4648
4649 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4650 hlist_add_head(&req->hash_node, list);
4651}
4652
4653static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4654 struct io_poll_iocb *poll,
4655 struct io_poll_table *ipt, __poll_t mask,
4656 wait_queue_func_t wake_func)
4657 __acquires(&ctx->completion_lock)
4658{
4659 struct io_ring_ctx *ctx = req->ctx;
4660 bool cancel = false;
4661
Jens Axboe18bceab2020-05-15 11:56:54 -06004662 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004663 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004664 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004665
4666 ipt->pt._key = mask;
4667 ipt->req = req;
4668 ipt->error = -EINVAL;
4669
Jens Axboed7718a92020-02-14 22:23:12 -07004670 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4671
4672 spin_lock_irq(&ctx->completion_lock);
4673 if (likely(poll->head)) {
4674 spin_lock(&poll->head->lock);
4675 if (unlikely(list_empty(&poll->wait.entry))) {
4676 if (ipt->error)
4677 cancel = true;
4678 ipt->error = 0;
4679 mask = 0;
4680 }
4681 if (mask || ipt->error)
4682 list_del_init(&poll->wait.entry);
4683 else if (cancel)
4684 WRITE_ONCE(poll->canceled, true);
4685 else if (!poll->done) /* actually waiting for an event */
4686 io_poll_req_insert(req);
4687 spin_unlock(&poll->head->lock);
4688 }
4689
4690 return mask;
4691}
4692
4693static bool io_arm_poll_handler(struct io_kiocb *req)
4694{
4695 const struct io_op_def *def = &io_op_defs[req->opcode];
4696 struct io_ring_ctx *ctx = req->ctx;
4697 struct async_poll *apoll;
4698 struct io_poll_table ipt;
4699 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004700 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004701
4702 if (!req->file || !file_can_poll(req->file))
4703 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004704 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004705 return false;
4706 if (!def->pollin && !def->pollout)
4707 return false;
4708
4709 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4710 if (unlikely(!apoll))
4711 return false;
4712
4713 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004714 if (req->flags & REQ_F_WORK_INITIALIZED)
4715 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004716 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004717
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004718 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004719 req->apoll = apoll;
4720 INIT_HLIST_NODE(&req->hash_node);
4721
Nathan Chancellor8755d972020-03-02 16:01:19 -07004722 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004723 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004724 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004725 if (def->pollout)
4726 mask |= POLLOUT | POLLWRNORM;
4727 mask |= POLLERR | POLLPRI;
4728
4729 ipt.pt._qproc = io_async_queue_proc;
4730
4731 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4732 io_async_wake);
4733 if (ret) {
4734 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004735 /* only remove double add if we did it here */
4736 if (!had_io)
4737 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004738 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004739 if (req->flags & REQ_F_WORK_INITIALIZED)
4740 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004741 kfree(apoll);
4742 return false;
4743 }
4744 spin_unlock_irq(&ctx->completion_lock);
4745 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4746 apoll->poll.events);
4747 return true;
4748}
4749
4750static bool __io_poll_remove_one(struct io_kiocb *req,
4751 struct io_poll_iocb *poll)
4752{
Jens Axboeb41e9852020-02-17 09:52:41 -07004753 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004754
4755 spin_lock(&poll->head->lock);
4756 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004757 if (!list_empty(&poll->wait.entry)) {
4758 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004759 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004760 }
4761 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004762 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004763 return do_complete;
4764}
4765
4766static bool io_poll_remove_one(struct io_kiocb *req)
4767{
4768 bool do_complete;
4769
4770 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004771 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004772 do_complete = __io_poll_remove_one(req, &req->poll);
4773 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004774 struct async_poll *apoll = req->apoll;
4775
Jens Axboed7718a92020-02-14 22:23:12 -07004776 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004777 do_complete = __io_poll_remove_one(req, &apoll->poll);
4778 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004779 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004780 /*
4781 * restore ->work because we will call
4782 * io_req_work_drop_env below when dropping the
4783 * final reference.
4784 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004785 if (req->flags & REQ_F_WORK_INITIALIZED)
4786 memcpy(&req->work, &apoll->work,
4787 sizeof(req->work));
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004788 kfree(apoll);
4789 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004790 }
4791
Jens Axboeb41e9852020-02-17 09:52:41 -07004792 if (do_complete) {
4793 io_cqring_fill_event(req, -ECANCELED);
4794 io_commit_cqring(req->ctx);
4795 req->flags |= REQ_F_COMP_LOCKED;
4796 io_put_req(req);
4797 }
4798
4799 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004800}
4801
4802static void io_poll_remove_all(struct io_ring_ctx *ctx)
4803{
Jens Axboe78076bb2019-12-04 19:56:40 -07004804 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004805 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004806 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004807
4808 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004809 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4810 struct hlist_head *list;
4811
4812 list = &ctx->cancel_hash[i];
4813 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004814 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004815 }
4816 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004817
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004818 if (posted)
4819 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004820}
4821
Jens Axboe47f46762019-11-09 17:43:02 -07004822static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4823{
Jens Axboe78076bb2019-12-04 19:56:40 -07004824 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004825 struct io_kiocb *req;
4826
Jens Axboe78076bb2019-12-04 19:56:40 -07004827 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4828 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004829 if (sqe_addr != req->user_data)
4830 continue;
4831 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004832 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004833 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004834 }
4835
4836 return -ENOENT;
4837}
4838
Jens Axboe3529d8c2019-12-19 18:24:38 -07004839static int io_poll_remove_prep(struct io_kiocb *req,
4840 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004841{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004842 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4843 return -EINVAL;
4844 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4845 sqe->poll_events)
4846 return -EINVAL;
4847
Jens Axboe0969e782019-12-17 18:40:57 -07004848 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004849 return 0;
4850}
4851
4852/*
4853 * Find a running poll command that matches one specified in sqe->addr,
4854 * and remove it if found.
4855 */
4856static int io_poll_remove(struct io_kiocb *req)
4857{
4858 struct io_ring_ctx *ctx = req->ctx;
4859 u64 addr;
4860 int ret;
4861
Jens Axboe0969e782019-12-17 18:40:57 -07004862 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004863 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004864 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004865 spin_unlock_irq(&ctx->completion_lock);
4866
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004867 if (ret < 0)
4868 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004869 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004870 return 0;
4871}
4872
Jens Axboe221c5eb2019-01-17 09:41:58 -07004873static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4874 void *key)
4875{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004876 struct io_kiocb *req = wait->private;
4877 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004878
Jens Axboed7718a92020-02-14 22:23:12 -07004879 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004880}
4881
Jens Axboe221c5eb2019-01-17 09:41:58 -07004882static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4883 struct poll_table_struct *p)
4884{
4885 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4886
Jens Axboed7718a92020-02-14 22:23:12 -07004887 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004888}
4889
Jens Axboe3529d8c2019-12-19 18:24:38 -07004890static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004891{
4892 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004893 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004894
4895 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4896 return -EINVAL;
4897 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4898 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004899 if (!poll->file)
4900 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004901
Jiufei Xue5769a352020-06-17 17:53:55 +08004902 events = READ_ONCE(sqe->poll32_events);
4903#ifdef __BIG_ENDIAN
4904 events = swahw32(events);
4905#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004906 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4907 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004908
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004909 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004910 return 0;
4911}
4912
Pavel Begunkov014db002020-03-03 21:33:12 +03004913static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004914{
4915 struct io_poll_iocb *poll = &req->poll;
4916 struct io_ring_ctx *ctx = req->ctx;
4917 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004918 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004919
Jens Axboe78076bb2019-12-04 19:56:40 -07004920 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004921 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004922 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004923
Jens Axboed7718a92020-02-14 22:23:12 -07004924 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4925 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004926
Jens Axboe8c838782019-03-12 15:48:16 -06004927 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004928 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004929 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004930 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004931 spin_unlock_irq(&ctx->completion_lock);
4932
Jens Axboe8c838782019-03-12 15:48:16 -06004933 if (mask) {
4934 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004935 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004936 }
Jens Axboe8c838782019-03-12 15:48:16 -06004937 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004938}
4939
Jens Axboe5262f562019-09-17 12:26:57 -06004940static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4941{
Jens Axboead8a48a2019-11-15 08:49:11 -07004942 struct io_timeout_data *data = container_of(timer,
4943 struct io_timeout_data, timer);
4944 struct io_kiocb *req = data->req;
4945 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004946 unsigned long flags;
4947
Jens Axboe5262f562019-09-17 12:26:57 -06004948 atomic_inc(&ctx->cq_timeouts);
4949
4950 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004951 /*
Jens Axboe11365042019-10-16 09:08:32 -06004952 * We could be racing with timeout deletion. If the list is empty,
4953 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004954 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004955 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004956 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004957
Jens Axboe78e19bb2019-11-06 15:21:34 -07004958 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004959 io_commit_cqring(ctx);
4960 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4961
4962 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004963 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004964 io_put_req(req);
4965 return HRTIMER_NORESTART;
4966}
4967
Jens Axboe47f46762019-11-09 17:43:02 -07004968static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4969{
4970 struct io_kiocb *req;
4971 int ret = -ENOENT;
4972
4973 list_for_each_entry(req, &ctx->timeout_list, list) {
4974 if (user_data == req->user_data) {
4975 list_del_init(&req->list);
4976 ret = 0;
4977 break;
4978 }
4979 }
4980
4981 if (ret == -ENOENT)
4982 return ret;
4983
Jens Axboe2d283902019-12-04 11:08:05 -07004984 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004985 if (ret == -1)
4986 return -EALREADY;
4987
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004988 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004989 io_cqring_fill_event(req, -ECANCELED);
4990 io_put_req(req);
4991 return 0;
4992}
4993
Jens Axboe3529d8c2019-12-19 18:24:38 -07004994static int io_timeout_remove_prep(struct io_kiocb *req,
4995 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004996{
Jens Axboeb29472e2019-12-17 18:50:29 -07004997 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4998 return -EINVAL;
4999 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
5000 return -EINVAL;
5001
5002 req->timeout.addr = READ_ONCE(sqe->addr);
5003 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5004 if (req->timeout.flags)
5005 return -EINVAL;
5006
Jens Axboeb29472e2019-12-17 18:50:29 -07005007 return 0;
5008}
5009
Jens Axboe11365042019-10-16 09:08:32 -06005010/*
5011 * Remove or update an existing timeout command
5012 */
Jens Axboefc4df992019-12-10 14:38:45 -07005013static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005014{
5015 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005016 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005017
Jens Axboe11365042019-10-16 09:08:32 -06005018 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005019 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005020
Jens Axboe47f46762019-11-09 17:43:02 -07005021 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005022 io_commit_cqring(ctx);
5023 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005024 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005025 if (ret < 0)
5026 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005027 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005028 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005029}
5030
Jens Axboe3529d8c2019-12-19 18:24:38 -07005031static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005032 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005033{
Jens Axboead8a48a2019-11-15 08:49:11 -07005034 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005035 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005036 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005037
Jens Axboead8a48a2019-11-15 08:49:11 -07005038 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005039 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005040 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005041 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005042 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005043 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005044 flags = READ_ONCE(sqe->timeout_flags);
5045 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005046 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005047
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005048 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005049
Jens Axboe3529d8c2019-12-19 18:24:38 -07005050 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005051 return -ENOMEM;
5052
5053 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005054 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005055 req->flags |= REQ_F_TIMEOUT;
5056
5057 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005058 return -EFAULT;
5059
Jens Axboe11365042019-10-16 09:08:32 -06005060 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005061 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005062 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005063 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005064
Jens Axboead8a48a2019-11-15 08:49:11 -07005065 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5066 return 0;
5067}
5068
Jens Axboefc4df992019-12-10 14:38:45 -07005069static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005070{
Jens Axboead8a48a2019-11-15 08:49:11 -07005071 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005072 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005073 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005074 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005075
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005076 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005077
Jens Axboe5262f562019-09-17 12:26:57 -06005078 /*
5079 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005080 * timeout event to be satisfied. If it isn't set, then this is
5081 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005082 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005083 if (!off) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005084 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07005085 entry = ctx->timeout_list.prev;
5086 goto add;
5087 }
Jens Axboe5262f562019-09-17 12:26:57 -06005088
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005089 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5090 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005091
5092 /*
5093 * Insertion sort, ensuring the first entry in the list is always
5094 * the one we need first.
5095 */
Jens Axboe5262f562019-09-17 12:26:57 -06005096 list_for_each_prev(entry, &ctx->timeout_list) {
5097 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06005098
Jens Axboe93bd25b2019-11-11 23:34:31 -07005099 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
5100 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005101 /* nxt.seq is behind @tail, otherwise would've been completed */
5102 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005103 break;
5104 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005105add:
Jens Axboe5262f562019-09-17 12:26:57 -06005106 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005107 data->timer.function = io_timeout_fn;
5108 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005109 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005110 return 0;
5111}
5112
Jens Axboe62755e32019-10-28 21:49:21 -06005113static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005114{
Jens Axboe62755e32019-10-28 21:49:21 -06005115 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005116
Jens Axboe62755e32019-10-28 21:49:21 -06005117 return req->user_data == (unsigned long) data;
5118}
5119
Jens Axboee977d6d2019-11-05 12:39:45 -07005120static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005121{
Jens Axboe62755e32019-10-28 21:49:21 -06005122 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005123 int ret = 0;
5124
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005125 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005126 switch (cancel_ret) {
5127 case IO_WQ_CANCEL_OK:
5128 ret = 0;
5129 break;
5130 case IO_WQ_CANCEL_RUNNING:
5131 ret = -EALREADY;
5132 break;
5133 case IO_WQ_CANCEL_NOTFOUND:
5134 ret = -ENOENT;
5135 break;
5136 }
5137
Jens Axboee977d6d2019-11-05 12:39:45 -07005138 return ret;
5139}
5140
Jens Axboe47f46762019-11-09 17:43:02 -07005141static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5142 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005143 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005144{
5145 unsigned long flags;
5146 int ret;
5147
5148 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5149 if (ret != -ENOENT) {
5150 spin_lock_irqsave(&ctx->completion_lock, flags);
5151 goto done;
5152 }
5153
5154 spin_lock_irqsave(&ctx->completion_lock, flags);
5155 ret = io_timeout_cancel(ctx, sqe_addr);
5156 if (ret != -ENOENT)
5157 goto done;
5158 ret = io_poll_cancel(ctx, sqe_addr);
5159done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005160 if (!ret)
5161 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005162 io_cqring_fill_event(req, ret);
5163 io_commit_cqring(ctx);
5164 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5165 io_cqring_ev_posted(ctx);
5166
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005167 if (ret < 0)
5168 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005169 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005170}
5171
Jens Axboe3529d8c2019-12-19 18:24:38 -07005172static int io_async_cancel_prep(struct io_kiocb *req,
5173 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005174{
Jens Axboefbf23842019-12-17 18:45:56 -07005175 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005176 return -EINVAL;
5177 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
5178 sqe->cancel_flags)
5179 return -EINVAL;
5180
Jens Axboefbf23842019-12-17 18:45:56 -07005181 req->cancel.addr = READ_ONCE(sqe->addr);
5182 return 0;
5183}
5184
Pavel Begunkov014db002020-03-03 21:33:12 +03005185static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005186{
5187 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005188
Pavel Begunkov014db002020-03-03 21:33:12 +03005189 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005190 return 0;
5191}
5192
Jens Axboe05f3fb32019-12-09 11:22:50 -07005193static int io_files_update_prep(struct io_kiocb *req,
5194 const struct io_uring_sqe *sqe)
5195{
5196 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
5197 return -EINVAL;
5198
5199 req->files_update.offset = READ_ONCE(sqe->off);
5200 req->files_update.nr_args = READ_ONCE(sqe->len);
5201 if (!req->files_update.nr_args)
5202 return -EINVAL;
5203 req->files_update.arg = READ_ONCE(sqe->addr);
5204 return 0;
5205}
5206
Jens Axboe229a7b62020-06-22 10:13:11 -06005207static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5208 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005209{
5210 struct io_ring_ctx *ctx = req->ctx;
5211 struct io_uring_files_update up;
5212 int ret;
5213
Jens Axboef86cd202020-01-29 13:46:44 -07005214 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005215 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005216
5217 up.offset = req->files_update.offset;
5218 up.fds = req->files_update.arg;
5219
5220 mutex_lock(&ctx->uring_lock);
5221 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5222 mutex_unlock(&ctx->uring_lock);
5223
5224 if (ret < 0)
5225 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005226 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005227 return 0;
5228}
5229
Jens Axboe3529d8c2019-12-19 18:24:38 -07005230static int io_req_defer_prep(struct io_kiocb *req,
Jens Axboec40f6372020-06-25 15:39:59 -06005231 const struct io_uring_sqe *sqe, bool for_async)
Jens Axboef67676d2019-12-02 11:03:47 -07005232{
Jens Axboee7815732019-12-17 19:45:06 -07005233 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005234
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005235 if (!sqe)
5236 return 0;
5237
Pavel Begunkov710c2bf2020-06-27 14:04:58 +03005238 if (io_op_defs[req->opcode].file_table) {
5239 io_req_init_async(req);
5240 ret = io_grab_files(req);
5241 if (unlikely(ret))
5242 return ret;
5243 }
5244
Jens Axboec40f6372020-06-25 15:39:59 -06005245 if (for_async || (req->flags & REQ_F_WORK_INITIALIZED)) {
5246 io_req_init_async(req);
Jens Axboec40f6372020-06-25 15:39:59 -06005247 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
Jens Axboef86cd202020-01-29 13:46:44 -07005248 }
5249
Jens Axboed625c6e2019-12-17 19:53:05 -07005250 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005251 case IORING_OP_NOP:
5252 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005253 case IORING_OP_READV:
5254 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005255 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005256 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005257 break;
5258 case IORING_OP_WRITEV:
5259 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005260 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005261 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005262 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005263 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005264 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005265 break;
5266 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005267 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005268 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005269 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005270 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005271 break;
5272 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005273 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005274 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005275 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005276 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005277 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005278 break;
5279 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005280 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005281 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005282 break;
Jens Axboef499a022019-12-02 16:28:46 -07005283 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005284 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005285 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005286 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005287 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005288 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005289 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005290 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005291 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005292 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005293 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005294 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005295 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005296 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005297 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005298 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005299 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005300 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005301 case IORING_OP_FALLOCATE:
5302 ret = io_fallocate_prep(req, sqe);
5303 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005304 case IORING_OP_OPENAT:
5305 ret = io_openat_prep(req, sqe);
5306 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005307 case IORING_OP_CLOSE:
5308 ret = io_close_prep(req, sqe);
5309 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005310 case IORING_OP_FILES_UPDATE:
5311 ret = io_files_update_prep(req, sqe);
5312 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005313 case IORING_OP_STATX:
5314 ret = io_statx_prep(req, sqe);
5315 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005316 case IORING_OP_FADVISE:
5317 ret = io_fadvise_prep(req, sqe);
5318 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005319 case IORING_OP_MADVISE:
5320 ret = io_madvise_prep(req, sqe);
5321 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005322 case IORING_OP_OPENAT2:
5323 ret = io_openat2_prep(req, sqe);
5324 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005325 case IORING_OP_EPOLL_CTL:
5326 ret = io_epoll_ctl_prep(req, sqe);
5327 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005328 case IORING_OP_SPLICE:
5329 ret = io_splice_prep(req, sqe);
5330 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005331 case IORING_OP_PROVIDE_BUFFERS:
5332 ret = io_provide_buffers_prep(req, sqe);
5333 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005334 case IORING_OP_REMOVE_BUFFERS:
5335 ret = io_remove_buffers_prep(req, sqe);
5336 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005337 case IORING_OP_TEE:
5338 ret = io_tee_prep(req, sqe);
5339 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005340 default:
Jens Axboee7815732019-12-17 19:45:06 -07005341 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5342 req->opcode);
5343 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005344 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005345 }
5346
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005347 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005348}
5349
Jens Axboe3529d8c2019-12-19 18:24:38 -07005350static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005351{
Jackie Liua197f662019-11-08 08:09:12 -07005352 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005353 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005354
Bob Liu9d858b22019-11-13 18:06:25 +08005355 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005356 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005357 return 0;
5358
Pavel Begunkov650b5482020-05-17 14:02:11 +03005359 if (!req->io) {
5360 if (io_alloc_async_ctx(req))
5361 return -EAGAIN;
Jens Axboec40f6372020-06-25 15:39:59 -06005362 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkov650b5482020-05-17 14:02:11 +03005363 if (ret < 0)
5364 return ret;
5365 }
Jens Axboe2d283902019-12-04 11:08:05 -07005366
Jens Axboede0617e2019-04-06 21:51:27 -06005367 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005368 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005369 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005370 return 0;
5371 }
5372
Jens Axboe915967f2019-11-21 09:01:20 -07005373 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005374 list_add_tail(&req->list, &ctx->defer_list);
5375 spin_unlock_irq(&ctx->completion_lock);
5376 return -EIOCBQUEUED;
5377}
5378
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005379static void io_cleanup_req(struct io_kiocb *req)
5380{
5381 struct io_async_ctx *io = req->io;
5382
5383 switch (req->opcode) {
5384 case IORING_OP_READV:
5385 case IORING_OP_READ_FIXED:
5386 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005387 if (req->flags & REQ_F_BUFFER_SELECTED)
5388 kfree((void *)(unsigned long)req->rw.addr);
5389 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005390 case IORING_OP_WRITEV:
5391 case IORING_OP_WRITE_FIXED:
5392 case IORING_OP_WRITE:
5393 if (io->rw.iov != io->rw.fast_iov)
5394 kfree(io->rw.iov);
5395 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005396 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005397 if (req->flags & REQ_F_BUFFER_SELECTED)
5398 kfree(req->sr_msg.kbuf);
5399 /* fallthrough */
5400 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005401 if (io->msg.iov != io->msg.fast_iov)
5402 kfree(io->msg.iov);
5403 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005404 case IORING_OP_RECV:
5405 if (req->flags & REQ_F_BUFFER_SELECTED)
5406 kfree(req->sr_msg.kbuf);
5407 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005408 case IORING_OP_OPENAT:
5409 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005410 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005411 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005412 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005413 io_put_file(req, req->splice.file_in,
5414 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5415 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005416 }
5417
5418 req->flags &= ~REQ_F_NEED_CLEANUP;
5419}
5420
Jens Axboe3529d8c2019-12-19 18:24:38 -07005421static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005422 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005423{
Jackie Liua197f662019-11-08 08:09:12 -07005424 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005425 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005426
Jens Axboed625c6e2019-12-17 19:53:05 -07005427 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005428 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005429 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005430 break;
5431 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005432 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005433 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005434 if (sqe) {
5435 ret = io_read_prep(req, sqe, force_nonblock);
5436 if (ret < 0)
5437 break;
5438 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005439 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005440 break;
5441 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005442 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005443 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005444 if (sqe) {
5445 ret = io_write_prep(req, sqe, force_nonblock);
5446 if (ret < 0)
5447 break;
5448 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005449 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005450 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005451 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005452 if (sqe) {
5453 ret = io_prep_fsync(req, sqe);
5454 if (ret < 0)
5455 break;
5456 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005457 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005458 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005459 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005460 if (sqe) {
5461 ret = io_poll_add_prep(req, sqe);
5462 if (ret)
5463 break;
5464 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005465 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005466 break;
5467 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005468 if (sqe) {
5469 ret = io_poll_remove_prep(req, sqe);
5470 if (ret < 0)
5471 break;
5472 }
Jens Axboefc4df992019-12-10 14:38:45 -07005473 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005474 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005475 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005476 if (sqe) {
5477 ret = io_prep_sfr(req, sqe);
5478 if (ret < 0)
5479 break;
5480 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005481 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005482 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005483 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005484 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005485 if (sqe) {
5486 ret = io_sendmsg_prep(req, sqe);
5487 if (ret < 0)
5488 break;
5489 }
Jens Axboefddafac2020-01-04 20:19:44 -07005490 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005491 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005492 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005493 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005494 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005495 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005496 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005497 if (sqe) {
5498 ret = io_recvmsg_prep(req, sqe);
5499 if (ret)
5500 break;
5501 }
Jens Axboefddafac2020-01-04 20:19:44 -07005502 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005503 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005504 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005505 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005506 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005507 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005508 if (sqe) {
5509 ret = io_timeout_prep(req, sqe, false);
5510 if (ret)
5511 break;
5512 }
Jens Axboefc4df992019-12-10 14:38:45 -07005513 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005514 break;
Jens Axboe11365042019-10-16 09:08:32 -06005515 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005516 if (sqe) {
5517 ret = io_timeout_remove_prep(req, sqe);
5518 if (ret)
5519 break;
5520 }
Jens Axboefc4df992019-12-10 14:38:45 -07005521 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005522 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005523 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005524 if (sqe) {
5525 ret = io_accept_prep(req, sqe);
5526 if (ret)
5527 break;
5528 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005529 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005530 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005531 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005532 if (sqe) {
5533 ret = io_connect_prep(req, sqe);
5534 if (ret)
5535 break;
5536 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005537 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005538 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005539 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005540 if (sqe) {
5541 ret = io_async_cancel_prep(req, sqe);
5542 if (ret)
5543 break;
5544 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005545 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005546 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005547 case IORING_OP_FALLOCATE:
5548 if (sqe) {
5549 ret = io_fallocate_prep(req, sqe);
5550 if (ret)
5551 break;
5552 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005553 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005554 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005555 case IORING_OP_OPENAT:
5556 if (sqe) {
5557 ret = io_openat_prep(req, sqe);
5558 if (ret)
5559 break;
5560 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005561 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005562 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005563 case IORING_OP_CLOSE:
5564 if (sqe) {
5565 ret = io_close_prep(req, sqe);
5566 if (ret)
5567 break;
5568 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005569 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005570 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005571 case IORING_OP_FILES_UPDATE:
5572 if (sqe) {
5573 ret = io_files_update_prep(req, sqe);
5574 if (ret)
5575 break;
5576 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005577 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005578 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005579 case IORING_OP_STATX:
5580 if (sqe) {
5581 ret = io_statx_prep(req, sqe);
5582 if (ret)
5583 break;
5584 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005585 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005586 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005587 case IORING_OP_FADVISE:
5588 if (sqe) {
5589 ret = io_fadvise_prep(req, sqe);
5590 if (ret)
5591 break;
5592 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005593 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005594 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005595 case IORING_OP_MADVISE:
5596 if (sqe) {
5597 ret = io_madvise_prep(req, sqe);
5598 if (ret)
5599 break;
5600 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005601 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005602 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005603 case IORING_OP_OPENAT2:
5604 if (sqe) {
5605 ret = io_openat2_prep(req, sqe);
5606 if (ret)
5607 break;
5608 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005609 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005610 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005611 case IORING_OP_EPOLL_CTL:
5612 if (sqe) {
5613 ret = io_epoll_ctl_prep(req, sqe);
5614 if (ret)
5615 break;
5616 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005617 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005618 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005619 case IORING_OP_SPLICE:
5620 if (sqe) {
5621 ret = io_splice_prep(req, sqe);
5622 if (ret < 0)
5623 break;
5624 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005625 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005626 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005627 case IORING_OP_PROVIDE_BUFFERS:
5628 if (sqe) {
5629 ret = io_provide_buffers_prep(req, sqe);
5630 if (ret)
5631 break;
5632 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005633 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005634 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005635 case IORING_OP_REMOVE_BUFFERS:
5636 if (sqe) {
5637 ret = io_remove_buffers_prep(req, sqe);
5638 if (ret)
5639 break;
5640 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005641 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005642 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005643 case IORING_OP_TEE:
5644 if (sqe) {
5645 ret = io_tee_prep(req, sqe);
5646 if (ret < 0)
5647 break;
5648 }
5649 ret = io_tee(req, force_nonblock);
5650 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005651 default:
5652 ret = -EINVAL;
5653 break;
5654 }
5655
5656 if (ret)
5657 return ret;
5658
Jens Axboeb5325762020-05-19 21:20:27 -06005659 /* If the op doesn't have a file, we're not polling for it */
5660 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005661 const bool in_async = io_wq_current_is_worker();
5662
Jens Axboe11ba8202020-01-15 21:51:17 -07005663 /* workqueue context doesn't hold uring_lock, grab it now */
5664 if (in_async)
5665 mutex_lock(&ctx->uring_lock);
5666
Jens Axboe2b188cc2019-01-07 10:46:33 -07005667 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005668
5669 if (in_async)
5670 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005671 }
5672
5673 return 0;
5674}
5675
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005676static void io_arm_async_linked_timeout(struct io_kiocb *req)
5677{
5678 struct io_kiocb *link;
5679
5680 /* link head's timeout is queued in io_queue_async_work() */
5681 if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5682 return;
5683
5684 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5685 io_queue_linked_timeout(link);
5686}
5687
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005688static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Jens Axboedef596e2019-01-09 08:59:42 -07005689{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005690 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005691 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005692
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005693 io_arm_async_linked_timeout(req);
5694
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005695 /* if NO_CANCEL is set, we must still run the work */
5696 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5697 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005698 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005699 }
Jens Axboe31b51512019-01-18 22:56:34 -07005700
Jens Axboe561fb042019-10-24 07:25:42 -06005701 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005702 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005703 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005704 /*
5705 * We can get EAGAIN for polled IO even though we're
5706 * forcing a sync submission from here, since we can't
5707 * wait for request slots on the block side.
5708 */
5709 if (ret != -EAGAIN)
5710 break;
5711 cond_resched();
5712 } while (1);
5713 }
Jens Axboe31b51512019-01-18 22:56:34 -07005714
Jens Axboe561fb042019-10-24 07:25:42 -06005715 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005716 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005717 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005718 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005719
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005720 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005721}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005722
Jens Axboe65e19f52019-10-26 07:20:21 -06005723static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5724 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005725{
Jens Axboe65e19f52019-10-26 07:20:21 -06005726 struct fixed_file_table *table;
5727
Jens Axboe05f3fb32019-12-09 11:22:50 -07005728 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005729 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005730}
5731
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005732static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5733 int fd, struct file **out_file, bool fixed)
5734{
5735 struct io_ring_ctx *ctx = req->ctx;
5736 struct file *file;
5737
5738 if (fixed) {
5739 if (unlikely(!ctx->file_data ||
5740 (unsigned) fd >= ctx->nr_user_files))
5741 return -EBADF;
5742 fd = array_index_nospec(fd, ctx->nr_user_files);
5743 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005744 if (file) {
5745 req->fixed_file_refs = ctx->file_data->cur_refs;
5746 percpu_ref_get(req->fixed_file_refs);
5747 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005748 } else {
5749 trace_io_uring_file_get(ctx, fd);
5750 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005751 }
5752
Jens Axboefd2206e2020-06-02 16:40:47 -06005753 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5754 *out_file = file;
5755 return 0;
5756 }
5757 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005758}
5759
Jens Axboe3529d8c2019-12-19 18:24:38 -07005760static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005761 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005762{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005763 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005764
Jens Axboe63ff8222020-05-07 14:56:15 -06005765 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005766 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005767 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005768
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005769 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005770}
5771
Jackie Liua197f662019-11-08 08:09:12 -07005772static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005773{
Jens Axboefcb323c2019-10-24 12:39:47 -06005774 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005775 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005776
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005777 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005778 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005779 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005780 return -EBADF;
5781
Jens Axboefcb323c2019-10-24 12:39:47 -06005782 rcu_read_lock();
5783 spin_lock_irq(&ctx->inflight_lock);
5784 /*
5785 * We use the f_ops->flush() handler to ensure that we can flush
5786 * out work accessing these files if the fd is closed. Check if
5787 * the fd has changed since we started down this path, and disallow
5788 * this operation if it has.
5789 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005790 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005791 list_add(&req->inflight_entry, &ctx->inflight_list);
5792 req->flags |= REQ_F_INFLIGHT;
5793 req->work.files = current->files;
5794 ret = 0;
5795 }
5796 spin_unlock_irq(&ctx->inflight_lock);
5797 rcu_read_unlock();
5798
5799 return ret;
5800}
5801
Jens Axboe2665abf2019-11-05 12:40:47 -07005802static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5803{
Jens Axboead8a48a2019-11-15 08:49:11 -07005804 struct io_timeout_data *data = container_of(timer,
5805 struct io_timeout_data, timer);
5806 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005807 struct io_ring_ctx *ctx = req->ctx;
5808 struct io_kiocb *prev = NULL;
5809 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005810
5811 spin_lock_irqsave(&ctx->completion_lock, flags);
5812
5813 /*
5814 * We don't expect the list to be empty, that will only happen if we
5815 * race with the completion of the linked work.
5816 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005817 if (!list_empty(&req->link_list)) {
5818 prev = list_entry(req->link_list.prev, struct io_kiocb,
5819 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005820 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005821 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005822 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5823 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005824 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005825 }
5826
5827 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5828
5829 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005830 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005831 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005832 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005833 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005834 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005835 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005836 return HRTIMER_NORESTART;
5837}
5838
Jens Axboead8a48a2019-11-15 08:49:11 -07005839static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005840{
Jens Axboe76a46e02019-11-10 23:34:16 -07005841 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005842
Jens Axboe76a46e02019-11-10 23:34:16 -07005843 /*
5844 * If the list is now empty, then our linked request finished before
5845 * we got a chance to setup the timer
5846 */
5847 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005848 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005849 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005850
Jens Axboead8a48a2019-11-15 08:49:11 -07005851 data->timer.function = io_link_timeout_fn;
5852 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5853 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005854 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005855 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005856
Jens Axboe2665abf2019-11-05 12:40:47 -07005857 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005858 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005859}
5860
Jens Axboead8a48a2019-11-15 08:49:11 -07005861static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005862{
5863 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005864
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005865 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005866 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005867 /* for polled retry, if flag is set, we already went through here */
5868 if (req->flags & REQ_F_POLLED)
5869 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005870
Pavel Begunkov44932332019-12-05 16:16:35 +03005871 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5872 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005873 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005874 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005875
Jens Axboe76a46e02019-11-10 23:34:16 -07005876 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005877 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005878}
5879
Jens Axboef13fad72020-06-22 09:34:30 -06005880static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5881 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005882{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005883 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005884 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005885 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005886 int ret;
5887
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005888again:
5889 linked_timeout = io_prep_linked_timeout(req);
5890
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005891 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5892 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005893 if (old_creds)
5894 revert_creds(old_creds);
5895 if (old_creds == req->work.creds)
5896 old_creds = NULL; /* restored original creds */
5897 else
5898 old_creds = override_creds(req->work.creds);
5899 }
5900
Jens Axboef13fad72020-06-22 09:34:30 -06005901 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06005902
5903 /*
5904 * We async punt it if the file wasn't marked NOWAIT, or if the file
5905 * doesn't support non-blocking read/write attempts
5906 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005907 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005908 if (io_arm_poll_handler(req)) {
5909 if (linked_timeout)
5910 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005911 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005912 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005913punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005914 io_req_init_async(req);
5915
Jens Axboef86cd202020-01-29 13:46:44 -07005916 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005917 ret = io_grab_files(req);
5918 if (ret)
5919 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005920 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005921
5922 /*
5923 * Queued up for async execution, worker will release
5924 * submit reference when the iocb is actually submitted.
5925 */
5926 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005927 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005928 }
Jens Axboee65ef562019-03-12 10:16:44 -06005929
Jens Axboefcb323c2019-10-24 12:39:47 -06005930err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005931 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005932 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005933 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005934
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005935 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005936 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005937 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005938 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005939 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005940 }
5941
Jens Axboee65ef562019-03-12 10:16:44 -06005942 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005943 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005944 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005945 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06005946 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005947 if (nxt) {
5948 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005949
5950 if (req->flags & REQ_F_FORCE_ASYNC)
5951 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005952 goto again;
5953 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005954exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005955 if (old_creds)
5956 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005957}
5958
Jens Axboef13fad72020-06-22 09:34:30 -06005959static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5960 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005961{
5962 int ret;
5963
Jens Axboe3529d8c2019-12-19 18:24:38 -07005964 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005965 if (ret) {
5966 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005967fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005968 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005969 io_put_req(req);
5970 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005971 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005972 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005973 if (!req->io) {
5974 ret = -EAGAIN;
5975 if (io_alloc_async_ctx(req))
5976 goto fail_req;
Jens Axboec40f6372020-06-25 15:39:59 -06005977 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005978 if (unlikely(ret < 0))
5979 goto fail_req;
5980 }
5981
Jens Axboece35a472019-12-17 08:04:44 -07005982 /*
5983 * Never try inline submit of IOSQE_ASYNC is set, go straight
5984 * to async execution.
5985 */
5986 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5987 io_queue_async_work(req);
5988 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06005989 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07005990 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005991}
5992
Jens Axboef13fad72020-06-22 09:34:30 -06005993static inline void io_queue_link_head(struct io_kiocb *req,
5994 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005995{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005996 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06005997 io_put_req(req);
5998 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005999 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006000 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006001}
6002
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006003static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006004 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006005{
Jackie Liua197f662019-11-08 08:09:12 -07006006 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006007 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006008
Jens Axboe9e645e112019-05-10 16:07:28 -06006009 /*
6010 * If we already have a head request, queue this one for async
6011 * submittal once the head completes. If we don't have a head but
6012 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6013 * submitted sync once the chain is complete. If none of those
6014 * conditions are true (normal request), then just queue it.
6015 */
6016 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006017 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006018
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006019 /*
6020 * Taking sequential execution of a link, draining both sides
6021 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6022 * requests in the link. So, it drains the head and the
6023 * next after the link request. The last one is done via
6024 * drain_next flag to persist the effect across calls.
6025 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006026 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006027 head->flags |= REQ_F_IO_DRAIN;
6028 ctx->drain_next = 1;
6029 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006030 if (io_alloc_async_ctx(req))
6031 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06006032
Jens Axboec40f6372020-06-25 15:39:59 -06006033 ret = io_req_defer_prep(req, sqe, false);
Jens Axboe2d283902019-12-04 11:08:05 -07006034 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006035 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006036 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006037 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006038 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006039 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006040 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006041 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006042
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006043 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006044 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006045 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006046 *link = NULL;
6047 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006048 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006049 if (unlikely(ctx->drain_next)) {
6050 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006051 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006052 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006053 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006054 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006055 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006056
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006057 if (io_alloc_async_ctx(req))
6058 return -EAGAIN;
6059
Pavel Begunkova6d45dd2020-06-27 14:04:57 +03006060 ret = io_req_defer_prep(req, sqe, false);
Pavel Begunkov711be032020-01-17 03:57:59 +03006061 if (ret)
6062 req->flags |= REQ_F_FAIL_LINK;
6063 *link = req;
6064 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006065 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006066 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006067 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006068
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006069 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006070}
6071
Jens Axboe9a56a232019-01-09 09:06:50 -07006072/*
6073 * Batched submission is done, ensure local IO is flushed out.
6074 */
6075static void io_submit_state_end(struct io_submit_state *state)
6076{
Jens Axboef13fad72020-06-22 09:34:30 -06006077 if (!list_empty(&state->comp.list))
6078 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006079 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006080 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006081 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006082 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006083}
6084
6085/*
6086 * Start submission side cache.
6087 */
6088static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006089 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006090{
6091 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006092#ifdef CONFIG_BLOCK
6093 state->plug.nowait = true;
6094#endif
Jens Axboe013538b2020-06-22 09:29:15 -06006095 state->comp.nr = 0;
6096 INIT_LIST_HEAD(&state->comp.list);
6097 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006098 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006099 state->file = NULL;
6100 state->ios_left = max_ios;
6101}
6102
Jens Axboe2b188cc2019-01-07 10:46:33 -07006103static void io_commit_sqring(struct io_ring_ctx *ctx)
6104{
Hristo Venev75b28af2019-08-26 17:23:46 +00006105 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006106
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006107 /*
6108 * Ensure any loads from the SQEs are done at this point,
6109 * since once we write the new head, the application could
6110 * write new data to them.
6111 */
6112 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006113}
6114
6115/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006116 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006117 * that is mapped by userspace. This means that care needs to be taken to
6118 * ensure that reads are stable, as we cannot rely on userspace always
6119 * being a good citizen. If members of the sqe are validated and then later
6120 * used, it's important that those reads are done through READ_ONCE() to
6121 * prevent a re-load down the line.
6122 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006123static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006124{
Hristo Venev75b28af2019-08-26 17:23:46 +00006125 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126 unsigned head;
6127
6128 /*
6129 * The cached sq head (or cq tail) serves two purposes:
6130 *
6131 * 1) allows us to batch the cost of updating the user visible
6132 * head updates.
6133 * 2) allows the kernel side to track the head on its own, even
6134 * though the application is the one updating it.
6135 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006136 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006137 if (likely(head < ctx->sq_entries))
6138 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006139
6140 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006141 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006142 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006143 return NULL;
6144}
6145
6146static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6147{
6148 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006149}
6150
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006151#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6152 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6153 IOSQE_BUFFER_SELECT)
6154
6155static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6156 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006157 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006158{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006159 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006160 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006161
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006162 /*
6163 * All io need record the previous position, if LINK vs DARIN,
6164 * it can be used to mark the position of the first IO in the
6165 * link list.
6166 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03006167 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006168 req->opcode = READ_ONCE(sqe->opcode);
6169 req->user_data = READ_ONCE(sqe->user_data);
6170 req->io = NULL;
6171 req->file = NULL;
6172 req->ctx = ctx;
6173 req->flags = 0;
6174 /* one is dropped after submission, the other at completion */
6175 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006176 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006177 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006178
6179 if (unlikely(req->opcode >= IORING_OP_LAST))
6180 return -EINVAL;
6181
Jens Axboe9d8426a2020-06-16 18:42:49 -06006182 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6183 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006184
6185 sqe_flags = READ_ONCE(sqe->flags);
6186 /* enforce forwards compatibility on users */
6187 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6188 return -EINVAL;
6189
6190 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6191 !io_op_defs[req->opcode].buffer_select)
6192 return -EOPNOTSUPP;
6193
6194 id = READ_ONCE(sqe->personality);
6195 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006196 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006197 req->work.creds = idr_find(&ctx->personality_idr, id);
6198 if (unlikely(!req->work.creds))
6199 return -EINVAL;
6200 get_cred(req->work.creds);
6201 }
6202
6203 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006204 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006205
Jens Axboe63ff8222020-05-07 14:56:15 -06006206 if (!io_op_defs[req->opcode].needs_file)
6207 return 0;
6208
6209 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006210}
6211
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006212static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006213 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006214{
Jens Axboeac8691c2020-06-01 08:30:41 -06006215 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006216 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006217 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006218
Jens Axboec4a2ed72019-11-21 21:01:26 -07006219 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006220 if (test_bit(0, &ctx->sq_check_overflow)) {
6221 if (!list_empty(&ctx->cq_overflow_list) &&
6222 !io_cqring_overflow_flush(ctx, false))
6223 return -EBUSY;
6224 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006225
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006226 /* make sure SQ entry isn't read before tail */
6227 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006228
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006229 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6230 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006231
Jens Axboe013538b2020-06-22 09:29:15 -06006232 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006233
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006234 ctx->ring_fd = ring_fd;
6235 ctx->ring_file = ring_file;
6236
Jens Axboe6c271ce2019-01-10 11:22:30 -07006237 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006238 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006239 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006240 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006241
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006242 sqe = io_get_sqe(ctx);
6243 if (unlikely(!sqe)) {
6244 io_consume_sqe(ctx);
6245 break;
6246 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006247 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006248 if (unlikely(!req)) {
6249 if (!submitted)
6250 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006251 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006252 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006253
Jens Axboeac8691c2020-06-01 08:30:41 -06006254 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006255 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006256 /* will complete beyond this point, count as submitted */
6257 submitted++;
6258
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006259 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006260fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006261 io_put_req(req);
6262 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006263 break;
6264 }
6265
Jens Axboe354420f2020-01-08 18:55:15 -07006266 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006267 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006268 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006269 if (err)
6270 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006271 }
6272
Pavel Begunkov9466f432020-01-25 22:34:01 +03006273 if (unlikely(submitted != nr)) {
6274 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6275
6276 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6277 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006278 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006279 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006280 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006281
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006282 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6283 io_commit_sqring(ctx);
6284
Jens Axboe6c271ce2019-01-10 11:22:30 -07006285 return submitted;
6286}
6287
6288static int io_sq_thread(void *data)
6289{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006290 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006291 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006292 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006293 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006294 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006295
Jens Axboe0f158b42020-05-14 17:18:39 -06006296 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006297
Jens Axboe181e4482019-11-25 08:52:30 -07006298 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006299
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006300 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006301 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006302 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006303
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006304 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006305 unsigned nr_events = 0;
6306
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006307 mutex_lock(&ctx->uring_lock);
6308 if (!list_empty(&ctx->poll_list))
6309 io_iopoll_getevents(ctx, &nr_events, 0);
6310 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006311 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006312 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006313 }
6314
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006315 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006316
6317 /*
6318 * If submit got -EBUSY, flag us as needing the application
6319 * to enter the kernel to reap and flush events.
6320 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006321 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006322 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006323 * Drop cur_mm before scheduling, we can't hold it for
6324 * long periods (or over schedule()). Do this before
6325 * adding ourselves to the waitqueue, as the unuse/drop
6326 * may sleep.
6327 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006328 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006329
6330 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006331 * We're polling. If we're within the defined idle
6332 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006333 * to sleep. The exception is if we got EBUSY doing
6334 * more IO, we should wait for the application to
6335 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006336 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006337 if (!list_empty(&ctx->poll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006338 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6339 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006340 if (current->task_works)
6341 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006342 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006343 continue;
6344 }
6345
Jens Axboe6c271ce2019-01-10 11:22:30 -07006346 prepare_to_wait(&ctx->sqo_wait, &wait,
6347 TASK_INTERRUPTIBLE);
6348
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006349 /*
6350 * While doing polled IO, before going to sleep, we need
6351 * to check if there are new reqs added to poll_list, it
6352 * is because reqs may have been punted to io worker and
6353 * will be added to poll_list later, hence check the
6354 * poll_list again.
6355 */
6356 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6357 !list_empty_careful(&ctx->poll_list)) {
6358 finish_wait(&ctx->sqo_wait, &wait);
6359 continue;
6360 }
6361
Jens Axboe6c271ce2019-01-10 11:22:30 -07006362 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006363 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006364 /* make sure to read SQ tail after writing flags */
6365 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006366
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006367 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006368 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006369 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006370 finish_wait(&ctx->sqo_wait, &wait);
6371 break;
6372 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006373 if (current->task_works) {
6374 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006375 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006376 continue;
6377 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006378 if (signal_pending(current))
6379 flush_signals(current);
6380 schedule();
6381 finish_wait(&ctx->sqo_wait, &wait);
6382
Hristo Venev75b28af2019-08-26 17:23:46 +00006383 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006384 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006385 continue;
6386 }
6387 finish_wait(&ctx->sqo_wait, &wait);
6388
Hristo Venev75b28af2019-08-26 17:23:46 +00006389 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006390 }
6391
Jens Axboe8a4955f2019-12-09 14:52:35 -07006392 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006393 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6394 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006395 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006396 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006397 }
6398
Jens Axboeb41e9852020-02-17 09:52:41 -07006399 if (current->task_works)
6400 task_work_run();
6401
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006402 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006403 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006404
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006405 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006406
Jens Axboe6c271ce2019-01-10 11:22:30 -07006407 return 0;
6408}
6409
Jens Axboebda52162019-09-24 13:47:15 -06006410struct io_wait_queue {
6411 struct wait_queue_entry wq;
6412 struct io_ring_ctx *ctx;
6413 unsigned to_wait;
6414 unsigned nr_timeouts;
6415};
6416
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006417static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006418{
6419 struct io_ring_ctx *ctx = iowq->ctx;
6420
6421 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006422 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006423 * started waiting. For timeouts, we always want to return to userspace,
6424 * regardless of event count.
6425 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006426 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006427 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6428}
6429
6430static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6431 int wake_flags, void *key)
6432{
6433 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6434 wq);
6435
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006436 /* use noflush == true, as we can't safely rely on locking context */
6437 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006438 return -1;
6439
6440 return autoremove_wake_function(curr, mode, wake_flags, key);
6441}
6442
Jens Axboe2b188cc2019-01-07 10:46:33 -07006443/*
6444 * Wait until events become available, if we don't already have some. The
6445 * application must reap them itself, as they reside on the shared cq ring.
6446 */
6447static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6448 const sigset_t __user *sig, size_t sigsz)
6449{
Jens Axboebda52162019-09-24 13:47:15 -06006450 struct io_wait_queue iowq = {
6451 .wq = {
6452 .private = current,
6453 .func = io_wake_function,
6454 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6455 },
6456 .ctx = ctx,
6457 .to_wait = min_events,
6458 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006459 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006460 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006461
Jens Axboeb41e9852020-02-17 09:52:41 -07006462 do {
6463 if (io_cqring_events(ctx, false) >= min_events)
6464 return 0;
6465 if (!current->task_works)
6466 break;
6467 task_work_run();
6468 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006469
6470 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006471#ifdef CONFIG_COMPAT
6472 if (in_compat_syscall())
6473 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006474 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006475 else
6476#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006477 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006478
Jens Axboe2b188cc2019-01-07 10:46:33 -07006479 if (ret)
6480 return ret;
6481 }
6482
Jens Axboebda52162019-09-24 13:47:15 -06006483 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006484 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006485 do {
6486 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6487 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006488 if (current->task_works)
6489 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006490 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006491 break;
6492 schedule();
6493 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006494 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006495 break;
6496 }
6497 } while (1);
6498 finish_wait(&ctx->wait, &iowq.wq);
6499
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006500 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006501
Hristo Venev75b28af2019-08-26 17:23:46 +00006502 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006503}
6504
Jens Axboe6b063142019-01-10 22:13:58 -07006505static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6506{
6507#if defined(CONFIG_UNIX)
6508 if (ctx->ring_sock) {
6509 struct sock *sock = ctx->ring_sock->sk;
6510 struct sk_buff *skb;
6511
6512 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6513 kfree_skb(skb);
6514 }
6515#else
6516 int i;
6517
Jens Axboe65e19f52019-10-26 07:20:21 -06006518 for (i = 0; i < ctx->nr_user_files; i++) {
6519 struct file *file;
6520
6521 file = io_file_from_index(ctx, i);
6522 if (file)
6523 fput(file);
6524 }
Jens Axboe6b063142019-01-10 22:13:58 -07006525#endif
6526}
6527
Jens Axboe05f3fb32019-12-09 11:22:50 -07006528static void io_file_ref_kill(struct percpu_ref *ref)
6529{
6530 struct fixed_file_data *data;
6531
6532 data = container_of(ref, struct fixed_file_data, refs);
6533 complete(&data->done);
6534}
6535
Jens Axboe6b063142019-01-10 22:13:58 -07006536static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6537{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006538 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006539 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006540 unsigned nr_tables, i;
6541
Jens Axboe05f3fb32019-12-09 11:22:50 -07006542 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006543 return -ENXIO;
6544
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006545 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006546 if (!list_empty(&data->ref_list))
6547 ref_node = list_first_entry(&data->ref_list,
6548 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006549 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006550 if (ref_node)
6551 percpu_ref_kill(&ref_node->refs);
6552
6553 percpu_ref_kill(&data->refs);
6554
6555 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006556 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006557 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006558
Jens Axboe6b063142019-01-10 22:13:58 -07006559 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006560 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6561 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006562 kfree(data->table[i].files);
6563 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006564 percpu_ref_exit(&data->refs);
6565 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006566 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006567 ctx->nr_user_files = 0;
6568 return 0;
6569}
6570
Jens Axboe6c271ce2019-01-10 11:22:30 -07006571static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6572{
6573 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006574 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006575 /*
6576 * The park is a bit of a work-around, without it we get
6577 * warning spews on shutdown with SQPOLL set and affinity
6578 * set to a single CPU.
6579 */
Jens Axboe06058632019-04-13 09:26:03 -06006580 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006581 kthread_stop(ctx->sqo_thread);
6582 ctx->sqo_thread = NULL;
6583 }
6584}
6585
Jens Axboe6b063142019-01-10 22:13:58 -07006586static void io_finish_async(struct io_ring_ctx *ctx)
6587{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006588 io_sq_thread_stop(ctx);
6589
Jens Axboe561fb042019-10-24 07:25:42 -06006590 if (ctx->io_wq) {
6591 io_wq_destroy(ctx->io_wq);
6592 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006593 }
6594}
6595
6596#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006597/*
6598 * Ensure the UNIX gc is aware of our file set, so we are certain that
6599 * the io_uring can be safely unregistered on process exit, even if we have
6600 * loops in the file referencing.
6601 */
6602static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6603{
6604 struct sock *sk = ctx->ring_sock->sk;
6605 struct scm_fp_list *fpl;
6606 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006607 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006608
Jens Axboe6b063142019-01-10 22:13:58 -07006609 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6610 if (!fpl)
6611 return -ENOMEM;
6612
6613 skb = alloc_skb(0, GFP_KERNEL);
6614 if (!skb) {
6615 kfree(fpl);
6616 return -ENOMEM;
6617 }
6618
6619 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006620
Jens Axboe08a45172019-10-03 08:11:03 -06006621 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006622 fpl->user = get_uid(ctx->user);
6623 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006624 struct file *file = io_file_from_index(ctx, i + offset);
6625
6626 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006627 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006628 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006629 unix_inflight(fpl->user, fpl->fp[nr_files]);
6630 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006631 }
6632
Jens Axboe08a45172019-10-03 08:11:03 -06006633 if (nr_files) {
6634 fpl->max = SCM_MAX_FD;
6635 fpl->count = nr_files;
6636 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006637 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006638 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6639 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006640
Jens Axboe08a45172019-10-03 08:11:03 -06006641 for (i = 0; i < nr_files; i++)
6642 fput(fpl->fp[i]);
6643 } else {
6644 kfree_skb(skb);
6645 kfree(fpl);
6646 }
Jens Axboe6b063142019-01-10 22:13:58 -07006647
6648 return 0;
6649}
6650
6651/*
6652 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6653 * causes regular reference counting to break down. We rely on the UNIX
6654 * garbage collection to take care of this problem for us.
6655 */
6656static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6657{
6658 unsigned left, total;
6659 int ret = 0;
6660
6661 total = 0;
6662 left = ctx->nr_user_files;
6663 while (left) {
6664 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006665
6666 ret = __io_sqe_files_scm(ctx, this_files, total);
6667 if (ret)
6668 break;
6669 left -= this_files;
6670 total += this_files;
6671 }
6672
6673 if (!ret)
6674 return 0;
6675
6676 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006677 struct file *file = io_file_from_index(ctx, total);
6678
6679 if (file)
6680 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006681 total++;
6682 }
6683
6684 return ret;
6685}
6686#else
6687static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6688{
6689 return 0;
6690}
6691#endif
6692
Jens Axboe65e19f52019-10-26 07:20:21 -06006693static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6694 unsigned nr_files)
6695{
6696 int i;
6697
6698 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006699 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006700 unsigned this_files;
6701
6702 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6703 table->files = kcalloc(this_files, sizeof(struct file *),
6704 GFP_KERNEL);
6705 if (!table->files)
6706 break;
6707 nr_files -= this_files;
6708 }
6709
6710 if (i == nr_tables)
6711 return 0;
6712
6713 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006714 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006715 kfree(table->files);
6716 }
6717 return 1;
6718}
6719
Jens Axboe05f3fb32019-12-09 11:22:50 -07006720static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006721{
6722#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006723 struct sock *sock = ctx->ring_sock->sk;
6724 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6725 struct sk_buff *skb;
6726 int i;
6727
6728 __skb_queue_head_init(&list);
6729
6730 /*
6731 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6732 * remove this entry and rearrange the file array.
6733 */
6734 skb = skb_dequeue(head);
6735 while (skb) {
6736 struct scm_fp_list *fp;
6737
6738 fp = UNIXCB(skb).fp;
6739 for (i = 0; i < fp->count; i++) {
6740 int left;
6741
6742 if (fp->fp[i] != file)
6743 continue;
6744
6745 unix_notinflight(fp->user, fp->fp[i]);
6746 left = fp->count - 1 - i;
6747 if (left) {
6748 memmove(&fp->fp[i], &fp->fp[i + 1],
6749 left * sizeof(struct file *));
6750 }
6751 fp->count--;
6752 if (!fp->count) {
6753 kfree_skb(skb);
6754 skb = NULL;
6755 } else {
6756 __skb_queue_tail(&list, skb);
6757 }
6758 fput(file);
6759 file = NULL;
6760 break;
6761 }
6762
6763 if (!file)
6764 break;
6765
6766 __skb_queue_tail(&list, skb);
6767
6768 skb = skb_dequeue(head);
6769 }
6770
6771 if (skb_peek(&list)) {
6772 spin_lock_irq(&head->lock);
6773 while ((skb = __skb_dequeue(&list)) != NULL)
6774 __skb_queue_tail(head, skb);
6775 spin_unlock_irq(&head->lock);
6776 }
6777#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006778 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006779#endif
6780}
6781
Jens Axboe05f3fb32019-12-09 11:22:50 -07006782struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006783 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006784 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006785};
6786
Jens Axboe4a38aed22020-05-14 17:21:15 -06006787static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006788{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006789 struct fixed_file_data *file_data = ref_node->file_data;
6790 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006791 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006792
6793 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006794 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006795 io_ring_file_put(ctx, pfile->file);
6796 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006797 }
6798
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006799 spin_lock(&file_data->lock);
6800 list_del(&ref_node->node);
6801 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006802
Xiaoguang Wang05589552020-03-31 14:05:18 +08006803 percpu_ref_exit(&ref_node->refs);
6804 kfree(ref_node);
6805 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006806}
6807
Jens Axboe4a38aed22020-05-14 17:21:15 -06006808static void io_file_put_work(struct work_struct *work)
6809{
6810 struct io_ring_ctx *ctx;
6811 struct llist_node *node;
6812
6813 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6814 node = llist_del_all(&ctx->file_put_llist);
6815
6816 while (node) {
6817 struct fixed_file_ref_node *ref_node;
6818 struct llist_node *next = node->next;
6819
6820 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6821 __io_file_put_work(ref_node);
6822 node = next;
6823 }
6824}
6825
Jens Axboe05f3fb32019-12-09 11:22:50 -07006826static void io_file_data_ref_zero(struct percpu_ref *ref)
6827{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006828 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006829 struct io_ring_ctx *ctx;
6830 bool first_add;
6831 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006832
Xiaoguang Wang05589552020-03-31 14:05:18 +08006833 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006834 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006835
Jens Axboe4a38aed22020-05-14 17:21:15 -06006836 if (percpu_ref_is_dying(&ctx->file_data->refs))
6837 delay = 0;
6838
6839 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6840 if (!delay)
6841 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6842 else if (first_add)
6843 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006844}
6845
6846static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6847 struct io_ring_ctx *ctx)
6848{
6849 struct fixed_file_ref_node *ref_node;
6850
6851 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6852 if (!ref_node)
6853 return ERR_PTR(-ENOMEM);
6854
6855 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6856 0, GFP_KERNEL)) {
6857 kfree(ref_node);
6858 return ERR_PTR(-ENOMEM);
6859 }
6860 INIT_LIST_HEAD(&ref_node->node);
6861 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006862 ref_node->file_data = ctx->file_data;
6863 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006864}
6865
6866static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6867{
6868 percpu_ref_exit(&ref_node->refs);
6869 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006870}
6871
6872static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6873 unsigned nr_args)
6874{
6875 __s32 __user *fds = (__s32 __user *) arg;
6876 unsigned nr_tables;
6877 struct file *file;
6878 int fd, ret = 0;
6879 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006880 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006881
6882 if (ctx->file_data)
6883 return -EBUSY;
6884 if (!nr_args)
6885 return -EINVAL;
6886 if (nr_args > IORING_MAX_FIXED_FILES)
6887 return -EMFILE;
6888
6889 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6890 if (!ctx->file_data)
6891 return -ENOMEM;
6892 ctx->file_data->ctx = ctx;
6893 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006894 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006895 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006896
6897 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6898 ctx->file_data->table = kcalloc(nr_tables,
6899 sizeof(struct fixed_file_table),
6900 GFP_KERNEL);
6901 if (!ctx->file_data->table) {
6902 kfree(ctx->file_data);
6903 ctx->file_data = NULL;
6904 return -ENOMEM;
6905 }
6906
Xiaoguang Wang05589552020-03-31 14:05:18 +08006907 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006908 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6909 kfree(ctx->file_data->table);
6910 kfree(ctx->file_data);
6911 ctx->file_data = NULL;
6912 return -ENOMEM;
6913 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006914
6915 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6916 percpu_ref_exit(&ctx->file_data->refs);
6917 kfree(ctx->file_data->table);
6918 kfree(ctx->file_data);
6919 ctx->file_data = NULL;
6920 return -ENOMEM;
6921 }
6922
6923 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6924 struct fixed_file_table *table;
6925 unsigned index;
6926
6927 ret = -EFAULT;
6928 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6929 break;
6930 /* allow sparse sets */
6931 if (fd == -1) {
6932 ret = 0;
6933 continue;
6934 }
6935
6936 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6937 index = i & IORING_FILE_TABLE_MASK;
6938 file = fget(fd);
6939
6940 ret = -EBADF;
6941 if (!file)
6942 break;
6943
6944 /*
6945 * Don't allow io_uring instances to be registered. If UNIX
6946 * isn't enabled, then this causes a reference cycle and this
6947 * instance can never get freed. If UNIX is enabled we'll
6948 * handle it just fine, but there's still no point in allowing
6949 * a ring fd as it doesn't support regular read/write anyway.
6950 */
6951 if (file->f_op == &io_uring_fops) {
6952 fput(file);
6953 break;
6954 }
6955 ret = 0;
6956 table->files[index] = file;
6957 }
6958
6959 if (ret) {
6960 for (i = 0; i < ctx->nr_user_files; i++) {
6961 file = io_file_from_index(ctx, i);
6962 if (file)
6963 fput(file);
6964 }
6965 for (i = 0; i < nr_tables; i++)
6966 kfree(ctx->file_data->table[i].files);
6967
6968 kfree(ctx->file_data->table);
6969 kfree(ctx->file_data);
6970 ctx->file_data = NULL;
6971 ctx->nr_user_files = 0;
6972 return ret;
6973 }
6974
6975 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006976 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006977 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006978 return ret;
6979 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006980
Xiaoguang Wang05589552020-03-31 14:05:18 +08006981 ref_node = alloc_fixed_file_ref_node(ctx);
6982 if (IS_ERR(ref_node)) {
6983 io_sqe_files_unregister(ctx);
6984 return PTR_ERR(ref_node);
6985 }
6986
6987 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006988 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006989 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006990 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006991 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006992 return ret;
6993}
6994
Jens Axboec3a31e62019-10-03 13:59:56 -06006995static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6996 int index)
6997{
6998#if defined(CONFIG_UNIX)
6999 struct sock *sock = ctx->ring_sock->sk;
7000 struct sk_buff_head *head = &sock->sk_receive_queue;
7001 struct sk_buff *skb;
7002
7003 /*
7004 * See if we can merge this file into an existing skb SCM_RIGHTS
7005 * file set. If there's no room, fall back to allocating a new skb
7006 * and filling it in.
7007 */
7008 spin_lock_irq(&head->lock);
7009 skb = skb_peek(head);
7010 if (skb) {
7011 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7012
7013 if (fpl->count < SCM_MAX_FD) {
7014 __skb_unlink(skb, head);
7015 spin_unlock_irq(&head->lock);
7016 fpl->fp[fpl->count] = get_file(file);
7017 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7018 fpl->count++;
7019 spin_lock_irq(&head->lock);
7020 __skb_queue_head(head, skb);
7021 } else {
7022 skb = NULL;
7023 }
7024 }
7025 spin_unlock_irq(&head->lock);
7026
7027 if (skb) {
7028 fput(file);
7029 return 0;
7030 }
7031
7032 return __io_sqe_files_scm(ctx, 1, index);
7033#else
7034 return 0;
7035#endif
7036}
7037
Hillf Dantona5318d32020-03-23 17:47:15 +08007038static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007039 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007040{
Hillf Dantona5318d32020-03-23 17:47:15 +08007041 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007042 struct percpu_ref *refs = data->cur_refs;
7043 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007044
Jens Axboe05f3fb32019-12-09 11:22:50 -07007045 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007046 if (!pfile)
7047 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007048
Xiaoguang Wang05589552020-03-31 14:05:18 +08007049 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007050 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007051 list_add(&pfile->list, &ref_node->file_list);
7052
Hillf Dantona5318d32020-03-23 17:47:15 +08007053 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007054}
7055
7056static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7057 struct io_uring_files_update *up,
7058 unsigned nr_args)
7059{
7060 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007061 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007062 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007063 __s32 __user *fds;
7064 int fd, i, err;
7065 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007066 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007067
Jens Axboe05f3fb32019-12-09 11:22:50 -07007068 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007069 return -EOVERFLOW;
7070 if (done > ctx->nr_user_files)
7071 return -EINVAL;
7072
Xiaoguang Wang05589552020-03-31 14:05:18 +08007073 ref_node = alloc_fixed_file_ref_node(ctx);
7074 if (IS_ERR(ref_node))
7075 return PTR_ERR(ref_node);
7076
Jens Axboec3a31e62019-10-03 13:59:56 -06007077 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007078 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007079 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007080 struct fixed_file_table *table;
7081 unsigned index;
7082
Jens Axboec3a31e62019-10-03 13:59:56 -06007083 err = 0;
7084 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7085 err = -EFAULT;
7086 break;
7087 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007088 i = array_index_nospec(up->offset, ctx->nr_user_files);
7089 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007090 index = i & IORING_FILE_TABLE_MASK;
7091 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007092 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007093 err = io_queue_file_removal(data, file);
7094 if (err)
7095 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007096 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007097 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007098 }
7099 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007100 file = fget(fd);
7101 if (!file) {
7102 err = -EBADF;
7103 break;
7104 }
7105 /*
7106 * Don't allow io_uring instances to be registered. If
7107 * UNIX isn't enabled, then this causes a reference
7108 * cycle and this instance can never get freed. If UNIX
7109 * is enabled we'll handle it just fine, but there's
7110 * still no point in allowing a ring fd as it doesn't
7111 * support regular read/write anyway.
7112 */
7113 if (file->f_op == &io_uring_fops) {
7114 fput(file);
7115 err = -EBADF;
7116 break;
7117 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007118 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007119 err = io_sqe_file_register(ctx, file, i);
7120 if (err)
7121 break;
7122 }
7123 nr_args--;
7124 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007125 up->offset++;
7126 }
7127
Xiaoguang Wang05589552020-03-31 14:05:18 +08007128 if (needs_switch) {
7129 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007130 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007131 list_add(&ref_node->node, &data->ref_list);
7132 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007133 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007134 percpu_ref_get(&ctx->file_data->refs);
7135 } else
7136 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007137
7138 return done ? done : err;
7139}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007140
Jens Axboe05f3fb32019-12-09 11:22:50 -07007141static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7142 unsigned nr_args)
7143{
7144 struct io_uring_files_update up;
7145
7146 if (!ctx->file_data)
7147 return -ENXIO;
7148 if (!nr_args)
7149 return -EINVAL;
7150 if (copy_from_user(&up, arg, sizeof(up)))
7151 return -EFAULT;
7152 if (up.resv)
7153 return -EINVAL;
7154
7155 return __io_sqe_files_update(ctx, &up, nr_args);
7156}
Jens Axboec3a31e62019-10-03 13:59:56 -06007157
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007158static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007159{
7160 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7161
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007162 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007163 io_put_req(req);
7164}
7165
Pavel Begunkov24369c22020-01-28 03:15:48 +03007166static int io_init_wq_offload(struct io_ring_ctx *ctx,
7167 struct io_uring_params *p)
7168{
7169 struct io_wq_data data;
7170 struct fd f;
7171 struct io_ring_ctx *ctx_attach;
7172 unsigned int concurrency;
7173 int ret = 0;
7174
7175 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007176 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007177 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007178
7179 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7180 /* Do QD, or 4 * CPUS, whatever is smallest */
7181 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7182
7183 ctx->io_wq = io_wq_create(concurrency, &data);
7184 if (IS_ERR(ctx->io_wq)) {
7185 ret = PTR_ERR(ctx->io_wq);
7186 ctx->io_wq = NULL;
7187 }
7188 return ret;
7189 }
7190
7191 f = fdget(p->wq_fd);
7192 if (!f.file)
7193 return -EBADF;
7194
7195 if (f.file->f_op != &io_uring_fops) {
7196 ret = -EINVAL;
7197 goto out_fput;
7198 }
7199
7200 ctx_attach = f.file->private_data;
7201 /* @io_wq is protected by holding the fd */
7202 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7203 ret = -EINVAL;
7204 goto out_fput;
7205 }
7206
7207 ctx->io_wq = ctx_attach->io_wq;
7208out_fput:
7209 fdput(f);
7210 return ret;
7211}
7212
Jens Axboe6c271ce2019-01-10 11:22:30 -07007213static int io_sq_offload_start(struct io_ring_ctx *ctx,
7214 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007215{
7216 int ret;
7217
7218 mmgrab(current->mm);
7219 ctx->sqo_mm = current->mm;
7220
Jens Axboe6c271ce2019-01-10 11:22:30 -07007221 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007222 ret = -EPERM;
7223 if (!capable(CAP_SYS_ADMIN))
7224 goto err;
7225
Jens Axboe917257d2019-04-13 09:28:55 -06007226 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7227 if (!ctx->sq_thread_idle)
7228 ctx->sq_thread_idle = HZ;
7229
Jens Axboe6c271ce2019-01-10 11:22:30 -07007230 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007231 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007232
Jens Axboe917257d2019-04-13 09:28:55 -06007233 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007234 if (cpu >= nr_cpu_ids)
7235 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007236 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007237 goto err;
7238
Jens Axboe6c271ce2019-01-10 11:22:30 -07007239 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7240 ctx, cpu,
7241 "io_uring-sq");
7242 } else {
7243 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7244 "io_uring-sq");
7245 }
7246 if (IS_ERR(ctx->sqo_thread)) {
7247 ret = PTR_ERR(ctx->sqo_thread);
7248 ctx->sqo_thread = NULL;
7249 goto err;
7250 }
7251 wake_up_process(ctx->sqo_thread);
7252 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7253 /* Can't have SQ_AFF without SQPOLL */
7254 ret = -EINVAL;
7255 goto err;
7256 }
7257
Pavel Begunkov24369c22020-01-28 03:15:48 +03007258 ret = io_init_wq_offload(ctx, p);
7259 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007260 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007261
7262 return 0;
7263err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007264 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007265 mmdrop(ctx->sqo_mm);
7266 ctx->sqo_mm = NULL;
7267 return ret;
7268}
7269
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007270static inline void __io_unaccount_mem(struct user_struct *user,
7271 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007272{
7273 atomic_long_sub(nr_pages, &user->locked_vm);
7274}
7275
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007276static inline int __io_account_mem(struct user_struct *user,
7277 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007278{
7279 unsigned long page_limit, cur_pages, new_pages;
7280
7281 /* Don't allow more pages than we can safely lock */
7282 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7283
7284 do {
7285 cur_pages = atomic_long_read(&user->locked_vm);
7286 new_pages = cur_pages + nr_pages;
7287 if (new_pages > page_limit)
7288 return -ENOMEM;
7289 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7290 new_pages) != cur_pages);
7291
7292 return 0;
7293}
7294
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007295static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7296 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007297{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007298 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007299 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007300
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007301 if (ctx->sqo_mm) {
7302 if (acct == ACCT_LOCKED)
7303 ctx->sqo_mm->locked_vm -= nr_pages;
7304 else if (acct == ACCT_PINNED)
7305 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7306 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007307}
7308
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007309static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7310 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007311{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007312 int ret;
7313
7314 if (ctx->limit_mem) {
7315 ret = __io_account_mem(ctx->user, nr_pages);
7316 if (ret)
7317 return ret;
7318 }
7319
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007320 if (ctx->sqo_mm) {
7321 if (acct == ACCT_LOCKED)
7322 ctx->sqo_mm->locked_vm += nr_pages;
7323 else if (acct == ACCT_PINNED)
7324 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7325 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007326
7327 return 0;
7328}
7329
Jens Axboe2b188cc2019-01-07 10:46:33 -07007330static void io_mem_free(void *ptr)
7331{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007332 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007333
Mark Rutland52e04ef2019-04-30 17:30:21 +01007334 if (!ptr)
7335 return;
7336
7337 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007338 if (put_page_testzero(page))
7339 free_compound_page(page);
7340}
7341
7342static void *io_mem_alloc(size_t size)
7343{
7344 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7345 __GFP_NORETRY;
7346
7347 return (void *) __get_free_pages(gfp_flags, get_order(size));
7348}
7349
Hristo Venev75b28af2019-08-26 17:23:46 +00007350static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7351 size_t *sq_offset)
7352{
7353 struct io_rings *rings;
7354 size_t off, sq_array_size;
7355
7356 off = struct_size(rings, cqes, cq_entries);
7357 if (off == SIZE_MAX)
7358 return SIZE_MAX;
7359
7360#ifdef CONFIG_SMP
7361 off = ALIGN(off, SMP_CACHE_BYTES);
7362 if (off == 0)
7363 return SIZE_MAX;
7364#endif
7365
7366 sq_array_size = array_size(sizeof(u32), sq_entries);
7367 if (sq_array_size == SIZE_MAX)
7368 return SIZE_MAX;
7369
7370 if (check_add_overflow(off, sq_array_size, &off))
7371 return SIZE_MAX;
7372
7373 if (sq_offset)
7374 *sq_offset = off;
7375
7376 return off;
7377}
7378
Jens Axboe2b188cc2019-01-07 10:46:33 -07007379static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7380{
Hristo Venev75b28af2019-08-26 17:23:46 +00007381 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007382
Hristo Venev75b28af2019-08-26 17:23:46 +00007383 pages = (size_t)1 << get_order(
7384 rings_size(sq_entries, cq_entries, NULL));
7385 pages += (size_t)1 << get_order(
7386 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007387
Hristo Venev75b28af2019-08-26 17:23:46 +00007388 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007389}
7390
Jens Axboeedafcce2019-01-09 09:16:05 -07007391static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7392{
7393 int i, j;
7394
7395 if (!ctx->user_bufs)
7396 return -ENXIO;
7397
7398 for (i = 0; i < ctx->nr_user_bufs; i++) {
7399 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7400
7401 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007402 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007403
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007404 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007405 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007406 imu->nr_bvecs = 0;
7407 }
7408
7409 kfree(ctx->user_bufs);
7410 ctx->user_bufs = NULL;
7411 ctx->nr_user_bufs = 0;
7412 return 0;
7413}
7414
7415static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7416 void __user *arg, unsigned index)
7417{
7418 struct iovec __user *src;
7419
7420#ifdef CONFIG_COMPAT
7421 if (ctx->compat) {
7422 struct compat_iovec __user *ciovs;
7423 struct compat_iovec ciov;
7424
7425 ciovs = (struct compat_iovec __user *) arg;
7426 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7427 return -EFAULT;
7428
Jens Axboed55e5f52019-12-11 16:12:15 -07007429 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007430 dst->iov_len = ciov.iov_len;
7431 return 0;
7432 }
7433#endif
7434 src = (struct iovec __user *) arg;
7435 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7436 return -EFAULT;
7437 return 0;
7438}
7439
7440static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7441 unsigned nr_args)
7442{
7443 struct vm_area_struct **vmas = NULL;
7444 struct page **pages = NULL;
7445 int i, j, got_pages = 0;
7446 int ret = -EINVAL;
7447
7448 if (ctx->user_bufs)
7449 return -EBUSY;
7450 if (!nr_args || nr_args > UIO_MAXIOV)
7451 return -EINVAL;
7452
7453 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7454 GFP_KERNEL);
7455 if (!ctx->user_bufs)
7456 return -ENOMEM;
7457
7458 for (i = 0; i < nr_args; i++) {
7459 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7460 unsigned long off, start, end, ubuf;
7461 int pret, nr_pages;
7462 struct iovec iov;
7463 size_t size;
7464
7465 ret = io_copy_iov(ctx, &iov, arg, i);
7466 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007467 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007468
7469 /*
7470 * Don't impose further limits on the size and buffer
7471 * constraints here, we'll -EINVAL later when IO is
7472 * submitted if they are wrong.
7473 */
7474 ret = -EFAULT;
7475 if (!iov.iov_base || !iov.iov_len)
7476 goto err;
7477
7478 /* arbitrary limit, but we need something */
7479 if (iov.iov_len > SZ_1G)
7480 goto err;
7481
7482 ubuf = (unsigned long) iov.iov_base;
7483 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7484 start = ubuf >> PAGE_SHIFT;
7485 nr_pages = end - start;
7486
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007487 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007488 if (ret)
7489 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007490
7491 ret = 0;
7492 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007493 kvfree(vmas);
7494 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007495 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007496 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007497 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007498 sizeof(struct vm_area_struct *),
7499 GFP_KERNEL);
7500 if (!pages || !vmas) {
7501 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007502 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007503 goto err;
7504 }
7505 got_pages = nr_pages;
7506 }
7507
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007508 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007509 GFP_KERNEL);
7510 ret = -ENOMEM;
7511 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007512 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007513 goto err;
7514 }
7515
7516 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007517 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007518 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007519 FOLL_WRITE | FOLL_LONGTERM,
7520 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007521 if (pret == nr_pages) {
7522 /* don't support file backed memory */
7523 for (j = 0; j < nr_pages; j++) {
7524 struct vm_area_struct *vma = vmas[j];
7525
7526 if (vma->vm_file &&
7527 !is_file_hugepages(vma->vm_file)) {
7528 ret = -EOPNOTSUPP;
7529 break;
7530 }
7531 }
7532 } else {
7533 ret = pret < 0 ? pret : -EFAULT;
7534 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007535 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007536 if (ret) {
7537 /*
7538 * if we did partial map, or found file backed vmas,
7539 * release any pages we did get
7540 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007541 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007542 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007543 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007544 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007545 goto err;
7546 }
7547
7548 off = ubuf & ~PAGE_MASK;
7549 size = iov.iov_len;
7550 for (j = 0; j < nr_pages; j++) {
7551 size_t vec_len;
7552
7553 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7554 imu->bvec[j].bv_page = pages[j];
7555 imu->bvec[j].bv_len = vec_len;
7556 imu->bvec[j].bv_offset = off;
7557 off = 0;
7558 size -= vec_len;
7559 }
7560 /* store original address for later verification */
7561 imu->ubuf = ubuf;
7562 imu->len = iov.iov_len;
7563 imu->nr_bvecs = nr_pages;
7564
7565 ctx->nr_user_bufs++;
7566 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007567 kvfree(pages);
7568 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007569 return 0;
7570err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007571 kvfree(pages);
7572 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007573 io_sqe_buffer_unregister(ctx);
7574 return ret;
7575}
7576
Jens Axboe9b402842019-04-11 11:45:41 -06007577static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7578{
7579 __s32 __user *fds = arg;
7580 int fd;
7581
7582 if (ctx->cq_ev_fd)
7583 return -EBUSY;
7584
7585 if (copy_from_user(&fd, fds, sizeof(*fds)))
7586 return -EFAULT;
7587
7588 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7589 if (IS_ERR(ctx->cq_ev_fd)) {
7590 int ret = PTR_ERR(ctx->cq_ev_fd);
7591 ctx->cq_ev_fd = NULL;
7592 return ret;
7593 }
7594
7595 return 0;
7596}
7597
7598static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7599{
7600 if (ctx->cq_ev_fd) {
7601 eventfd_ctx_put(ctx->cq_ev_fd);
7602 ctx->cq_ev_fd = NULL;
7603 return 0;
7604 }
7605
7606 return -ENXIO;
7607}
7608
Jens Axboe5a2e7452020-02-23 16:23:11 -07007609static int __io_destroy_buffers(int id, void *p, void *data)
7610{
7611 struct io_ring_ctx *ctx = data;
7612 struct io_buffer *buf = p;
7613
Jens Axboe067524e2020-03-02 16:32:28 -07007614 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007615 return 0;
7616}
7617
7618static void io_destroy_buffers(struct io_ring_ctx *ctx)
7619{
7620 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7621 idr_destroy(&ctx->io_buffer_idr);
7622}
7623
Jens Axboe2b188cc2019-01-07 10:46:33 -07007624static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7625{
Jens Axboe6b063142019-01-10 22:13:58 -07007626 io_finish_async(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007627 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007628 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007629 ctx->sqo_mm = NULL;
7630 }
Jens Axboedef596e2019-01-09 08:59:42 -07007631
7632 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007633 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007634 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007635 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007636 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007637 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007638
Jens Axboe2b188cc2019-01-07 10:46:33 -07007639#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007640 if (ctx->ring_sock) {
7641 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007642 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007643 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007644#endif
7645
Hristo Venev75b28af2019-08-26 17:23:46 +00007646 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007647 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007648
7649 percpu_ref_exit(&ctx->refs);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007650 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7651 ACCT_LOCKED);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007652 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007653 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007654 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007655 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007656 kfree(ctx);
7657}
7658
7659static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7660{
7661 struct io_ring_ctx *ctx = file->private_data;
7662 __poll_t mask = 0;
7663
7664 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007665 /*
7666 * synchronizes with barrier from wq_has_sleeper call in
7667 * io_commit_cqring
7668 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007669 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007670 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7671 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007672 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007673 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007674 mask |= EPOLLIN | EPOLLRDNORM;
7675
7676 return mask;
7677}
7678
7679static int io_uring_fasync(int fd, struct file *file, int on)
7680{
7681 struct io_ring_ctx *ctx = file->private_data;
7682
7683 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7684}
7685
Jens Axboe071698e2020-01-28 10:04:42 -07007686static int io_remove_personalities(int id, void *p, void *data)
7687{
7688 struct io_ring_ctx *ctx = data;
7689 const struct cred *cred;
7690
7691 cred = idr_remove(&ctx->personality_idr, id);
7692 if (cred)
7693 put_cred(cred);
7694 return 0;
7695}
7696
Jens Axboe85faa7b2020-04-09 18:14:00 -06007697static void io_ring_exit_work(struct work_struct *work)
7698{
7699 struct io_ring_ctx *ctx;
7700
7701 ctx = container_of(work, struct io_ring_ctx, exit_work);
7702 if (ctx->rings)
7703 io_cqring_overflow_flush(ctx, true);
7704
Jens Axboe56952e92020-06-17 15:00:04 -06007705 /*
7706 * If we're doing polled IO and end up having requests being
7707 * submitted async (out-of-line), then completions can come in while
7708 * we're waiting for refs to drop. We need to reap these manually,
7709 * as nobody else will be looking for them.
7710 */
7711 while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7712 io_iopoll_reap_events(ctx);
7713 if (ctx->rings)
7714 io_cqring_overflow_flush(ctx, true);
7715 }
Jens Axboe85faa7b2020-04-09 18:14:00 -06007716 io_ring_ctx_free(ctx);
7717}
7718
Jens Axboe2b188cc2019-01-07 10:46:33 -07007719static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7720{
7721 mutex_lock(&ctx->uring_lock);
7722 percpu_ref_kill(&ctx->refs);
7723 mutex_unlock(&ctx->uring_lock);
7724
Jens Axboe5262f562019-09-17 12:26:57 -06007725 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007726 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007727
7728 if (ctx->io_wq)
7729 io_wq_cancel_all(ctx->io_wq);
7730
Jens Axboedef596e2019-01-09 08:59:42 -07007731 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007732 /* if we failed setting up the ctx, we might not have any rings */
7733 if (ctx->rings)
7734 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007735 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007736 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7737 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007738}
7739
7740static int io_uring_release(struct inode *inode, struct file *file)
7741{
7742 struct io_ring_ctx *ctx = file->private_data;
7743
7744 file->private_data = NULL;
7745 io_ring_ctx_wait_and_kill(ctx);
7746 return 0;
7747}
7748
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007749static bool io_wq_files_match(struct io_wq_work *work, void *data)
7750{
7751 struct files_struct *files = data;
7752
7753 return work->files == files;
7754}
7755
Jens Axboefcb323c2019-10-24 12:39:47 -06007756static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7757 struct files_struct *files)
7758{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007759 if (list_empty_careful(&ctx->inflight_list))
7760 return;
7761
7762 /* cancel all at once, should be faster than doing it one by one*/
7763 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7764
Jens Axboefcb323c2019-10-24 12:39:47 -06007765 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007766 struct io_kiocb *cancel_req = NULL, *req;
7767 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007768
7769 spin_lock_irq(&ctx->inflight_lock);
7770 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007771 if (req->work.files != files)
7772 continue;
7773 /* req is being completed, ignore */
7774 if (!refcount_inc_not_zero(&req->refs))
7775 continue;
7776 cancel_req = req;
7777 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007778 }
Jens Axboe768134d2019-11-10 20:30:53 -07007779 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007780 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007781 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007782 spin_unlock_irq(&ctx->inflight_lock);
7783
Jens Axboe768134d2019-11-10 20:30:53 -07007784 /* We need to keep going until we don't find a matching req */
7785 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007786 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007787
Jens Axboe2ca10252020-02-13 17:17:35 -07007788 if (cancel_req->flags & REQ_F_OVERFLOW) {
7789 spin_lock_irq(&ctx->completion_lock);
7790 list_del(&cancel_req->list);
7791 cancel_req->flags &= ~REQ_F_OVERFLOW;
7792 if (list_empty(&ctx->cq_overflow_list)) {
7793 clear_bit(0, &ctx->sq_check_overflow);
7794 clear_bit(0, &ctx->cq_check_overflow);
7795 }
7796 spin_unlock_irq(&ctx->completion_lock);
7797
7798 WRITE_ONCE(ctx->rings->cq_overflow,
7799 atomic_inc_return(&ctx->cached_cq_overflow));
7800
7801 /*
7802 * Put inflight ref and overflow ref. If that's
7803 * all we had, then we're done with this request.
7804 */
7805 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007806 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007807 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007808 continue;
7809 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007810 } else {
7811 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7812 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007813 }
7814
Jens Axboefcb323c2019-10-24 12:39:47 -06007815 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007816 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007817 }
7818}
7819
Pavel Begunkov801dd572020-06-15 10:33:14 +03007820static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007821{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007822 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7823 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007824
Pavel Begunkov801dd572020-06-15 10:33:14 +03007825 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007826}
7827
Jens Axboefcb323c2019-10-24 12:39:47 -06007828static int io_uring_flush(struct file *file, void *data)
7829{
7830 struct io_ring_ctx *ctx = file->private_data;
7831
7832 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007833
7834 /*
7835 * If the task is going away, cancel work it may have pending
7836 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007837 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7838 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007839
Jens Axboefcb323c2019-10-24 12:39:47 -06007840 return 0;
7841}
7842
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007843static void *io_uring_validate_mmap_request(struct file *file,
7844 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007845{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007846 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007847 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007848 struct page *page;
7849 void *ptr;
7850
7851 switch (offset) {
7852 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007853 case IORING_OFF_CQ_RING:
7854 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007855 break;
7856 case IORING_OFF_SQES:
7857 ptr = ctx->sq_sqes;
7858 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007859 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007860 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007861 }
7862
7863 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007864 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007865 return ERR_PTR(-EINVAL);
7866
7867 return ptr;
7868}
7869
7870#ifdef CONFIG_MMU
7871
7872static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7873{
7874 size_t sz = vma->vm_end - vma->vm_start;
7875 unsigned long pfn;
7876 void *ptr;
7877
7878 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7879 if (IS_ERR(ptr))
7880 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007881
7882 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7883 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7884}
7885
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007886#else /* !CONFIG_MMU */
7887
7888static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7889{
7890 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7891}
7892
7893static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7894{
7895 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7896}
7897
7898static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7899 unsigned long addr, unsigned long len,
7900 unsigned long pgoff, unsigned long flags)
7901{
7902 void *ptr;
7903
7904 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7905 if (IS_ERR(ptr))
7906 return PTR_ERR(ptr);
7907
7908 return (unsigned long) ptr;
7909}
7910
7911#endif /* !CONFIG_MMU */
7912
Jens Axboe2b188cc2019-01-07 10:46:33 -07007913SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7914 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7915 size_t, sigsz)
7916{
7917 struct io_ring_ctx *ctx;
7918 long ret = -EBADF;
7919 int submitted = 0;
7920 struct fd f;
7921
Jens Axboeb41e9852020-02-17 09:52:41 -07007922 if (current->task_works)
7923 task_work_run();
7924
Jens Axboe6c271ce2019-01-10 11:22:30 -07007925 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007926 return -EINVAL;
7927
7928 f = fdget(fd);
7929 if (!f.file)
7930 return -EBADF;
7931
7932 ret = -EOPNOTSUPP;
7933 if (f.file->f_op != &io_uring_fops)
7934 goto out_fput;
7935
7936 ret = -ENXIO;
7937 ctx = f.file->private_data;
7938 if (!percpu_ref_tryget(&ctx->refs))
7939 goto out_fput;
7940
Jens Axboe6c271ce2019-01-10 11:22:30 -07007941 /*
7942 * For SQ polling, the thread will do all submissions and completions.
7943 * Just return the requested submit count, and wake the thread if
7944 * we were asked to.
7945 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007946 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007947 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007948 if (!list_empty_careful(&ctx->cq_overflow_list))
7949 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007950 if (flags & IORING_ENTER_SQ_WAKEUP)
7951 wake_up(&ctx->sqo_wait);
7952 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007953 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007954 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007955 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007956 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007957
7958 if (submitted != to_submit)
7959 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007960 }
7961 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007962 unsigned nr_events = 0;
7963
Jens Axboe2b188cc2019-01-07 10:46:33 -07007964 min_complete = min(min_complete, ctx->cq_entries);
7965
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007966 /*
7967 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7968 * space applications don't need to do io completion events
7969 * polling again, they can rely on io_sq_thread to do polling
7970 * work, which can reduce cpu usage and uring_lock contention.
7971 */
7972 if (ctx->flags & IORING_SETUP_IOPOLL &&
7973 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007974 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007975 } else {
7976 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7977 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007978 }
7979
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007980out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007981 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007982out_fput:
7983 fdput(f);
7984 return submitted ? submitted : ret;
7985}
7986
Tobias Klauserbebdb652020-02-26 18:38:32 +01007987#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007988static int io_uring_show_cred(int id, void *p, void *data)
7989{
7990 const struct cred *cred = p;
7991 struct seq_file *m = data;
7992 struct user_namespace *uns = seq_user_ns(m);
7993 struct group_info *gi;
7994 kernel_cap_t cap;
7995 unsigned __capi;
7996 int g;
7997
7998 seq_printf(m, "%5d\n", id);
7999 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8000 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8001 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8002 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8003 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8004 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8005 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8006 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8007 seq_puts(m, "\n\tGroups:\t");
8008 gi = cred->group_info;
8009 for (g = 0; g < gi->ngroups; g++) {
8010 seq_put_decimal_ull(m, g ? " " : "",
8011 from_kgid_munged(uns, gi->gid[g]));
8012 }
8013 seq_puts(m, "\n\tCapEff:\t");
8014 cap = cred->cap_effective;
8015 CAP_FOR_EACH_U32(__capi)
8016 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8017 seq_putc(m, '\n');
8018 return 0;
8019}
8020
8021static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8022{
8023 int i;
8024
8025 mutex_lock(&ctx->uring_lock);
8026 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
8027 for (i = 0; i < ctx->nr_user_files; i++) {
8028 struct fixed_file_table *table;
8029 struct file *f;
8030
8031 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8032 f = table->files[i & IORING_FILE_TABLE_MASK];
8033 if (f)
8034 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8035 else
8036 seq_printf(m, "%5u: <none>\n", i);
8037 }
8038 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
8039 for (i = 0; i < ctx->nr_user_bufs; i++) {
8040 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8041
8042 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8043 (unsigned int) buf->len);
8044 }
8045 if (!idr_is_empty(&ctx->personality_idr)) {
8046 seq_printf(m, "Personalities:\n");
8047 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8048 }
Jens Axboed7718a92020-02-14 22:23:12 -07008049 seq_printf(m, "PollList:\n");
8050 spin_lock_irq(&ctx->completion_lock);
8051 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8052 struct hlist_head *list = &ctx->cancel_hash[i];
8053 struct io_kiocb *req;
8054
8055 hlist_for_each_entry(req, list, hash_node)
8056 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8057 req->task->task_works != NULL);
8058 }
8059 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008060 mutex_unlock(&ctx->uring_lock);
8061}
8062
8063static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8064{
8065 struct io_ring_ctx *ctx = f->private_data;
8066
8067 if (percpu_ref_tryget(&ctx->refs)) {
8068 __io_uring_show_fdinfo(ctx, m);
8069 percpu_ref_put(&ctx->refs);
8070 }
8071}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008072#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008073
Jens Axboe2b188cc2019-01-07 10:46:33 -07008074static const struct file_operations io_uring_fops = {
8075 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008076 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008077 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008078#ifndef CONFIG_MMU
8079 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8080 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8081#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008082 .poll = io_uring_poll,
8083 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008084#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008085 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008086#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008087};
8088
8089static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8090 struct io_uring_params *p)
8091{
Hristo Venev75b28af2019-08-26 17:23:46 +00008092 struct io_rings *rings;
8093 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008094
Hristo Venev75b28af2019-08-26 17:23:46 +00008095 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8096 if (size == SIZE_MAX)
8097 return -EOVERFLOW;
8098
8099 rings = io_mem_alloc(size);
8100 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008101 return -ENOMEM;
8102
Hristo Venev75b28af2019-08-26 17:23:46 +00008103 ctx->rings = rings;
8104 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8105 rings->sq_ring_mask = p->sq_entries - 1;
8106 rings->cq_ring_mask = p->cq_entries - 1;
8107 rings->sq_ring_entries = p->sq_entries;
8108 rings->cq_ring_entries = p->cq_entries;
8109 ctx->sq_mask = rings->sq_ring_mask;
8110 ctx->cq_mask = rings->cq_ring_mask;
8111 ctx->sq_entries = rings->sq_ring_entries;
8112 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008113
8114 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008115 if (size == SIZE_MAX) {
8116 io_mem_free(ctx->rings);
8117 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008118 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008119 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008120
8121 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008122 if (!ctx->sq_sqes) {
8123 io_mem_free(ctx->rings);
8124 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008125 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008126 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008127
Jens Axboe2b188cc2019-01-07 10:46:33 -07008128 return 0;
8129}
8130
8131/*
8132 * Allocate an anonymous fd, this is what constitutes the application
8133 * visible backing of an io_uring instance. The application mmaps this
8134 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8135 * we have to tie this fd to a socket for file garbage collection purposes.
8136 */
8137static int io_uring_get_fd(struct io_ring_ctx *ctx)
8138{
8139 struct file *file;
8140 int ret;
8141
8142#if defined(CONFIG_UNIX)
8143 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8144 &ctx->ring_sock);
8145 if (ret)
8146 return ret;
8147#endif
8148
8149 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8150 if (ret < 0)
8151 goto err;
8152
8153 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8154 O_RDWR | O_CLOEXEC);
8155 if (IS_ERR(file)) {
8156 put_unused_fd(ret);
8157 ret = PTR_ERR(file);
8158 goto err;
8159 }
8160
8161#if defined(CONFIG_UNIX)
8162 ctx->ring_sock->file = file;
8163#endif
8164 fd_install(ret, file);
8165 return ret;
8166err:
8167#if defined(CONFIG_UNIX)
8168 sock_release(ctx->ring_sock);
8169 ctx->ring_sock = NULL;
8170#endif
8171 return ret;
8172}
8173
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008174static int io_uring_create(unsigned entries, struct io_uring_params *p,
8175 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008176{
8177 struct user_struct *user = NULL;
8178 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008179 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008180 int ret;
8181
Jens Axboe8110c1a2019-12-28 15:39:54 -07008182 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008183 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008184 if (entries > IORING_MAX_ENTRIES) {
8185 if (!(p->flags & IORING_SETUP_CLAMP))
8186 return -EINVAL;
8187 entries = IORING_MAX_ENTRIES;
8188 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008189
8190 /*
8191 * Use twice as many entries for the CQ ring. It's possible for the
8192 * application to drive a higher depth than the size of the SQ ring,
8193 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008194 * some flexibility in overcommitting a bit. If the application has
8195 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8196 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008197 */
8198 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008199 if (p->flags & IORING_SETUP_CQSIZE) {
8200 /*
8201 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8202 * to a power-of-two, if it isn't already. We do NOT impose
8203 * any cq vs sq ring sizing.
8204 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008205 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008206 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008207 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8208 if (!(p->flags & IORING_SETUP_CLAMP))
8209 return -EINVAL;
8210 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8211 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008212 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8213 } else {
8214 p->cq_entries = 2 * p->sq_entries;
8215 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008216
8217 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008218 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008219
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008220 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008221 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008222 ring_pages(p->sq_entries, p->cq_entries));
8223 if (ret) {
8224 free_uid(user);
8225 return ret;
8226 }
8227 }
8228
8229 ctx = io_ring_ctx_alloc(p);
8230 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008231 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008232 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008233 p->cq_entries));
8234 free_uid(user);
8235 return -ENOMEM;
8236 }
8237 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008238 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008239 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008240
8241 ret = io_allocate_scq_urings(ctx, p);
8242 if (ret)
8243 goto err;
8244
Jens Axboe6c271ce2019-01-10 11:22:30 -07008245 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008246 if (ret)
8247 goto err;
8248
Jens Axboe2b188cc2019-01-07 10:46:33 -07008249 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008250 p->sq_off.head = offsetof(struct io_rings, sq.head);
8251 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8252 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8253 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8254 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8255 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8256 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008257
8258 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008259 p->cq_off.head = offsetof(struct io_rings, cq.head);
8260 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8261 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8262 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8263 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8264 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008265 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008266
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008267 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8268 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008269 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8270 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008271
8272 if (copy_to_user(params, p, sizeof(*p))) {
8273 ret = -EFAULT;
8274 goto err;
8275 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008276 /*
8277 * Install ring fd as the very last thing, so we don't risk someone
8278 * having closed it before we finish setup
8279 */
8280 ret = io_uring_get_fd(ctx);
8281 if (ret < 0)
8282 goto err;
8283
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008284 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008285 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8286 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008287 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008288 return ret;
8289err:
8290 io_ring_ctx_wait_and_kill(ctx);
8291 return ret;
8292}
8293
8294/*
8295 * Sets up an aio uring context, and returns the fd. Applications asks for a
8296 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8297 * params structure passed in.
8298 */
8299static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8300{
8301 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008302 int i;
8303
8304 if (copy_from_user(&p, params, sizeof(p)))
8305 return -EFAULT;
8306 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8307 if (p.resv[i])
8308 return -EINVAL;
8309 }
8310
Jens Axboe6c271ce2019-01-10 11:22:30 -07008311 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008312 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008313 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008314 return -EINVAL;
8315
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008316 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008317}
8318
8319SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8320 struct io_uring_params __user *, params)
8321{
8322 return io_uring_setup(entries, params);
8323}
8324
Jens Axboe66f4af92020-01-16 15:36:52 -07008325static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8326{
8327 struct io_uring_probe *p;
8328 size_t size;
8329 int i, ret;
8330
8331 size = struct_size(p, ops, nr_args);
8332 if (size == SIZE_MAX)
8333 return -EOVERFLOW;
8334 p = kzalloc(size, GFP_KERNEL);
8335 if (!p)
8336 return -ENOMEM;
8337
8338 ret = -EFAULT;
8339 if (copy_from_user(p, arg, size))
8340 goto out;
8341 ret = -EINVAL;
8342 if (memchr_inv(p, 0, size))
8343 goto out;
8344
8345 p->last_op = IORING_OP_LAST - 1;
8346 if (nr_args > IORING_OP_LAST)
8347 nr_args = IORING_OP_LAST;
8348
8349 for (i = 0; i < nr_args; i++) {
8350 p->ops[i].op = i;
8351 if (!io_op_defs[i].not_supported)
8352 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8353 }
8354 p->ops_len = i;
8355
8356 ret = 0;
8357 if (copy_to_user(arg, p, size))
8358 ret = -EFAULT;
8359out:
8360 kfree(p);
8361 return ret;
8362}
8363
Jens Axboe071698e2020-01-28 10:04:42 -07008364static int io_register_personality(struct io_ring_ctx *ctx)
8365{
8366 const struct cred *creds = get_current_cred();
8367 int id;
8368
8369 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8370 USHRT_MAX, GFP_KERNEL);
8371 if (id < 0)
8372 put_cred(creds);
8373 return id;
8374}
8375
8376static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8377{
8378 const struct cred *old_creds;
8379
8380 old_creds = idr_remove(&ctx->personality_idr, id);
8381 if (old_creds) {
8382 put_cred(old_creds);
8383 return 0;
8384 }
8385
8386 return -EINVAL;
8387}
8388
8389static bool io_register_op_must_quiesce(int op)
8390{
8391 switch (op) {
8392 case IORING_UNREGISTER_FILES:
8393 case IORING_REGISTER_FILES_UPDATE:
8394 case IORING_REGISTER_PROBE:
8395 case IORING_REGISTER_PERSONALITY:
8396 case IORING_UNREGISTER_PERSONALITY:
8397 return false;
8398 default:
8399 return true;
8400 }
8401}
8402
Jens Axboeedafcce2019-01-09 09:16:05 -07008403static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8404 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008405 __releases(ctx->uring_lock)
8406 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008407{
8408 int ret;
8409
Jens Axboe35fa71a2019-04-22 10:23:23 -06008410 /*
8411 * We're inside the ring mutex, if the ref is already dying, then
8412 * someone else killed the ctx or is already going through
8413 * io_uring_register().
8414 */
8415 if (percpu_ref_is_dying(&ctx->refs))
8416 return -ENXIO;
8417
Jens Axboe071698e2020-01-28 10:04:42 -07008418 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008419 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008420
Jens Axboe05f3fb32019-12-09 11:22:50 -07008421 /*
8422 * Drop uring mutex before waiting for references to exit. If
8423 * another thread is currently inside io_uring_enter() it might
8424 * need to grab the uring_lock to make progress. If we hold it
8425 * here across the drain wait, then we can deadlock. It's safe
8426 * to drop the mutex here, since no new references will come in
8427 * after we've killed the percpu ref.
8428 */
8429 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008430 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008431 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008432 if (ret) {
8433 percpu_ref_resurrect(&ctx->refs);
8434 ret = -EINTR;
8435 goto out;
8436 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008437 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008438
8439 switch (opcode) {
8440 case IORING_REGISTER_BUFFERS:
8441 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8442 break;
8443 case IORING_UNREGISTER_BUFFERS:
8444 ret = -EINVAL;
8445 if (arg || nr_args)
8446 break;
8447 ret = io_sqe_buffer_unregister(ctx);
8448 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008449 case IORING_REGISTER_FILES:
8450 ret = io_sqe_files_register(ctx, arg, nr_args);
8451 break;
8452 case IORING_UNREGISTER_FILES:
8453 ret = -EINVAL;
8454 if (arg || nr_args)
8455 break;
8456 ret = io_sqe_files_unregister(ctx);
8457 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008458 case IORING_REGISTER_FILES_UPDATE:
8459 ret = io_sqe_files_update(ctx, arg, nr_args);
8460 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008461 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008462 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008463 ret = -EINVAL;
8464 if (nr_args != 1)
8465 break;
8466 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008467 if (ret)
8468 break;
8469 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8470 ctx->eventfd_async = 1;
8471 else
8472 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008473 break;
8474 case IORING_UNREGISTER_EVENTFD:
8475 ret = -EINVAL;
8476 if (arg || nr_args)
8477 break;
8478 ret = io_eventfd_unregister(ctx);
8479 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008480 case IORING_REGISTER_PROBE:
8481 ret = -EINVAL;
8482 if (!arg || nr_args > 256)
8483 break;
8484 ret = io_probe(ctx, arg, nr_args);
8485 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008486 case IORING_REGISTER_PERSONALITY:
8487 ret = -EINVAL;
8488 if (arg || nr_args)
8489 break;
8490 ret = io_register_personality(ctx);
8491 break;
8492 case IORING_UNREGISTER_PERSONALITY:
8493 ret = -EINVAL;
8494 if (arg)
8495 break;
8496 ret = io_unregister_personality(ctx, nr_args);
8497 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008498 default:
8499 ret = -EINVAL;
8500 break;
8501 }
8502
Jens Axboe071698e2020-01-28 10:04:42 -07008503 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008504 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008505 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008506out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008507 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008508 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008509 return ret;
8510}
8511
8512SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8513 void __user *, arg, unsigned int, nr_args)
8514{
8515 struct io_ring_ctx *ctx;
8516 long ret = -EBADF;
8517 struct fd f;
8518
8519 f = fdget(fd);
8520 if (!f.file)
8521 return -EBADF;
8522
8523 ret = -EOPNOTSUPP;
8524 if (f.file->f_op != &io_uring_fops)
8525 goto out_fput;
8526
8527 ctx = f.file->private_data;
8528
8529 mutex_lock(&ctx->uring_lock);
8530 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8531 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008532 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8533 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008534out_fput:
8535 fdput(f);
8536 return ret;
8537}
8538
Jens Axboe2b188cc2019-01-07 10:46:33 -07008539static int __init io_uring_init(void)
8540{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008541#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8542 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8543 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8544} while (0)
8545
8546#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8547 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8548 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8549 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8550 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8551 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8552 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8553 BUILD_BUG_SQE_ELEM(8, __u64, off);
8554 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8555 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008556 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008557 BUILD_BUG_SQE_ELEM(24, __u32, len);
8558 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8559 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8560 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8561 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008562 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8563 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008564 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8565 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8566 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8567 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8568 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8569 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8570 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8571 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008572 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008573 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8574 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8575 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008576 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008577
Jens Axboed3656342019-12-18 09:50:26 -07008578 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008579 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008580 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8581 return 0;
8582};
8583__initcall(io_uring_init);