blob: 2e44b378826564f547102c4212e1c4cf0e9454e1 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200145 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application.
149 *
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
152 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000153 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200155 * Runtime CQ flags
156 *
157 * Written by the application, shouldn't be modified by the
158 * kernel.
159 */
160 u32 cq_flags;
161 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 * Number of completion events lost because the queue was full;
163 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800164 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * the completion queue.
166 *
167 * Written by the kernel, shouldn't be modified by the
168 * application (i.e. get number of "new events" by comparing to
169 * cached value).
170 *
171 * As completion events come in out of order this counter is not
172 * ordered with any other data.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200175 /*
176 * Ring buffer of completion events.
177 *
178 * The kernel writes completion events fresh every time they are
179 * produced, so the application is allowed to modify pending
180 * entries.
181 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000182 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700183};
184
Jens Axboeedafcce2019-01-09 09:16:05 -0700185struct io_mapped_ubuf {
186 u64 ubuf;
187 size_t len;
188 struct bio_vec *bvec;
189 unsigned int nr_bvecs;
190};
191
Jens Axboe65e19f52019-10-26 07:20:21 -0600192struct fixed_file_table {
193 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700194};
195
Xiaoguang Wang05589552020-03-31 14:05:18 +0800196struct fixed_file_ref_node {
197 struct percpu_ref refs;
198 struct list_head node;
199 struct list_head file_list;
200 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600201 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202};
203
Jens Axboe05f3fb32019-12-09 11:22:50 -0700204struct fixed_file_data {
205 struct fixed_file_table *table;
206 struct io_ring_ctx *ctx;
207
Xiaoguang Wang05589552020-03-31 14:05:18 +0800208 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700210 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct list_head ref_list;
212 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213};
214
Jens Axboe5a2e7452020-02-23 16:23:11 -0700215struct io_buffer {
216 struct list_head list;
217 __u64 addr;
218 __s32 len;
219 __u16 bid;
220};
221
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222struct io_ring_ctx {
223 struct {
224 struct percpu_ref refs;
225 } ____cacheline_aligned_in_smp;
226
227 struct {
228 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800229 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700230 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800231 unsigned int cq_overflow_flushed: 1;
232 unsigned int drain_next: 1;
233 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Hristo Venev75b28af2019-08-26 17:23:46 +0000235 /*
236 * Ring buffer of indices into array of io_uring_sqe, which is
237 * mmapped by the application using the IORING_OFF_SQES offset.
238 *
239 * This indirection could e.g. be used to assign fixed
240 * io_uring_sqe entries to operations and only submit them to
241 * the queue when needed.
242 *
243 * The kernel modifies neither the indices array nor the entries
244 * array.
245 */
246 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700247 unsigned cached_sq_head;
248 unsigned sq_entries;
249 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700250 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600251 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700252 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700253 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600254
255 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600256 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700257 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258
Jens Axboefcb323c2019-10-24 12:39:47 -0600259 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700260 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 } ____cacheline_aligned_in_smp;
262
Hristo Venev75b28af2019-08-26 17:23:46 +0000263 struct io_rings *rings;
264
Jens Axboe2b188cc2019-01-07 10:46:33 -0700265 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600266 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700267 struct task_struct *sqo_thread; /* if using sq thread polling */
268 struct mm_struct *sqo_mm;
269 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270
Jens Axboe6b063142019-01-10 22:13:58 -0700271 /*
272 * If used, fixed file set. Writers must ensure that ->refs is dead,
273 * readers must ensure that ->refs is alive as long as the file* is
274 * used. Only updated through io_uring_register(2).
275 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700276 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700277 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300278 int ring_fd;
279 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700280
Jens Axboeedafcce2019-01-09 09:16:05 -0700281 /* if used, fixed mapped user buffers */
282 unsigned nr_user_bufs;
283 struct io_mapped_ubuf *user_bufs;
284
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285 struct user_struct *user;
286
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700287 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700288
Jens Axboe0f158b42020-05-14 17:18:39 -0600289 struct completion ref_comp;
290 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700291
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700292 /* if all else fails... */
293 struct io_kiocb *fallback_req;
294
Jens Axboe206aefd2019-11-07 18:27:42 -0700295#if defined(CONFIG_UNIX)
296 struct socket *ring_sock;
297#endif
298
Jens Axboe5a2e7452020-02-23 16:23:11 -0700299 struct idr io_buffer_idr;
300
Jens Axboe071698e2020-01-28 10:04:42 -0700301 struct idr personality_idr;
302
Jens Axboe206aefd2019-11-07 18:27:42 -0700303 struct {
304 unsigned cached_cq_tail;
305 unsigned cq_entries;
306 unsigned cq_mask;
307 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700308 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700309 struct wait_queue_head cq_wait;
310 struct fasync_struct *cq_fasync;
311 struct eventfd_ctx *cq_ev_fd;
312 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
314 struct {
315 struct mutex uring_lock;
316 wait_queue_head_t wait;
317 } ____cacheline_aligned_in_smp;
318
319 struct {
320 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700321
Jens Axboedef596e2019-01-09 08:59:42 -0700322 /*
323 * ->poll_list is protected by the ctx->uring_lock for
324 * io_uring instances that don't use IORING_SETUP_SQPOLL.
325 * For SQPOLL, only the single threaded io_sq_thread() will
326 * manipulate the list, hence no extra locking is needed there.
327 */
328 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700329 struct hlist_head *cancel_hash;
330 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700331 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600332
333 spinlock_t inflight_lock;
334 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600336
Jens Axboe4a38aed22020-05-14 17:21:15 -0600337 struct delayed_work file_put_work;
338 struct llist_head file_put_llist;
339
Jens Axboe85faa7b2020-04-09 18:14:00 -0600340 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341};
342
Jens Axboe09bb8392019-03-13 12:39:28 -0600343/*
344 * First field must be the file pointer in all the
345 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
346 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700347struct io_poll_iocb {
348 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700349 union {
350 struct wait_queue_head *head;
351 u64 addr;
352 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700353 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600354 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700355 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700356 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700357};
358
Jens Axboeb5dba592019-12-11 14:02:38 -0700359struct io_close {
360 struct file *file;
361 struct file *put_file;
362 int fd;
363};
364
Jens Axboead8a48a2019-11-15 08:49:11 -0700365struct io_timeout_data {
366 struct io_kiocb *req;
367 struct hrtimer timer;
368 struct timespec64 ts;
369 enum hrtimer_mode mode;
370};
371
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700372struct io_accept {
373 struct file *file;
374 struct sockaddr __user *addr;
375 int __user *addr_len;
376 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600377 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700378};
379
380struct io_sync {
381 struct file *file;
382 loff_t len;
383 loff_t off;
384 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700385 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700386};
387
Jens Axboefbf23842019-12-17 18:45:56 -0700388struct io_cancel {
389 struct file *file;
390 u64 addr;
391};
392
Jens Axboeb29472e2019-12-17 18:50:29 -0700393struct io_timeout {
394 struct file *file;
395 u64 addr;
396 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300397 u32 off;
398 u32 target_seq;
Jens Axboeb29472e2019-12-17 18:50:29 -0700399};
400
Jens Axboe9adbd452019-12-20 08:45:55 -0700401struct io_rw {
402 /* NOTE: kiocb has the file as the first member, so don't do it here */
403 struct kiocb kiocb;
404 u64 addr;
405 u64 len;
406};
407
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700408struct io_connect {
409 struct file *file;
410 struct sockaddr __user *addr;
411 int addr_len;
412};
413
Jens Axboee47293f2019-12-20 08:58:21 -0700414struct io_sr_msg {
415 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700416 union {
417 struct user_msghdr __user *msg;
418 void __user *buf;
419 };
Jens Axboee47293f2019-12-20 08:58:21 -0700420 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700421 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700422 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700423 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700424};
425
Jens Axboe15b71ab2019-12-11 11:20:36 -0700426struct io_open {
427 struct file *file;
428 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700429 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700430 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600431 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700432};
433
Jens Axboe05f3fb32019-12-09 11:22:50 -0700434struct io_files_update {
435 struct file *file;
436 u64 arg;
437 u32 nr_args;
438 u32 offset;
439};
440
Jens Axboe4840e412019-12-25 22:03:45 -0700441struct io_fadvise {
442 struct file *file;
443 u64 offset;
444 u32 len;
445 u32 advice;
446};
447
Jens Axboec1ca7572019-12-25 22:18:28 -0700448struct io_madvise {
449 struct file *file;
450 u64 addr;
451 u32 len;
452 u32 advice;
453};
454
Jens Axboe3e4827b2020-01-08 15:18:09 -0700455struct io_epoll {
456 struct file *file;
457 int epfd;
458 int op;
459 int fd;
460 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461};
462
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300463struct io_splice {
464 struct file *file_out;
465 struct file *file_in;
466 loff_t off_out;
467 loff_t off_in;
468 u64 len;
469 unsigned int flags;
470};
471
Jens Axboeddf0322d2020-02-23 16:41:33 -0700472struct io_provide_buf {
473 struct file *file;
474 __u64 addr;
475 __s32 len;
476 __u32 bgid;
477 __u16 nbufs;
478 __u16 bid;
479};
480
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700481struct io_statx {
482 struct file *file;
483 int dfd;
484 unsigned int mask;
485 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700486 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700487 struct statx __user *buffer;
488};
489
Jens Axboef499a022019-12-02 16:28:46 -0700490struct io_async_connect {
491 struct sockaddr_storage address;
492};
493
Jens Axboe03b12302019-12-02 18:50:25 -0700494struct io_async_msghdr {
495 struct iovec fast_iov[UIO_FASTIOV];
496 struct iovec *iov;
497 struct sockaddr __user *uaddr;
498 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700499 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700500};
501
Jens Axboef67676d2019-12-02 11:03:47 -0700502struct io_async_rw {
503 struct iovec fast_iov[UIO_FASTIOV];
504 struct iovec *iov;
505 ssize_t nr_segs;
506 ssize_t size;
Jens Axboebcf5a062020-05-22 09:24:42 -0600507 struct wait_page_queue wpq;
508 struct callback_head task_work;
Jens Axboef67676d2019-12-02 11:03:47 -0700509};
510
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700511struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700512 union {
513 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700514 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700515 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700516 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700517 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700518};
519
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300520enum {
521 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
522 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
523 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
524 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
525 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700526 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300527
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300528 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300529 REQ_F_LINK_NEXT_BIT,
530 REQ_F_FAIL_LINK_BIT,
531 REQ_F_INFLIGHT_BIT,
532 REQ_F_CUR_POS_BIT,
533 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300534 REQ_F_LINK_TIMEOUT_BIT,
535 REQ_F_TIMEOUT_BIT,
536 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300537 REQ_F_TIMEOUT_NOSEQ_BIT,
538 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300539 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700540 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700541 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700542 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600543 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300544 REQ_F_QUEUE_TIMEOUT_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800545 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300546 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700547
548 /* not a real bit, just to check we're not overflowing the space */
549 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300550};
551
552enum {
553 /* ctx owns file */
554 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
555 /* drain existing IO first */
556 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
557 /* linked sqes */
558 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
559 /* doesn't sever on completion < 0 */
560 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
561 /* IOSQE_ASYNC */
562 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700563 /* IOSQE_BUFFER_SELECT */
564 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300565
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300566 /* head of a link */
567 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568 /* already grabbed next link */
569 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
570 /* fail rest of links */
571 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
572 /* on inflight list */
573 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
574 /* read/write uses file position */
575 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
576 /* must not punt to workers */
577 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300578 /* has linked timeout */
579 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
580 /* timeout request */
581 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
582 /* regular file */
583 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300584 /* no timeout sequence */
585 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
586 /* completion under lock */
587 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300588 /* needs cleanup */
589 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700590 /* in overflow list */
591 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700592 /* already went through poll handler */
593 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700594 /* buffer already selected */
595 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600596 /* doesn't need file table for this request */
597 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300598 /* needs to queue linked timeout */
599 REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800600 /* io_wq_work is initialized */
601 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300602 /* req->task is refcounted */
603 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700604};
605
606struct async_poll {
607 struct io_poll_iocb poll;
608 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609};
610
Jens Axboe09bb8392019-03-13 12:39:28 -0600611/*
612 * NOTE! Each of the iocb union members has the file pointer
613 * as the first entry in their struct definition. So you can
614 * access the file pointer through any of the sub-structs,
615 * or directly as just 'ki_filp' in this struct.
616 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700618 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600619 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700620 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700621 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700622 struct io_accept accept;
623 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700624 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700625 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700626 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700627 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700628 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700629 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700630 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700631 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700632 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700633 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300634 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700635 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700636 struct io_statx statx;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700637 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700639 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300640 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700641 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800642 /* polled IO has completed */
643 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700645 u16 buf_index;
646
Jens Axboe2b188cc2019-01-07 10:46:33 -0700647 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700648 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700650 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600651 struct task_struct *task;
652 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700653 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600654 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600655 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700656
Jens Axboed7718a92020-02-14 22:23:12 -0700657 struct list_head link_list;
658
Jens Axboefcb323c2019-10-24 12:39:47 -0600659 struct list_head inflight_entry;
660
Xiaoguang Wang05589552020-03-31 14:05:18 +0800661 struct percpu_ref *fixed_file_refs;
662
Jens Axboeb41e9852020-02-17 09:52:41 -0700663 union {
664 /*
665 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700666 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
667 * async armed poll handlers for regular commands. The latter
668 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700669 */
670 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700671 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700672 struct hlist_node hash_node;
673 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700674 };
675 struct io_wq_work work;
676 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700677};
678
Jens Axboedef596e2019-01-09 08:59:42 -0700679#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700680
Jens Axboe013538b2020-06-22 09:29:15 -0600681struct io_comp_state {
682 unsigned int nr;
683 struct list_head list;
684 struct io_ring_ctx *ctx;
685};
686
Jens Axboe9a56a232019-01-09 09:06:50 -0700687struct io_submit_state {
688 struct blk_plug plug;
689
690 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700691 * io_kiocb alloc cache
692 */
693 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300694 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700695
696 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600697 * Batch completion logic
698 */
699 struct io_comp_state comp;
700
701 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700702 * File reference cache
703 */
704 struct file *file;
705 unsigned int fd;
706 unsigned int has_refs;
707 unsigned int used_refs;
708 unsigned int ios_left;
709};
710
Jens Axboed3656342019-12-18 09:50:26 -0700711struct io_op_def {
712 /* needs req->io allocated for deferral/async */
713 unsigned async_ctx : 1;
714 /* needs current->mm setup, does mm access */
715 unsigned needs_mm : 1;
716 /* needs req->file assigned */
717 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600718 /* don't fail if file grab fails */
719 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700720 /* hash wq insertion if file is a regular file */
721 unsigned hash_reg_file : 1;
722 /* unbound wq insertion if file is a non-regular file */
723 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700724 /* opcode is not supported by this kernel */
725 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700726 /* needs file table */
727 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700728 /* needs ->fs */
729 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700730 /* set if opcode supports polled "wait" */
731 unsigned pollin : 1;
732 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700733 /* op supports buffer selection */
734 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700735};
736
737static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_NOP] = {},
739 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700740 .async_ctx = 1,
741 .needs_mm = 1,
742 .needs_file = 1,
743 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700744 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700745 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .async_ctx = 1,
749 .needs_mm = 1,
750 .needs_file = 1,
751 .hash_reg_file = 1,
752 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700753 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700756 .needs_file = 1,
757 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300758 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700759 .needs_file = 1,
760 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700761 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700762 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700764 .needs_file = 1,
765 .hash_reg_file = 1,
766 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700767 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .needs_file = 1,
771 .unbound_nonreg_file = 1,
772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_POLL_REMOVE] = {},
774 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_file = 1,
776 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300777 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .async_ctx = 1,
779 .needs_mm = 1,
780 .needs_file = 1,
781 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700782 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700783 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .async_ctx = 1,
787 .needs_mm = 1,
788 .needs_file = 1,
789 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700790 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700791 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700792 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700793 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300794 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700795 .async_ctx = 1,
796 .needs_mm = 1,
797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_TIMEOUT_REMOVE] = {},
799 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700800 .needs_mm = 1,
801 .needs_file = 1,
802 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700803 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700804 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700805 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300806 [IORING_OP_ASYNC_CANCEL] = {},
807 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700808 .async_ctx = 1,
809 .needs_mm = 1,
810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700812 .async_ctx = 1,
813 .needs_mm = 1,
814 .needs_file = 1,
815 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700816 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .needs_file = 1,
820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700822 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700823 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600826 .needs_file = 1,
827 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700828 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700831 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700832 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700833 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700835 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700836 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600837 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700838 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300839 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700840 .needs_mm = 1,
841 .needs_file = 1,
842 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700843 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700844 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700847 .needs_mm = 1,
848 .needs_file = 1,
849 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700850 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700853 .needs_file = 1,
854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700856 .needs_mm = 1,
857 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300858 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700859 .needs_mm = 1,
860 .needs_file = 1,
861 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700862 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700865 .needs_mm = 1,
866 .needs_file = 1,
867 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700868 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700869 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700872 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700873 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700874 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700875 [IORING_OP_EPOLL_CTL] = {
876 .unbound_nonreg_file = 1,
877 .file_table = 1,
878 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300879 [IORING_OP_SPLICE] = {
880 .needs_file = 1,
881 .hash_reg_file = 1,
882 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700883 },
884 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700885 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300886 [IORING_OP_TEE] = {
887 .needs_file = 1,
888 .hash_reg_file = 1,
889 .unbound_nonreg_file = 1,
890 },
Jens Axboed3656342019-12-18 09:50:26 -0700891};
892
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700893enum io_mem_account {
894 ACCT_LOCKED,
895 ACCT_PINNED,
896};
897
Jens Axboe78e19bb2019-11-06 15:21:34 -0700898static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800899static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600900static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700901static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700902static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
903static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700904static int __io_sqe_files_update(struct io_ring_ctx *ctx,
905 struct io_uring_files_update *ip,
906 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700907static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300908static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700909static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
910 int fd, struct file **out_file, bool fixed);
911static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600912 const struct io_uring_sqe *sqe,
913 struct io_comp_state *cs);
Jens Axboede0617e2019-04-06 21:51:27 -0600914
Jens Axboeb63534c2020-06-04 11:28:00 -0600915static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
916 struct iovec **iovec, struct iov_iter *iter,
917 bool needs_lock);
918static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
919 struct iovec *iovec, struct iovec *fast_iov,
920 struct iov_iter *iter);
921
Jens Axboe2b188cc2019-01-07 10:46:33 -0700922static struct kmem_cache *req_cachep;
923
924static const struct file_operations io_uring_fops;
925
926struct sock *io_uring_get_socket(struct file *file)
927{
928#if defined(CONFIG_UNIX)
929 if (file->f_op == &io_uring_fops) {
930 struct io_ring_ctx *ctx = file->private_data;
931
932 return ctx->ring_sock->sk;
933 }
934#endif
935 return NULL;
936}
937EXPORT_SYMBOL(io_uring_get_socket);
938
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300939static void io_get_req_task(struct io_kiocb *req)
940{
941 if (req->flags & REQ_F_TASK_PINNED)
942 return;
943 get_task_struct(req->task);
944 req->flags |= REQ_F_TASK_PINNED;
945}
946
947/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
948static void __io_put_req_task(struct io_kiocb *req)
949{
950 if (req->flags & REQ_F_TASK_PINNED)
951 put_task_struct(req->task);
952}
953
Jens Axboec40f6372020-06-25 15:39:59 -0600954static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
955{
956 struct mm_struct *mm = current->mm;
957
958 if (mm) {
959 kthread_unuse_mm(mm);
960 mmput(mm);
961 }
962}
963
964static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
965{
966 if (!current->mm) {
967 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
968 return -EFAULT;
969 kthread_use_mm(ctx->sqo_mm);
970 }
971
972 return 0;
973}
974
975static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
976 struct io_kiocb *req)
977{
978 if (!io_op_defs[req->opcode].needs_mm)
979 return 0;
980 return __io_sq_thread_acquire_mm(ctx);
981}
982
983static inline void req_set_fail_links(struct io_kiocb *req)
984{
985 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
986 req->flags |= REQ_F_FAIL_LINK;
987}
988
Jens Axboe4a38aed22020-05-14 17:21:15 -0600989static void io_file_put_work(struct work_struct *work);
990
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800991/*
992 * Note: must call io_req_init_async() for the first time you
993 * touch any members of io_wq_work.
994 */
995static inline void io_req_init_async(struct io_kiocb *req)
996{
997 if (req->flags & REQ_F_WORK_INITIALIZED)
998 return;
999
1000 memset(&req->work, 0, sizeof(req->work));
1001 req->flags |= REQ_F_WORK_INITIALIZED;
1002}
1003
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001004static inline bool io_async_submit(struct io_ring_ctx *ctx)
1005{
1006 return ctx->flags & IORING_SETUP_SQPOLL;
1007}
1008
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1010{
1011 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1012
Jens Axboe0f158b42020-05-14 17:18:39 -06001013 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001014}
1015
1016static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1017{
1018 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001019 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001020
1021 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1022 if (!ctx)
1023 return NULL;
1024
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001025 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1026 if (!ctx->fallback_req)
1027 goto err;
1028
Jens Axboe78076bb2019-12-04 19:56:40 -07001029 /*
1030 * Use 5 bits less than the max cq entries, that should give us around
1031 * 32 entries per hash list if totally full and uniformly spread.
1032 */
1033 hash_bits = ilog2(p->cq_entries);
1034 hash_bits -= 5;
1035 if (hash_bits <= 0)
1036 hash_bits = 1;
1037 ctx->cancel_hash_bits = hash_bits;
1038 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1039 GFP_KERNEL);
1040 if (!ctx->cancel_hash)
1041 goto err;
1042 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1043
Roman Gushchin21482892019-05-07 10:01:48 -07001044 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001045 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1046 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047
1048 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001049 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001050 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001051 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001052 init_completion(&ctx->ref_comp);
1053 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001054 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001055 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056 mutex_init(&ctx->uring_lock);
1057 init_waitqueue_head(&ctx->wait);
1058 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001059 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001060 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001061 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001062 init_waitqueue_head(&ctx->inflight_wait);
1063 spin_lock_init(&ctx->inflight_lock);
1064 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001065 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1066 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001067 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001068err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001069 if (ctx->fallback_req)
1070 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001071 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001072 kfree(ctx);
1073 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074}
1075
Bob Liu9d858b22019-11-13 18:06:25 +08001076static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06001077{
Jackie Liua197f662019-11-08 08:09:12 -07001078 struct io_ring_ctx *ctx = req->ctx;
1079
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001080 return req->sequence != ctx->cached_cq_tail
1081 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -06001082}
1083
Bob Liu9d858b22019-11-13 18:06:25 +08001084static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001085{
Pavel Begunkov87987892020-01-18 01:22:30 +03001086 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +08001087 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001088
Bob Liu9d858b22019-11-13 18:06:25 +08001089 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001090}
1091
Jens Axboede0617e2019-04-06 21:51:27 -06001092static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001093{
Hristo Venev75b28af2019-08-26 17:23:46 +00001094 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095
Pavel Begunkov07910152020-01-17 03:52:46 +03001096 /* order cqe stores with ring update */
1097 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098
Pavel Begunkov07910152020-01-17 03:52:46 +03001099 if (wq_has_sleeper(&ctx->cq_wait)) {
1100 wake_up_interruptible(&ctx->cq_wait);
1101 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102 }
1103}
1104
Jens Axboecccf0ee2020-01-27 16:34:48 -07001105static inline void io_req_work_grab_env(struct io_kiocb *req,
1106 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001107{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001108 if (!req->work.mm && def->needs_mm) {
1109 mmgrab(current->mm);
1110 req->work.mm = current->mm;
1111 }
1112 if (!req->work.creds)
1113 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001114 if (!req->work.fs && def->needs_fs) {
1115 spin_lock(&current->fs->lock);
1116 if (!current->fs->in_exec) {
1117 req->work.fs = current->fs;
1118 req->work.fs->users++;
1119 } else {
1120 req->work.flags |= IO_WQ_WORK_CANCEL;
1121 }
1122 spin_unlock(&current->fs->lock);
1123 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001124}
1125
1126static inline void io_req_work_drop_env(struct io_kiocb *req)
1127{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001128 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1129 return;
1130
Jens Axboecccf0ee2020-01-27 16:34:48 -07001131 if (req->work.mm) {
1132 mmdrop(req->work.mm);
1133 req->work.mm = NULL;
1134 }
1135 if (req->work.creds) {
1136 put_cred(req->work.creds);
1137 req->work.creds = NULL;
1138 }
Jens Axboeff002b32020-02-07 16:05:21 -07001139 if (req->work.fs) {
1140 struct fs_struct *fs = req->work.fs;
1141
1142 spin_lock(&req->work.fs->lock);
1143 if (--fs->users)
1144 fs = NULL;
1145 spin_unlock(&req->work.fs->lock);
1146 if (fs)
1147 free_fs_struct(fs);
1148 }
Jens Axboe561fb042019-10-24 07:25:42 -06001149}
1150
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001151static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001152 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001153{
Jens Axboed3656342019-12-18 09:50:26 -07001154 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001155
Jens Axboed3656342019-12-18 09:50:26 -07001156 if (req->flags & REQ_F_ISREG) {
1157 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001158 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001159 } else {
1160 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001161 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001162 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001163
Pavel Begunkov59960b92020-06-15 16:36:30 +03001164 io_req_init_async(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001165 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001166
Jens Axboe94ae5e72019-11-14 19:39:52 -07001167 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001168}
1169
Jackie Liua197f662019-11-08 08:09:12 -07001170static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001171{
Jackie Liua197f662019-11-08 08:09:12 -07001172 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001173 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001174
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001175 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001176
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001177 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1178 &req->work, req->flags);
1179 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001180
1181 if (link)
1182 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001183}
1184
Jens Axboe5262f562019-09-17 12:26:57 -06001185static void io_kill_timeout(struct io_kiocb *req)
1186{
1187 int ret;
1188
Jens Axboe2d283902019-12-04 11:08:05 -07001189 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001190 if (ret != -1) {
1191 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001192 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001193 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001194 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001195 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001196 }
1197}
1198
1199static void io_kill_timeouts(struct io_ring_ctx *ctx)
1200{
1201 struct io_kiocb *req, *tmp;
1202
1203 spin_lock_irq(&ctx->completion_lock);
1204 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1205 io_kill_timeout(req);
1206 spin_unlock_irq(&ctx->completion_lock);
1207}
1208
Pavel Begunkov04518942020-05-26 20:34:05 +03001209static void __io_queue_deferred(struct io_ring_ctx *ctx)
1210{
1211 do {
1212 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1213 struct io_kiocb, list);
1214
1215 if (req_need_defer(req))
1216 break;
1217 list_del_init(&req->list);
1218 io_queue_async_work(req);
1219 } while (!list_empty(&ctx->defer_list));
1220}
1221
Pavel Begunkov360428f2020-05-30 14:54:17 +03001222static void io_flush_timeouts(struct io_ring_ctx *ctx)
1223{
1224 while (!list_empty(&ctx->timeout_list)) {
1225 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1226 struct io_kiocb, list);
1227
1228 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1229 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001230 if (req->timeout.target_seq != ctx->cached_cq_tail
1231 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001232 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001233
Pavel Begunkov360428f2020-05-30 14:54:17 +03001234 list_del_init(&req->list);
1235 io_kill_timeout(req);
1236 }
1237}
1238
Jens Axboede0617e2019-04-06 21:51:27 -06001239static void io_commit_cqring(struct io_ring_ctx *ctx)
1240{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001241 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001242 __io_commit_cqring(ctx);
1243
Pavel Begunkov04518942020-05-26 20:34:05 +03001244 if (unlikely(!list_empty(&ctx->defer_list)))
1245 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001246}
1247
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1249{
Hristo Venev75b28af2019-08-26 17:23:46 +00001250 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251 unsigned tail;
1252
1253 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001254 /*
1255 * writes to the cq entry need to come after reading head; the
1256 * control dependency is enough as we're using WRITE_ONCE to
1257 * fill the cq entry
1258 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001259 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001260 return NULL;
1261
1262 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001263 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264}
1265
Jens Axboef2842ab2020-01-08 11:04:00 -07001266static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1267{
Jens Axboef0b493e2020-02-01 21:30:11 -07001268 if (!ctx->cq_ev_fd)
1269 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001270 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1271 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001272 if (!ctx->eventfd_async)
1273 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001274 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001275}
1276
Jens Axboeb41e9852020-02-17 09:52:41 -07001277static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001278{
1279 if (waitqueue_active(&ctx->wait))
1280 wake_up(&ctx->wait);
1281 if (waitqueue_active(&ctx->sqo_wait))
1282 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001283 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001284 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001285}
1286
Jens Axboec4a2ed72019-11-21 21:01:26 -07001287/* Returns true if there are no backlogged entries after the flush */
1288static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001289{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001290 struct io_rings *rings = ctx->rings;
1291 struct io_uring_cqe *cqe;
1292 struct io_kiocb *req;
1293 unsigned long flags;
1294 LIST_HEAD(list);
1295
1296 if (!force) {
1297 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001298 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001299 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1300 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001301 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001302 }
1303
1304 spin_lock_irqsave(&ctx->completion_lock, flags);
1305
1306 /* if force is set, the ring is going away. always drop after that */
1307 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001308 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001309
Jens Axboec4a2ed72019-11-21 21:01:26 -07001310 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001311 while (!list_empty(&ctx->cq_overflow_list)) {
1312 cqe = io_get_cqring(ctx);
1313 if (!cqe && !force)
1314 break;
1315
1316 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1317 list);
1318 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001319 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001320 if (cqe) {
1321 WRITE_ONCE(cqe->user_data, req->user_data);
1322 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001323 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001324 } else {
1325 WRITE_ONCE(ctx->rings->cq_overflow,
1326 atomic_inc_return(&ctx->cached_cq_overflow));
1327 }
1328 }
1329
1330 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001331 if (cqe) {
1332 clear_bit(0, &ctx->sq_check_overflow);
1333 clear_bit(0, &ctx->cq_check_overflow);
1334 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001335 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1336 io_cqring_ev_posted(ctx);
1337
1338 while (!list_empty(&list)) {
1339 req = list_first_entry(&list, struct io_kiocb, list);
1340 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001341 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001342 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001343
1344 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001345}
1346
Jens Axboebcda7ba2020-02-23 16:42:51 -07001347static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001349 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350 struct io_uring_cqe *cqe;
1351
Jens Axboe78e19bb2019-11-06 15:21:34 -07001352 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001353
Jens Axboe2b188cc2019-01-07 10:46:33 -07001354 /*
1355 * If we can't get a cq entry, userspace overflowed the
1356 * submission (by quite a lot). Increment the overflow count in
1357 * the ring.
1358 */
1359 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001360 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001361 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001362 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001363 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001364 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001365 WRITE_ONCE(ctx->rings->cq_overflow,
1366 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001367 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001368 if (list_empty(&ctx->cq_overflow_list)) {
1369 set_bit(0, &ctx->sq_check_overflow);
1370 set_bit(0, &ctx->cq_check_overflow);
1371 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001372 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001373 refcount_inc(&req->refs);
1374 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001375 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001376 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001377 }
1378}
1379
Jens Axboebcda7ba2020-02-23 16:42:51 -07001380static void io_cqring_fill_event(struct io_kiocb *req, long res)
1381{
1382 __io_cqring_fill_event(req, res, 0);
1383}
1384
Jens Axboee1e16092020-06-22 09:17:17 -06001385static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001386{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001387 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001388 unsigned long flags;
1389
1390 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001391 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001392 io_commit_cqring(ctx);
1393 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1394
Jens Axboe8c838782019-03-12 15:48:16 -06001395 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001396}
1397
Jens Axboe229a7b62020-06-22 10:13:11 -06001398static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001399{
Jens Axboe229a7b62020-06-22 10:13:11 -06001400 struct io_ring_ctx *ctx = cs->ctx;
1401
1402 spin_lock_irq(&ctx->completion_lock);
1403 while (!list_empty(&cs->list)) {
1404 struct io_kiocb *req;
1405
1406 req = list_first_entry(&cs->list, struct io_kiocb, list);
1407 list_del(&req->list);
1408 io_cqring_fill_event(req, req->result);
1409 if (!(req->flags & REQ_F_LINK_HEAD)) {
1410 req->flags |= REQ_F_COMP_LOCKED;
1411 io_put_req(req);
1412 } else {
1413 spin_unlock_irq(&ctx->completion_lock);
1414 io_put_req(req);
1415 spin_lock_irq(&ctx->completion_lock);
1416 }
1417 }
1418 io_commit_cqring(ctx);
1419 spin_unlock_irq(&ctx->completion_lock);
1420
1421 io_cqring_ev_posted(ctx);
1422 cs->nr = 0;
1423}
1424
1425static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1426 struct io_comp_state *cs)
1427{
1428 if (!cs) {
1429 io_cqring_add_event(req, res, cflags);
1430 io_put_req(req);
1431 } else {
1432 req->result = res;
1433 list_add_tail(&req->list, &cs->list);
1434 if (++cs->nr >= 32)
1435 io_submit_flush_completions(cs);
1436 }
Jens Axboee1e16092020-06-22 09:17:17 -06001437}
1438
1439static void io_req_complete(struct io_kiocb *req, long res)
1440{
Jens Axboe229a7b62020-06-22 10:13:11 -06001441 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001442}
1443
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001444static inline bool io_is_fallback_req(struct io_kiocb *req)
1445{
1446 return req == (struct io_kiocb *)
1447 ((unsigned long) req->ctx->fallback_req & ~1UL);
1448}
1449
1450static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1451{
1452 struct io_kiocb *req;
1453
1454 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001455 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001456 return req;
1457
1458 return NULL;
1459}
1460
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001461static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1462 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001463{
Jens Axboefd6fab22019-03-14 16:30:06 -06001464 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465 struct io_kiocb *req;
1466
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001467 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001468 size_t sz;
1469 int ret;
1470
1471 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001472 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1473
1474 /*
1475 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1476 * retry single alloc to be on the safe side.
1477 */
1478 if (unlikely(ret <= 0)) {
1479 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1480 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001481 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001482 ret = 1;
1483 }
Jens Axboe2579f912019-01-09 09:10:43 -07001484 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001485 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001486 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001487 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001488 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001489 }
1490
Jens Axboe2579f912019-01-09 09:10:43 -07001491 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001492fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001493 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001494}
1495
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001496static inline void io_put_file(struct io_kiocb *req, struct file *file,
1497 bool fixed)
1498{
1499 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001500 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001501 else
1502 fput(file);
1503}
1504
Jens Axboec6ca97b302019-12-28 12:11:08 -07001505static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001506{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001507 if (req->flags & REQ_F_NEED_CLEANUP)
1508 io_cleanup_req(req);
1509
YueHaibing96fd84d2020-01-07 22:22:44 +08001510 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001511 if (req->file)
1512 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001513 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001514 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001515}
1516
1517static void __io_free_req(struct io_kiocb *req)
1518{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001519 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001520
Jens Axboefcb323c2019-10-24 12:39:47 -06001521 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001522 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001523 unsigned long flags;
1524
1525 spin_lock_irqsave(&ctx->inflight_lock, flags);
1526 list_del(&req->inflight_entry);
1527 if (waitqueue_active(&ctx->inflight_wait))
1528 wake_up(&ctx->inflight_wait);
1529 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1530 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001531
1532 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001533 if (likely(!io_is_fallback_req(req)))
1534 kmem_cache_free(req_cachep, req);
1535 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001536 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001537}
1538
Jens Axboec6ca97b302019-12-28 12:11:08 -07001539struct req_batch {
1540 void *reqs[IO_IOPOLL_BATCH];
1541 int to_free;
1542 int need_iter;
1543};
1544
1545static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1546{
1547 if (!rb->to_free)
1548 return;
1549 if (rb->need_iter) {
1550 int i, inflight = 0;
1551 unsigned long flags;
1552
1553 for (i = 0; i < rb->to_free; i++) {
1554 struct io_kiocb *req = rb->reqs[i];
1555
Jens Axboec6ca97b302019-12-28 12:11:08 -07001556 if (req->flags & REQ_F_INFLIGHT)
1557 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001558 __io_req_aux_free(req);
1559 }
1560 if (!inflight)
1561 goto do_free;
1562
1563 spin_lock_irqsave(&ctx->inflight_lock, flags);
1564 for (i = 0; i < rb->to_free; i++) {
1565 struct io_kiocb *req = rb->reqs[i];
1566
Jens Axboe10fef4b2020-01-09 07:52:28 -07001567 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001568 list_del(&req->inflight_entry);
1569 if (!--inflight)
1570 break;
1571 }
1572 }
1573 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1574
1575 if (waitqueue_active(&ctx->inflight_wait))
1576 wake_up(&ctx->inflight_wait);
1577 }
1578do_free:
1579 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1580 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001581 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001582}
1583
Jackie Liua197f662019-11-08 08:09:12 -07001584static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001585{
Jackie Liua197f662019-11-08 08:09:12 -07001586 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001587 int ret;
1588
Jens Axboe2d283902019-12-04 11:08:05 -07001589 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001590 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001591 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001592 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001593 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001594 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001595 return true;
1596 }
1597
1598 return false;
1599}
1600
Jens Axboeba816ad2019-09-28 11:36:45 -06001601static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001602{
Jens Axboe2665abf2019-11-05 12:40:47 -07001603 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001604 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001605
Jens Axboe4d7dd462019-11-20 13:03:52 -07001606 /* Already got next link */
1607 if (req->flags & REQ_F_LINK_NEXT)
1608 return;
1609
Jens Axboe9e645e112019-05-10 16:07:28 -06001610 /*
1611 * The list should never be empty when we are called here. But could
1612 * potentially happen if the chain is messed up, check to be on the
1613 * safe side.
1614 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001615 while (!list_empty(&req->link_list)) {
1616 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1617 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001618
Pavel Begunkov44932332019-12-05 16:16:35 +03001619 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1620 (nxt->flags & REQ_F_TIMEOUT))) {
1621 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001622 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001623 req->flags &= ~REQ_F_LINK_TIMEOUT;
1624 continue;
1625 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001626
Pavel Begunkov44932332019-12-05 16:16:35 +03001627 list_del_init(&req->link_list);
1628 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001629 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001630 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001631 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001632 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001633
Jens Axboe4d7dd462019-11-20 13:03:52 -07001634 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001635 if (wake_ev)
1636 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001637}
1638
1639/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001640 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001641 */
1642static void io_fail_links(struct io_kiocb *req)
1643{
Jens Axboe2665abf2019-11-05 12:40:47 -07001644 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001645 unsigned long flags;
1646
1647 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001648
1649 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001650 struct io_kiocb *link = list_first_entry(&req->link_list,
1651 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001652
Pavel Begunkov44932332019-12-05 16:16:35 +03001653 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001654 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001655
1656 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001657 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001658 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001659 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001660 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001661 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001662 }
Jens Axboe5d960722019-11-19 15:31:28 -07001663 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001664 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001665
1666 io_commit_cqring(ctx);
1667 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1668 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001669}
1670
Jens Axboe4d7dd462019-11-20 13:03:52 -07001671static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001672{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001673 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001674 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001675
Jens Axboe9e645e112019-05-10 16:07:28 -06001676 /*
1677 * If LINK is set, we have dependent requests in this chain. If we
1678 * didn't fail this request, queue the first one up, moving any other
1679 * dependencies to the next request. In case of failure, fail the rest
1680 * of the chain.
1681 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001682 if (req->flags & REQ_F_FAIL_LINK) {
1683 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001684 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1685 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001686 struct io_ring_ctx *ctx = req->ctx;
1687 unsigned long flags;
1688
1689 /*
1690 * If this is a timeout link, we could be racing with the
1691 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001692 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001693 */
1694 spin_lock_irqsave(&ctx->completion_lock, flags);
1695 io_req_link_next(req, nxt);
1696 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1697 } else {
1698 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001699 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001700}
Jens Axboe9e645e112019-05-10 16:07:28 -06001701
Jens Axboec40f6372020-06-25 15:39:59 -06001702static void __io_req_task_cancel(struct io_kiocb *req, int error)
1703{
1704 struct io_ring_ctx *ctx = req->ctx;
1705
1706 spin_lock_irq(&ctx->completion_lock);
1707 io_cqring_fill_event(req, error);
1708 io_commit_cqring(ctx);
1709 spin_unlock_irq(&ctx->completion_lock);
1710
1711 io_cqring_ev_posted(ctx);
1712 req_set_fail_links(req);
1713 io_double_put_req(req);
1714}
1715
1716static void io_req_task_cancel(struct callback_head *cb)
1717{
1718 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1719
1720 __io_req_task_cancel(req, -ECANCELED);
1721}
1722
1723static void __io_req_task_submit(struct io_kiocb *req)
1724{
1725 struct io_ring_ctx *ctx = req->ctx;
1726
1727 __set_current_state(TASK_RUNNING);
1728 if (!__io_sq_thread_acquire_mm(ctx)) {
1729 mutex_lock(&ctx->uring_lock);
1730 __io_queue_sqe(req, NULL, NULL);
1731 mutex_unlock(&ctx->uring_lock);
1732 } else {
1733 __io_req_task_cancel(req, -EFAULT);
1734 }
1735}
1736
1737static void io_req_task_submit(struct callback_head *cb)
1738{
1739 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1740
1741 __io_req_task_submit(req);
1742}
1743
1744static void io_req_task_queue(struct io_kiocb *req)
1745{
1746 struct task_struct *tsk = req->task;
1747 int ret;
1748
1749 init_task_work(&req->task_work, io_req_task_submit);
1750
1751 ret = task_work_add(tsk, &req->task_work, true);
1752 if (unlikely(ret)) {
1753 init_task_work(&req->task_work, io_req_task_cancel);
1754 tsk = io_wq_get_task(req->ctx->io_wq);
1755 task_work_add(tsk, &req->task_work, true);
1756 }
1757 wake_up_process(tsk);
1758}
1759
Jackie Liuc69f8db2019-11-09 11:00:08 +08001760static void io_free_req(struct io_kiocb *req)
1761{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001762 struct io_kiocb *nxt = NULL;
1763
1764 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001765 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001766
Jens Axboec40f6372020-06-25 15:39:59 -06001767 if (nxt) {
1768 if (nxt->flags & REQ_F_WORK_INITIALIZED)
1769 io_queue_async_work(nxt);
1770 else
1771 io_req_task_queue(nxt);
1772 }
Jackie Liuc69f8db2019-11-09 11:00:08 +08001773}
1774
Jens Axboeba816ad2019-09-28 11:36:45 -06001775/*
1776 * Drop reference to request, return next in chain (if there is one) if this
1777 * was the last reference to this request.
1778 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001779__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001780static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001781{
Jens Axboe2a44f462020-02-25 13:25:41 -07001782 if (refcount_dec_and_test(&req->refs)) {
1783 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001784 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001785 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786}
1787
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788static void io_put_req(struct io_kiocb *req)
1789{
Jens Axboedef596e2019-01-09 08:59:42 -07001790 if (refcount_dec_and_test(&req->refs))
1791 io_free_req(req);
1792}
1793
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001794static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001795{
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001796 struct io_kiocb *link, *nxt = NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001797
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001798 /*
1799 * A ref is owned by io-wq in which context we're. So, if that's the
1800 * last one, it's safe to steal next work. False negatives are Ok,
1801 * it just will be re-punted async in io_put_work()
1802 */
1803 if (refcount_read(&req->refs) != 1)
1804 return NULL;
1805
1806 io_req_find_next(req, &nxt);
1807 if (!nxt)
1808 return NULL;
1809
1810 if ((nxt->flags & REQ_F_ISREG) && io_op_defs[nxt->opcode].hash_reg_file)
1811 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
1812
1813 link = io_prep_linked_timeout(nxt);
1814 if (link)
1815 nxt->flags |= REQ_F_QUEUE_TIMEOUT;
1816 return &nxt->work;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001817}
1818
Jens Axboe978db572019-11-14 22:39:04 -07001819/*
1820 * Must only be used if we don't need to care about links, usually from
1821 * within the completion handling itself.
1822 */
1823static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001824{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001825 /* drop both submit and complete references */
1826 if (refcount_sub_and_test(2, &req->refs))
1827 __io_free_req(req);
1828}
1829
Jens Axboe978db572019-11-14 22:39:04 -07001830static void io_double_put_req(struct io_kiocb *req)
1831{
1832 /* drop both submit and complete references */
1833 if (refcount_sub_and_test(2, &req->refs))
1834 io_free_req(req);
1835}
1836
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001837static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001838{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001839 struct io_rings *rings = ctx->rings;
1840
Jens Axboead3eb2c2019-12-18 17:12:20 -07001841 if (test_bit(0, &ctx->cq_check_overflow)) {
1842 /*
1843 * noflush == true is from the waitqueue handler, just ensure
1844 * we wake up the task, and the next invocation will flush the
1845 * entries. We cannot safely to it from here.
1846 */
1847 if (noflush && !list_empty(&ctx->cq_overflow_list))
1848 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001849
Jens Axboead3eb2c2019-12-18 17:12:20 -07001850 io_cqring_overflow_flush(ctx, false);
1851 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001852
Jens Axboea3a0e432019-08-20 11:03:11 -06001853 /* See comment at the top of this file */
1854 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001855 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001856}
1857
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001858static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1859{
1860 struct io_rings *rings = ctx->rings;
1861
1862 /* make sure SQ entry isn't read before tail */
1863 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1864}
1865
Jens Axboe8237e042019-12-28 10:48:22 -07001866static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001867{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001868 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001869 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001870
Jens Axboe9d9e88a2020-05-13 12:53:19 -06001871 if (req->file || req->io)
Jens Axboec6ca97b302019-12-28 12:11:08 -07001872 rb->need_iter++;
1873
1874 rb->reqs[rb->to_free++] = req;
1875 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1876 io_free_req_many(req->ctx, rb);
1877 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001878}
1879
Jens Axboebcda7ba2020-02-23 16:42:51 -07001880static int io_put_kbuf(struct io_kiocb *req)
1881{
Jens Axboe4d954c22020-02-27 07:31:19 -07001882 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001883 int cflags;
1884
Jens Axboe4d954c22020-02-27 07:31:19 -07001885 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001886 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1887 cflags |= IORING_CQE_F_BUFFER;
1888 req->rw.addr = 0;
1889 kfree(kbuf);
1890 return cflags;
1891}
1892
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001893static void io_iopoll_queue(struct list_head *again)
1894{
1895 struct io_kiocb *req;
1896
1897 do {
1898 req = list_first_entry(again, struct io_kiocb, list);
1899 list_del(&req->list);
1900 refcount_inc(&req->refs);
1901 io_queue_async_work(req);
1902 } while (!list_empty(again));
1903}
1904
Jens Axboedef596e2019-01-09 08:59:42 -07001905/*
1906 * Find and free completed poll iocbs
1907 */
1908static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1909 struct list_head *done)
1910{
Jens Axboe8237e042019-12-28 10:48:22 -07001911 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001912 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001913 LIST_HEAD(again);
1914
1915 /* order with ->result store in io_complete_rw_iopoll() */
1916 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001917
Jens Axboec6ca97b302019-12-28 12:11:08 -07001918 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001919 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001920 int cflags = 0;
1921
Jens Axboedef596e2019-01-09 08:59:42 -07001922 req = list_first_entry(done, struct io_kiocb, list);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001923 if (READ_ONCE(req->result) == -EAGAIN) {
1924 req->iopoll_completed = 0;
1925 list_move_tail(&req->list, &again);
1926 continue;
1927 }
Jens Axboedef596e2019-01-09 08:59:42 -07001928 list_del(&req->list);
1929
Jens Axboebcda7ba2020-02-23 16:42:51 -07001930 if (req->flags & REQ_F_BUFFER_SELECTED)
1931 cflags = io_put_kbuf(req);
1932
1933 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001934 (*nr_events)++;
1935
Jens Axboe8237e042019-12-28 10:48:22 -07001936 if (refcount_dec_and_test(&req->refs) &&
1937 !io_req_multi_free(&rb, req))
1938 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001939 }
Jens Axboedef596e2019-01-09 08:59:42 -07001940
Jens Axboe09bb8392019-03-13 12:39:28 -06001941 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001942 if (ctx->flags & IORING_SETUP_SQPOLL)
1943 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001944 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001945
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001946 if (!list_empty(&again))
1947 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001948}
1949
Jens Axboedef596e2019-01-09 08:59:42 -07001950static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1951 long min)
1952{
1953 struct io_kiocb *req, *tmp;
1954 LIST_HEAD(done);
1955 bool spin;
1956 int ret;
1957
1958 /*
1959 * Only spin for completions if we don't have multiple devices hanging
1960 * off our complete list, and we're under the requested amount.
1961 */
1962 spin = !ctx->poll_multi_file && *nr_events < min;
1963
1964 ret = 0;
1965 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001966 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001967
1968 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001969 * Move completed and retryable entries to our local lists.
1970 * If we find a request that requires polling, break out
1971 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001972 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08001973 if (READ_ONCE(req->iopoll_completed)) {
Jens Axboedef596e2019-01-09 08:59:42 -07001974 list_move_tail(&req->list, &done);
1975 continue;
1976 }
1977 if (!list_empty(&done))
1978 break;
1979
1980 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1981 if (ret < 0)
1982 break;
1983
1984 if (ret && spin)
1985 spin = false;
1986 ret = 0;
1987 }
1988
1989 if (!list_empty(&done))
1990 io_iopoll_complete(ctx, nr_events, &done);
1991
1992 return ret;
1993}
1994
1995/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001996 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001997 * non-spinning poll check - we'll still enter the driver poll loop, but only
1998 * as a non-spinning completion check.
1999 */
2000static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2001 long min)
2002{
Jens Axboe08f54392019-08-21 22:19:11 -06002003 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002004 int ret;
2005
2006 ret = io_do_iopoll(ctx, nr_events, min);
2007 if (ret < 0)
2008 return ret;
2009 if (!min || *nr_events >= min)
2010 return 0;
2011 }
2012
2013 return 1;
2014}
2015
2016/*
2017 * We can't just wait for polled events to come to us, we have to actively
2018 * find and complete them.
2019 */
2020static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
2021{
2022 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2023 return;
2024
2025 mutex_lock(&ctx->uring_lock);
2026 while (!list_empty(&ctx->poll_list)) {
2027 unsigned int nr_events = 0;
2028
2029 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06002030
2031 /*
2032 * Ensure we allow local-to-the-cpu processing to take place,
2033 * in this case we need to ensure that we reap all events.
2034 */
2035 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07002036 }
2037 mutex_unlock(&ctx->uring_lock);
2038}
2039
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002040static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
2041 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002042{
Jens Axboe2b2ed972019-10-25 10:06:15 -06002043 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002044
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002045 /*
2046 * We disallow the app entering submit/complete with polling, but we
2047 * still need to lock the ring to prevent racing with polled issue
2048 * that got punted to a workqueue.
2049 */
2050 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002051 do {
2052 int tmin = 0;
2053
Jens Axboe500f9fb2019-08-19 12:15:59 -06002054 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002055 * Don't enter poll loop if we already have events pending.
2056 * If we do, we can potentially be spinning for commands that
2057 * already triggered a CQE (eg in error).
2058 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002059 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002060 break;
2061
2062 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002063 * If a submit got punted to a workqueue, we can have the
2064 * application entering polling for a command before it gets
2065 * issued. That app will hold the uring_lock for the duration
2066 * of the poll right here, so we need to take a breather every
2067 * now and then to ensure that the issue has a chance to add
2068 * the poll to the issued list. Otherwise we can spin here
2069 * forever, while the workqueue is stuck trying to acquire the
2070 * very same mutex.
2071 */
2072 if (!(++iters & 7)) {
2073 mutex_unlock(&ctx->uring_lock);
2074 mutex_lock(&ctx->uring_lock);
2075 }
2076
Jens Axboedef596e2019-01-09 08:59:42 -07002077 if (*nr_events < min)
2078 tmin = min - *nr_events;
2079
2080 ret = io_iopoll_getevents(ctx, nr_events, tmin);
2081 if (ret <= 0)
2082 break;
2083 ret = 0;
2084 } while (min && !*nr_events && !need_resched());
2085
Jens Axboe500f9fb2019-08-19 12:15:59 -06002086 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002087 return ret;
2088}
2089
Jens Axboe491381ce2019-10-17 09:20:46 -06002090static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091{
Jens Axboe491381ce2019-10-17 09:20:46 -06002092 /*
2093 * Tell lockdep we inherited freeze protection from submission
2094 * thread.
2095 */
2096 if (req->flags & REQ_F_ISREG) {
2097 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002098
Jens Axboe491381ce2019-10-17 09:20:46 -06002099 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002100 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002101 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002102}
2103
Jens Axboea1d7c392020-06-22 11:09:46 -06002104static void io_complete_rw_common(struct kiocb *kiocb, long res,
2105 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002106{
Jens Axboe9adbd452019-12-20 08:45:55 -07002107 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002108 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002109
Jens Axboe491381ce2019-10-17 09:20:46 -06002110 if (kiocb->ki_flags & IOCB_WRITE)
2111 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002112
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002113 if (res != req->result)
2114 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002115 if (req->flags & REQ_F_BUFFER_SELECTED)
2116 cflags = io_put_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002117 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002118}
2119
Jens Axboeb63534c2020-06-04 11:28:00 -06002120#ifdef CONFIG_BLOCK
2121static bool io_resubmit_prep(struct io_kiocb *req, int error)
2122{
2123 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2124 ssize_t ret = -ECANCELED;
2125 struct iov_iter iter;
2126 int rw;
2127
2128 if (error) {
2129 ret = error;
2130 goto end_req;
2131 }
2132
2133 switch (req->opcode) {
2134 case IORING_OP_READV:
2135 case IORING_OP_READ_FIXED:
2136 case IORING_OP_READ:
2137 rw = READ;
2138 break;
2139 case IORING_OP_WRITEV:
2140 case IORING_OP_WRITE_FIXED:
2141 case IORING_OP_WRITE:
2142 rw = WRITE;
2143 break;
2144 default:
2145 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2146 req->opcode);
2147 goto end_req;
2148 }
2149
2150 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2151 if (ret < 0)
2152 goto end_req;
2153 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2154 if (!ret)
2155 return true;
2156 kfree(iovec);
2157end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002158 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002159 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002160 return false;
2161}
2162
2163static void io_rw_resubmit(struct callback_head *cb)
2164{
2165 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2166 struct io_ring_ctx *ctx = req->ctx;
2167 int err;
2168
2169 __set_current_state(TASK_RUNNING);
2170
2171 err = io_sq_thread_acquire_mm(ctx, req);
2172
2173 if (io_resubmit_prep(req, err)) {
2174 refcount_inc(&req->refs);
2175 io_queue_async_work(req);
2176 }
2177}
2178#endif
2179
2180static bool io_rw_reissue(struct io_kiocb *req, long res)
2181{
2182#ifdef CONFIG_BLOCK
2183 struct task_struct *tsk;
2184 int ret;
2185
2186 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2187 return false;
2188
2189 tsk = req->task;
2190 init_task_work(&req->task_work, io_rw_resubmit);
2191 ret = task_work_add(tsk, &req->task_work, true);
2192 if (!ret)
2193 return true;
2194#endif
2195 return false;
2196}
2197
Jens Axboea1d7c392020-06-22 11:09:46 -06002198static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2199 struct io_comp_state *cs)
2200{
2201 if (!io_rw_reissue(req, res))
2202 io_complete_rw_common(&req->rw.kiocb, res, cs);
2203}
2204
Jens Axboeba816ad2019-09-28 11:36:45 -06002205static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2206{
Jens Axboe9adbd452019-12-20 08:45:55 -07002207 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002208
Jens Axboea1d7c392020-06-22 11:09:46 -06002209 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002210}
2211
Jens Axboedef596e2019-01-09 08:59:42 -07002212static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2213{
Jens Axboe9adbd452019-12-20 08:45:55 -07002214 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002215
Jens Axboe491381ce2019-10-17 09:20:46 -06002216 if (kiocb->ki_flags & IOCB_WRITE)
2217 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002218
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002219 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002220 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002221
2222 WRITE_ONCE(req->result, res);
2223 /* order with io_poll_complete() checking ->result */
2224 if (res != -EAGAIN) {
2225 smp_wmb();
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002226 WRITE_ONCE(req->iopoll_completed, 1);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002227 }
Jens Axboedef596e2019-01-09 08:59:42 -07002228}
2229
2230/*
2231 * After the iocb has been issued, it's safe to be found on the poll list.
2232 * Adding the kiocb to the list AFTER submission ensures that we don't
2233 * find it from a io_iopoll_getevents() thread before the issuer is done
2234 * accessing the kiocb cookie.
2235 */
2236static void io_iopoll_req_issued(struct io_kiocb *req)
2237{
2238 struct io_ring_ctx *ctx = req->ctx;
2239
2240 /*
2241 * Track whether we have multiple files in our lists. This will impact
2242 * how we do polling eventually, not spinning if we're on potentially
2243 * different devices.
2244 */
2245 if (list_empty(&ctx->poll_list)) {
2246 ctx->poll_multi_file = false;
2247 } else if (!ctx->poll_multi_file) {
2248 struct io_kiocb *list_req;
2249
2250 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2251 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002252 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002253 ctx->poll_multi_file = true;
2254 }
2255
2256 /*
2257 * For fast devices, IO may have already completed. If it has, add
2258 * it to the front so we find it first.
2259 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002260 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002261 list_add(&req->list, &ctx->poll_list);
2262 else
2263 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002264
2265 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2266 wq_has_sleeper(&ctx->sqo_wait))
2267 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002268}
2269
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002270static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002271{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002272 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002273
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002274 if (diff)
2275 fput_many(state->file, diff);
2276 state->file = NULL;
2277}
2278
2279static inline void io_state_file_put(struct io_submit_state *state)
2280{
2281 if (state->file)
2282 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002283}
2284
2285/*
2286 * Get as many references to a file as we have IOs left in this submission,
2287 * assuming most submissions are for one file, or at least that each file
2288 * has more than one submission.
2289 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002290static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002291{
2292 if (!state)
2293 return fget(fd);
2294
2295 if (state->file) {
2296 if (state->fd == fd) {
2297 state->used_refs++;
2298 state->ios_left--;
2299 return state->file;
2300 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002301 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002302 }
2303 state->file = fget_many(fd, state->ios_left);
2304 if (!state->file)
2305 return NULL;
2306
2307 state->fd = fd;
2308 state->has_refs = state->ios_left;
2309 state->used_refs = 1;
2310 state->ios_left--;
2311 return state->file;
2312}
2313
Jens Axboe4503b762020-06-01 10:00:27 -06002314static bool io_bdev_nowait(struct block_device *bdev)
2315{
2316#ifdef CONFIG_BLOCK
2317 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2318#else
2319 return true;
2320#endif
2321}
2322
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323/*
2324 * If we tracked the file through the SCM inflight mechanism, we could support
2325 * any file. For now, just ensure that anything potentially problematic is done
2326 * inline.
2327 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002328static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002329{
2330 umode_t mode = file_inode(file)->i_mode;
2331
Jens Axboe4503b762020-06-01 10:00:27 -06002332 if (S_ISBLK(mode)) {
2333 if (io_bdev_nowait(file->f_inode->i_bdev))
2334 return true;
2335 return false;
2336 }
2337 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002338 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002339 if (S_ISREG(mode)) {
2340 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2341 file->f_op != &io_uring_fops)
2342 return true;
2343 return false;
2344 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002345
Jens Axboec5b85622020-06-09 19:23:05 -06002346 /* any ->read/write should understand O_NONBLOCK */
2347 if (file->f_flags & O_NONBLOCK)
2348 return true;
2349
Jens Axboeaf197f52020-04-28 13:15:06 -06002350 if (!(file->f_mode & FMODE_NOWAIT))
2351 return false;
2352
2353 if (rw == READ)
2354 return file->f_op->read_iter != NULL;
2355
2356 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002357}
2358
Jens Axboe3529d8c2019-12-19 18:24:38 -07002359static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2360 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002361{
Jens Axboedef596e2019-01-09 08:59:42 -07002362 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002363 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002364 unsigned ioprio;
2365 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002366
Jens Axboe491381ce2019-10-17 09:20:46 -06002367 if (S_ISREG(file_inode(req->file)->i_mode))
2368 req->flags |= REQ_F_ISREG;
2369
Jens Axboe2b188cc2019-01-07 10:46:33 -07002370 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002371 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2372 req->flags |= REQ_F_CUR_POS;
2373 kiocb->ki_pos = req->file->f_pos;
2374 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002375 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002376 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2377 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2378 if (unlikely(ret))
2379 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002380
2381 ioprio = READ_ONCE(sqe->ioprio);
2382 if (ioprio) {
2383 ret = ioprio_check_cap(ioprio);
2384 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002385 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002386
2387 kiocb->ki_ioprio = ioprio;
2388 } else
2389 kiocb->ki_ioprio = get_current_ioprio();
2390
Stefan Bühler8449eed2019-04-27 20:34:19 +02002391 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002392 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002393 req->flags |= REQ_F_NOWAIT;
2394
Jens Axboeb63534c2020-06-04 11:28:00 -06002395 if (kiocb->ki_flags & IOCB_DIRECT)
2396 io_get_req_task(req);
2397
Stefan Bühler8449eed2019-04-27 20:34:19 +02002398 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002399 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002400
Jens Axboedef596e2019-01-09 08:59:42 -07002401 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002402 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2403 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002404 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002405
Jens Axboedef596e2019-01-09 08:59:42 -07002406 kiocb->ki_flags |= IOCB_HIPRI;
2407 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002408 req->result = 0;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002409 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002410 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002411 if (kiocb->ki_flags & IOCB_HIPRI)
2412 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002413 kiocb->ki_complete = io_complete_rw;
2414 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002415
Jens Axboe3529d8c2019-12-19 18:24:38 -07002416 req->rw.addr = READ_ONCE(sqe->addr);
2417 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002418 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002419 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002420}
2421
2422static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2423{
2424 switch (ret) {
2425 case -EIOCBQUEUED:
2426 break;
2427 case -ERESTARTSYS:
2428 case -ERESTARTNOINTR:
2429 case -ERESTARTNOHAND:
2430 case -ERESTART_RESTARTBLOCK:
2431 /*
2432 * We can't just restart the syscall, since previously
2433 * submitted sqes may already be in progress. Just fail this
2434 * IO with EINTR.
2435 */
2436 ret = -EINTR;
2437 /* fall through */
2438 default:
2439 kiocb->ki_complete(kiocb, ret, 0);
2440 }
2441}
2442
Jens Axboea1d7c392020-06-22 11:09:46 -06002443static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2444 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002445{
Jens Axboeba042912019-12-25 16:33:42 -07002446 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2447
2448 if (req->flags & REQ_F_CUR_POS)
2449 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002450 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002451 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002452 else
2453 io_rw_done(kiocb, ret);
2454}
2455
Jens Axboe9adbd452019-12-20 08:45:55 -07002456static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002457 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002458{
Jens Axboe9adbd452019-12-20 08:45:55 -07002459 struct io_ring_ctx *ctx = req->ctx;
2460 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002461 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002462 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002463 size_t offset;
2464 u64 buf_addr;
2465
2466 /* attempt to use fixed buffers without having provided iovecs */
2467 if (unlikely(!ctx->user_bufs))
2468 return -EFAULT;
2469
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002470 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002471 if (unlikely(buf_index >= ctx->nr_user_bufs))
2472 return -EFAULT;
2473
2474 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2475 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002476 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002477
2478 /* overflow */
2479 if (buf_addr + len < buf_addr)
2480 return -EFAULT;
2481 /* not inside the mapped region */
2482 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2483 return -EFAULT;
2484
2485 /*
2486 * May not be a start of buffer, set size appropriately
2487 * and advance us to the beginning.
2488 */
2489 offset = buf_addr - imu->ubuf;
2490 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002491
2492 if (offset) {
2493 /*
2494 * Don't use iov_iter_advance() here, as it's really slow for
2495 * using the latter parts of a big fixed buffer - it iterates
2496 * over each segment manually. We can cheat a bit here, because
2497 * we know that:
2498 *
2499 * 1) it's a BVEC iter, we set it up
2500 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2501 * first and last bvec
2502 *
2503 * So just find our index, and adjust the iterator afterwards.
2504 * If the offset is within the first bvec (or the whole first
2505 * bvec, just use iov_iter_advance(). This makes it easier
2506 * since we can just skip the first segment, which may not
2507 * be PAGE_SIZE aligned.
2508 */
2509 const struct bio_vec *bvec = imu->bvec;
2510
2511 if (offset <= bvec->bv_len) {
2512 iov_iter_advance(iter, offset);
2513 } else {
2514 unsigned long seg_skip;
2515
2516 /* skip first vec */
2517 offset -= bvec->bv_len;
2518 seg_skip = 1 + (offset >> PAGE_SHIFT);
2519
2520 iter->bvec = bvec + seg_skip;
2521 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002522 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002523 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002524 }
2525 }
2526
Jens Axboe5e559562019-11-13 16:12:46 -07002527 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002528}
2529
Jens Axboebcda7ba2020-02-23 16:42:51 -07002530static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2531{
2532 if (needs_lock)
2533 mutex_unlock(&ctx->uring_lock);
2534}
2535
2536static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2537{
2538 /*
2539 * "Normal" inline submissions always hold the uring_lock, since we
2540 * grab it from the system call. Same is true for the SQPOLL offload.
2541 * The only exception is when we've detached the request and issue it
2542 * from an async worker thread, grab the lock for that case.
2543 */
2544 if (needs_lock)
2545 mutex_lock(&ctx->uring_lock);
2546}
2547
2548static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2549 int bgid, struct io_buffer *kbuf,
2550 bool needs_lock)
2551{
2552 struct io_buffer *head;
2553
2554 if (req->flags & REQ_F_BUFFER_SELECTED)
2555 return kbuf;
2556
2557 io_ring_submit_lock(req->ctx, needs_lock);
2558
2559 lockdep_assert_held(&req->ctx->uring_lock);
2560
2561 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2562 if (head) {
2563 if (!list_empty(&head->list)) {
2564 kbuf = list_last_entry(&head->list, struct io_buffer,
2565 list);
2566 list_del(&kbuf->list);
2567 } else {
2568 kbuf = head;
2569 idr_remove(&req->ctx->io_buffer_idr, bgid);
2570 }
2571 if (*len > kbuf->len)
2572 *len = kbuf->len;
2573 } else {
2574 kbuf = ERR_PTR(-ENOBUFS);
2575 }
2576
2577 io_ring_submit_unlock(req->ctx, needs_lock);
2578
2579 return kbuf;
2580}
2581
Jens Axboe4d954c22020-02-27 07:31:19 -07002582static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2583 bool needs_lock)
2584{
2585 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002586 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002587
2588 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002589 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002590 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2591 if (IS_ERR(kbuf))
2592 return kbuf;
2593 req->rw.addr = (u64) (unsigned long) kbuf;
2594 req->flags |= REQ_F_BUFFER_SELECTED;
2595 return u64_to_user_ptr(kbuf->addr);
2596}
2597
2598#ifdef CONFIG_COMPAT
2599static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2600 bool needs_lock)
2601{
2602 struct compat_iovec __user *uiov;
2603 compat_ssize_t clen;
2604 void __user *buf;
2605 ssize_t len;
2606
2607 uiov = u64_to_user_ptr(req->rw.addr);
2608 if (!access_ok(uiov, sizeof(*uiov)))
2609 return -EFAULT;
2610 if (__get_user(clen, &uiov->iov_len))
2611 return -EFAULT;
2612 if (clen < 0)
2613 return -EINVAL;
2614
2615 len = clen;
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 = (compat_size_t) len;
2621 return 0;
2622}
2623#endif
2624
2625static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2626 bool needs_lock)
2627{
2628 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2629 void __user *buf;
2630 ssize_t len;
2631
2632 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2633 return -EFAULT;
2634
2635 len = iov[0].iov_len;
2636 if (len < 0)
2637 return -EINVAL;
2638 buf = io_rw_buffer_select(req, &len, needs_lock);
2639 if (IS_ERR(buf))
2640 return PTR_ERR(buf);
2641 iov[0].iov_base = buf;
2642 iov[0].iov_len = len;
2643 return 0;
2644}
2645
2646static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2647 bool needs_lock)
2648{
Jens Axboedddb3e22020-06-04 11:27:01 -06002649 if (req->flags & REQ_F_BUFFER_SELECTED) {
2650 struct io_buffer *kbuf;
2651
2652 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2653 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2654 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002655 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002656 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002657 if (!req->rw.len)
2658 return 0;
2659 else if (req->rw.len > 1)
2660 return -EINVAL;
2661
2662#ifdef CONFIG_COMPAT
2663 if (req->ctx->compat)
2664 return io_compat_import(req, iov, needs_lock);
2665#endif
2666
2667 return __io_iov_buffer_select(req, iov, needs_lock);
2668}
2669
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002670static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002671 struct iovec **iovec, struct iov_iter *iter,
2672 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002673{
Jens Axboe9adbd452019-12-20 08:45:55 -07002674 void __user *buf = u64_to_user_ptr(req->rw.addr);
2675 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002676 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002677 u8 opcode;
2678
Jens Axboed625c6e2019-12-17 19:53:05 -07002679 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002680 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002681 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002682 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002683 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002684
Jens Axboebcda7ba2020-02-23 16:42:51 -07002685 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002686 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002687 return -EINVAL;
2688
Jens Axboe3a6820f2019-12-22 15:19:35 -07002689 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002690 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002691 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2692 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002693 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002694 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002695 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002696 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002697 }
2698
Jens Axboe3a6820f2019-12-22 15:19:35 -07002699 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2700 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002701 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002702 }
2703
Jens Axboef67676d2019-12-02 11:03:47 -07002704 if (req->io) {
2705 struct io_async_rw *iorw = &req->io->rw;
2706
2707 *iovec = iorw->iov;
2708 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2709 if (iorw->iov == iorw->fast_iov)
2710 *iovec = NULL;
2711 return iorw->size;
2712 }
2713
Jens Axboe4d954c22020-02-27 07:31:19 -07002714 if (req->flags & REQ_F_BUFFER_SELECT) {
2715 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002716 if (!ret) {
2717 ret = (*iovec)->iov_len;
2718 iov_iter_init(iter, rw, *iovec, 1, ret);
2719 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002720 *iovec = NULL;
2721 return ret;
2722 }
2723
Jens Axboe2b188cc2019-01-07 10:46:33 -07002724#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002725 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002726 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2727 iovec, iter);
2728#endif
2729
2730 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2731}
2732
Jens Axboe32960612019-09-23 11:05:34 -06002733/*
2734 * For files that don't have ->read_iter() and ->write_iter(), handle them
2735 * by looping over ->read() or ->write() manually.
2736 */
2737static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2738 struct iov_iter *iter)
2739{
2740 ssize_t ret = 0;
2741
2742 /*
2743 * Don't support polled IO through this interface, and we can't
2744 * support non-blocking either. For the latter, this just causes
2745 * the kiocb to be handled from an async context.
2746 */
2747 if (kiocb->ki_flags & IOCB_HIPRI)
2748 return -EOPNOTSUPP;
2749 if (kiocb->ki_flags & IOCB_NOWAIT)
2750 return -EAGAIN;
2751
2752 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002753 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002754 ssize_t nr;
2755
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002756 if (!iov_iter_is_bvec(iter)) {
2757 iovec = iov_iter_iovec(iter);
2758 } else {
2759 /* fixed buffers import bvec */
2760 iovec.iov_base = kmap(iter->bvec->bv_page)
2761 + iter->iov_offset;
2762 iovec.iov_len = min(iter->count,
2763 iter->bvec->bv_len - iter->iov_offset);
2764 }
2765
Jens Axboe32960612019-09-23 11:05:34 -06002766 if (rw == READ) {
2767 nr = file->f_op->read(file, iovec.iov_base,
2768 iovec.iov_len, &kiocb->ki_pos);
2769 } else {
2770 nr = file->f_op->write(file, iovec.iov_base,
2771 iovec.iov_len, &kiocb->ki_pos);
2772 }
2773
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002774 if (iov_iter_is_bvec(iter))
2775 kunmap(iter->bvec->bv_page);
2776
Jens Axboe32960612019-09-23 11:05:34 -06002777 if (nr < 0) {
2778 if (!ret)
2779 ret = nr;
2780 break;
2781 }
2782 ret += nr;
2783 if (nr != iovec.iov_len)
2784 break;
2785 iov_iter_advance(iter, nr);
2786 }
2787
2788 return ret;
2789}
2790
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002791static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002792 struct iovec *iovec, struct iovec *fast_iov,
2793 struct iov_iter *iter)
2794{
2795 req->io->rw.nr_segs = iter->nr_segs;
2796 req->io->rw.size = io_size;
2797 req->io->rw.iov = iovec;
2798 if (!req->io->rw.iov) {
2799 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002800 if (req->io->rw.iov != fast_iov)
2801 memcpy(req->io->rw.iov, fast_iov,
2802 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002803 } else {
2804 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002805 }
2806}
2807
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002808static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2809{
2810 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2811 return req->io == NULL;
2812}
2813
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002814static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002815{
Jens Axboed3656342019-12-18 09:50:26 -07002816 if (!io_op_defs[req->opcode].async_ctx)
2817 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002818
2819 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002820}
2821
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002822static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2823 struct iovec *iovec, struct iovec *fast_iov,
2824 struct iov_iter *iter)
2825{
Jens Axboe980ad262020-01-24 23:08:54 -07002826 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002827 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002828 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002829 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002830 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002831
Jens Axboe5d204bc2020-01-31 12:06:52 -07002832 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2833 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002834 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002835}
2836
Jens Axboe3529d8c2019-12-19 18:24:38 -07002837static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2838 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002839{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002840 struct io_async_ctx *io;
2841 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002842 ssize_t ret;
2843
Jens Axboe3529d8c2019-12-19 18:24:38 -07002844 ret = io_prep_rw(req, sqe, force_nonblock);
2845 if (ret)
2846 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002847
Jens Axboe3529d8c2019-12-19 18:24:38 -07002848 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2849 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002850
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002851 /* either don't need iovec imported or already have it */
2852 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002853 return 0;
2854
2855 io = req->io;
2856 io->rw.iov = io->rw.fast_iov;
2857 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002858 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002859 req->io = io;
2860 if (ret < 0)
2861 return ret;
2862
2863 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2864 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002865}
2866
Jens Axboebcf5a062020-05-22 09:24:42 -06002867static void io_async_buf_cancel(struct callback_head *cb)
2868{
2869 struct io_async_rw *rw;
2870 struct io_kiocb *req;
2871
2872 rw = container_of(cb, struct io_async_rw, task_work);
2873 req = rw->wpq.wait.private;
Jens Axboec40f6372020-06-25 15:39:59 -06002874 __io_req_task_cancel(req, -ECANCELED);
Jens Axboebcf5a062020-05-22 09:24:42 -06002875}
2876
2877static void io_async_buf_retry(struct callback_head *cb)
2878{
2879 struct io_async_rw *rw;
Jens Axboebcf5a062020-05-22 09:24:42 -06002880 struct io_kiocb *req;
2881
2882 rw = container_of(cb, struct io_async_rw, task_work);
2883 req = rw->wpq.wait.private;
Jens Axboebcf5a062020-05-22 09:24:42 -06002884
Jens Axboec40f6372020-06-25 15:39:59 -06002885 __io_req_task_submit(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06002886}
2887
2888static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2889 int sync, void *arg)
2890{
2891 struct wait_page_queue *wpq;
2892 struct io_kiocb *req = wait->private;
2893 struct io_async_rw *rw = &req->io->rw;
2894 struct wait_page_key *key = arg;
2895 struct task_struct *tsk;
2896 int ret;
2897
2898 wpq = container_of(wait, struct wait_page_queue, wait);
2899
2900 ret = wake_page_match(wpq, key);
2901 if (ret != 1)
2902 return ret;
2903
2904 list_del_init(&wait->entry);
2905
2906 init_task_work(&rw->task_work, io_async_buf_retry);
2907 /* submit ref gets dropped, acquire a new one */
2908 refcount_inc(&req->refs);
2909 tsk = req->task;
2910 ret = task_work_add(tsk, &rw->task_work, true);
2911 if (unlikely(ret)) {
2912 /* queue just for cancelation */
2913 init_task_work(&rw->task_work, io_async_buf_cancel);
2914 tsk = io_wq_get_task(req->ctx->io_wq);
2915 task_work_add(tsk, &rw->task_work, true);
2916 }
2917 wake_up_process(tsk);
2918 return 1;
2919}
2920
2921static bool io_rw_should_retry(struct io_kiocb *req)
2922{
2923 struct kiocb *kiocb = &req->rw.kiocb;
2924 int ret;
2925
2926 /* never retry for NOWAIT, we just complete with -EAGAIN */
2927 if (req->flags & REQ_F_NOWAIT)
2928 return false;
2929
2930 /* already tried, or we're doing O_DIRECT */
2931 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
2932 return false;
2933 /*
2934 * just use poll if we can, and don't attempt if the fs doesn't
2935 * support callback based unlocks
2936 */
2937 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
2938 return false;
2939
2940 /*
2941 * If request type doesn't require req->io to defer in general,
2942 * we need to allocate it here
2943 */
2944 if (!req->io && __io_alloc_async_ctx(req))
2945 return false;
2946
2947 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
2948 io_async_buf_func, req);
2949 if (!ret) {
2950 io_get_req_task(req);
2951 return true;
2952 }
2953
2954 return false;
2955}
2956
2957static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
2958{
2959 if (req->file->f_op->read_iter)
2960 return call_read_iter(req->file, &req->rw.kiocb, iter);
2961 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
2962}
2963
Jens Axboea1d7c392020-06-22 11:09:46 -06002964static int io_read(struct io_kiocb *req, bool force_nonblock,
2965 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002966{
2967 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002968 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002969 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002970 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002971 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002972
Jens Axboebcda7ba2020-02-23 16:42:51 -07002973 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002974 if (ret < 0)
2975 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002976
Jens Axboefd6c2e42019-12-18 12:19:41 -07002977 /* Ensure we clear previously set non-block flag */
2978 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002979 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002980
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002981 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002982 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002983 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002984 req->result = io_size;
2985
Pavel Begunkov24c74672020-06-21 13:09:51 +03002986 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06002987 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002988 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002989
Jens Axboe31b51512019-01-18 22:56:34 -07002990 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002991 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002992 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06002993 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06002994 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002995
Jens Axboebcf5a062020-05-22 09:24:42 -06002996 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002997
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002998 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06002999 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003000 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003001 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003002 iter.count = iov_count;
3003 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003004copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003005 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003006 inline_vecs, &iter);
3007 if (ret)
3008 goto out_free;
Jens Axboebcf5a062020-05-22 09:24:42 -06003009 /* if we can retry, do so with the callbacks armed */
3010 if (io_rw_should_retry(req)) {
3011 ret2 = io_iter_do_read(req, &iter);
3012 if (ret2 == -EIOCBQUEUED) {
3013 goto out_free;
3014 } else if (ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003015 kiocb_done(kiocb, ret2, cs);
Jens Axboebcf5a062020-05-22 09:24:42 -06003016 goto out_free;
3017 }
3018 }
3019 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07003020 return -EAGAIN;
3021 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003022 }
Jens Axboef67676d2019-12-02 11:03:47 -07003023out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003024 if (!(req->flags & REQ_F_NEED_CLEANUP))
3025 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003026 return ret;
3027}
3028
Jens Axboe3529d8c2019-12-19 18:24:38 -07003029static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3030 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003031{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003032 struct io_async_ctx *io;
3033 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07003034 ssize_t ret;
3035
Jens Axboe3529d8c2019-12-19 18:24:38 -07003036 ret = io_prep_rw(req, sqe, force_nonblock);
3037 if (ret)
3038 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003039
Jens Axboe3529d8c2019-12-19 18:24:38 -07003040 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3041 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003042
Jens Axboe4ed734b2020-03-20 11:23:41 -06003043 req->fsize = rlimit(RLIMIT_FSIZE);
3044
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003045 /* either don't need iovec imported or already have it */
3046 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003047 return 0;
3048
3049 io = req->io;
3050 io->rw.iov = io->rw.fast_iov;
3051 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003052 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003053 req->io = io;
3054 if (ret < 0)
3055 return ret;
3056
3057 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
3058 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003059}
3060
Jens Axboea1d7c392020-06-22 11:09:46 -06003061static int io_write(struct io_kiocb *req, bool force_nonblock,
3062 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003063{
3064 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003065 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003066 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003067 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003068 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003069
Jens Axboebcda7ba2020-02-23 16:42:51 -07003070 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003071 if (ret < 0)
3072 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003073
Jens Axboefd6c2e42019-12-18 12:19:41 -07003074 /* Ensure we clear previously set non-block flag */
3075 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003076 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003077
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08003078 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003079 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03003080 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07003081 req->result = io_size;
3082
Pavel Begunkov24c74672020-06-21 13:09:51 +03003083 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003084 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003085 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003086
Jens Axboe10d59342019-12-09 20:16:22 -07003087 /* file path doesn't support NOWAIT for non-direct_IO */
3088 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3089 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003090 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003091
Jens Axboe31b51512019-01-18 22:56:34 -07003092 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003093 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003094 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003095 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003096 ssize_t ret2;
3097
Jens Axboe2b188cc2019-01-07 10:46:33 -07003098 /*
3099 * Open-code file_start_write here to grab freeze protection,
3100 * which will be released by another thread in
3101 * io_complete_rw(). Fool lockdep by telling it the lock got
3102 * released so that it doesn't complain about the held lock when
3103 * we return to userspace.
3104 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003105 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003106 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003107 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003108 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003109 SB_FREEZE_WRITE);
3110 }
3111 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003112
Jens Axboe4ed734b2020-03-20 11:23:41 -06003113 if (!force_nonblock)
3114 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3115
Jens Axboe9adbd452019-12-20 08:45:55 -07003116 if (req->file->f_op->write_iter)
3117 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003118 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003119 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003120
3121 if (!force_nonblock)
3122 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3123
Jens Axboefaac9962020-02-07 15:45:22 -07003124 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003125 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003126 * retry them without IOCB_NOWAIT.
3127 */
3128 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3129 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003130 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003131 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003132 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003133 iter.count = iov_count;
3134 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003135copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003136 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003137 inline_vecs, &iter);
3138 if (ret)
3139 goto out_free;
3140 return -EAGAIN;
3141 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003142 }
Jens Axboe31b51512019-01-18 22:56:34 -07003143out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003144 if (!(req->flags & REQ_F_NEED_CLEANUP))
3145 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003146 return ret;
3147}
3148
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003149static int __io_splice_prep(struct io_kiocb *req,
3150 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003151{
3152 struct io_splice* sp = &req->splice;
3153 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3154 int ret;
3155
3156 if (req->flags & REQ_F_NEED_CLEANUP)
3157 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003158 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3159 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003160
3161 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003162 sp->len = READ_ONCE(sqe->len);
3163 sp->flags = READ_ONCE(sqe->splice_flags);
3164
3165 if (unlikely(sp->flags & ~valid_flags))
3166 return -EINVAL;
3167
3168 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3169 (sp->flags & SPLICE_F_FD_IN_FIXED));
3170 if (ret)
3171 return ret;
3172 req->flags |= REQ_F_NEED_CLEANUP;
3173
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003174 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3175 /*
3176 * Splice operation will be punted aync, and here need to
3177 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3178 */
3179 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003180 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003181 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003182
3183 return 0;
3184}
3185
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003186static int io_tee_prep(struct io_kiocb *req,
3187 const struct io_uring_sqe *sqe)
3188{
3189 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3190 return -EINVAL;
3191 return __io_splice_prep(req, sqe);
3192}
3193
3194static int io_tee(struct io_kiocb *req, bool force_nonblock)
3195{
3196 struct io_splice *sp = &req->splice;
3197 struct file *in = sp->file_in;
3198 struct file *out = sp->file_out;
3199 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3200 long ret = 0;
3201
3202 if (force_nonblock)
3203 return -EAGAIN;
3204 if (sp->len)
3205 ret = do_tee(in, out, sp->len, flags);
3206
3207 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3208 req->flags &= ~REQ_F_NEED_CLEANUP;
3209
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003210 if (ret != sp->len)
3211 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003212 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003213 return 0;
3214}
3215
3216static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3217{
3218 struct io_splice* sp = &req->splice;
3219
3220 sp->off_in = READ_ONCE(sqe->splice_off_in);
3221 sp->off_out = READ_ONCE(sqe->off);
3222 return __io_splice_prep(req, sqe);
3223}
3224
Pavel Begunkov014db002020-03-03 21:33:12 +03003225static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003226{
3227 struct io_splice *sp = &req->splice;
3228 struct file *in = sp->file_in;
3229 struct file *out = sp->file_out;
3230 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3231 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003232 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003233
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003234 if (force_nonblock)
3235 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003236
3237 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3238 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003239
Jens Axboe948a7742020-05-17 14:21:38 -06003240 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003241 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003242
3243 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3244 req->flags &= ~REQ_F_NEED_CLEANUP;
3245
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003246 if (ret != sp->len)
3247 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003248 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003249 return 0;
3250}
3251
Jens Axboe2b188cc2019-01-07 10:46:33 -07003252/*
3253 * IORING_OP_NOP just posts a completion event, nothing else.
3254 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003255static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003256{
3257 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003258
Jens Axboedef596e2019-01-09 08:59:42 -07003259 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3260 return -EINVAL;
3261
Jens Axboe229a7b62020-06-22 10:13:11 -06003262 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003263 return 0;
3264}
3265
Jens Axboe3529d8c2019-12-19 18:24:38 -07003266static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003267{
Jens Axboe6b063142019-01-10 22:13:58 -07003268 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003269
Jens Axboe09bb8392019-03-13 12:39:28 -06003270 if (!req->file)
3271 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003272
Jens Axboe6b063142019-01-10 22:13:58 -07003273 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003274 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003275 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003276 return -EINVAL;
3277
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003278 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3279 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3280 return -EINVAL;
3281
3282 req->sync.off = READ_ONCE(sqe->off);
3283 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003284 return 0;
3285}
3286
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003287static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003288{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003289 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003290 int ret;
3291
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003292 /* fsync always requires a blocking context */
3293 if (force_nonblock)
3294 return -EAGAIN;
3295
Jens Axboe9adbd452019-12-20 08:45:55 -07003296 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003297 end > 0 ? end : LLONG_MAX,
3298 req->sync.flags & IORING_FSYNC_DATASYNC);
3299 if (ret < 0)
3300 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003301 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003302 return 0;
3303}
3304
Jens Axboed63d1b52019-12-10 10:38:56 -07003305static int io_fallocate_prep(struct io_kiocb *req,
3306 const struct io_uring_sqe *sqe)
3307{
3308 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3309 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003310 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3311 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003312
3313 req->sync.off = READ_ONCE(sqe->off);
3314 req->sync.len = READ_ONCE(sqe->addr);
3315 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003316 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07003317 return 0;
3318}
3319
Pavel Begunkov014db002020-03-03 21:33:12 +03003320static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003321{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003322 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003323
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003324 /* fallocate always requiring blocking context */
3325 if (force_nonblock)
3326 return -EAGAIN;
3327
3328 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3329 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3330 req->sync.len);
3331 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3332 if (ret < 0)
3333 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003334 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003335 return 0;
3336}
3337
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003338static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003339{
Jens Axboef8748882020-01-08 17:47:02 -07003340 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003341 int ret;
3342
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003343 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003344 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003345 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003346 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003347 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003348 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003349
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003350 /* open.how should be already initialised */
3351 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003352 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003353
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003354 req->open.dfd = READ_ONCE(sqe->fd);
3355 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003356 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003357 if (IS_ERR(req->open.filename)) {
3358 ret = PTR_ERR(req->open.filename);
3359 req->open.filename = NULL;
3360 return ret;
3361 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003362 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003363 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003364 return 0;
3365}
3366
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003367static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3368{
3369 u64 flags, mode;
3370
3371 if (req->flags & REQ_F_NEED_CLEANUP)
3372 return 0;
3373 mode = READ_ONCE(sqe->len);
3374 flags = READ_ONCE(sqe->open_flags);
3375 req->open.how = build_open_how(flags, mode);
3376 return __io_openat_prep(req, sqe);
3377}
3378
Jens Axboecebdb982020-01-08 17:59:24 -07003379static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3380{
3381 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003382 size_t len;
3383 int ret;
3384
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003385 if (req->flags & REQ_F_NEED_CLEANUP)
3386 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003387 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3388 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003389 if (len < OPEN_HOW_SIZE_VER0)
3390 return -EINVAL;
3391
3392 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3393 len);
3394 if (ret)
3395 return ret;
3396
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003397 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003398}
3399
Pavel Begunkov014db002020-03-03 21:33:12 +03003400static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003401{
3402 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003403 struct file *file;
3404 int ret;
3405
Jens Axboef86cd202020-01-29 13:46:44 -07003406 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003407 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003408
Jens Axboecebdb982020-01-08 17:59:24 -07003409 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003410 if (ret)
3411 goto err;
3412
Jens Axboe4022e7a2020-03-19 19:23:18 -06003413 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003414 if (ret < 0)
3415 goto err;
3416
3417 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3418 if (IS_ERR(file)) {
3419 put_unused_fd(ret);
3420 ret = PTR_ERR(file);
3421 } else {
3422 fsnotify_open(file);
3423 fd_install(ret, file);
3424 }
3425err:
3426 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003427 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003428 if (ret < 0)
3429 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003430 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003431 return 0;
3432}
3433
Pavel Begunkov014db002020-03-03 21:33:12 +03003434static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003435{
Pavel Begunkov014db002020-03-03 21:33:12 +03003436 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003437}
3438
Jens Axboe067524e2020-03-02 16:32:28 -07003439static int io_remove_buffers_prep(struct io_kiocb *req,
3440 const struct io_uring_sqe *sqe)
3441{
3442 struct io_provide_buf *p = &req->pbuf;
3443 u64 tmp;
3444
3445 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3446 return -EINVAL;
3447
3448 tmp = READ_ONCE(sqe->fd);
3449 if (!tmp || tmp > USHRT_MAX)
3450 return -EINVAL;
3451
3452 memset(p, 0, sizeof(*p));
3453 p->nbufs = tmp;
3454 p->bgid = READ_ONCE(sqe->buf_group);
3455 return 0;
3456}
3457
3458static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3459 int bgid, unsigned nbufs)
3460{
3461 unsigned i = 0;
3462
3463 /* shouldn't happen */
3464 if (!nbufs)
3465 return 0;
3466
3467 /* the head kbuf is the list itself */
3468 while (!list_empty(&buf->list)) {
3469 struct io_buffer *nxt;
3470
3471 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3472 list_del(&nxt->list);
3473 kfree(nxt);
3474 if (++i == nbufs)
3475 return i;
3476 }
3477 i++;
3478 kfree(buf);
3479 idr_remove(&ctx->io_buffer_idr, bgid);
3480
3481 return i;
3482}
3483
Jens Axboe229a7b62020-06-22 10:13:11 -06003484static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3485 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003486{
3487 struct io_provide_buf *p = &req->pbuf;
3488 struct io_ring_ctx *ctx = req->ctx;
3489 struct io_buffer *head;
3490 int ret = 0;
3491
3492 io_ring_submit_lock(ctx, !force_nonblock);
3493
3494 lockdep_assert_held(&ctx->uring_lock);
3495
3496 ret = -ENOENT;
3497 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3498 if (head)
3499 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3500
3501 io_ring_submit_lock(ctx, !force_nonblock);
3502 if (ret < 0)
3503 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003504 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003505 return 0;
3506}
3507
Jens Axboeddf0322d2020-02-23 16:41:33 -07003508static int io_provide_buffers_prep(struct io_kiocb *req,
3509 const struct io_uring_sqe *sqe)
3510{
3511 struct io_provide_buf *p = &req->pbuf;
3512 u64 tmp;
3513
3514 if (sqe->ioprio || sqe->rw_flags)
3515 return -EINVAL;
3516
3517 tmp = READ_ONCE(sqe->fd);
3518 if (!tmp || tmp > USHRT_MAX)
3519 return -E2BIG;
3520 p->nbufs = tmp;
3521 p->addr = READ_ONCE(sqe->addr);
3522 p->len = READ_ONCE(sqe->len);
3523
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003524 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003525 return -EFAULT;
3526
3527 p->bgid = READ_ONCE(sqe->buf_group);
3528 tmp = READ_ONCE(sqe->off);
3529 if (tmp > USHRT_MAX)
3530 return -E2BIG;
3531 p->bid = tmp;
3532 return 0;
3533}
3534
3535static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3536{
3537 struct io_buffer *buf;
3538 u64 addr = pbuf->addr;
3539 int i, bid = pbuf->bid;
3540
3541 for (i = 0; i < pbuf->nbufs; i++) {
3542 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3543 if (!buf)
3544 break;
3545
3546 buf->addr = addr;
3547 buf->len = pbuf->len;
3548 buf->bid = bid;
3549 addr += pbuf->len;
3550 bid++;
3551 if (!*head) {
3552 INIT_LIST_HEAD(&buf->list);
3553 *head = buf;
3554 } else {
3555 list_add_tail(&buf->list, &(*head)->list);
3556 }
3557 }
3558
3559 return i ? i : -ENOMEM;
3560}
3561
Jens Axboe229a7b62020-06-22 10:13:11 -06003562static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3563 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003564{
3565 struct io_provide_buf *p = &req->pbuf;
3566 struct io_ring_ctx *ctx = req->ctx;
3567 struct io_buffer *head, *list;
3568 int ret = 0;
3569
3570 io_ring_submit_lock(ctx, !force_nonblock);
3571
3572 lockdep_assert_held(&ctx->uring_lock);
3573
3574 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3575
3576 ret = io_add_buffers(p, &head);
3577 if (ret < 0)
3578 goto out;
3579
3580 if (!list) {
3581 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3582 GFP_KERNEL);
3583 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003584 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003585 goto out;
3586 }
3587 }
3588out:
3589 io_ring_submit_unlock(ctx, !force_nonblock);
3590 if (ret < 0)
3591 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003592 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003593 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003594}
3595
Jens Axboe3e4827b2020-01-08 15:18:09 -07003596static int io_epoll_ctl_prep(struct io_kiocb *req,
3597 const struct io_uring_sqe *sqe)
3598{
3599#if defined(CONFIG_EPOLL)
3600 if (sqe->ioprio || sqe->buf_index)
3601 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003602 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3603 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003604
3605 req->epoll.epfd = READ_ONCE(sqe->fd);
3606 req->epoll.op = READ_ONCE(sqe->len);
3607 req->epoll.fd = READ_ONCE(sqe->off);
3608
3609 if (ep_op_has_event(req->epoll.op)) {
3610 struct epoll_event __user *ev;
3611
3612 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3613 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3614 return -EFAULT;
3615 }
3616
3617 return 0;
3618#else
3619 return -EOPNOTSUPP;
3620#endif
3621}
3622
Jens Axboe229a7b62020-06-22 10:13:11 -06003623static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3624 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003625{
3626#if defined(CONFIG_EPOLL)
3627 struct io_epoll *ie = &req->epoll;
3628 int ret;
3629
3630 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3631 if (force_nonblock && ret == -EAGAIN)
3632 return -EAGAIN;
3633
3634 if (ret < 0)
3635 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003636 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003637 return 0;
3638#else
3639 return -EOPNOTSUPP;
3640#endif
3641}
3642
Jens Axboec1ca7572019-12-25 22:18:28 -07003643static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3644{
3645#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3646 if (sqe->ioprio || sqe->buf_index || sqe->off)
3647 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003648 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3649 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003650
3651 req->madvise.addr = READ_ONCE(sqe->addr);
3652 req->madvise.len = READ_ONCE(sqe->len);
3653 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3654 return 0;
3655#else
3656 return -EOPNOTSUPP;
3657#endif
3658}
3659
Pavel Begunkov014db002020-03-03 21:33:12 +03003660static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003661{
3662#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3663 struct io_madvise *ma = &req->madvise;
3664 int ret;
3665
3666 if (force_nonblock)
3667 return -EAGAIN;
3668
3669 ret = do_madvise(ma->addr, ma->len, ma->advice);
3670 if (ret < 0)
3671 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003672 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003673 return 0;
3674#else
3675 return -EOPNOTSUPP;
3676#endif
3677}
3678
Jens Axboe4840e412019-12-25 22:03:45 -07003679static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3680{
3681 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3682 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003683 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3684 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003685
3686 req->fadvise.offset = READ_ONCE(sqe->off);
3687 req->fadvise.len = READ_ONCE(sqe->len);
3688 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3689 return 0;
3690}
3691
Pavel Begunkov014db002020-03-03 21:33:12 +03003692static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003693{
3694 struct io_fadvise *fa = &req->fadvise;
3695 int ret;
3696
Jens Axboe3e694262020-02-01 09:22:49 -07003697 if (force_nonblock) {
3698 switch (fa->advice) {
3699 case POSIX_FADV_NORMAL:
3700 case POSIX_FADV_RANDOM:
3701 case POSIX_FADV_SEQUENTIAL:
3702 break;
3703 default:
3704 return -EAGAIN;
3705 }
3706 }
Jens Axboe4840e412019-12-25 22:03:45 -07003707
3708 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3709 if (ret < 0)
3710 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003711 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003712 return 0;
3713}
3714
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003715static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3716{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003717 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3718 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003719 if (sqe->ioprio || sqe->buf_index)
3720 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003721 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003722 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003723
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003724 req->statx.dfd = READ_ONCE(sqe->fd);
3725 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003726 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003727 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3728 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003729
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003730 return 0;
3731}
3732
Pavel Begunkov014db002020-03-03 21:33:12 +03003733static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003734{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003735 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003736 int ret;
3737
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003738 if (force_nonblock) {
3739 /* only need file table for an actual valid fd */
3740 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3741 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003742 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003743 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003744
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003745 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3746 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003747
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003748 if (ret < 0)
3749 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003750 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003751 return 0;
3752}
3753
Jens Axboeb5dba592019-12-11 14:02:38 -07003754static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3755{
3756 /*
3757 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003758 * leave the 'file' in an undeterminate state, and here need to modify
3759 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003760 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003761 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003762 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3763
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003764 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3765 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003766 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3767 sqe->rw_flags || sqe->buf_index)
3768 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003769 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003770 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003771
3772 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003773 if ((req->file && req->file->f_op == &io_uring_fops) ||
3774 req->close.fd == req->ctx->ring_fd)
3775 return -EBADF;
3776
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003777 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003778 return 0;
3779}
3780
Jens Axboe229a7b62020-06-22 10:13:11 -06003781static int io_close(struct io_kiocb *req, bool force_nonblock,
3782 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003783{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003784 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003785 int ret;
3786
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003787 /* might be already done during nonblock submission */
3788 if (!close->put_file) {
3789 ret = __close_fd_get_file(close->fd, &close->put_file);
3790 if (ret < 0)
3791 return (ret == -ENOENT) ? -EBADF : ret;
3792 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003793
3794 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003795 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003796 /* was never set, but play safe */
3797 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003798 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003799 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003800 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003801 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003802
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003803 /* No ->flush() or already async, safely close from here */
3804 ret = filp_close(close->put_file, req->work.files);
3805 if (ret < 0)
3806 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003807 fput(close->put_file);
3808 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003809 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003810 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003811}
3812
Jens Axboe3529d8c2019-12-19 18:24:38 -07003813static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003814{
3815 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003816
3817 if (!req->file)
3818 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003819
3820 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3821 return -EINVAL;
3822 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3823 return -EINVAL;
3824
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003825 req->sync.off = READ_ONCE(sqe->off);
3826 req->sync.len = READ_ONCE(sqe->len);
3827 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003828 return 0;
3829}
3830
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003831static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003832{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003833 int ret;
3834
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003835 /* sync_file_range always requires a blocking context */
3836 if (force_nonblock)
3837 return -EAGAIN;
3838
Jens Axboe9adbd452019-12-20 08:45:55 -07003839 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003840 req->sync.flags);
3841 if (ret < 0)
3842 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003843 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003844 return 0;
3845}
3846
YueHaibing469956e2020-03-04 15:53:52 +08003847#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003848static int io_setup_async_msg(struct io_kiocb *req,
3849 struct io_async_msghdr *kmsg)
3850{
3851 if (req->io)
3852 return -EAGAIN;
3853 if (io_alloc_async_ctx(req)) {
3854 if (kmsg->iov != kmsg->fast_iov)
3855 kfree(kmsg->iov);
3856 return -ENOMEM;
3857 }
3858 req->flags |= REQ_F_NEED_CLEANUP;
3859 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3860 return -EAGAIN;
3861}
3862
Jens Axboe3529d8c2019-12-19 18:24:38 -07003863static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003864{
Jens Axboee47293f2019-12-20 08:58:21 -07003865 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003866 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003867 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003868
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003869 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3870 return -EINVAL;
3871
Jens Axboee47293f2019-12-20 08:58:21 -07003872 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3873 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003874 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003875
Jens Axboed8768362020-02-27 14:17:49 -07003876#ifdef CONFIG_COMPAT
3877 if (req->ctx->compat)
3878 sr->msg_flags |= MSG_CMSG_COMPAT;
3879#endif
3880
Jens Axboefddafac2020-01-04 20:19:44 -07003881 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003882 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003883 /* iovec is already imported */
3884 if (req->flags & REQ_F_NEED_CLEANUP)
3885 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003886
Jens Axboed9688562019-12-09 19:35:20 -07003887 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003888 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003889 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003890 if (!ret)
3891 req->flags |= REQ_F_NEED_CLEANUP;
3892 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003893}
3894
Jens Axboe229a7b62020-06-22 10:13:11 -06003895static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3896 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07003897{
Jens Axboe0b416c32019-12-15 10:57:46 -07003898 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003899 struct socket *sock;
3900 int ret;
3901
Jens Axboe03b12302019-12-02 18:50:25 -07003902 sock = sock_from_file(req->file, &ret);
3903 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003904 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003905 unsigned flags;
3906
Jens Axboe03b12302019-12-02 18:50:25 -07003907 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003908 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003909 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003910 /* if iov is set, it's allocated already */
3911 if (!kmsg->iov)
3912 kmsg->iov = kmsg->fast_iov;
3913 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003914 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003915 struct io_sr_msg *sr = &req->sr_msg;
3916
Jens Axboe0b416c32019-12-15 10:57:46 -07003917 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003918 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003919
3920 io.msg.iov = io.msg.fast_iov;
3921 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3922 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003923 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003924 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003925 }
3926
Jens Axboee47293f2019-12-20 08:58:21 -07003927 flags = req->sr_msg.msg_flags;
3928 if (flags & MSG_DONTWAIT)
3929 req->flags |= REQ_F_NOWAIT;
3930 else if (force_nonblock)
3931 flags |= MSG_DONTWAIT;
3932
Jens Axboe0b416c32019-12-15 10:57:46 -07003933 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003934 if (force_nonblock && ret == -EAGAIN)
3935 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003936 if (ret == -ERESTARTSYS)
3937 ret = -EINTR;
3938 }
3939
Pavel Begunkov1e950812020-02-06 19:51:16 +03003940 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003941 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003942 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003943 if (ret < 0)
3944 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003945 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07003946 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003947}
3948
Jens Axboe229a7b62020-06-22 10:13:11 -06003949static int io_send(struct io_kiocb *req, bool force_nonblock,
3950 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07003951{
Jens Axboefddafac2020-01-04 20:19:44 -07003952 struct socket *sock;
3953 int ret;
3954
Jens Axboefddafac2020-01-04 20:19:44 -07003955 sock = sock_from_file(req->file, &ret);
3956 if (sock) {
3957 struct io_sr_msg *sr = &req->sr_msg;
3958 struct msghdr msg;
3959 struct iovec iov;
3960 unsigned flags;
3961
3962 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3963 &msg.msg_iter);
3964 if (ret)
3965 return ret;
3966
3967 msg.msg_name = NULL;
3968 msg.msg_control = NULL;
3969 msg.msg_controllen = 0;
3970 msg.msg_namelen = 0;
3971
3972 flags = req->sr_msg.msg_flags;
3973 if (flags & MSG_DONTWAIT)
3974 req->flags |= REQ_F_NOWAIT;
3975 else if (force_nonblock)
3976 flags |= MSG_DONTWAIT;
3977
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003978 msg.msg_flags = flags;
3979 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003980 if (force_nonblock && ret == -EAGAIN)
3981 return -EAGAIN;
3982 if (ret == -ERESTARTSYS)
3983 ret = -EINTR;
3984 }
3985
Jens Axboefddafac2020-01-04 20:19:44 -07003986 if (ret < 0)
3987 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003988 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07003989 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003990}
3991
Jens Axboe52de1fe2020-02-27 10:15:42 -07003992static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3993{
3994 struct io_sr_msg *sr = &req->sr_msg;
3995 struct iovec __user *uiov;
3996 size_t iov_len;
3997 int ret;
3998
3999 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
4000 &uiov, &iov_len);
4001 if (ret)
4002 return ret;
4003
4004 if (req->flags & REQ_F_BUFFER_SELECT) {
4005 if (iov_len > 1)
4006 return -EINVAL;
4007 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
4008 return -EFAULT;
4009 sr->len = io->msg.iov[0].iov_len;
4010 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
4011 sr->len);
4012 io->msg.iov = NULL;
4013 } else {
4014 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4015 &io->msg.iov, &io->msg.msg.msg_iter);
4016 if (ret > 0)
4017 ret = 0;
4018 }
4019
4020 return ret;
4021}
4022
4023#ifdef CONFIG_COMPAT
4024static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
4025 struct io_async_ctx *io)
4026{
4027 struct compat_msghdr __user *msg_compat;
4028 struct io_sr_msg *sr = &req->sr_msg;
4029 struct compat_iovec __user *uiov;
4030 compat_uptr_t ptr;
4031 compat_size_t len;
4032 int ret;
4033
4034 msg_compat = (struct compat_msghdr __user *) sr->msg;
4035 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
4036 &ptr, &len);
4037 if (ret)
4038 return ret;
4039
4040 uiov = compat_ptr(ptr);
4041 if (req->flags & REQ_F_BUFFER_SELECT) {
4042 compat_ssize_t clen;
4043
4044 if (len > 1)
4045 return -EINVAL;
4046 if (!access_ok(uiov, sizeof(*uiov)))
4047 return -EFAULT;
4048 if (__get_user(clen, &uiov->iov_len))
4049 return -EFAULT;
4050 if (clen < 0)
4051 return -EINVAL;
4052 sr->len = io->msg.iov[0].iov_len;
4053 io->msg.iov = NULL;
4054 } else {
4055 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
4056 &io->msg.iov,
4057 &io->msg.msg.msg_iter);
4058 if (ret < 0)
4059 return ret;
4060 }
4061
4062 return 0;
4063}
Jens Axboe03b12302019-12-02 18:50:25 -07004064#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004065
4066static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
4067{
4068 io->msg.iov = io->msg.fast_iov;
4069
4070#ifdef CONFIG_COMPAT
4071 if (req->ctx->compat)
4072 return __io_compat_recvmsg_copy_hdr(req, io);
4073#endif
4074
4075 return __io_recvmsg_copy_hdr(req, io);
4076}
4077
Jens Axboebcda7ba2020-02-23 16:42:51 -07004078static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
4079 int *cflags, bool needs_lock)
4080{
4081 struct io_sr_msg *sr = &req->sr_msg;
4082 struct io_buffer *kbuf;
4083
4084 if (!(req->flags & REQ_F_BUFFER_SELECT))
4085 return NULL;
4086
4087 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4088 if (IS_ERR(kbuf))
4089 return kbuf;
4090
4091 sr->kbuf = kbuf;
4092 req->flags |= REQ_F_BUFFER_SELECTED;
4093
4094 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
4095 *cflags |= IORING_CQE_F_BUFFER;
4096 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004097}
4098
Jens Axboe3529d8c2019-12-19 18:24:38 -07004099static int io_recvmsg_prep(struct io_kiocb *req,
4100 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004101{
Jens Axboee47293f2019-12-20 08:58:21 -07004102 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004103 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004104 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004105
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004106 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4107 return -EINVAL;
4108
Jens Axboe3529d8c2019-12-19 18:24:38 -07004109 sr->msg_flags = READ_ONCE(sqe->msg_flags);
4110 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004111 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004112 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004113
Jens Axboed8768362020-02-27 14:17:49 -07004114#ifdef CONFIG_COMPAT
4115 if (req->ctx->compat)
4116 sr->msg_flags |= MSG_CMSG_COMPAT;
4117#endif
4118
Jens Axboefddafac2020-01-04 20:19:44 -07004119 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004120 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004121 /* iovec is already imported */
4122 if (req->flags & REQ_F_NEED_CLEANUP)
4123 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004124
Jens Axboe52de1fe2020-02-27 10:15:42 -07004125 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004126 if (!ret)
4127 req->flags |= REQ_F_NEED_CLEANUP;
4128 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004129}
4130
Jens Axboe229a7b62020-06-22 10:13:11 -06004131static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4132 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004133{
Jens Axboe0b416c32019-12-15 10:57:46 -07004134 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004135 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004136 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004137
Jens Axboe0fa03c62019-04-19 13:34:07 -06004138 sock = sock_from_file(req->file, &ret);
4139 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07004140 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004141 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004142 unsigned flags;
4143
Jens Axboe03b12302019-12-02 18:50:25 -07004144 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07004145 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004146 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07004147 /* if iov is set, it's allocated already */
4148 if (!kmsg->iov)
4149 kmsg->iov = kmsg->fast_iov;
4150 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004151 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07004152 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004153 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004154
Jens Axboe52de1fe2020-02-27 10:15:42 -07004155 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07004156 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004157 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004158 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06004159
Jens Axboe52de1fe2020-02-27 10:15:42 -07004160 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4161 if (IS_ERR(kbuf)) {
4162 return PTR_ERR(kbuf);
4163 } else if (kbuf) {
4164 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4165 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4166 1, req->sr_msg.len);
4167 }
4168
Jens Axboee47293f2019-12-20 08:58:21 -07004169 flags = req->sr_msg.msg_flags;
4170 if (flags & MSG_DONTWAIT)
4171 req->flags |= REQ_F_NOWAIT;
4172 else if (force_nonblock)
4173 flags |= MSG_DONTWAIT;
4174
4175 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
4176 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004177 if (force_nonblock && ret == -EAGAIN)
4178 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07004179 if (ret == -ERESTARTSYS)
4180 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004181 }
4182
Pavel Begunkov1e950812020-02-06 19:51:16 +03004183 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004184 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004185 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004186 if (ret < 0)
4187 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004188 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004189 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004190}
4191
Jens Axboe229a7b62020-06-22 10:13:11 -06004192static int io_recv(struct io_kiocb *req, bool force_nonblock,
4193 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004194{
Jens Axboebcda7ba2020-02-23 16:42:51 -07004195 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07004196 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004197 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004198
Jens Axboefddafac2020-01-04 20:19:44 -07004199 sock = sock_from_file(req->file, &ret);
4200 if (sock) {
4201 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004202 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004203 struct msghdr msg;
4204 struct iovec iov;
4205 unsigned flags;
4206
Jens Axboebcda7ba2020-02-23 16:42:51 -07004207 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4208 if (IS_ERR(kbuf))
4209 return PTR_ERR(kbuf);
4210 else if (kbuf)
4211 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004212
Jens Axboebcda7ba2020-02-23 16:42:51 -07004213 ret = import_single_range(READ, buf, sr->len, &iov,
4214 &msg.msg_iter);
4215 if (ret) {
4216 kfree(kbuf);
4217 return ret;
4218 }
4219
4220 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004221 msg.msg_name = NULL;
4222 msg.msg_control = NULL;
4223 msg.msg_controllen = 0;
4224 msg.msg_namelen = 0;
4225 msg.msg_iocb = NULL;
4226 msg.msg_flags = 0;
4227
4228 flags = req->sr_msg.msg_flags;
4229 if (flags & MSG_DONTWAIT)
4230 req->flags |= REQ_F_NOWAIT;
4231 else if (force_nonblock)
4232 flags |= MSG_DONTWAIT;
4233
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004234 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07004235 if (force_nonblock && ret == -EAGAIN)
4236 return -EAGAIN;
4237 if (ret == -ERESTARTSYS)
4238 ret = -EINTR;
4239 }
4240
Jens Axboebcda7ba2020-02-23 16:42:51 -07004241 kfree(kbuf);
4242 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004243 if (ret < 0)
4244 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004245 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004246 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004247}
4248
Jens Axboe3529d8c2019-12-19 18:24:38 -07004249static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004250{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004251 struct io_accept *accept = &req->accept;
4252
Jens Axboe17f2fe32019-10-17 14:42:58 -06004253 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4254 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004255 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004256 return -EINVAL;
4257
Jens Axboed55e5f52019-12-11 16:12:15 -07004258 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4259 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004260 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004261 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004262 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004263}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004264
Jens Axboe229a7b62020-06-22 10:13:11 -06004265static int io_accept(struct io_kiocb *req, bool force_nonblock,
4266 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004267{
4268 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004269 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004270 int ret;
4271
Jiufei Xuee697dee2020-06-10 13:41:59 +08004272 if (req->file->f_flags & O_NONBLOCK)
4273 req->flags |= REQ_F_NOWAIT;
4274
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004275 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004276 accept->addr_len, accept->flags,
4277 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004278 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004279 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004280 if (ret < 0) {
4281 if (ret == -ERESTARTSYS)
4282 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004283 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004284 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004285 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004286 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004287}
4288
Jens Axboe3529d8c2019-12-19 18:24:38 -07004289static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004290{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004291 struct io_connect *conn = &req->connect;
4292 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004293
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004294 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4295 return -EINVAL;
4296 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4297 return -EINVAL;
4298
Jens Axboe3529d8c2019-12-19 18:24:38 -07004299 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4300 conn->addr_len = READ_ONCE(sqe->addr2);
4301
4302 if (!io)
4303 return 0;
4304
4305 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004306 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004307}
4308
Jens Axboe229a7b62020-06-22 10:13:11 -06004309static int io_connect(struct io_kiocb *req, bool force_nonblock,
4310 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004311{
Jens Axboef499a022019-12-02 16:28:46 -07004312 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004313 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004314 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004315
Jens Axboef499a022019-12-02 16:28:46 -07004316 if (req->io) {
4317 io = req->io;
4318 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004319 ret = move_addr_to_kernel(req->connect.addr,
4320 req->connect.addr_len,
4321 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004322 if (ret)
4323 goto out;
4324 io = &__io;
4325 }
4326
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004327 file_flags = force_nonblock ? O_NONBLOCK : 0;
4328
4329 ret = __sys_connect_file(req->file, &io->connect.address,
4330 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004331 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004332 if (req->io)
4333 return -EAGAIN;
4334 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004335 ret = -ENOMEM;
4336 goto out;
4337 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004338 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004339 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004340 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004341 if (ret == -ERESTARTSYS)
4342 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004343out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004344 if (ret < 0)
4345 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004346 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004347 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004348}
YueHaibing469956e2020-03-04 15:53:52 +08004349#else /* !CONFIG_NET */
4350static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4351{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004352 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004353}
4354
YueHaibing469956e2020-03-04 15:53:52 +08004355static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004356{
YueHaibing469956e2020-03-04 15:53:52 +08004357 return -EOPNOTSUPP;
4358}
4359
4360static int io_send(struct io_kiocb *req, bool force_nonblock)
4361{
4362 return -EOPNOTSUPP;
4363}
4364
4365static int io_recvmsg_prep(struct io_kiocb *req,
4366 const struct io_uring_sqe *sqe)
4367{
4368 return -EOPNOTSUPP;
4369}
4370
4371static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4372{
4373 return -EOPNOTSUPP;
4374}
4375
4376static int io_recv(struct io_kiocb *req, bool force_nonblock)
4377{
4378 return -EOPNOTSUPP;
4379}
4380
4381static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4382{
4383 return -EOPNOTSUPP;
4384}
4385
4386static int io_accept(struct io_kiocb *req, bool force_nonblock)
4387{
4388 return -EOPNOTSUPP;
4389}
4390
4391static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4392{
4393 return -EOPNOTSUPP;
4394}
4395
4396static int io_connect(struct io_kiocb *req, bool force_nonblock)
4397{
4398 return -EOPNOTSUPP;
4399}
4400#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004401
Jens Axboed7718a92020-02-14 22:23:12 -07004402struct io_poll_table {
4403 struct poll_table_struct pt;
4404 struct io_kiocb *req;
4405 int error;
4406};
4407
Jens Axboed7718a92020-02-14 22:23:12 -07004408static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4409 __poll_t mask, task_work_func_t func)
4410{
4411 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004412 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004413
4414 /* for instances that support it check for an event match first: */
4415 if (mask && !(mask & poll->events))
4416 return 0;
4417
4418 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4419
4420 list_del_init(&poll->wait.entry);
4421
4422 tsk = req->task;
4423 req->result = mask;
4424 init_task_work(&req->task_work, func);
4425 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004426 * If this fails, then the task is exiting. When a task exits, the
4427 * work gets canceled, so just cancel this request as well instead
4428 * of executing it. We can't safely execute it anyway, as we may not
4429 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004430 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004431 ret = task_work_add(tsk, &req->task_work, true);
4432 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004433 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004434 tsk = io_wq_get_task(req->ctx->io_wq);
4435 task_work_add(tsk, &req->task_work, true);
4436 }
Jens Axboed7718a92020-02-14 22:23:12 -07004437 wake_up_process(tsk);
4438 return 1;
4439}
4440
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004441static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4442 __acquires(&req->ctx->completion_lock)
4443{
4444 struct io_ring_ctx *ctx = req->ctx;
4445
4446 if (!req->result && !READ_ONCE(poll->canceled)) {
4447 struct poll_table_struct pt = { ._key = poll->events };
4448
4449 req->result = vfs_poll(req->file, &pt) & poll->events;
4450 }
4451
4452 spin_lock_irq(&ctx->completion_lock);
4453 if (!req->result && !READ_ONCE(poll->canceled)) {
4454 add_wait_queue(poll->head, &poll->wait);
4455 return true;
4456 }
4457
4458 return false;
4459}
4460
Jens Axboe18bceab2020-05-15 11:56:54 -06004461static void io_poll_remove_double(struct io_kiocb *req)
4462{
4463 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4464
4465 lockdep_assert_held(&req->ctx->completion_lock);
4466
4467 if (poll && poll->head) {
4468 struct wait_queue_head *head = poll->head;
4469
4470 spin_lock(&head->lock);
4471 list_del_init(&poll->wait.entry);
4472 if (poll->wait.private)
4473 refcount_dec(&req->refs);
4474 poll->head = NULL;
4475 spin_unlock(&head->lock);
4476 }
4477}
4478
4479static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4480{
4481 struct io_ring_ctx *ctx = req->ctx;
4482
4483 io_poll_remove_double(req);
4484 req->poll.done = true;
4485 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4486 io_commit_cqring(ctx);
4487}
4488
4489static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4490{
4491 struct io_ring_ctx *ctx = req->ctx;
4492
4493 if (io_poll_rewait(req, &req->poll)) {
4494 spin_unlock_irq(&ctx->completion_lock);
4495 return;
4496 }
4497
4498 hash_del(&req->hash_node);
4499 io_poll_complete(req, req->result, 0);
4500 req->flags |= REQ_F_COMP_LOCKED;
4501 io_put_req_find_next(req, nxt);
4502 spin_unlock_irq(&ctx->completion_lock);
4503
4504 io_cqring_ev_posted(ctx);
4505}
4506
4507static void io_poll_task_func(struct callback_head *cb)
4508{
4509 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4510 struct io_kiocb *nxt = NULL;
4511
4512 io_poll_task_handler(req, &nxt);
4513 if (nxt) {
4514 struct io_ring_ctx *ctx = nxt->ctx;
4515
4516 mutex_lock(&ctx->uring_lock);
Jens Axboef13fad72020-06-22 09:34:30 -06004517 __io_queue_sqe(nxt, NULL, NULL);
Jens Axboe18bceab2020-05-15 11:56:54 -06004518 mutex_unlock(&ctx->uring_lock);
4519 }
4520}
4521
4522static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4523 int sync, void *key)
4524{
4525 struct io_kiocb *req = wait->private;
4526 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4527 __poll_t mask = key_to_poll(key);
4528
4529 /* for instances that support it check for an event match first: */
4530 if (mask && !(mask & poll->events))
4531 return 0;
4532
4533 if (req->poll.head) {
4534 bool done;
4535
4536 spin_lock(&req->poll.head->lock);
4537 done = list_empty(&req->poll.wait.entry);
4538 if (!done)
4539 list_del_init(&req->poll.wait.entry);
4540 spin_unlock(&req->poll.head->lock);
4541 if (!done)
4542 __io_async_wake(req, poll, mask, io_poll_task_func);
4543 }
4544 refcount_dec(&req->refs);
4545 return 1;
4546}
4547
4548static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4549 wait_queue_func_t wake_func)
4550{
4551 poll->head = NULL;
4552 poll->done = false;
4553 poll->canceled = false;
4554 poll->events = events;
4555 INIT_LIST_HEAD(&poll->wait.entry);
4556 init_waitqueue_func_entry(&poll->wait, wake_func);
4557}
4558
4559static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4560 struct wait_queue_head *head)
4561{
4562 struct io_kiocb *req = pt->req;
4563
4564 /*
4565 * If poll->head is already set, it's because the file being polled
4566 * uses multiple waitqueues for poll handling (eg one for read, one
4567 * for write). Setup a separate io_poll_iocb if this happens.
4568 */
4569 if (unlikely(poll->head)) {
4570 /* already have a 2nd entry, fail a third attempt */
4571 if (req->io) {
4572 pt->error = -EINVAL;
4573 return;
4574 }
4575 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4576 if (!poll) {
4577 pt->error = -ENOMEM;
4578 return;
4579 }
4580 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4581 refcount_inc(&req->refs);
4582 poll->wait.private = req;
4583 req->io = (void *) poll;
4584 }
4585
4586 pt->error = 0;
4587 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004588
4589 if (poll->events & EPOLLEXCLUSIVE)
4590 add_wait_queue_exclusive(head, &poll->wait);
4591 else
4592 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004593}
4594
4595static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4596 struct poll_table_struct *p)
4597{
4598 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4599
4600 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4601}
4602
Jens Axboed7718a92020-02-14 22:23:12 -07004603static void io_async_task_func(struct callback_head *cb)
4604{
4605 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4606 struct async_poll *apoll = req->apoll;
4607 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004608 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004609
4610 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4611
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004612 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004613 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004614 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004615 }
4616
Jens Axboe31067252020-05-17 17:43:31 -06004617 /* If req is still hashed, it cannot have been canceled. Don't check. */
4618 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004619 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004620 } else {
4621 canceled = READ_ONCE(apoll->poll.canceled);
4622 if (canceled) {
4623 io_cqring_fill_event(req, -ECANCELED);
4624 io_commit_cqring(ctx);
4625 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004626 }
4627
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004628 spin_unlock_irq(&ctx->completion_lock);
4629
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004630 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004631 if (req->flags & REQ_F_WORK_INITIALIZED)
4632 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004633 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004634
Jens Axboe31067252020-05-17 17:43:31 -06004635 if (!canceled) {
4636 __set_current_state(TASK_RUNNING);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004637 if (io_sq_thread_acquire_mm(ctx, req)) {
Jens Axboee1e16092020-06-22 09:17:17 -06004638 io_cqring_add_event(req, -EFAULT, 0);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004639 goto end_req;
4640 }
Jens Axboe31067252020-05-17 17:43:31 -06004641 mutex_lock(&ctx->uring_lock);
Jens Axboef13fad72020-06-22 09:34:30 -06004642 __io_queue_sqe(req, NULL, NULL);
Jens Axboe31067252020-05-17 17:43:31 -06004643 mutex_unlock(&ctx->uring_lock);
4644 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004645 io_cqring_ev_posted(ctx);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004646end_req:
Jens Axboe2bae0472020-04-13 11:16:34 -06004647 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004648 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004649 }
Jens Axboed7718a92020-02-14 22:23:12 -07004650}
4651
4652static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4653 void *key)
4654{
4655 struct io_kiocb *req = wait->private;
4656 struct io_poll_iocb *poll = &req->apoll->poll;
4657
4658 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4659 key_to_poll(key));
4660
4661 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4662}
4663
4664static void io_poll_req_insert(struct io_kiocb *req)
4665{
4666 struct io_ring_ctx *ctx = req->ctx;
4667 struct hlist_head *list;
4668
4669 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4670 hlist_add_head(&req->hash_node, list);
4671}
4672
4673static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4674 struct io_poll_iocb *poll,
4675 struct io_poll_table *ipt, __poll_t mask,
4676 wait_queue_func_t wake_func)
4677 __acquires(&ctx->completion_lock)
4678{
4679 struct io_ring_ctx *ctx = req->ctx;
4680 bool cancel = false;
4681
Jens Axboe18bceab2020-05-15 11:56:54 -06004682 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004683 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004684 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004685
4686 ipt->pt._key = mask;
4687 ipt->req = req;
4688 ipt->error = -EINVAL;
4689
Jens Axboed7718a92020-02-14 22:23:12 -07004690 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4691
4692 spin_lock_irq(&ctx->completion_lock);
4693 if (likely(poll->head)) {
4694 spin_lock(&poll->head->lock);
4695 if (unlikely(list_empty(&poll->wait.entry))) {
4696 if (ipt->error)
4697 cancel = true;
4698 ipt->error = 0;
4699 mask = 0;
4700 }
4701 if (mask || ipt->error)
4702 list_del_init(&poll->wait.entry);
4703 else if (cancel)
4704 WRITE_ONCE(poll->canceled, true);
4705 else if (!poll->done) /* actually waiting for an event */
4706 io_poll_req_insert(req);
4707 spin_unlock(&poll->head->lock);
4708 }
4709
4710 return mask;
4711}
4712
4713static bool io_arm_poll_handler(struct io_kiocb *req)
4714{
4715 const struct io_op_def *def = &io_op_defs[req->opcode];
4716 struct io_ring_ctx *ctx = req->ctx;
4717 struct async_poll *apoll;
4718 struct io_poll_table ipt;
4719 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004720 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004721
4722 if (!req->file || !file_can_poll(req->file))
4723 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004724 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004725 return false;
4726 if (!def->pollin && !def->pollout)
4727 return false;
4728
4729 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4730 if (unlikely(!apoll))
4731 return false;
4732
4733 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004734 if (req->flags & REQ_F_WORK_INITIALIZED)
4735 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004736 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004737
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004738 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004739 req->apoll = apoll;
4740 INIT_HLIST_NODE(&req->hash_node);
4741
Nathan Chancellor8755d972020-03-02 16:01:19 -07004742 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004743 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004744 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004745 if (def->pollout)
4746 mask |= POLLOUT | POLLWRNORM;
4747 mask |= POLLERR | POLLPRI;
4748
4749 ipt.pt._qproc = io_async_queue_proc;
4750
4751 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4752 io_async_wake);
4753 if (ret) {
4754 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004755 /* only remove double add if we did it here */
4756 if (!had_io)
4757 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004758 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004759 if (req->flags & REQ_F_WORK_INITIALIZED)
4760 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004761 kfree(apoll);
4762 return false;
4763 }
4764 spin_unlock_irq(&ctx->completion_lock);
4765 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4766 apoll->poll.events);
4767 return true;
4768}
4769
4770static bool __io_poll_remove_one(struct io_kiocb *req,
4771 struct io_poll_iocb *poll)
4772{
Jens Axboeb41e9852020-02-17 09:52:41 -07004773 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004774
4775 spin_lock(&poll->head->lock);
4776 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004777 if (!list_empty(&poll->wait.entry)) {
4778 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004779 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004780 }
4781 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004782 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004783 return do_complete;
4784}
4785
4786static bool io_poll_remove_one(struct io_kiocb *req)
4787{
4788 bool do_complete;
4789
4790 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004791 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004792 do_complete = __io_poll_remove_one(req, &req->poll);
4793 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004794 struct async_poll *apoll = req->apoll;
4795
Jens Axboed7718a92020-02-14 22:23:12 -07004796 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004797 do_complete = __io_poll_remove_one(req, &apoll->poll);
4798 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004799 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004800 /*
4801 * restore ->work because we will call
4802 * io_req_work_drop_env below when dropping the
4803 * final reference.
4804 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004805 if (req->flags & REQ_F_WORK_INITIALIZED)
4806 memcpy(&req->work, &apoll->work,
4807 sizeof(req->work));
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004808 kfree(apoll);
4809 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004810 }
4811
Jens Axboeb41e9852020-02-17 09:52:41 -07004812 if (do_complete) {
4813 io_cqring_fill_event(req, -ECANCELED);
4814 io_commit_cqring(req->ctx);
4815 req->flags |= REQ_F_COMP_LOCKED;
4816 io_put_req(req);
4817 }
4818
4819 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004820}
4821
4822static void io_poll_remove_all(struct io_ring_ctx *ctx)
4823{
Jens Axboe78076bb2019-12-04 19:56:40 -07004824 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004825 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004826 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004827
4828 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004829 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4830 struct hlist_head *list;
4831
4832 list = &ctx->cancel_hash[i];
4833 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004834 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004835 }
4836 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004837
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004838 if (posted)
4839 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004840}
4841
Jens Axboe47f46762019-11-09 17:43:02 -07004842static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4843{
Jens Axboe78076bb2019-12-04 19:56:40 -07004844 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004845 struct io_kiocb *req;
4846
Jens Axboe78076bb2019-12-04 19:56:40 -07004847 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4848 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004849 if (sqe_addr != req->user_data)
4850 continue;
4851 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004852 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004853 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004854 }
4855
4856 return -ENOENT;
4857}
4858
Jens Axboe3529d8c2019-12-19 18:24:38 -07004859static int io_poll_remove_prep(struct io_kiocb *req,
4860 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004861{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004862 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4863 return -EINVAL;
4864 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4865 sqe->poll_events)
4866 return -EINVAL;
4867
Jens Axboe0969e782019-12-17 18:40:57 -07004868 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004869 return 0;
4870}
4871
4872/*
4873 * Find a running poll command that matches one specified in sqe->addr,
4874 * and remove it if found.
4875 */
4876static int io_poll_remove(struct io_kiocb *req)
4877{
4878 struct io_ring_ctx *ctx = req->ctx;
4879 u64 addr;
4880 int ret;
4881
Jens Axboe0969e782019-12-17 18:40:57 -07004882 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004883 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004884 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004885 spin_unlock_irq(&ctx->completion_lock);
4886
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004887 if (ret < 0)
4888 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004889 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004890 return 0;
4891}
4892
Jens Axboe221c5eb2019-01-17 09:41:58 -07004893static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4894 void *key)
4895{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004896 struct io_kiocb *req = wait->private;
4897 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004898
Jens Axboed7718a92020-02-14 22:23:12 -07004899 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004900}
4901
Jens Axboe221c5eb2019-01-17 09:41:58 -07004902static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4903 struct poll_table_struct *p)
4904{
4905 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4906
Jens Axboed7718a92020-02-14 22:23:12 -07004907 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004908}
4909
Jens Axboe3529d8c2019-12-19 18:24:38 -07004910static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004911{
4912 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004913 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004914
4915 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4916 return -EINVAL;
4917 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4918 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004919 if (!poll->file)
4920 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004921
Jiufei Xue5769a352020-06-17 17:53:55 +08004922 events = READ_ONCE(sqe->poll32_events);
4923#ifdef __BIG_ENDIAN
4924 events = swahw32(events);
4925#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004926 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4927 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004928
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004929 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004930 return 0;
4931}
4932
Pavel Begunkov014db002020-03-03 21:33:12 +03004933static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004934{
4935 struct io_poll_iocb *poll = &req->poll;
4936 struct io_ring_ctx *ctx = req->ctx;
4937 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004938 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004939
Jens Axboe78076bb2019-12-04 19:56:40 -07004940 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004941 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004942 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004943
Jens Axboed7718a92020-02-14 22:23:12 -07004944 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4945 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004946
Jens Axboe8c838782019-03-12 15:48:16 -06004947 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004948 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004949 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004950 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004951 spin_unlock_irq(&ctx->completion_lock);
4952
Jens Axboe8c838782019-03-12 15:48:16 -06004953 if (mask) {
4954 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004955 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004956 }
Jens Axboe8c838782019-03-12 15:48:16 -06004957 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004958}
4959
Jens Axboe5262f562019-09-17 12:26:57 -06004960static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4961{
Jens Axboead8a48a2019-11-15 08:49:11 -07004962 struct io_timeout_data *data = container_of(timer,
4963 struct io_timeout_data, timer);
4964 struct io_kiocb *req = data->req;
4965 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004966 unsigned long flags;
4967
Jens Axboe5262f562019-09-17 12:26:57 -06004968 atomic_inc(&ctx->cq_timeouts);
4969
4970 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004971 /*
Jens Axboe11365042019-10-16 09:08:32 -06004972 * We could be racing with timeout deletion. If the list is empty,
4973 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004974 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004975 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004976 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004977
Jens Axboe78e19bb2019-11-06 15:21:34 -07004978 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004979 io_commit_cqring(ctx);
4980 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4981
4982 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004983 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004984 io_put_req(req);
4985 return HRTIMER_NORESTART;
4986}
4987
Jens Axboe47f46762019-11-09 17:43:02 -07004988static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4989{
4990 struct io_kiocb *req;
4991 int ret = -ENOENT;
4992
4993 list_for_each_entry(req, &ctx->timeout_list, list) {
4994 if (user_data == req->user_data) {
4995 list_del_init(&req->list);
4996 ret = 0;
4997 break;
4998 }
4999 }
5000
5001 if (ret == -ENOENT)
5002 return ret;
5003
Jens Axboe2d283902019-12-04 11:08:05 -07005004 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005005 if (ret == -1)
5006 return -EALREADY;
5007
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005008 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005009 io_cqring_fill_event(req, -ECANCELED);
5010 io_put_req(req);
5011 return 0;
5012}
5013
Jens Axboe3529d8c2019-12-19 18:24:38 -07005014static int io_timeout_remove_prep(struct io_kiocb *req,
5015 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005016{
Jens Axboeb29472e2019-12-17 18:50:29 -07005017 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5018 return -EINVAL;
5019 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
5020 return -EINVAL;
5021
5022 req->timeout.addr = READ_ONCE(sqe->addr);
5023 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5024 if (req->timeout.flags)
5025 return -EINVAL;
5026
Jens Axboeb29472e2019-12-17 18:50:29 -07005027 return 0;
5028}
5029
Jens Axboe11365042019-10-16 09:08:32 -06005030/*
5031 * Remove or update an existing timeout command
5032 */
Jens Axboefc4df992019-12-10 14:38:45 -07005033static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005034{
5035 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005036 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005037
Jens Axboe11365042019-10-16 09:08:32 -06005038 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005039 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005040
Jens Axboe47f46762019-11-09 17:43:02 -07005041 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005042 io_commit_cqring(ctx);
5043 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005044 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005045 if (ret < 0)
5046 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005047 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005048 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005049}
5050
Jens Axboe3529d8c2019-12-19 18:24:38 -07005051static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005052 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005053{
Jens Axboead8a48a2019-11-15 08:49:11 -07005054 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005055 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005056 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005057
Jens Axboead8a48a2019-11-15 08:49:11 -07005058 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005059 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005060 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005061 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005062 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005063 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005064 flags = READ_ONCE(sqe->timeout_flags);
5065 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005066 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005067
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005068 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005069
Jens Axboe3529d8c2019-12-19 18:24:38 -07005070 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005071 return -ENOMEM;
5072
5073 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005074 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005075 req->flags |= REQ_F_TIMEOUT;
5076
5077 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005078 return -EFAULT;
5079
Jens Axboe11365042019-10-16 09:08:32 -06005080 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005081 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005082 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005083 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005084
Jens Axboead8a48a2019-11-15 08:49:11 -07005085 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5086 return 0;
5087}
5088
Jens Axboefc4df992019-12-10 14:38:45 -07005089static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005090{
Jens Axboead8a48a2019-11-15 08:49:11 -07005091 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005092 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005093 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005094 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005095
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005096 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005097
Jens Axboe5262f562019-09-17 12:26:57 -06005098 /*
5099 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005100 * timeout event to be satisfied. If it isn't set, then this is
5101 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005102 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005103 if (!off) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005104 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07005105 entry = ctx->timeout_list.prev;
5106 goto add;
5107 }
Jens Axboe5262f562019-09-17 12:26:57 -06005108
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005109 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5110 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005111
5112 /*
5113 * Insertion sort, ensuring the first entry in the list is always
5114 * the one we need first.
5115 */
Jens Axboe5262f562019-09-17 12:26:57 -06005116 list_for_each_prev(entry, &ctx->timeout_list) {
5117 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06005118
Jens Axboe93bd25b2019-11-11 23:34:31 -07005119 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
5120 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005121 /* nxt.seq is behind @tail, otherwise would've been completed */
5122 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005123 break;
5124 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005125add:
Jens Axboe5262f562019-09-17 12:26:57 -06005126 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005127 data->timer.function = io_timeout_fn;
5128 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005129 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005130 return 0;
5131}
5132
Jens Axboe62755e32019-10-28 21:49:21 -06005133static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005134{
Jens Axboe62755e32019-10-28 21:49:21 -06005135 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005136
Jens Axboe62755e32019-10-28 21:49:21 -06005137 return req->user_data == (unsigned long) data;
5138}
5139
Jens Axboee977d6d2019-11-05 12:39:45 -07005140static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005141{
Jens Axboe62755e32019-10-28 21:49:21 -06005142 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005143 int ret = 0;
5144
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005145 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005146 switch (cancel_ret) {
5147 case IO_WQ_CANCEL_OK:
5148 ret = 0;
5149 break;
5150 case IO_WQ_CANCEL_RUNNING:
5151 ret = -EALREADY;
5152 break;
5153 case IO_WQ_CANCEL_NOTFOUND:
5154 ret = -ENOENT;
5155 break;
5156 }
5157
Jens Axboee977d6d2019-11-05 12:39:45 -07005158 return ret;
5159}
5160
Jens Axboe47f46762019-11-09 17:43:02 -07005161static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5162 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005163 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005164{
5165 unsigned long flags;
5166 int ret;
5167
5168 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5169 if (ret != -ENOENT) {
5170 spin_lock_irqsave(&ctx->completion_lock, flags);
5171 goto done;
5172 }
5173
5174 spin_lock_irqsave(&ctx->completion_lock, flags);
5175 ret = io_timeout_cancel(ctx, sqe_addr);
5176 if (ret != -ENOENT)
5177 goto done;
5178 ret = io_poll_cancel(ctx, sqe_addr);
5179done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005180 if (!ret)
5181 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005182 io_cqring_fill_event(req, ret);
5183 io_commit_cqring(ctx);
5184 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5185 io_cqring_ev_posted(ctx);
5186
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005187 if (ret < 0)
5188 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005189 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005190}
5191
Jens Axboe3529d8c2019-12-19 18:24:38 -07005192static int io_async_cancel_prep(struct io_kiocb *req,
5193 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005194{
Jens Axboefbf23842019-12-17 18:45:56 -07005195 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005196 return -EINVAL;
5197 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
5198 sqe->cancel_flags)
5199 return -EINVAL;
5200
Jens Axboefbf23842019-12-17 18:45:56 -07005201 req->cancel.addr = READ_ONCE(sqe->addr);
5202 return 0;
5203}
5204
Pavel Begunkov014db002020-03-03 21:33:12 +03005205static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005206{
5207 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005208
Pavel Begunkov014db002020-03-03 21:33:12 +03005209 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005210 return 0;
5211}
5212
Jens Axboe05f3fb32019-12-09 11:22:50 -07005213static int io_files_update_prep(struct io_kiocb *req,
5214 const struct io_uring_sqe *sqe)
5215{
5216 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
5217 return -EINVAL;
5218
5219 req->files_update.offset = READ_ONCE(sqe->off);
5220 req->files_update.nr_args = READ_ONCE(sqe->len);
5221 if (!req->files_update.nr_args)
5222 return -EINVAL;
5223 req->files_update.arg = READ_ONCE(sqe->addr);
5224 return 0;
5225}
5226
Jens Axboe229a7b62020-06-22 10:13:11 -06005227static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5228 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005229{
5230 struct io_ring_ctx *ctx = req->ctx;
5231 struct io_uring_files_update up;
5232 int ret;
5233
Jens Axboef86cd202020-01-29 13:46:44 -07005234 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005235 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005236
5237 up.offset = req->files_update.offset;
5238 up.fds = req->files_update.arg;
5239
5240 mutex_lock(&ctx->uring_lock);
5241 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5242 mutex_unlock(&ctx->uring_lock);
5243
5244 if (ret < 0)
5245 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005246 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005247 return 0;
5248}
5249
Jens Axboe3529d8c2019-12-19 18:24:38 -07005250static int io_req_defer_prep(struct io_kiocb *req,
Jens Axboec40f6372020-06-25 15:39:59 -06005251 const struct io_uring_sqe *sqe, bool for_async)
Jens Axboef67676d2019-12-02 11:03:47 -07005252{
Jens Axboee7815732019-12-17 19:45:06 -07005253 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005254
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005255 if (!sqe)
5256 return 0;
5257
Jens Axboec40f6372020-06-25 15:39:59 -06005258 if (for_async || (req->flags & REQ_F_WORK_INITIALIZED)) {
5259 io_req_init_async(req);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005260
Jens Axboec40f6372020-06-25 15:39:59 -06005261 if (io_op_defs[req->opcode].file_table) {
5262 ret = io_grab_files(req);
5263 if (unlikely(ret))
5264 return ret;
5265 }
5266
5267 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
Jens Axboef86cd202020-01-29 13:46:44 -07005268 }
5269
Jens Axboed625c6e2019-12-17 19:53:05 -07005270 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005271 case IORING_OP_NOP:
5272 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005273 case IORING_OP_READV:
5274 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005275 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005276 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005277 break;
5278 case IORING_OP_WRITEV:
5279 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005280 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005281 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005282 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005283 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005284 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005285 break;
5286 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005287 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005288 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005289 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005290 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005291 break;
5292 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005293 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005294 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005295 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005296 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005297 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005298 break;
5299 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005300 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005301 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005302 break;
Jens Axboef499a022019-12-02 16:28:46 -07005303 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005304 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005305 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005306 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005307 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005308 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005309 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005310 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005311 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005312 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005313 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005314 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005315 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005316 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005317 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005318 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005319 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005320 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005321 case IORING_OP_FALLOCATE:
5322 ret = io_fallocate_prep(req, sqe);
5323 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005324 case IORING_OP_OPENAT:
5325 ret = io_openat_prep(req, sqe);
5326 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005327 case IORING_OP_CLOSE:
5328 ret = io_close_prep(req, sqe);
5329 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005330 case IORING_OP_FILES_UPDATE:
5331 ret = io_files_update_prep(req, sqe);
5332 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005333 case IORING_OP_STATX:
5334 ret = io_statx_prep(req, sqe);
5335 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005336 case IORING_OP_FADVISE:
5337 ret = io_fadvise_prep(req, sqe);
5338 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005339 case IORING_OP_MADVISE:
5340 ret = io_madvise_prep(req, sqe);
5341 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005342 case IORING_OP_OPENAT2:
5343 ret = io_openat2_prep(req, sqe);
5344 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005345 case IORING_OP_EPOLL_CTL:
5346 ret = io_epoll_ctl_prep(req, sqe);
5347 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005348 case IORING_OP_SPLICE:
5349 ret = io_splice_prep(req, sqe);
5350 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005351 case IORING_OP_PROVIDE_BUFFERS:
5352 ret = io_provide_buffers_prep(req, sqe);
5353 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005354 case IORING_OP_REMOVE_BUFFERS:
5355 ret = io_remove_buffers_prep(req, sqe);
5356 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005357 case IORING_OP_TEE:
5358 ret = io_tee_prep(req, sqe);
5359 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005360 default:
Jens Axboee7815732019-12-17 19:45:06 -07005361 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5362 req->opcode);
5363 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005364 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005365 }
5366
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005367 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005368}
5369
Jens Axboe3529d8c2019-12-19 18:24:38 -07005370static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005371{
Jackie Liua197f662019-11-08 08:09:12 -07005372 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005373 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005374
Bob Liu9d858b22019-11-13 18:06:25 +08005375 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005376 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005377 return 0;
5378
Pavel Begunkov650b5482020-05-17 14:02:11 +03005379 if (!req->io) {
5380 if (io_alloc_async_ctx(req))
5381 return -EAGAIN;
Jens Axboec40f6372020-06-25 15:39:59 -06005382 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkov650b5482020-05-17 14:02:11 +03005383 if (ret < 0)
5384 return ret;
5385 }
Jens Axboe2d283902019-12-04 11:08:05 -07005386
Jens Axboede0617e2019-04-06 21:51:27 -06005387 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005388 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005389 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005390 return 0;
5391 }
5392
Jens Axboe915967f2019-11-21 09:01:20 -07005393 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005394 list_add_tail(&req->list, &ctx->defer_list);
5395 spin_unlock_irq(&ctx->completion_lock);
5396 return -EIOCBQUEUED;
5397}
5398
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005399static void io_cleanup_req(struct io_kiocb *req)
5400{
5401 struct io_async_ctx *io = req->io;
5402
5403 switch (req->opcode) {
5404 case IORING_OP_READV:
5405 case IORING_OP_READ_FIXED:
5406 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005407 if (req->flags & REQ_F_BUFFER_SELECTED)
5408 kfree((void *)(unsigned long)req->rw.addr);
5409 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005410 case IORING_OP_WRITEV:
5411 case IORING_OP_WRITE_FIXED:
5412 case IORING_OP_WRITE:
5413 if (io->rw.iov != io->rw.fast_iov)
5414 kfree(io->rw.iov);
5415 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005416 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005417 if (req->flags & REQ_F_BUFFER_SELECTED)
5418 kfree(req->sr_msg.kbuf);
5419 /* fallthrough */
5420 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005421 if (io->msg.iov != io->msg.fast_iov)
5422 kfree(io->msg.iov);
5423 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005424 case IORING_OP_RECV:
5425 if (req->flags & REQ_F_BUFFER_SELECTED)
5426 kfree(req->sr_msg.kbuf);
5427 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005428 case IORING_OP_OPENAT:
5429 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005430 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005431 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005432 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005433 io_put_file(req, req->splice.file_in,
5434 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5435 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005436 }
5437
5438 req->flags &= ~REQ_F_NEED_CLEANUP;
5439}
5440
Jens Axboe3529d8c2019-12-19 18:24:38 -07005441static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005442 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005443{
Jackie Liua197f662019-11-08 08:09:12 -07005444 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005445 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005446
Jens Axboed625c6e2019-12-17 19:53:05 -07005447 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005448 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005449 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005450 break;
5451 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005452 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005453 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005454 if (sqe) {
5455 ret = io_read_prep(req, sqe, force_nonblock);
5456 if (ret < 0)
5457 break;
5458 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005459 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005460 break;
5461 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005462 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005463 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005464 if (sqe) {
5465 ret = io_write_prep(req, sqe, force_nonblock);
5466 if (ret < 0)
5467 break;
5468 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005469 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005470 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005471 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005472 if (sqe) {
5473 ret = io_prep_fsync(req, sqe);
5474 if (ret < 0)
5475 break;
5476 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005477 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005478 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005479 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005480 if (sqe) {
5481 ret = io_poll_add_prep(req, sqe);
5482 if (ret)
5483 break;
5484 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005485 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005486 break;
5487 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005488 if (sqe) {
5489 ret = io_poll_remove_prep(req, sqe);
5490 if (ret < 0)
5491 break;
5492 }
Jens Axboefc4df992019-12-10 14:38:45 -07005493 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005494 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005495 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005496 if (sqe) {
5497 ret = io_prep_sfr(req, sqe);
5498 if (ret < 0)
5499 break;
5500 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005501 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005502 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005503 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005504 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005505 if (sqe) {
5506 ret = io_sendmsg_prep(req, sqe);
5507 if (ret < 0)
5508 break;
5509 }
Jens Axboefddafac2020-01-04 20:19:44 -07005510 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005511 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005512 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005513 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005514 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005515 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005516 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005517 if (sqe) {
5518 ret = io_recvmsg_prep(req, sqe);
5519 if (ret)
5520 break;
5521 }
Jens Axboefddafac2020-01-04 20:19:44 -07005522 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005523 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005524 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005525 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005526 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005527 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005528 if (sqe) {
5529 ret = io_timeout_prep(req, sqe, false);
5530 if (ret)
5531 break;
5532 }
Jens Axboefc4df992019-12-10 14:38:45 -07005533 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005534 break;
Jens Axboe11365042019-10-16 09:08:32 -06005535 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005536 if (sqe) {
5537 ret = io_timeout_remove_prep(req, sqe);
5538 if (ret)
5539 break;
5540 }
Jens Axboefc4df992019-12-10 14:38:45 -07005541 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005542 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005543 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005544 if (sqe) {
5545 ret = io_accept_prep(req, sqe);
5546 if (ret)
5547 break;
5548 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005549 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005550 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005551 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005552 if (sqe) {
5553 ret = io_connect_prep(req, sqe);
5554 if (ret)
5555 break;
5556 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005557 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005558 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005559 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005560 if (sqe) {
5561 ret = io_async_cancel_prep(req, sqe);
5562 if (ret)
5563 break;
5564 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005565 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005566 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005567 case IORING_OP_FALLOCATE:
5568 if (sqe) {
5569 ret = io_fallocate_prep(req, sqe);
5570 if (ret)
5571 break;
5572 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005573 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005574 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005575 case IORING_OP_OPENAT:
5576 if (sqe) {
5577 ret = io_openat_prep(req, sqe);
5578 if (ret)
5579 break;
5580 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005581 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005582 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005583 case IORING_OP_CLOSE:
5584 if (sqe) {
5585 ret = io_close_prep(req, sqe);
5586 if (ret)
5587 break;
5588 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005589 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005590 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005591 case IORING_OP_FILES_UPDATE:
5592 if (sqe) {
5593 ret = io_files_update_prep(req, sqe);
5594 if (ret)
5595 break;
5596 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005597 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005598 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005599 case IORING_OP_STATX:
5600 if (sqe) {
5601 ret = io_statx_prep(req, sqe);
5602 if (ret)
5603 break;
5604 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005605 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005606 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005607 case IORING_OP_FADVISE:
5608 if (sqe) {
5609 ret = io_fadvise_prep(req, sqe);
5610 if (ret)
5611 break;
5612 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005613 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005614 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005615 case IORING_OP_MADVISE:
5616 if (sqe) {
5617 ret = io_madvise_prep(req, sqe);
5618 if (ret)
5619 break;
5620 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005621 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005622 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005623 case IORING_OP_OPENAT2:
5624 if (sqe) {
5625 ret = io_openat2_prep(req, sqe);
5626 if (ret)
5627 break;
5628 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005629 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005630 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005631 case IORING_OP_EPOLL_CTL:
5632 if (sqe) {
5633 ret = io_epoll_ctl_prep(req, sqe);
5634 if (ret)
5635 break;
5636 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005637 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005638 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005639 case IORING_OP_SPLICE:
5640 if (sqe) {
5641 ret = io_splice_prep(req, sqe);
5642 if (ret < 0)
5643 break;
5644 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005645 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005646 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005647 case IORING_OP_PROVIDE_BUFFERS:
5648 if (sqe) {
5649 ret = io_provide_buffers_prep(req, sqe);
5650 if (ret)
5651 break;
5652 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005653 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005654 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005655 case IORING_OP_REMOVE_BUFFERS:
5656 if (sqe) {
5657 ret = io_remove_buffers_prep(req, sqe);
5658 if (ret)
5659 break;
5660 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005661 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005662 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005663 case IORING_OP_TEE:
5664 if (sqe) {
5665 ret = io_tee_prep(req, sqe);
5666 if (ret < 0)
5667 break;
5668 }
5669 ret = io_tee(req, force_nonblock);
5670 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005671 default:
5672 ret = -EINVAL;
5673 break;
5674 }
5675
5676 if (ret)
5677 return ret;
5678
Jens Axboeb5325762020-05-19 21:20:27 -06005679 /* If the op doesn't have a file, we're not polling for it */
5680 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005681 const bool in_async = io_wq_current_is_worker();
5682
Jens Axboe9e645e112019-05-10 16:07:28 -06005683 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005684 return -EAGAIN;
5685
Jens Axboe11ba8202020-01-15 21:51:17 -07005686 /* workqueue context doesn't hold uring_lock, grab it now */
5687 if (in_async)
5688 mutex_lock(&ctx->uring_lock);
5689
Jens Axboe2b188cc2019-01-07 10:46:33 -07005690 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005691
5692 if (in_async)
5693 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005694 }
5695
5696 return 0;
5697}
5698
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005699static void io_arm_async_linked_timeout(struct io_kiocb *req)
5700{
5701 struct io_kiocb *link;
5702
5703 /* link head's timeout is queued in io_queue_async_work() */
5704 if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5705 return;
5706
5707 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5708 io_queue_linked_timeout(link);
5709}
5710
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005711static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Jens Axboedef596e2019-01-09 08:59:42 -07005712{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005713 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005714 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005715
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005716 io_arm_async_linked_timeout(req);
5717
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005718 /* if NO_CANCEL is set, we must still run the work */
5719 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5720 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005721 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005722 }
Jens Axboe31b51512019-01-18 22:56:34 -07005723
Jens Axboe561fb042019-10-24 07:25:42 -06005724 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005725 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005726 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005727 /*
5728 * We can get EAGAIN for polled IO even though we're
5729 * forcing a sync submission from here, since we can't
5730 * wait for request slots on the block side.
5731 */
5732 if (ret != -EAGAIN)
5733 break;
5734 cond_resched();
5735 } while (1);
5736 }
Jens Axboe31b51512019-01-18 22:56:34 -07005737
Jens Axboe561fb042019-10-24 07:25:42 -06005738 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005739 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005740 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005741 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005742
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005743 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005744}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005745
Jens Axboe65e19f52019-10-26 07:20:21 -06005746static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5747 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005748{
Jens Axboe65e19f52019-10-26 07:20:21 -06005749 struct fixed_file_table *table;
5750
Jens Axboe05f3fb32019-12-09 11:22:50 -07005751 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005752 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005753}
5754
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005755static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5756 int fd, struct file **out_file, bool fixed)
5757{
5758 struct io_ring_ctx *ctx = req->ctx;
5759 struct file *file;
5760
5761 if (fixed) {
5762 if (unlikely(!ctx->file_data ||
5763 (unsigned) fd >= ctx->nr_user_files))
5764 return -EBADF;
5765 fd = array_index_nospec(fd, ctx->nr_user_files);
5766 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005767 if (file) {
5768 req->fixed_file_refs = ctx->file_data->cur_refs;
5769 percpu_ref_get(req->fixed_file_refs);
5770 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005771 } else {
5772 trace_io_uring_file_get(ctx, fd);
5773 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005774 }
5775
Jens Axboefd2206e2020-06-02 16:40:47 -06005776 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5777 *out_file = file;
5778 return 0;
5779 }
5780 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005781}
5782
Jens Axboe3529d8c2019-12-19 18:24:38 -07005783static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005784 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005785{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005786 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005787
Jens Axboe63ff8222020-05-07 14:56:15 -06005788 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005789 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005790 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005791
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005792 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005793}
5794
Jackie Liua197f662019-11-08 08:09:12 -07005795static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005796{
Jens Axboefcb323c2019-10-24 12:39:47 -06005797 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005798 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005799
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005800 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005801 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005802 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005803 return -EBADF;
5804
Jens Axboefcb323c2019-10-24 12:39:47 -06005805 rcu_read_lock();
5806 spin_lock_irq(&ctx->inflight_lock);
5807 /*
5808 * We use the f_ops->flush() handler to ensure that we can flush
5809 * out work accessing these files if the fd is closed. Check if
5810 * the fd has changed since we started down this path, and disallow
5811 * this operation if it has.
5812 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005813 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005814 list_add(&req->inflight_entry, &ctx->inflight_list);
5815 req->flags |= REQ_F_INFLIGHT;
5816 req->work.files = current->files;
5817 ret = 0;
5818 }
5819 spin_unlock_irq(&ctx->inflight_lock);
5820 rcu_read_unlock();
5821
5822 return ret;
5823}
5824
Jens Axboe2665abf2019-11-05 12:40:47 -07005825static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5826{
Jens Axboead8a48a2019-11-15 08:49:11 -07005827 struct io_timeout_data *data = container_of(timer,
5828 struct io_timeout_data, timer);
5829 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005830 struct io_ring_ctx *ctx = req->ctx;
5831 struct io_kiocb *prev = NULL;
5832 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005833
5834 spin_lock_irqsave(&ctx->completion_lock, flags);
5835
5836 /*
5837 * We don't expect the list to be empty, that will only happen if we
5838 * race with the completion of the linked work.
5839 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005840 if (!list_empty(&req->link_list)) {
5841 prev = list_entry(req->link_list.prev, struct io_kiocb,
5842 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005843 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005844 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005845 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5846 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005847 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005848 }
5849
5850 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5851
5852 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005853 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005854 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005855 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005856 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005857 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005858 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005859 return HRTIMER_NORESTART;
5860}
5861
Jens Axboead8a48a2019-11-15 08:49:11 -07005862static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005863{
Jens Axboe76a46e02019-11-10 23:34:16 -07005864 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005865
Jens Axboe76a46e02019-11-10 23:34:16 -07005866 /*
5867 * If the list is now empty, then our linked request finished before
5868 * we got a chance to setup the timer
5869 */
5870 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005871 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005872 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005873
Jens Axboead8a48a2019-11-15 08:49:11 -07005874 data->timer.function = io_link_timeout_fn;
5875 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5876 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005877 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005878 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005879
Jens Axboe2665abf2019-11-05 12:40:47 -07005880 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005881 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005882}
5883
Jens Axboead8a48a2019-11-15 08:49:11 -07005884static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005885{
5886 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005887
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005888 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005889 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005890 /* for polled retry, if flag is set, we already went through here */
5891 if (req->flags & REQ_F_POLLED)
5892 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005893
Pavel Begunkov44932332019-12-05 16:16:35 +03005894 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5895 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005896 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005897 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005898
Jens Axboe76a46e02019-11-10 23:34:16 -07005899 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005900 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005901}
5902
Jens Axboef13fad72020-06-22 09:34:30 -06005903static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5904 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005905{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005906 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005907 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005908 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005909 int ret;
5910
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005911again:
5912 linked_timeout = io_prep_linked_timeout(req);
5913
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005914 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5915 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005916 if (old_creds)
5917 revert_creds(old_creds);
5918 if (old_creds == req->work.creds)
5919 old_creds = NULL; /* restored original creds */
5920 else
5921 old_creds = override_creds(req->work.creds);
5922 }
5923
Jens Axboef13fad72020-06-22 09:34:30 -06005924 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06005925
5926 /*
5927 * We async punt it if the file wasn't marked NOWAIT, or if the file
5928 * doesn't support non-blocking read/write attempts
5929 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005930 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005931 if (io_arm_poll_handler(req)) {
5932 if (linked_timeout)
5933 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005934 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005935 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005936punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005937 io_req_init_async(req);
5938
Jens Axboef86cd202020-01-29 13:46:44 -07005939 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005940 ret = io_grab_files(req);
5941 if (ret)
5942 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005943 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005944
5945 /*
5946 * Queued up for async execution, worker will release
5947 * submit reference when the iocb is actually submitted.
5948 */
5949 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005950 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005951 }
Jens Axboee65ef562019-03-12 10:16:44 -06005952
Jens Axboefcb323c2019-10-24 12:39:47 -06005953err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005954 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005955 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005956 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005957
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005958 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005959 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005960 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005961 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005962 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005963 }
5964
Jens Axboee65ef562019-03-12 10:16:44 -06005965 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005966 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005967 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005968 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06005969 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005970 if (nxt) {
5971 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005972
5973 if (req->flags & REQ_F_FORCE_ASYNC)
5974 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005975 goto again;
5976 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005977exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005978 if (old_creds)
5979 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005980}
5981
Jens Axboef13fad72020-06-22 09:34:30 -06005982static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5983 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005984{
5985 int ret;
5986
Jens Axboe3529d8c2019-12-19 18:24:38 -07005987 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005988 if (ret) {
5989 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005990fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005991 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005992 io_put_req(req);
5993 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005994 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005995 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005996 if (!req->io) {
5997 ret = -EAGAIN;
5998 if (io_alloc_async_ctx(req))
5999 goto fail_req;
Jens Axboec40f6372020-06-25 15:39:59 -06006000 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006001 if (unlikely(ret < 0))
6002 goto fail_req;
6003 }
6004
Jens Axboece35a472019-12-17 08:04:44 -07006005 /*
6006 * Never try inline submit of IOSQE_ASYNC is set, go straight
6007 * to async execution.
6008 */
6009 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6010 io_queue_async_work(req);
6011 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006012 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006013 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006014}
6015
Jens Axboef13fad72020-06-22 09:34:30 -06006016static inline void io_queue_link_head(struct io_kiocb *req,
6017 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006018{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006019 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006020 io_put_req(req);
6021 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006022 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006023 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006024}
6025
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006026static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006027 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006028{
Jackie Liua197f662019-11-08 08:09:12 -07006029 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006030 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006031
Jens Axboe9e645e112019-05-10 16:07:28 -06006032 /*
6033 * If we already have a head request, queue this one for async
6034 * submittal once the head completes. If we don't have a head but
6035 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6036 * submitted sync once the chain is complete. If none of those
6037 * conditions are true (normal request), then just queue it.
6038 */
6039 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006040 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006041
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006042 /*
6043 * Taking sequential execution of a link, draining both sides
6044 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6045 * requests in the link. So, it drains the head and the
6046 * next after the link request. The last one is done via
6047 * drain_next flag to persist the effect across calls.
6048 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006049 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006050 head->flags |= REQ_F_IO_DRAIN;
6051 ctx->drain_next = 1;
6052 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006053 if (io_alloc_async_ctx(req))
6054 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06006055
Jens Axboec40f6372020-06-25 15:39:59 -06006056 ret = io_req_defer_prep(req, sqe, false);
Jens Axboe2d283902019-12-04 11:08:05 -07006057 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006058 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006059 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006060 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006061 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006062 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006063 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006064 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006065
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006066 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006067 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006068 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006069 *link = NULL;
6070 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006071 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006072 if (unlikely(ctx->drain_next)) {
6073 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006074 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006075 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006076 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006077 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006078 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006079
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006080 if (io_alloc_async_ctx(req))
6081 return -EAGAIN;
6082
Jens Axboec40f6372020-06-25 15:39:59 -06006083 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkov711be032020-01-17 03:57:59 +03006084 if (ret)
6085 req->flags |= REQ_F_FAIL_LINK;
6086 *link = req;
6087 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006088 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006089 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006090 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006091
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006092 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006093}
6094
Jens Axboe9a56a232019-01-09 09:06:50 -07006095/*
6096 * Batched submission is done, ensure local IO is flushed out.
6097 */
6098static void io_submit_state_end(struct io_submit_state *state)
6099{
Jens Axboef13fad72020-06-22 09:34:30 -06006100 if (!list_empty(&state->comp.list))
6101 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006102 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006103 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006104 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006105 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006106}
6107
6108/*
6109 * Start submission side cache.
6110 */
6111static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006112 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006113{
6114 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006115#ifdef CONFIG_BLOCK
6116 state->plug.nowait = true;
6117#endif
Jens Axboe013538b2020-06-22 09:29:15 -06006118 state->comp.nr = 0;
6119 INIT_LIST_HEAD(&state->comp.list);
6120 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006121 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006122 state->file = NULL;
6123 state->ios_left = max_ios;
6124}
6125
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126static void io_commit_sqring(struct io_ring_ctx *ctx)
6127{
Hristo Venev75b28af2019-08-26 17:23:46 +00006128 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006129
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006130 /*
6131 * Ensure any loads from the SQEs are done at this point,
6132 * since once we write the new head, the application could
6133 * write new data to them.
6134 */
6135 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006136}
6137
6138/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006139 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006140 * that is mapped by userspace. This means that care needs to be taken to
6141 * ensure that reads are stable, as we cannot rely on userspace always
6142 * being a good citizen. If members of the sqe are validated and then later
6143 * used, it's important that those reads are done through READ_ONCE() to
6144 * prevent a re-load down the line.
6145 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006146static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147{
Hristo Venev75b28af2019-08-26 17:23:46 +00006148 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006149 unsigned head;
6150
6151 /*
6152 * The cached sq head (or cq tail) serves two purposes:
6153 *
6154 * 1) allows us to batch the cost of updating the user visible
6155 * head updates.
6156 * 2) allows the kernel side to track the head on its own, even
6157 * though the application is the one updating it.
6158 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006159 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006160 if (likely(head < ctx->sq_entries))
6161 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006162
6163 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006164 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006165 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006166 return NULL;
6167}
6168
6169static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6170{
6171 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006172}
6173
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006174#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6175 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6176 IOSQE_BUFFER_SELECT)
6177
6178static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6179 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006180 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006181{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006182 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006183 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006184
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006185 /*
6186 * All io need record the previous position, if LINK vs DARIN,
6187 * it can be used to mark the position of the first IO in the
6188 * link list.
6189 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03006190 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006191 req->opcode = READ_ONCE(sqe->opcode);
6192 req->user_data = READ_ONCE(sqe->user_data);
6193 req->io = NULL;
6194 req->file = NULL;
6195 req->ctx = ctx;
6196 req->flags = 0;
6197 /* one is dropped after submission, the other at completion */
6198 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006199 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006200 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006201
6202 if (unlikely(req->opcode >= IORING_OP_LAST))
6203 return -EINVAL;
6204
Jens Axboe9d8426a2020-06-16 18:42:49 -06006205 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6206 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006207
6208 sqe_flags = READ_ONCE(sqe->flags);
6209 /* enforce forwards compatibility on users */
6210 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6211 return -EINVAL;
6212
6213 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6214 !io_op_defs[req->opcode].buffer_select)
6215 return -EOPNOTSUPP;
6216
6217 id = READ_ONCE(sqe->personality);
6218 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006219 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006220 req->work.creds = idr_find(&ctx->personality_idr, id);
6221 if (unlikely(!req->work.creds))
6222 return -EINVAL;
6223 get_cred(req->work.creds);
6224 }
6225
6226 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006227 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006228
Jens Axboe63ff8222020-05-07 14:56:15 -06006229 if (!io_op_defs[req->opcode].needs_file)
6230 return 0;
6231
6232 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006233}
6234
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006235static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006236 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006237{
Jens Axboeac8691c2020-06-01 08:30:41 -06006238 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006239 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006240 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006241
Jens Axboec4a2ed72019-11-21 21:01:26 -07006242 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006243 if (test_bit(0, &ctx->sq_check_overflow)) {
6244 if (!list_empty(&ctx->cq_overflow_list) &&
6245 !io_cqring_overflow_flush(ctx, false))
6246 return -EBUSY;
6247 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006248
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006249 /* make sure SQ entry isn't read before tail */
6250 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006251
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006252 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6253 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006254
Jens Axboe013538b2020-06-22 09:29:15 -06006255 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006256
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006257 ctx->ring_fd = ring_fd;
6258 ctx->ring_file = ring_file;
6259
Jens Axboe6c271ce2019-01-10 11:22:30 -07006260 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006261 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006262 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006263 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006264
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006265 sqe = io_get_sqe(ctx);
6266 if (unlikely(!sqe)) {
6267 io_consume_sqe(ctx);
6268 break;
6269 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006270 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006271 if (unlikely(!req)) {
6272 if (!submitted)
6273 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006274 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006275 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006276
Jens Axboeac8691c2020-06-01 08:30:41 -06006277 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006278 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006279 /* will complete beyond this point, count as submitted */
6280 submitted++;
6281
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006282 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006283fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006284 io_put_req(req);
6285 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006286 break;
6287 }
6288
Jens Axboe354420f2020-01-08 18:55:15 -07006289 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006290 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006291 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006292 if (err)
6293 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006294 }
6295
Pavel Begunkov9466f432020-01-25 22:34:01 +03006296 if (unlikely(submitted != nr)) {
6297 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6298
6299 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6300 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006301 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006302 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006303 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006304
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006305 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6306 io_commit_sqring(ctx);
6307
Jens Axboe6c271ce2019-01-10 11:22:30 -07006308 return submitted;
6309}
6310
6311static int io_sq_thread(void *data)
6312{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006313 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006314 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006315 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006316 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006317 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006318
Jens Axboe0f158b42020-05-14 17:18:39 -06006319 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006320
Jens Axboe181e4482019-11-25 08:52:30 -07006321 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006322
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006323 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006324 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006325 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006326
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006327 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006328 unsigned nr_events = 0;
6329
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006330 mutex_lock(&ctx->uring_lock);
6331 if (!list_empty(&ctx->poll_list))
6332 io_iopoll_getevents(ctx, &nr_events, 0);
6333 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006334 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006335 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006336 }
6337
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006338 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006339
6340 /*
6341 * If submit got -EBUSY, flag us as needing the application
6342 * to enter the kernel to reap and flush events.
6343 */
6344 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006345 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006346 * Drop cur_mm before scheduling, we can't hold it for
6347 * long periods (or over schedule()). Do this before
6348 * adding ourselves to the waitqueue, as the unuse/drop
6349 * may sleep.
6350 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006351 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006352
6353 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006354 * We're polling. If we're within the defined idle
6355 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006356 * to sleep. The exception is if we got EBUSY doing
6357 * more IO, we should wait for the application to
6358 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006359 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006360 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07006361 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6362 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006363 if (current->task_works)
6364 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006365 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006366 continue;
6367 }
6368
Jens Axboe6c271ce2019-01-10 11:22:30 -07006369 prepare_to_wait(&ctx->sqo_wait, &wait,
6370 TASK_INTERRUPTIBLE);
6371
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006372 /*
6373 * While doing polled IO, before going to sleep, we need
6374 * to check if there are new reqs added to poll_list, it
6375 * is because reqs may have been punted to io worker and
6376 * will be added to poll_list later, hence check the
6377 * poll_list again.
6378 */
6379 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6380 !list_empty_careful(&ctx->poll_list)) {
6381 finish_wait(&ctx->sqo_wait, &wait);
6382 continue;
6383 }
6384
Jens Axboe6c271ce2019-01-10 11:22:30 -07006385 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006386 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006387 /* make sure to read SQ tail after writing flags */
6388 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006389
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006390 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006391 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006392 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006393 finish_wait(&ctx->sqo_wait, &wait);
6394 break;
6395 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006396 if (current->task_works) {
6397 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006398 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006399 continue;
6400 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006401 if (signal_pending(current))
6402 flush_signals(current);
6403 schedule();
6404 finish_wait(&ctx->sqo_wait, &wait);
6405
Hristo Venev75b28af2019-08-26 17:23:46 +00006406 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006407 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006408 continue;
6409 }
6410 finish_wait(&ctx->sqo_wait, &wait);
6411
Hristo Venev75b28af2019-08-26 17:23:46 +00006412 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006413 }
6414
Jens Axboe8a4955f2019-12-09 14:52:35 -07006415 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006416 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6417 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006418 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006419 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006420 }
6421
Jens Axboeb41e9852020-02-17 09:52:41 -07006422 if (current->task_works)
6423 task_work_run();
6424
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006425 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006426 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006427
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006428 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006429
Jens Axboe6c271ce2019-01-10 11:22:30 -07006430 return 0;
6431}
6432
Jens Axboebda52162019-09-24 13:47:15 -06006433struct io_wait_queue {
6434 struct wait_queue_entry wq;
6435 struct io_ring_ctx *ctx;
6436 unsigned to_wait;
6437 unsigned nr_timeouts;
6438};
6439
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006440static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006441{
6442 struct io_ring_ctx *ctx = iowq->ctx;
6443
6444 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006445 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006446 * started waiting. For timeouts, we always want to return to userspace,
6447 * regardless of event count.
6448 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006449 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006450 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6451}
6452
6453static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6454 int wake_flags, void *key)
6455{
6456 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6457 wq);
6458
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006459 /* use noflush == true, as we can't safely rely on locking context */
6460 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006461 return -1;
6462
6463 return autoremove_wake_function(curr, mode, wake_flags, key);
6464}
6465
Jens Axboe2b188cc2019-01-07 10:46:33 -07006466/*
6467 * Wait until events become available, if we don't already have some. The
6468 * application must reap them itself, as they reside on the shared cq ring.
6469 */
6470static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6471 const sigset_t __user *sig, size_t sigsz)
6472{
Jens Axboebda52162019-09-24 13:47:15 -06006473 struct io_wait_queue iowq = {
6474 .wq = {
6475 .private = current,
6476 .func = io_wake_function,
6477 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6478 },
6479 .ctx = ctx,
6480 .to_wait = min_events,
6481 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006482 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006483 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006484
Jens Axboeb41e9852020-02-17 09:52:41 -07006485 do {
6486 if (io_cqring_events(ctx, false) >= min_events)
6487 return 0;
6488 if (!current->task_works)
6489 break;
6490 task_work_run();
6491 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006492
6493 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006494#ifdef CONFIG_COMPAT
6495 if (in_compat_syscall())
6496 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006497 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006498 else
6499#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006500 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006501
Jens Axboe2b188cc2019-01-07 10:46:33 -07006502 if (ret)
6503 return ret;
6504 }
6505
Jens Axboebda52162019-09-24 13:47:15 -06006506 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006507 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006508 do {
6509 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6510 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006511 if (current->task_works)
6512 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006513 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006514 break;
6515 schedule();
6516 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006517 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006518 break;
6519 }
6520 } while (1);
6521 finish_wait(&ctx->wait, &iowq.wq);
6522
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006523 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006524
Hristo Venev75b28af2019-08-26 17:23:46 +00006525 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006526}
6527
Jens Axboe6b063142019-01-10 22:13:58 -07006528static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6529{
6530#if defined(CONFIG_UNIX)
6531 if (ctx->ring_sock) {
6532 struct sock *sock = ctx->ring_sock->sk;
6533 struct sk_buff *skb;
6534
6535 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6536 kfree_skb(skb);
6537 }
6538#else
6539 int i;
6540
Jens Axboe65e19f52019-10-26 07:20:21 -06006541 for (i = 0; i < ctx->nr_user_files; i++) {
6542 struct file *file;
6543
6544 file = io_file_from_index(ctx, i);
6545 if (file)
6546 fput(file);
6547 }
Jens Axboe6b063142019-01-10 22:13:58 -07006548#endif
6549}
6550
Jens Axboe05f3fb32019-12-09 11:22:50 -07006551static void io_file_ref_kill(struct percpu_ref *ref)
6552{
6553 struct fixed_file_data *data;
6554
6555 data = container_of(ref, struct fixed_file_data, refs);
6556 complete(&data->done);
6557}
6558
Jens Axboe6b063142019-01-10 22:13:58 -07006559static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6560{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006561 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006562 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006563 unsigned nr_tables, i;
6564
Jens Axboe05f3fb32019-12-09 11:22:50 -07006565 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006566 return -ENXIO;
6567
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006568 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006569 if (!list_empty(&data->ref_list))
6570 ref_node = list_first_entry(&data->ref_list,
6571 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006572 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006573 if (ref_node)
6574 percpu_ref_kill(&ref_node->refs);
6575
6576 percpu_ref_kill(&data->refs);
6577
6578 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006579 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006580 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006581
Jens Axboe6b063142019-01-10 22:13:58 -07006582 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006583 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6584 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006585 kfree(data->table[i].files);
6586 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006587 percpu_ref_exit(&data->refs);
6588 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006589 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006590 ctx->nr_user_files = 0;
6591 return 0;
6592}
6593
Jens Axboe6c271ce2019-01-10 11:22:30 -07006594static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6595{
6596 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006597 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006598 /*
6599 * The park is a bit of a work-around, without it we get
6600 * warning spews on shutdown with SQPOLL set and affinity
6601 * set to a single CPU.
6602 */
Jens Axboe06058632019-04-13 09:26:03 -06006603 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006604 kthread_stop(ctx->sqo_thread);
6605 ctx->sqo_thread = NULL;
6606 }
6607}
6608
Jens Axboe6b063142019-01-10 22:13:58 -07006609static void io_finish_async(struct io_ring_ctx *ctx)
6610{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006611 io_sq_thread_stop(ctx);
6612
Jens Axboe561fb042019-10-24 07:25:42 -06006613 if (ctx->io_wq) {
6614 io_wq_destroy(ctx->io_wq);
6615 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006616 }
6617}
6618
6619#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006620/*
6621 * Ensure the UNIX gc is aware of our file set, so we are certain that
6622 * the io_uring can be safely unregistered on process exit, even if we have
6623 * loops in the file referencing.
6624 */
6625static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6626{
6627 struct sock *sk = ctx->ring_sock->sk;
6628 struct scm_fp_list *fpl;
6629 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006630 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006631
Jens Axboe6b063142019-01-10 22:13:58 -07006632 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6633 if (!fpl)
6634 return -ENOMEM;
6635
6636 skb = alloc_skb(0, GFP_KERNEL);
6637 if (!skb) {
6638 kfree(fpl);
6639 return -ENOMEM;
6640 }
6641
6642 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006643
Jens Axboe08a45172019-10-03 08:11:03 -06006644 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006645 fpl->user = get_uid(ctx->user);
6646 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006647 struct file *file = io_file_from_index(ctx, i + offset);
6648
6649 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006650 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006651 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006652 unix_inflight(fpl->user, fpl->fp[nr_files]);
6653 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006654 }
6655
Jens Axboe08a45172019-10-03 08:11:03 -06006656 if (nr_files) {
6657 fpl->max = SCM_MAX_FD;
6658 fpl->count = nr_files;
6659 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006660 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006661 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6662 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006663
Jens Axboe08a45172019-10-03 08:11:03 -06006664 for (i = 0; i < nr_files; i++)
6665 fput(fpl->fp[i]);
6666 } else {
6667 kfree_skb(skb);
6668 kfree(fpl);
6669 }
Jens Axboe6b063142019-01-10 22:13:58 -07006670
6671 return 0;
6672}
6673
6674/*
6675 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6676 * causes regular reference counting to break down. We rely on the UNIX
6677 * garbage collection to take care of this problem for us.
6678 */
6679static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6680{
6681 unsigned left, total;
6682 int ret = 0;
6683
6684 total = 0;
6685 left = ctx->nr_user_files;
6686 while (left) {
6687 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006688
6689 ret = __io_sqe_files_scm(ctx, this_files, total);
6690 if (ret)
6691 break;
6692 left -= this_files;
6693 total += this_files;
6694 }
6695
6696 if (!ret)
6697 return 0;
6698
6699 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006700 struct file *file = io_file_from_index(ctx, total);
6701
6702 if (file)
6703 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006704 total++;
6705 }
6706
6707 return ret;
6708}
6709#else
6710static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6711{
6712 return 0;
6713}
6714#endif
6715
Jens Axboe65e19f52019-10-26 07:20:21 -06006716static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6717 unsigned nr_files)
6718{
6719 int i;
6720
6721 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006722 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006723 unsigned this_files;
6724
6725 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6726 table->files = kcalloc(this_files, sizeof(struct file *),
6727 GFP_KERNEL);
6728 if (!table->files)
6729 break;
6730 nr_files -= this_files;
6731 }
6732
6733 if (i == nr_tables)
6734 return 0;
6735
6736 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006737 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006738 kfree(table->files);
6739 }
6740 return 1;
6741}
6742
Jens Axboe05f3fb32019-12-09 11:22:50 -07006743static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006744{
6745#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006746 struct sock *sock = ctx->ring_sock->sk;
6747 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6748 struct sk_buff *skb;
6749 int i;
6750
6751 __skb_queue_head_init(&list);
6752
6753 /*
6754 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6755 * remove this entry and rearrange the file array.
6756 */
6757 skb = skb_dequeue(head);
6758 while (skb) {
6759 struct scm_fp_list *fp;
6760
6761 fp = UNIXCB(skb).fp;
6762 for (i = 0; i < fp->count; i++) {
6763 int left;
6764
6765 if (fp->fp[i] != file)
6766 continue;
6767
6768 unix_notinflight(fp->user, fp->fp[i]);
6769 left = fp->count - 1 - i;
6770 if (left) {
6771 memmove(&fp->fp[i], &fp->fp[i + 1],
6772 left * sizeof(struct file *));
6773 }
6774 fp->count--;
6775 if (!fp->count) {
6776 kfree_skb(skb);
6777 skb = NULL;
6778 } else {
6779 __skb_queue_tail(&list, skb);
6780 }
6781 fput(file);
6782 file = NULL;
6783 break;
6784 }
6785
6786 if (!file)
6787 break;
6788
6789 __skb_queue_tail(&list, skb);
6790
6791 skb = skb_dequeue(head);
6792 }
6793
6794 if (skb_peek(&list)) {
6795 spin_lock_irq(&head->lock);
6796 while ((skb = __skb_dequeue(&list)) != NULL)
6797 __skb_queue_tail(head, skb);
6798 spin_unlock_irq(&head->lock);
6799 }
6800#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006801 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006802#endif
6803}
6804
Jens Axboe05f3fb32019-12-09 11:22:50 -07006805struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006806 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006807 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006808};
6809
Jens Axboe4a38aed22020-05-14 17:21:15 -06006810static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006811{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006812 struct fixed_file_data *file_data = ref_node->file_data;
6813 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006814 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006815
6816 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006817 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006818 io_ring_file_put(ctx, pfile->file);
6819 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006820 }
6821
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006822 spin_lock(&file_data->lock);
6823 list_del(&ref_node->node);
6824 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006825
Xiaoguang Wang05589552020-03-31 14:05:18 +08006826 percpu_ref_exit(&ref_node->refs);
6827 kfree(ref_node);
6828 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006829}
6830
Jens Axboe4a38aed22020-05-14 17:21:15 -06006831static void io_file_put_work(struct work_struct *work)
6832{
6833 struct io_ring_ctx *ctx;
6834 struct llist_node *node;
6835
6836 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6837 node = llist_del_all(&ctx->file_put_llist);
6838
6839 while (node) {
6840 struct fixed_file_ref_node *ref_node;
6841 struct llist_node *next = node->next;
6842
6843 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6844 __io_file_put_work(ref_node);
6845 node = next;
6846 }
6847}
6848
Jens Axboe05f3fb32019-12-09 11:22:50 -07006849static void io_file_data_ref_zero(struct percpu_ref *ref)
6850{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006851 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006852 struct io_ring_ctx *ctx;
6853 bool first_add;
6854 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006855
Xiaoguang Wang05589552020-03-31 14:05:18 +08006856 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006857 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006858
Jens Axboe4a38aed22020-05-14 17:21:15 -06006859 if (percpu_ref_is_dying(&ctx->file_data->refs))
6860 delay = 0;
6861
6862 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6863 if (!delay)
6864 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6865 else if (first_add)
6866 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006867}
6868
6869static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6870 struct io_ring_ctx *ctx)
6871{
6872 struct fixed_file_ref_node *ref_node;
6873
6874 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6875 if (!ref_node)
6876 return ERR_PTR(-ENOMEM);
6877
6878 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6879 0, GFP_KERNEL)) {
6880 kfree(ref_node);
6881 return ERR_PTR(-ENOMEM);
6882 }
6883 INIT_LIST_HEAD(&ref_node->node);
6884 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006885 ref_node->file_data = ctx->file_data;
6886 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006887}
6888
6889static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6890{
6891 percpu_ref_exit(&ref_node->refs);
6892 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006893}
6894
6895static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6896 unsigned nr_args)
6897{
6898 __s32 __user *fds = (__s32 __user *) arg;
6899 unsigned nr_tables;
6900 struct file *file;
6901 int fd, ret = 0;
6902 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006903 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006904
6905 if (ctx->file_data)
6906 return -EBUSY;
6907 if (!nr_args)
6908 return -EINVAL;
6909 if (nr_args > IORING_MAX_FIXED_FILES)
6910 return -EMFILE;
6911
6912 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6913 if (!ctx->file_data)
6914 return -ENOMEM;
6915 ctx->file_data->ctx = ctx;
6916 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006917 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006918 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006919
6920 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6921 ctx->file_data->table = kcalloc(nr_tables,
6922 sizeof(struct fixed_file_table),
6923 GFP_KERNEL);
6924 if (!ctx->file_data->table) {
6925 kfree(ctx->file_data);
6926 ctx->file_data = NULL;
6927 return -ENOMEM;
6928 }
6929
Xiaoguang Wang05589552020-03-31 14:05:18 +08006930 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006931 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6932 kfree(ctx->file_data->table);
6933 kfree(ctx->file_data);
6934 ctx->file_data = NULL;
6935 return -ENOMEM;
6936 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006937
6938 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6939 percpu_ref_exit(&ctx->file_data->refs);
6940 kfree(ctx->file_data->table);
6941 kfree(ctx->file_data);
6942 ctx->file_data = NULL;
6943 return -ENOMEM;
6944 }
6945
6946 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6947 struct fixed_file_table *table;
6948 unsigned index;
6949
6950 ret = -EFAULT;
6951 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6952 break;
6953 /* allow sparse sets */
6954 if (fd == -1) {
6955 ret = 0;
6956 continue;
6957 }
6958
6959 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6960 index = i & IORING_FILE_TABLE_MASK;
6961 file = fget(fd);
6962
6963 ret = -EBADF;
6964 if (!file)
6965 break;
6966
6967 /*
6968 * Don't allow io_uring instances to be registered. If UNIX
6969 * isn't enabled, then this causes a reference cycle and this
6970 * instance can never get freed. If UNIX is enabled we'll
6971 * handle it just fine, but there's still no point in allowing
6972 * a ring fd as it doesn't support regular read/write anyway.
6973 */
6974 if (file->f_op == &io_uring_fops) {
6975 fput(file);
6976 break;
6977 }
6978 ret = 0;
6979 table->files[index] = file;
6980 }
6981
6982 if (ret) {
6983 for (i = 0; i < ctx->nr_user_files; i++) {
6984 file = io_file_from_index(ctx, i);
6985 if (file)
6986 fput(file);
6987 }
6988 for (i = 0; i < nr_tables; i++)
6989 kfree(ctx->file_data->table[i].files);
6990
6991 kfree(ctx->file_data->table);
6992 kfree(ctx->file_data);
6993 ctx->file_data = NULL;
6994 ctx->nr_user_files = 0;
6995 return ret;
6996 }
6997
6998 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006999 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007000 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007001 return ret;
7002 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007003
Xiaoguang Wang05589552020-03-31 14:05:18 +08007004 ref_node = alloc_fixed_file_ref_node(ctx);
7005 if (IS_ERR(ref_node)) {
7006 io_sqe_files_unregister(ctx);
7007 return PTR_ERR(ref_node);
7008 }
7009
7010 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007011 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007012 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007013 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007014 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007015 return ret;
7016}
7017
Jens Axboec3a31e62019-10-03 13:59:56 -06007018static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7019 int index)
7020{
7021#if defined(CONFIG_UNIX)
7022 struct sock *sock = ctx->ring_sock->sk;
7023 struct sk_buff_head *head = &sock->sk_receive_queue;
7024 struct sk_buff *skb;
7025
7026 /*
7027 * See if we can merge this file into an existing skb SCM_RIGHTS
7028 * file set. If there's no room, fall back to allocating a new skb
7029 * and filling it in.
7030 */
7031 spin_lock_irq(&head->lock);
7032 skb = skb_peek(head);
7033 if (skb) {
7034 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7035
7036 if (fpl->count < SCM_MAX_FD) {
7037 __skb_unlink(skb, head);
7038 spin_unlock_irq(&head->lock);
7039 fpl->fp[fpl->count] = get_file(file);
7040 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7041 fpl->count++;
7042 spin_lock_irq(&head->lock);
7043 __skb_queue_head(head, skb);
7044 } else {
7045 skb = NULL;
7046 }
7047 }
7048 spin_unlock_irq(&head->lock);
7049
7050 if (skb) {
7051 fput(file);
7052 return 0;
7053 }
7054
7055 return __io_sqe_files_scm(ctx, 1, index);
7056#else
7057 return 0;
7058#endif
7059}
7060
Hillf Dantona5318d32020-03-23 17:47:15 +08007061static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007062 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007063{
Hillf Dantona5318d32020-03-23 17:47:15 +08007064 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007065 struct percpu_ref *refs = data->cur_refs;
7066 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007067
Jens Axboe05f3fb32019-12-09 11:22:50 -07007068 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007069 if (!pfile)
7070 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007071
Xiaoguang Wang05589552020-03-31 14:05:18 +08007072 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007073 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007074 list_add(&pfile->list, &ref_node->file_list);
7075
Hillf Dantona5318d32020-03-23 17:47:15 +08007076 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007077}
7078
7079static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7080 struct io_uring_files_update *up,
7081 unsigned nr_args)
7082{
7083 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007084 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007085 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007086 __s32 __user *fds;
7087 int fd, i, err;
7088 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007089 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007090
Jens Axboe05f3fb32019-12-09 11:22:50 -07007091 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007092 return -EOVERFLOW;
7093 if (done > ctx->nr_user_files)
7094 return -EINVAL;
7095
Xiaoguang Wang05589552020-03-31 14:05:18 +08007096 ref_node = alloc_fixed_file_ref_node(ctx);
7097 if (IS_ERR(ref_node))
7098 return PTR_ERR(ref_node);
7099
Jens Axboec3a31e62019-10-03 13:59:56 -06007100 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007101 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007102 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007103 struct fixed_file_table *table;
7104 unsigned index;
7105
Jens Axboec3a31e62019-10-03 13:59:56 -06007106 err = 0;
7107 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7108 err = -EFAULT;
7109 break;
7110 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007111 i = array_index_nospec(up->offset, ctx->nr_user_files);
7112 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007113 index = i & IORING_FILE_TABLE_MASK;
7114 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007115 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007116 err = io_queue_file_removal(data, file);
7117 if (err)
7118 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007119 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007120 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007121 }
7122 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007123 file = fget(fd);
7124 if (!file) {
7125 err = -EBADF;
7126 break;
7127 }
7128 /*
7129 * Don't allow io_uring instances to be registered. If
7130 * UNIX isn't enabled, then this causes a reference
7131 * cycle and this instance can never get freed. If UNIX
7132 * is enabled we'll handle it just fine, but there's
7133 * still no point in allowing a ring fd as it doesn't
7134 * support regular read/write anyway.
7135 */
7136 if (file->f_op == &io_uring_fops) {
7137 fput(file);
7138 err = -EBADF;
7139 break;
7140 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007141 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007142 err = io_sqe_file_register(ctx, file, i);
7143 if (err)
7144 break;
7145 }
7146 nr_args--;
7147 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007148 up->offset++;
7149 }
7150
Xiaoguang Wang05589552020-03-31 14:05:18 +08007151 if (needs_switch) {
7152 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007153 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007154 list_add(&ref_node->node, &data->ref_list);
7155 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007156 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007157 percpu_ref_get(&ctx->file_data->refs);
7158 } else
7159 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007160
7161 return done ? done : err;
7162}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007163
Jens Axboe05f3fb32019-12-09 11:22:50 -07007164static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7165 unsigned nr_args)
7166{
7167 struct io_uring_files_update up;
7168
7169 if (!ctx->file_data)
7170 return -ENXIO;
7171 if (!nr_args)
7172 return -EINVAL;
7173 if (copy_from_user(&up, arg, sizeof(up)))
7174 return -EFAULT;
7175 if (up.resv)
7176 return -EINVAL;
7177
7178 return __io_sqe_files_update(ctx, &up, nr_args);
7179}
Jens Axboec3a31e62019-10-03 13:59:56 -06007180
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007181static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007182{
7183 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7184
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007185 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007186 io_put_req(req);
7187}
7188
Pavel Begunkov24369c22020-01-28 03:15:48 +03007189static int io_init_wq_offload(struct io_ring_ctx *ctx,
7190 struct io_uring_params *p)
7191{
7192 struct io_wq_data data;
7193 struct fd f;
7194 struct io_ring_ctx *ctx_attach;
7195 unsigned int concurrency;
7196 int ret = 0;
7197
7198 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007199 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007200 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007201
7202 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7203 /* Do QD, or 4 * CPUS, whatever is smallest */
7204 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7205
7206 ctx->io_wq = io_wq_create(concurrency, &data);
7207 if (IS_ERR(ctx->io_wq)) {
7208 ret = PTR_ERR(ctx->io_wq);
7209 ctx->io_wq = NULL;
7210 }
7211 return ret;
7212 }
7213
7214 f = fdget(p->wq_fd);
7215 if (!f.file)
7216 return -EBADF;
7217
7218 if (f.file->f_op != &io_uring_fops) {
7219 ret = -EINVAL;
7220 goto out_fput;
7221 }
7222
7223 ctx_attach = f.file->private_data;
7224 /* @io_wq is protected by holding the fd */
7225 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7226 ret = -EINVAL;
7227 goto out_fput;
7228 }
7229
7230 ctx->io_wq = ctx_attach->io_wq;
7231out_fput:
7232 fdput(f);
7233 return ret;
7234}
7235
Jens Axboe6c271ce2019-01-10 11:22:30 -07007236static int io_sq_offload_start(struct io_ring_ctx *ctx,
7237 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007238{
7239 int ret;
7240
7241 mmgrab(current->mm);
7242 ctx->sqo_mm = current->mm;
7243
Jens Axboe6c271ce2019-01-10 11:22:30 -07007244 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007245 ret = -EPERM;
7246 if (!capable(CAP_SYS_ADMIN))
7247 goto err;
7248
Jens Axboe917257d2019-04-13 09:28:55 -06007249 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7250 if (!ctx->sq_thread_idle)
7251 ctx->sq_thread_idle = HZ;
7252
Jens Axboe6c271ce2019-01-10 11:22:30 -07007253 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007254 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007255
Jens Axboe917257d2019-04-13 09:28:55 -06007256 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007257 if (cpu >= nr_cpu_ids)
7258 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007259 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007260 goto err;
7261
Jens Axboe6c271ce2019-01-10 11:22:30 -07007262 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7263 ctx, cpu,
7264 "io_uring-sq");
7265 } else {
7266 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7267 "io_uring-sq");
7268 }
7269 if (IS_ERR(ctx->sqo_thread)) {
7270 ret = PTR_ERR(ctx->sqo_thread);
7271 ctx->sqo_thread = NULL;
7272 goto err;
7273 }
7274 wake_up_process(ctx->sqo_thread);
7275 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7276 /* Can't have SQ_AFF without SQPOLL */
7277 ret = -EINVAL;
7278 goto err;
7279 }
7280
Pavel Begunkov24369c22020-01-28 03:15:48 +03007281 ret = io_init_wq_offload(ctx, p);
7282 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007283 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007284
7285 return 0;
7286err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007287 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007288 mmdrop(ctx->sqo_mm);
7289 ctx->sqo_mm = NULL;
7290 return ret;
7291}
7292
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007293static inline void __io_unaccount_mem(struct user_struct *user,
7294 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007295{
7296 atomic_long_sub(nr_pages, &user->locked_vm);
7297}
7298
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007299static inline int __io_account_mem(struct user_struct *user,
7300 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007301{
7302 unsigned long page_limit, cur_pages, new_pages;
7303
7304 /* Don't allow more pages than we can safely lock */
7305 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7306
7307 do {
7308 cur_pages = atomic_long_read(&user->locked_vm);
7309 new_pages = cur_pages + nr_pages;
7310 if (new_pages > page_limit)
7311 return -ENOMEM;
7312 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7313 new_pages) != cur_pages);
7314
7315 return 0;
7316}
7317
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007318static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7319 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007320{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007321 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007322 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007323
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007324 if (ctx->sqo_mm) {
7325 if (acct == ACCT_LOCKED)
7326 ctx->sqo_mm->locked_vm -= nr_pages;
7327 else if (acct == ACCT_PINNED)
7328 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7329 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007330}
7331
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007332static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7333 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007334{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007335 int ret;
7336
7337 if (ctx->limit_mem) {
7338 ret = __io_account_mem(ctx->user, nr_pages);
7339 if (ret)
7340 return ret;
7341 }
7342
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007343 if (ctx->sqo_mm) {
7344 if (acct == ACCT_LOCKED)
7345 ctx->sqo_mm->locked_vm += nr_pages;
7346 else if (acct == ACCT_PINNED)
7347 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7348 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007349
7350 return 0;
7351}
7352
Jens Axboe2b188cc2019-01-07 10:46:33 -07007353static void io_mem_free(void *ptr)
7354{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007355 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007356
Mark Rutland52e04ef2019-04-30 17:30:21 +01007357 if (!ptr)
7358 return;
7359
7360 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007361 if (put_page_testzero(page))
7362 free_compound_page(page);
7363}
7364
7365static void *io_mem_alloc(size_t size)
7366{
7367 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7368 __GFP_NORETRY;
7369
7370 return (void *) __get_free_pages(gfp_flags, get_order(size));
7371}
7372
Hristo Venev75b28af2019-08-26 17:23:46 +00007373static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7374 size_t *sq_offset)
7375{
7376 struct io_rings *rings;
7377 size_t off, sq_array_size;
7378
7379 off = struct_size(rings, cqes, cq_entries);
7380 if (off == SIZE_MAX)
7381 return SIZE_MAX;
7382
7383#ifdef CONFIG_SMP
7384 off = ALIGN(off, SMP_CACHE_BYTES);
7385 if (off == 0)
7386 return SIZE_MAX;
7387#endif
7388
7389 sq_array_size = array_size(sizeof(u32), sq_entries);
7390 if (sq_array_size == SIZE_MAX)
7391 return SIZE_MAX;
7392
7393 if (check_add_overflow(off, sq_array_size, &off))
7394 return SIZE_MAX;
7395
7396 if (sq_offset)
7397 *sq_offset = off;
7398
7399 return off;
7400}
7401
Jens Axboe2b188cc2019-01-07 10:46:33 -07007402static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7403{
Hristo Venev75b28af2019-08-26 17:23:46 +00007404 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007405
Hristo Venev75b28af2019-08-26 17:23:46 +00007406 pages = (size_t)1 << get_order(
7407 rings_size(sq_entries, cq_entries, NULL));
7408 pages += (size_t)1 << get_order(
7409 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007410
Hristo Venev75b28af2019-08-26 17:23:46 +00007411 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007412}
7413
Jens Axboeedafcce2019-01-09 09:16:05 -07007414static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7415{
7416 int i, j;
7417
7418 if (!ctx->user_bufs)
7419 return -ENXIO;
7420
7421 for (i = 0; i < ctx->nr_user_bufs; i++) {
7422 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7423
7424 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007425 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007426
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007427 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007428 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007429 imu->nr_bvecs = 0;
7430 }
7431
7432 kfree(ctx->user_bufs);
7433 ctx->user_bufs = NULL;
7434 ctx->nr_user_bufs = 0;
7435 return 0;
7436}
7437
7438static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7439 void __user *arg, unsigned index)
7440{
7441 struct iovec __user *src;
7442
7443#ifdef CONFIG_COMPAT
7444 if (ctx->compat) {
7445 struct compat_iovec __user *ciovs;
7446 struct compat_iovec ciov;
7447
7448 ciovs = (struct compat_iovec __user *) arg;
7449 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7450 return -EFAULT;
7451
Jens Axboed55e5f52019-12-11 16:12:15 -07007452 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007453 dst->iov_len = ciov.iov_len;
7454 return 0;
7455 }
7456#endif
7457 src = (struct iovec __user *) arg;
7458 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7459 return -EFAULT;
7460 return 0;
7461}
7462
7463static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7464 unsigned nr_args)
7465{
7466 struct vm_area_struct **vmas = NULL;
7467 struct page **pages = NULL;
7468 int i, j, got_pages = 0;
7469 int ret = -EINVAL;
7470
7471 if (ctx->user_bufs)
7472 return -EBUSY;
7473 if (!nr_args || nr_args > UIO_MAXIOV)
7474 return -EINVAL;
7475
7476 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7477 GFP_KERNEL);
7478 if (!ctx->user_bufs)
7479 return -ENOMEM;
7480
7481 for (i = 0; i < nr_args; i++) {
7482 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7483 unsigned long off, start, end, ubuf;
7484 int pret, nr_pages;
7485 struct iovec iov;
7486 size_t size;
7487
7488 ret = io_copy_iov(ctx, &iov, arg, i);
7489 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007490 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007491
7492 /*
7493 * Don't impose further limits on the size and buffer
7494 * constraints here, we'll -EINVAL later when IO is
7495 * submitted if they are wrong.
7496 */
7497 ret = -EFAULT;
7498 if (!iov.iov_base || !iov.iov_len)
7499 goto err;
7500
7501 /* arbitrary limit, but we need something */
7502 if (iov.iov_len > SZ_1G)
7503 goto err;
7504
7505 ubuf = (unsigned long) iov.iov_base;
7506 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7507 start = ubuf >> PAGE_SHIFT;
7508 nr_pages = end - start;
7509
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007510 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007511 if (ret)
7512 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007513
7514 ret = 0;
7515 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007516 kvfree(vmas);
7517 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007518 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007519 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007520 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007521 sizeof(struct vm_area_struct *),
7522 GFP_KERNEL);
7523 if (!pages || !vmas) {
7524 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007525 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007526 goto err;
7527 }
7528 got_pages = nr_pages;
7529 }
7530
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007531 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007532 GFP_KERNEL);
7533 ret = -ENOMEM;
7534 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007535 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007536 goto err;
7537 }
7538
7539 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007540 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007541 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007542 FOLL_WRITE | FOLL_LONGTERM,
7543 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007544 if (pret == nr_pages) {
7545 /* don't support file backed memory */
7546 for (j = 0; j < nr_pages; j++) {
7547 struct vm_area_struct *vma = vmas[j];
7548
7549 if (vma->vm_file &&
7550 !is_file_hugepages(vma->vm_file)) {
7551 ret = -EOPNOTSUPP;
7552 break;
7553 }
7554 }
7555 } else {
7556 ret = pret < 0 ? pret : -EFAULT;
7557 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007558 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007559 if (ret) {
7560 /*
7561 * if we did partial map, or found file backed vmas,
7562 * release any pages we did get
7563 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007564 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007565 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007566 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007567 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007568 goto err;
7569 }
7570
7571 off = ubuf & ~PAGE_MASK;
7572 size = iov.iov_len;
7573 for (j = 0; j < nr_pages; j++) {
7574 size_t vec_len;
7575
7576 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7577 imu->bvec[j].bv_page = pages[j];
7578 imu->bvec[j].bv_len = vec_len;
7579 imu->bvec[j].bv_offset = off;
7580 off = 0;
7581 size -= vec_len;
7582 }
7583 /* store original address for later verification */
7584 imu->ubuf = ubuf;
7585 imu->len = iov.iov_len;
7586 imu->nr_bvecs = nr_pages;
7587
7588 ctx->nr_user_bufs++;
7589 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007590 kvfree(pages);
7591 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007592 return 0;
7593err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007594 kvfree(pages);
7595 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007596 io_sqe_buffer_unregister(ctx);
7597 return ret;
7598}
7599
Jens Axboe9b402842019-04-11 11:45:41 -06007600static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7601{
7602 __s32 __user *fds = arg;
7603 int fd;
7604
7605 if (ctx->cq_ev_fd)
7606 return -EBUSY;
7607
7608 if (copy_from_user(&fd, fds, sizeof(*fds)))
7609 return -EFAULT;
7610
7611 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7612 if (IS_ERR(ctx->cq_ev_fd)) {
7613 int ret = PTR_ERR(ctx->cq_ev_fd);
7614 ctx->cq_ev_fd = NULL;
7615 return ret;
7616 }
7617
7618 return 0;
7619}
7620
7621static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7622{
7623 if (ctx->cq_ev_fd) {
7624 eventfd_ctx_put(ctx->cq_ev_fd);
7625 ctx->cq_ev_fd = NULL;
7626 return 0;
7627 }
7628
7629 return -ENXIO;
7630}
7631
Jens Axboe5a2e7452020-02-23 16:23:11 -07007632static int __io_destroy_buffers(int id, void *p, void *data)
7633{
7634 struct io_ring_ctx *ctx = data;
7635 struct io_buffer *buf = p;
7636
Jens Axboe067524e2020-03-02 16:32:28 -07007637 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007638 return 0;
7639}
7640
7641static void io_destroy_buffers(struct io_ring_ctx *ctx)
7642{
7643 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7644 idr_destroy(&ctx->io_buffer_idr);
7645}
7646
Jens Axboe2b188cc2019-01-07 10:46:33 -07007647static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7648{
Jens Axboe6b063142019-01-10 22:13:58 -07007649 io_finish_async(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007650 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007651 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007652 ctx->sqo_mm = NULL;
7653 }
Jens Axboedef596e2019-01-09 08:59:42 -07007654
7655 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007656 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007657 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007658 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007659 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007660 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007661
Jens Axboe2b188cc2019-01-07 10:46:33 -07007662#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007663 if (ctx->ring_sock) {
7664 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007665 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007666 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007667#endif
7668
Hristo Venev75b28af2019-08-26 17:23:46 +00007669 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007670 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007671
7672 percpu_ref_exit(&ctx->refs);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007673 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7674 ACCT_LOCKED);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007675 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007676 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007677 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007678 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007679 kfree(ctx);
7680}
7681
7682static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7683{
7684 struct io_ring_ctx *ctx = file->private_data;
7685 __poll_t mask = 0;
7686
7687 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007688 /*
7689 * synchronizes with barrier from wq_has_sleeper call in
7690 * io_commit_cqring
7691 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007692 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007693 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7694 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007695 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007696 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007697 mask |= EPOLLIN | EPOLLRDNORM;
7698
7699 return mask;
7700}
7701
7702static int io_uring_fasync(int fd, struct file *file, int on)
7703{
7704 struct io_ring_ctx *ctx = file->private_data;
7705
7706 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7707}
7708
Jens Axboe071698e2020-01-28 10:04:42 -07007709static int io_remove_personalities(int id, void *p, void *data)
7710{
7711 struct io_ring_ctx *ctx = data;
7712 const struct cred *cred;
7713
7714 cred = idr_remove(&ctx->personality_idr, id);
7715 if (cred)
7716 put_cred(cred);
7717 return 0;
7718}
7719
Jens Axboe85faa7b2020-04-09 18:14:00 -06007720static void io_ring_exit_work(struct work_struct *work)
7721{
7722 struct io_ring_ctx *ctx;
7723
7724 ctx = container_of(work, struct io_ring_ctx, exit_work);
7725 if (ctx->rings)
7726 io_cqring_overflow_flush(ctx, true);
7727
Jens Axboe56952e92020-06-17 15:00:04 -06007728 /*
7729 * If we're doing polled IO and end up having requests being
7730 * submitted async (out-of-line), then completions can come in while
7731 * we're waiting for refs to drop. We need to reap these manually,
7732 * as nobody else will be looking for them.
7733 */
7734 while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7735 io_iopoll_reap_events(ctx);
7736 if (ctx->rings)
7737 io_cqring_overflow_flush(ctx, true);
7738 }
Jens Axboe85faa7b2020-04-09 18:14:00 -06007739 io_ring_ctx_free(ctx);
7740}
7741
Jens Axboe2b188cc2019-01-07 10:46:33 -07007742static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7743{
7744 mutex_lock(&ctx->uring_lock);
7745 percpu_ref_kill(&ctx->refs);
7746 mutex_unlock(&ctx->uring_lock);
7747
Jens Axboe5262f562019-09-17 12:26:57 -06007748 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007749 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007750
7751 if (ctx->io_wq)
7752 io_wq_cancel_all(ctx->io_wq);
7753
Jens Axboedef596e2019-01-09 08:59:42 -07007754 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007755 /* if we failed setting up the ctx, we might not have any rings */
7756 if (ctx->rings)
7757 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007758 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007759 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7760 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007761}
7762
7763static int io_uring_release(struct inode *inode, struct file *file)
7764{
7765 struct io_ring_ctx *ctx = file->private_data;
7766
7767 file->private_data = NULL;
7768 io_ring_ctx_wait_and_kill(ctx);
7769 return 0;
7770}
7771
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007772static bool io_wq_files_match(struct io_wq_work *work, void *data)
7773{
7774 struct files_struct *files = data;
7775
7776 return work->files == files;
7777}
7778
Jens Axboefcb323c2019-10-24 12:39:47 -06007779static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7780 struct files_struct *files)
7781{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007782 if (list_empty_careful(&ctx->inflight_list))
7783 return;
7784
7785 /* cancel all at once, should be faster than doing it one by one*/
7786 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7787
Jens Axboefcb323c2019-10-24 12:39:47 -06007788 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007789 struct io_kiocb *cancel_req = NULL, *req;
7790 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007791
7792 spin_lock_irq(&ctx->inflight_lock);
7793 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007794 if (req->work.files != files)
7795 continue;
7796 /* req is being completed, ignore */
7797 if (!refcount_inc_not_zero(&req->refs))
7798 continue;
7799 cancel_req = req;
7800 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007801 }
Jens Axboe768134d2019-11-10 20:30:53 -07007802 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007803 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007804 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007805 spin_unlock_irq(&ctx->inflight_lock);
7806
Jens Axboe768134d2019-11-10 20:30:53 -07007807 /* We need to keep going until we don't find a matching req */
7808 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007809 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007810
Jens Axboe2ca10252020-02-13 17:17:35 -07007811 if (cancel_req->flags & REQ_F_OVERFLOW) {
7812 spin_lock_irq(&ctx->completion_lock);
7813 list_del(&cancel_req->list);
7814 cancel_req->flags &= ~REQ_F_OVERFLOW;
7815 if (list_empty(&ctx->cq_overflow_list)) {
7816 clear_bit(0, &ctx->sq_check_overflow);
7817 clear_bit(0, &ctx->cq_check_overflow);
7818 }
7819 spin_unlock_irq(&ctx->completion_lock);
7820
7821 WRITE_ONCE(ctx->rings->cq_overflow,
7822 atomic_inc_return(&ctx->cached_cq_overflow));
7823
7824 /*
7825 * Put inflight ref and overflow ref. If that's
7826 * all we had, then we're done with this request.
7827 */
7828 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007829 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007830 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007831 continue;
7832 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007833 } else {
7834 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7835 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007836 }
7837
Jens Axboefcb323c2019-10-24 12:39:47 -06007838 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007839 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007840 }
7841}
7842
Pavel Begunkov801dd572020-06-15 10:33:14 +03007843static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007844{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007845 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7846 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007847
Pavel Begunkov801dd572020-06-15 10:33:14 +03007848 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007849}
7850
Jens Axboefcb323c2019-10-24 12:39:47 -06007851static int io_uring_flush(struct file *file, void *data)
7852{
7853 struct io_ring_ctx *ctx = file->private_data;
7854
7855 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007856
7857 /*
7858 * If the task is going away, cancel work it may have pending
7859 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007860 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7861 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007862
Jens Axboefcb323c2019-10-24 12:39:47 -06007863 return 0;
7864}
7865
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007866static void *io_uring_validate_mmap_request(struct file *file,
7867 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007868{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007869 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007870 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007871 struct page *page;
7872 void *ptr;
7873
7874 switch (offset) {
7875 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007876 case IORING_OFF_CQ_RING:
7877 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007878 break;
7879 case IORING_OFF_SQES:
7880 ptr = ctx->sq_sqes;
7881 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007882 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007883 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007884 }
7885
7886 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007887 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007888 return ERR_PTR(-EINVAL);
7889
7890 return ptr;
7891}
7892
7893#ifdef CONFIG_MMU
7894
7895static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7896{
7897 size_t sz = vma->vm_end - vma->vm_start;
7898 unsigned long pfn;
7899 void *ptr;
7900
7901 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7902 if (IS_ERR(ptr))
7903 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007904
7905 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7906 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7907}
7908
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007909#else /* !CONFIG_MMU */
7910
7911static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7912{
7913 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7914}
7915
7916static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7917{
7918 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7919}
7920
7921static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7922 unsigned long addr, unsigned long len,
7923 unsigned long pgoff, unsigned long flags)
7924{
7925 void *ptr;
7926
7927 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7928 if (IS_ERR(ptr))
7929 return PTR_ERR(ptr);
7930
7931 return (unsigned long) ptr;
7932}
7933
7934#endif /* !CONFIG_MMU */
7935
Jens Axboe2b188cc2019-01-07 10:46:33 -07007936SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7937 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7938 size_t, sigsz)
7939{
7940 struct io_ring_ctx *ctx;
7941 long ret = -EBADF;
7942 int submitted = 0;
7943 struct fd f;
7944
Jens Axboeb41e9852020-02-17 09:52:41 -07007945 if (current->task_works)
7946 task_work_run();
7947
Jens Axboe6c271ce2019-01-10 11:22:30 -07007948 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007949 return -EINVAL;
7950
7951 f = fdget(fd);
7952 if (!f.file)
7953 return -EBADF;
7954
7955 ret = -EOPNOTSUPP;
7956 if (f.file->f_op != &io_uring_fops)
7957 goto out_fput;
7958
7959 ret = -ENXIO;
7960 ctx = f.file->private_data;
7961 if (!percpu_ref_tryget(&ctx->refs))
7962 goto out_fput;
7963
Jens Axboe6c271ce2019-01-10 11:22:30 -07007964 /*
7965 * For SQ polling, the thread will do all submissions and completions.
7966 * Just return the requested submit count, and wake the thread if
7967 * we were asked to.
7968 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007969 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007970 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007971 if (!list_empty_careful(&ctx->cq_overflow_list))
7972 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007973 if (flags & IORING_ENTER_SQ_WAKEUP)
7974 wake_up(&ctx->sqo_wait);
7975 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007976 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007977 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007978 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007979 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007980
7981 if (submitted != to_submit)
7982 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007983 }
7984 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007985 unsigned nr_events = 0;
7986
Jens Axboe2b188cc2019-01-07 10:46:33 -07007987 min_complete = min(min_complete, ctx->cq_entries);
7988
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007989 /*
7990 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7991 * space applications don't need to do io completion events
7992 * polling again, they can rely on io_sq_thread to do polling
7993 * work, which can reduce cpu usage and uring_lock contention.
7994 */
7995 if (ctx->flags & IORING_SETUP_IOPOLL &&
7996 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007997 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007998 } else {
7999 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8000 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008001 }
8002
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008003out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008004 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008005out_fput:
8006 fdput(f);
8007 return submitted ? submitted : ret;
8008}
8009
Tobias Klauserbebdb652020-02-26 18:38:32 +01008010#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008011static int io_uring_show_cred(int id, void *p, void *data)
8012{
8013 const struct cred *cred = p;
8014 struct seq_file *m = data;
8015 struct user_namespace *uns = seq_user_ns(m);
8016 struct group_info *gi;
8017 kernel_cap_t cap;
8018 unsigned __capi;
8019 int g;
8020
8021 seq_printf(m, "%5d\n", id);
8022 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8023 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8024 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8025 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8026 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8027 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8028 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8029 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8030 seq_puts(m, "\n\tGroups:\t");
8031 gi = cred->group_info;
8032 for (g = 0; g < gi->ngroups; g++) {
8033 seq_put_decimal_ull(m, g ? " " : "",
8034 from_kgid_munged(uns, gi->gid[g]));
8035 }
8036 seq_puts(m, "\n\tCapEff:\t");
8037 cap = cred->cap_effective;
8038 CAP_FOR_EACH_U32(__capi)
8039 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8040 seq_putc(m, '\n');
8041 return 0;
8042}
8043
8044static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8045{
8046 int i;
8047
8048 mutex_lock(&ctx->uring_lock);
8049 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
8050 for (i = 0; i < ctx->nr_user_files; i++) {
8051 struct fixed_file_table *table;
8052 struct file *f;
8053
8054 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8055 f = table->files[i & IORING_FILE_TABLE_MASK];
8056 if (f)
8057 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8058 else
8059 seq_printf(m, "%5u: <none>\n", i);
8060 }
8061 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
8062 for (i = 0; i < ctx->nr_user_bufs; i++) {
8063 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8064
8065 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8066 (unsigned int) buf->len);
8067 }
8068 if (!idr_is_empty(&ctx->personality_idr)) {
8069 seq_printf(m, "Personalities:\n");
8070 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8071 }
Jens Axboed7718a92020-02-14 22:23:12 -07008072 seq_printf(m, "PollList:\n");
8073 spin_lock_irq(&ctx->completion_lock);
8074 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8075 struct hlist_head *list = &ctx->cancel_hash[i];
8076 struct io_kiocb *req;
8077
8078 hlist_for_each_entry(req, list, hash_node)
8079 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8080 req->task->task_works != NULL);
8081 }
8082 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008083 mutex_unlock(&ctx->uring_lock);
8084}
8085
8086static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8087{
8088 struct io_ring_ctx *ctx = f->private_data;
8089
8090 if (percpu_ref_tryget(&ctx->refs)) {
8091 __io_uring_show_fdinfo(ctx, m);
8092 percpu_ref_put(&ctx->refs);
8093 }
8094}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008095#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008096
Jens Axboe2b188cc2019-01-07 10:46:33 -07008097static const struct file_operations io_uring_fops = {
8098 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008099 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008100 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008101#ifndef CONFIG_MMU
8102 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8103 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8104#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008105 .poll = io_uring_poll,
8106 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008107#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008108 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008109#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008110};
8111
8112static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8113 struct io_uring_params *p)
8114{
Hristo Venev75b28af2019-08-26 17:23:46 +00008115 struct io_rings *rings;
8116 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008117
Hristo Venev75b28af2019-08-26 17:23:46 +00008118 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8119 if (size == SIZE_MAX)
8120 return -EOVERFLOW;
8121
8122 rings = io_mem_alloc(size);
8123 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008124 return -ENOMEM;
8125
Hristo Venev75b28af2019-08-26 17:23:46 +00008126 ctx->rings = rings;
8127 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8128 rings->sq_ring_mask = p->sq_entries - 1;
8129 rings->cq_ring_mask = p->cq_entries - 1;
8130 rings->sq_ring_entries = p->sq_entries;
8131 rings->cq_ring_entries = p->cq_entries;
8132 ctx->sq_mask = rings->sq_ring_mask;
8133 ctx->cq_mask = rings->cq_ring_mask;
8134 ctx->sq_entries = rings->sq_ring_entries;
8135 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008136
8137 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008138 if (size == SIZE_MAX) {
8139 io_mem_free(ctx->rings);
8140 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008141 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008142 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008143
8144 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008145 if (!ctx->sq_sqes) {
8146 io_mem_free(ctx->rings);
8147 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008148 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008149 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008150
Jens Axboe2b188cc2019-01-07 10:46:33 -07008151 return 0;
8152}
8153
8154/*
8155 * Allocate an anonymous fd, this is what constitutes the application
8156 * visible backing of an io_uring instance. The application mmaps this
8157 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8158 * we have to tie this fd to a socket for file garbage collection purposes.
8159 */
8160static int io_uring_get_fd(struct io_ring_ctx *ctx)
8161{
8162 struct file *file;
8163 int ret;
8164
8165#if defined(CONFIG_UNIX)
8166 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8167 &ctx->ring_sock);
8168 if (ret)
8169 return ret;
8170#endif
8171
8172 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8173 if (ret < 0)
8174 goto err;
8175
8176 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8177 O_RDWR | O_CLOEXEC);
8178 if (IS_ERR(file)) {
8179 put_unused_fd(ret);
8180 ret = PTR_ERR(file);
8181 goto err;
8182 }
8183
8184#if defined(CONFIG_UNIX)
8185 ctx->ring_sock->file = file;
8186#endif
8187 fd_install(ret, file);
8188 return ret;
8189err:
8190#if defined(CONFIG_UNIX)
8191 sock_release(ctx->ring_sock);
8192 ctx->ring_sock = NULL;
8193#endif
8194 return ret;
8195}
8196
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008197static int io_uring_create(unsigned entries, struct io_uring_params *p,
8198 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008199{
8200 struct user_struct *user = NULL;
8201 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008202 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008203 int ret;
8204
Jens Axboe8110c1a2019-12-28 15:39:54 -07008205 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008206 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008207 if (entries > IORING_MAX_ENTRIES) {
8208 if (!(p->flags & IORING_SETUP_CLAMP))
8209 return -EINVAL;
8210 entries = IORING_MAX_ENTRIES;
8211 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008212
8213 /*
8214 * Use twice as many entries for the CQ ring. It's possible for the
8215 * application to drive a higher depth than the size of the SQ ring,
8216 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008217 * some flexibility in overcommitting a bit. If the application has
8218 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8219 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008220 */
8221 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008222 if (p->flags & IORING_SETUP_CQSIZE) {
8223 /*
8224 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8225 * to a power-of-two, if it isn't already. We do NOT impose
8226 * any cq vs sq ring sizing.
8227 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008228 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008229 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008230 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8231 if (!(p->flags & IORING_SETUP_CLAMP))
8232 return -EINVAL;
8233 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8234 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008235 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8236 } else {
8237 p->cq_entries = 2 * p->sq_entries;
8238 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008239
8240 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008241 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008242
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008243 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008244 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008245 ring_pages(p->sq_entries, p->cq_entries));
8246 if (ret) {
8247 free_uid(user);
8248 return ret;
8249 }
8250 }
8251
8252 ctx = io_ring_ctx_alloc(p);
8253 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008254 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008255 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008256 p->cq_entries));
8257 free_uid(user);
8258 return -ENOMEM;
8259 }
8260 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008261 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008262 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008263
8264 ret = io_allocate_scq_urings(ctx, p);
8265 if (ret)
8266 goto err;
8267
Jens Axboe6c271ce2019-01-10 11:22:30 -07008268 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008269 if (ret)
8270 goto err;
8271
Jens Axboe2b188cc2019-01-07 10:46:33 -07008272 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008273 p->sq_off.head = offsetof(struct io_rings, sq.head);
8274 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8275 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8276 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8277 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8278 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8279 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008280
8281 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008282 p->cq_off.head = offsetof(struct io_rings, cq.head);
8283 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8284 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8285 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8286 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8287 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008288 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008289
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008290 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8291 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008292 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8293 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008294
8295 if (copy_to_user(params, p, sizeof(*p))) {
8296 ret = -EFAULT;
8297 goto err;
8298 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008299 /*
8300 * Install ring fd as the very last thing, so we don't risk someone
8301 * having closed it before we finish setup
8302 */
8303 ret = io_uring_get_fd(ctx);
8304 if (ret < 0)
8305 goto err;
8306
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008307 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008308 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8309 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008310 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008311 return ret;
8312err:
8313 io_ring_ctx_wait_and_kill(ctx);
8314 return ret;
8315}
8316
8317/*
8318 * Sets up an aio uring context, and returns the fd. Applications asks for a
8319 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8320 * params structure passed in.
8321 */
8322static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8323{
8324 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008325 int i;
8326
8327 if (copy_from_user(&p, params, sizeof(p)))
8328 return -EFAULT;
8329 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8330 if (p.resv[i])
8331 return -EINVAL;
8332 }
8333
Jens Axboe6c271ce2019-01-10 11:22:30 -07008334 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008335 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008336 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008337 return -EINVAL;
8338
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008339 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008340}
8341
8342SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8343 struct io_uring_params __user *, params)
8344{
8345 return io_uring_setup(entries, params);
8346}
8347
Jens Axboe66f4af92020-01-16 15:36:52 -07008348static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8349{
8350 struct io_uring_probe *p;
8351 size_t size;
8352 int i, ret;
8353
8354 size = struct_size(p, ops, nr_args);
8355 if (size == SIZE_MAX)
8356 return -EOVERFLOW;
8357 p = kzalloc(size, GFP_KERNEL);
8358 if (!p)
8359 return -ENOMEM;
8360
8361 ret = -EFAULT;
8362 if (copy_from_user(p, arg, size))
8363 goto out;
8364 ret = -EINVAL;
8365 if (memchr_inv(p, 0, size))
8366 goto out;
8367
8368 p->last_op = IORING_OP_LAST - 1;
8369 if (nr_args > IORING_OP_LAST)
8370 nr_args = IORING_OP_LAST;
8371
8372 for (i = 0; i < nr_args; i++) {
8373 p->ops[i].op = i;
8374 if (!io_op_defs[i].not_supported)
8375 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8376 }
8377 p->ops_len = i;
8378
8379 ret = 0;
8380 if (copy_to_user(arg, p, size))
8381 ret = -EFAULT;
8382out:
8383 kfree(p);
8384 return ret;
8385}
8386
Jens Axboe071698e2020-01-28 10:04:42 -07008387static int io_register_personality(struct io_ring_ctx *ctx)
8388{
8389 const struct cred *creds = get_current_cred();
8390 int id;
8391
8392 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8393 USHRT_MAX, GFP_KERNEL);
8394 if (id < 0)
8395 put_cred(creds);
8396 return id;
8397}
8398
8399static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8400{
8401 const struct cred *old_creds;
8402
8403 old_creds = idr_remove(&ctx->personality_idr, id);
8404 if (old_creds) {
8405 put_cred(old_creds);
8406 return 0;
8407 }
8408
8409 return -EINVAL;
8410}
8411
8412static bool io_register_op_must_quiesce(int op)
8413{
8414 switch (op) {
8415 case IORING_UNREGISTER_FILES:
8416 case IORING_REGISTER_FILES_UPDATE:
8417 case IORING_REGISTER_PROBE:
8418 case IORING_REGISTER_PERSONALITY:
8419 case IORING_UNREGISTER_PERSONALITY:
8420 return false;
8421 default:
8422 return true;
8423 }
8424}
8425
Jens Axboeedafcce2019-01-09 09:16:05 -07008426static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8427 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008428 __releases(ctx->uring_lock)
8429 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008430{
8431 int ret;
8432
Jens Axboe35fa71a2019-04-22 10:23:23 -06008433 /*
8434 * We're inside the ring mutex, if the ref is already dying, then
8435 * someone else killed the ctx or is already going through
8436 * io_uring_register().
8437 */
8438 if (percpu_ref_is_dying(&ctx->refs))
8439 return -ENXIO;
8440
Jens Axboe071698e2020-01-28 10:04:42 -07008441 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008442 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008443
Jens Axboe05f3fb32019-12-09 11:22:50 -07008444 /*
8445 * Drop uring mutex before waiting for references to exit. If
8446 * another thread is currently inside io_uring_enter() it might
8447 * need to grab the uring_lock to make progress. If we hold it
8448 * here across the drain wait, then we can deadlock. It's safe
8449 * to drop the mutex here, since no new references will come in
8450 * after we've killed the percpu ref.
8451 */
8452 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008453 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008454 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008455 if (ret) {
8456 percpu_ref_resurrect(&ctx->refs);
8457 ret = -EINTR;
8458 goto out;
8459 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008460 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008461
8462 switch (opcode) {
8463 case IORING_REGISTER_BUFFERS:
8464 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8465 break;
8466 case IORING_UNREGISTER_BUFFERS:
8467 ret = -EINVAL;
8468 if (arg || nr_args)
8469 break;
8470 ret = io_sqe_buffer_unregister(ctx);
8471 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008472 case IORING_REGISTER_FILES:
8473 ret = io_sqe_files_register(ctx, arg, nr_args);
8474 break;
8475 case IORING_UNREGISTER_FILES:
8476 ret = -EINVAL;
8477 if (arg || nr_args)
8478 break;
8479 ret = io_sqe_files_unregister(ctx);
8480 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008481 case IORING_REGISTER_FILES_UPDATE:
8482 ret = io_sqe_files_update(ctx, arg, nr_args);
8483 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008484 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008485 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008486 ret = -EINVAL;
8487 if (nr_args != 1)
8488 break;
8489 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008490 if (ret)
8491 break;
8492 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8493 ctx->eventfd_async = 1;
8494 else
8495 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008496 break;
8497 case IORING_UNREGISTER_EVENTFD:
8498 ret = -EINVAL;
8499 if (arg || nr_args)
8500 break;
8501 ret = io_eventfd_unregister(ctx);
8502 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008503 case IORING_REGISTER_PROBE:
8504 ret = -EINVAL;
8505 if (!arg || nr_args > 256)
8506 break;
8507 ret = io_probe(ctx, arg, nr_args);
8508 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008509 case IORING_REGISTER_PERSONALITY:
8510 ret = -EINVAL;
8511 if (arg || nr_args)
8512 break;
8513 ret = io_register_personality(ctx);
8514 break;
8515 case IORING_UNREGISTER_PERSONALITY:
8516 ret = -EINVAL;
8517 if (arg)
8518 break;
8519 ret = io_unregister_personality(ctx, nr_args);
8520 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008521 default:
8522 ret = -EINVAL;
8523 break;
8524 }
8525
Jens Axboe071698e2020-01-28 10:04:42 -07008526 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008527 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008528 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008529out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008530 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008531 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008532 return ret;
8533}
8534
8535SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8536 void __user *, arg, unsigned int, nr_args)
8537{
8538 struct io_ring_ctx *ctx;
8539 long ret = -EBADF;
8540 struct fd f;
8541
8542 f = fdget(fd);
8543 if (!f.file)
8544 return -EBADF;
8545
8546 ret = -EOPNOTSUPP;
8547 if (f.file->f_op != &io_uring_fops)
8548 goto out_fput;
8549
8550 ctx = f.file->private_data;
8551
8552 mutex_lock(&ctx->uring_lock);
8553 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8554 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008555 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8556 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008557out_fput:
8558 fdput(f);
8559 return ret;
8560}
8561
Jens Axboe2b188cc2019-01-07 10:46:33 -07008562static int __init io_uring_init(void)
8563{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008564#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8565 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8566 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8567} while (0)
8568
8569#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8570 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8571 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8572 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8573 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8574 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8575 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8576 BUILD_BUG_SQE_ELEM(8, __u64, off);
8577 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8578 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008579 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008580 BUILD_BUG_SQE_ELEM(24, __u32, len);
8581 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8582 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8583 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8584 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008585 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8586 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008587 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8588 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8589 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8590 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8591 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8592 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8593 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8594 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008595 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008596 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8597 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8598 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008599 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008600
Jens Axboed3656342019-12-18 09:50:26 -07008601 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008602 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008603 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8604 return 0;
8605};
8606__initcall(io_uring_init);