blob: 43ddda2a3d49017c10b1c6d1e33deeb087aae6b8 [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);
Jens Axboe2237d762020-06-26 13:44:16 -0600908static void io_complete_rw_common(struct kiocb *kiocb, long res,
909 struct io_comp_state *cs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300910static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700911static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
912 int fd, struct file **out_file, bool fixed);
913static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600914 const struct io_uring_sqe *sqe,
915 struct io_comp_state *cs);
Jens Axboede0617e2019-04-06 21:51:27 -0600916
Jens Axboeb63534c2020-06-04 11:28:00 -0600917static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
918 struct iovec **iovec, struct iov_iter *iter,
919 bool needs_lock);
920static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
921 struct iovec *iovec, struct iovec *fast_iov,
922 struct iov_iter *iter);
923
Jens Axboe2b188cc2019-01-07 10:46:33 -0700924static struct kmem_cache *req_cachep;
925
926static const struct file_operations io_uring_fops;
927
928struct sock *io_uring_get_socket(struct file *file)
929{
930#if defined(CONFIG_UNIX)
931 if (file->f_op == &io_uring_fops) {
932 struct io_ring_ctx *ctx = file->private_data;
933
934 return ctx->ring_sock->sk;
935 }
936#endif
937 return NULL;
938}
939EXPORT_SYMBOL(io_uring_get_socket);
940
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300941static void io_get_req_task(struct io_kiocb *req)
942{
943 if (req->flags & REQ_F_TASK_PINNED)
944 return;
945 get_task_struct(req->task);
946 req->flags |= REQ_F_TASK_PINNED;
947}
948
949/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
950static void __io_put_req_task(struct io_kiocb *req)
951{
952 if (req->flags & REQ_F_TASK_PINNED)
953 put_task_struct(req->task);
954}
955
Jens Axboec40f6372020-06-25 15:39:59 -0600956static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
957{
958 struct mm_struct *mm = current->mm;
959
960 if (mm) {
961 kthread_unuse_mm(mm);
962 mmput(mm);
963 }
964}
965
966static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
967{
968 if (!current->mm) {
969 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
970 return -EFAULT;
971 kthread_use_mm(ctx->sqo_mm);
972 }
973
974 return 0;
975}
976
977static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
978 struct io_kiocb *req)
979{
980 if (!io_op_defs[req->opcode].needs_mm)
981 return 0;
982 return __io_sq_thread_acquire_mm(ctx);
983}
984
985static inline void req_set_fail_links(struct io_kiocb *req)
986{
987 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
988 req->flags |= REQ_F_FAIL_LINK;
989}
990
Jens Axboe4a38aed22020-05-14 17:21:15 -0600991static void io_file_put_work(struct work_struct *work);
992
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800993/*
994 * Note: must call io_req_init_async() for the first time you
995 * touch any members of io_wq_work.
996 */
997static inline void io_req_init_async(struct io_kiocb *req)
998{
999 if (req->flags & REQ_F_WORK_INITIALIZED)
1000 return;
1001
1002 memset(&req->work, 0, sizeof(req->work));
1003 req->flags |= REQ_F_WORK_INITIALIZED;
1004}
1005
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001006static inline bool io_async_submit(struct io_ring_ctx *ctx)
1007{
1008 return ctx->flags & IORING_SETUP_SQPOLL;
1009}
1010
Jens Axboe2b188cc2019-01-07 10:46:33 -07001011static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1012{
1013 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1014
Jens Axboe0f158b42020-05-14 17:18:39 -06001015 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001016}
1017
1018static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1019{
1020 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001021 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022
1023 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1024 if (!ctx)
1025 return NULL;
1026
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001027 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1028 if (!ctx->fallback_req)
1029 goto err;
1030
Jens Axboe78076bb2019-12-04 19:56:40 -07001031 /*
1032 * Use 5 bits less than the max cq entries, that should give us around
1033 * 32 entries per hash list if totally full and uniformly spread.
1034 */
1035 hash_bits = ilog2(p->cq_entries);
1036 hash_bits -= 5;
1037 if (hash_bits <= 0)
1038 hash_bits = 1;
1039 ctx->cancel_hash_bits = hash_bits;
1040 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1041 GFP_KERNEL);
1042 if (!ctx->cancel_hash)
1043 goto err;
1044 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1045
Roman Gushchin21482892019-05-07 10:01:48 -07001046 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001047 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1048 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049
1050 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001051 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001052 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001053 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001054 init_completion(&ctx->ref_comp);
1055 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001056 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001057 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001058 mutex_init(&ctx->uring_lock);
1059 init_waitqueue_head(&ctx->wait);
1060 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001061 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001062 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001063 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001064 init_waitqueue_head(&ctx->inflight_wait);
1065 spin_lock_init(&ctx->inflight_lock);
1066 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001067 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1068 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001069 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001070err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001071 if (ctx->fallback_req)
1072 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001073 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001074 kfree(ctx);
1075 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076}
1077
Bob Liu9d858b22019-11-13 18:06:25 +08001078static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06001079{
Jackie Liua197f662019-11-08 08:09:12 -07001080 struct io_ring_ctx *ctx = req->ctx;
1081
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001082 return req->sequence != ctx->cached_cq_tail
1083 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -06001084}
1085
Bob Liu9d858b22019-11-13 18:06:25 +08001086static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001087{
Pavel Begunkov87987892020-01-18 01:22:30 +03001088 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +08001089 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001090
Bob Liu9d858b22019-11-13 18:06:25 +08001091 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001092}
1093
Jens Axboede0617e2019-04-06 21:51:27 -06001094static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095{
Hristo Venev75b28af2019-08-26 17:23:46 +00001096 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001097
Pavel Begunkov07910152020-01-17 03:52:46 +03001098 /* order cqe stores with ring update */
1099 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100
Pavel Begunkov07910152020-01-17 03:52:46 +03001101 if (wq_has_sleeper(&ctx->cq_wait)) {
1102 wake_up_interruptible(&ctx->cq_wait);
1103 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104 }
1105}
1106
Jens Axboecccf0ee2020-01-27 16:34:48 -07001107static inline void io_req_work_grab_env(struct io_kiocb *req,
1108 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001109{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001110 if (!req->work.mm && def->needs_mm) {
1111 mmgrab(current->mm);
1112 req->work.mm = current->mm;
1113 }
1114 if (!req->work.creds)
1115 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001116 if (!req->work.fs && def->needs_fs) {
1117 spin_lock(&current->fs->lock);
1118 if (!current->fs->in_exec) {
1119 req->work.fs = current->fs;
1120 req->work.fs->users++;
1121 } else {
1122 req->work.flags |= IO_WQ_WORK_CANCEL;
1123 }
1124 spin_unlock(&current->fs->lock);
1125 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001126}
1127
1128static inline void io_req_work_drop_env(struct io_kiocb *req)
1129{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001130 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1131 return;
1132
Jens Axboecccf0ee2020-01-27 16:34:48 -07001133 if (req->work.mm) {
1134 mmdrop(req->work.mm);
1135 req->work.mm = NULL;
1136 }
1137 if (req->work.creds) {
1138 put_cred(req->work.creds);
1139 req->work.creds = NULL;
1140 }
Jens Axboeff002b32020-02-07 16:05:21 -07001141 if (req->work.fs) {
1142 struct fs_struct *fs = req->work.fs;
1143
1144 spin_lock(&req->work.fs->lock);
1145 if (--fs->users)
1146 fs = NULL;
1147 spin_unlock(&req->work.fs->lock);
1148 if (fs)
1149 free_fs_struct(fs);
1150 }
Jens Axboe561fb042019-10-24 07:25:42 -06001151}
1152
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001153static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001154 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001155{
Jens Axboed3656342019-12-18 09:50:26 -07001156 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001157
Jens Axboed3656342019-12-18 09:50:26 -07001158 if (req->flags & REQ_F_ISREG) {
1159 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001160 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001161 } else {
1162 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001163 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001164 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001165
Pavel Begunkov59960b92020-06-15 16:36:30 +03001166 io_req_init_async(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001167 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001168
Jens Axboe94ae5e72019-11-14 19:39:52 -07001169 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001170}
1171
Jackie Liua197f662019-11-08 08:09:12 -07001172static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001173{
Jackie Liua197f662019-11-08 08:09:12 -07001174 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001175 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001176
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001177 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001178
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001179 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1180 &req->work, req->flags);
1181 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001182
1183 if (link)
1184 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001185}
1186
Jens Axboe5262f562019-09-17 12:26:57 -06001187static void io_kill_timeout(struct io_kiocb *req)
1188{
1189 int ret;
1190
Jens Axboe2d283902019-12-04 11:08:05 -07001191 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001192 if (ret != -1) {
1193 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001194 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001195 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001196 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001197 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001198 }
1199}
1200
1201static void io_kill_timeouts(struct io_ring_ctx *ctx)
1202{
1203 struct io_kiocb *req, *tmp;
1204
1205 spin_lock_irq(&ctx->completion_lock);
1206 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1207 io_kill_timeout(req);
1208 spin_unlock_irq(&ctx->completion_lock);
1209}
1210
Pavel Begunkov04518942020-05-26 20:34:05 +03001211static void __io_queue_deferred(struct io_ring_ctx *ctx)
1212{
1213 do {
1214 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1215 struct io_kiocb, list);
1216
1217 if (req_need_defer(req))
1218 break;
1219 list_del_init(&req->list);
1220 io_queue_async_work(req);
1221 } while (!list_empty(&ctx->defer_list));
1222}
1223
Pavel Begunkov360428f2020-05-30 14:54:17 +03001224static void io_flush_timeouts(struct io_ring_ctx *ctx)
1225{
1226 while (!list_empty(&ctx->timeout_list)) {
1227 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1228 struct io_kiocb, list);
1229
1230 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1231 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001232 if (req->timeout.target_seq != ctx->cached_cq_tail
1233 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001234 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001235
Pavel Begunkov360428f2020-05-30 14:54:17 +03001236 list_del_init(&req->list);
1237 io_kill_timeout(req);
1238 }
1239}
1240
Jens Axboede0617e2019-04-06 21:51:27 -06001241static void io_commit_cqring(struct io_ring_ctx *ctx)
1242{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001243 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001244 __io_commit_cqring(ctx);
1245
Pavel Begunkov04518942020-05-26 20:34:05 +03001246 if (unlikely(!list_empty(&ctx->defer_list)))
1247 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001248}
1249
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1251{
Hristo Venev75b28af2019-08-26 17:23:46 +00001252 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253 unsigned tail;
1254
1255 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001256 /*
1257 * writes to the cq entry need to come after reading head; the
1258 * control dependency is enough as we're using WRITE_ONCE to
1259 * fill the cq entry
1260 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001261 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262 return NULL;
1263
1264 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001265 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266}
1267
Jens Axboef2842ab2020-01-08 11:04:00 -07001268static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1269{
Jens Axboef0b493e2020-02-01 21:30:11 -07001270 if (!ctx->cq_ev_fd)
1271 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001272 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1273 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001274 if (!ctx->eventfd_async)
1275 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001276 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001277}
1278
Jens Axboeb41e9852020-02-17 09:52:41 -07001279static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001280{
1281 if (waitqueue_active(&ctx->wait))
1282 wake_up(&ctx->wait);
1283 if (waitqueue_active(&ctx->sqo_wait))
1284 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001285 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001286 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001287}
1288
Jens Axboec4a2ed72019-11-21 21:01:26 -07001289/* Returns true if there are no backlogged entries after the flush */
1290static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001291{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001292 struct io_rings *rings = ctx->rings;
1293 struct io_uring_cqe *cqe;
1294 struct io_kiocb *req;
1295 unsigned long flags;
1296 LIST_HEAD(list);
1297
1298 if (!force) {
1299 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001300 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001301 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1302 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001303 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001304 }
1305
1306 spin_lock_irqsave(&ctx->completion_lock, flags);
1307
1308 /* if force is set, the ring is going away. always drop after that */
1309 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001310 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001311
Jens Axboec4a2ed72019-11-21 21:01:26 -07001312 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001313 while (!list_empty(&ctx->cq_overflow_list)) {
1314 cqe = io_get_cqring(ctx);
1315 if (!cqe && !force)
1316 break;
1317
1318 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1319 list);
1320 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001321 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001322 if (cqe) {
1323 WRITE_ONCE(cqe->user_data, req->user_data);
1324 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001325 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001326 } else {
1327 WRITE_ONCE(ctx->rings->cq_overflow,
1328 atomic_inc_return(&ctx->cached_cq_overflow));
1329 }
1330 }
1331
1332 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001333 if (cqe) {
1334 clear_bit(0, &ctx->sq_check_overflow);
1335 clear_bit(0, &ctx->cq_check_overflow);
1336 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001337 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1338 io_cqring_ev_posted(ctx);
1339
1340 while (!list_empty(&list)) {
1341 req = list_first_entry(&list, struct io_kiocb, list);
1342 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001343 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001344 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001345
1346 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001347}
1348
Jens Axboebcda7ba2020-02-23 16:42:51 -07001349static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001351 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352 struct io_uring_cqe *cqe;
1353
Jens Axboe78e19bb2019-11-06 15:21:34 -07001354 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001355
Jens Axboe2b188cc2019-01-07 10:46:33 -07001356 /*
1357 * If we can't get a cq entry, userspace overflowed the
1358 * submission (by quite a lot). Increment the overflow count in
1359 * the ring.
1360 */
1361 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001362 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001363 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001365 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001366 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367 WRITE_ONCE(ctx->rings->cq_overflow,
1368 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001369 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001370 if (list_empty(&ctx->cq_overflow_list)) {
1371 set_bit(0, &ctx->sq_check_overflow);
1372 set_bit(0, &ctx->cq_check_overflow);
1373 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001374 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001375 refcount_inc(&req->refs);
1376 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001377 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001378 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379 }
1380}
1381
Jens Axboebcda7ba2020-02-23 16:42:51 -07001382static void io_cqring_fill_event(struct io_kiocb *req, long res)
1383{
1384 __io_cqring_fill_event(req, res, 0);
1385}
1386
Jens Axboee1e16092020-06-22 09:17:17 -06001387static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001388{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001389 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390 unsigned long flags;
1391
1392 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001393 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001394 io_commit_cqring(ctx);
1395 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1396
Jens Axboe8c838782019-03-12 15:48:16 -06001397 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398}
1399
Jens Axboe229a7b62020-06-22 10:13:11 -06001400static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001401{
Jens Axboe229a7b62020-06-22 10:13:11 -06001402 struct io_ring_ctx *ctx = cs->ctx;
1403
1404 spin_lock_irq(&ctx->completion_lock);
1405 while (!list_empty(&cs->list)) {
1406 struct io_kiocb *req;
1407
1408 req = list_first_entry(&cs->list, struct io_kiocb, list);
1409 list_del(&req->list);
1410 io_cqring_fill_event(req, req->result);
1411 if (!(req->flags & REQ_F_LINK_HEAD)) {
1412 req->flags |= REQ_F_COMP_LOCKED;
1413 io_put_req(req);
1414 } else {
1415 spin_unlock_irq(&ctx->completion_lock);
1416 io_put_req(req);
1417 spin_lock_irq(&ctx->completion_lock);
1418 }
1419 }
1420 io_commit_cqring(ctx);
1421 spin_unlock_irq(&ctx->completion_lock);
1422
1423 io_cqring_ev_posted(ctx);
1424 cs->nr = 0;
1425}
1426
1427static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1428 struct io_comp_state *cs)
1429{
1430 if (!cs) {
1431 io_cqring_add_event(req, res, cflags);
1432 io_put_req(req);
1433 } else {
1434 req->result = res;
1435 list_add_tail(&req->list, &cs->list);
1436 if (++cs->nr >= 32)
1437 io_submit_flush_completions(cs);
1438 }
Jens Axboee1e16092020-06-22 09:17:17 -06001439}
1440
1441static void io_req_complete(struct io_kiocb *req, long res)
1442{
Jens Axboe229a7b62020-06-22 10:13:11 -06001443 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001444}
1445
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001446static inline bool io_is_fallback_req(struct io_kiocb *req)
1447{
1448 return req == (struct io_kiocb *)
1449 ((unsigned long) req->ctx->fallback_req & ~1UL);
1450}
1451
1452static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1453{
1454 struct io_kiocb *req;
1455
1456 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001457 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001458 return req;
1459
1460 return NULL;
1461}
1462
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001463static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1464 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465{
Jens Axboefd6fab22019-03-14 16:30:06 -06001466 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001467 struct io_kiocb *req;
1468
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001469 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001470 size_t sz;
1471 int ret;
1472
1473 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001474 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1475
1476 /*
1477 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1478 * retry single alloc to be on the safe side.
1479 */
1480 if (unlikely(ret <= 0)) {
1481 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1482 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001483 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001484 ret = 1;
1485 }
Jens Axboe2579f912019-01-09 09:10:43 -07001486 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001487 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001488 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001489 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001490 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001491 }
1492
Jens Axboe2579f912019-01-09 09:10:43 -07001493 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001494fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001495 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001496}
1497
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001498static inline void io_put_file(struct io_kiocb *req, struct file *file,
1499 bool fixed)
1500{
1501 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001502 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001503 else
1504 fput(file);
1505}
1506
Jens Axboec6ca97b302019-12-28 12:11:08 -07001507static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001508{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001509 if (req->flags & REQ_F_NEED_CLEANUP)
1510 io_cleanup_req(req);
1511
YueHaibing96fd84d2020-01-07 22:22:44 +08001512 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001513 if (req->file)
1514 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001515 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001516 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001517}
1518
1519static void __io_free_req(struct io_kiocb *req)
1520{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001521 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001522
Jens Axboefcb323c2019-10-24 12:39:47 -06001523 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001524 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001525 unsigned long flags;
1526
1527 spin_lock_irqsave(&ctx->inflight_lock, flags);
1528 list_del(&req->inflight_entry);
1529 if (waitqueue_active(&ctx->inflight_wait))
1530 wake_up(&ctx->inflight_wait);
1531 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1532 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001533
1534 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001535 if (likely(!io_is_fallback_req(req)))
1536 kmem_cache_free(req_cachep, req);
1537 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001538 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001539}
1540
Jens Axboec6ca97b302019-12-28 12:11:08 -07001541struct req_batch {
1542 void *reqs[IO_IOPOLL_BATCH];
1543 int to_free;
1544 int need_iter;
1545};
1546
1547static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1548{
1549 if (!rb->to_free)
1550 return;
1551 if (rb->need_iter) {
1552 int i, inflight = 0;
1553 unsigned long flags;
1554
1555 for (i = 0; i < rb->to_free; i++) {
1556 struct io_kiocb *req = rb->reqs[i];
1557
Jens Axboec6ca97b302019-12-28 12:11:08 -07001558 if (req->flags & REQ_F_INFLIGHT)
1559 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001560 __io_req_aux_free(req);
1561 }
1562 if (!inflight)
1563 goto do_free;
1564
1565 spin_lock_irqsave(&ctx->inflight_lock, flags);
1566 for (i = 0; i < rb->to_free; i++) {
1567 struct io_kiocb *req = rb->reqs[i];
1568
Jens Axboe10fef4b2020-01-09 07:52:28 -07001569 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001570 list_del(&req->inflight_entry);
1571 if (!--inflight)
1572 break;
1573 }
1574 }
1575 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1576
1577 if (waitqueue_active(&ctx->inflight_wait))
1578 wake_up(&ctx->inflight_wait);
1579 }
1580do_free:
1581 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1582 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001583 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001584}
1585
Jackie Liua197f662019-11-08 08:09:12 -07001586static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001587{
Jackie Liua197f662019-11-08 08:09:12 -07001588 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001589 int ret;
1590
Jens Axboe2d283902019-12-04 11:08:05 -07001591 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001592 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001593 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001594 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001595 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001596 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001597 return true;
1598 }
1599
1600 return false;
1601}
1602
Jens Axboeba816ad2019-09-28 11:36:45 -06001603static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001604{
Jens Axboe2665abf2019-11-05 12:40:47 -07001605 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001606 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001607
Jens Axboe4d7dd462019-11-20 13:03:52 -07001608 /* Already got next link */
1609 if (req->flags & REQ_F_LINK_NEXT)
1610 return;
1611
Jens Axboe9e645e112019-05-10 16:07:28 -06001612 /*
1613 * The list should never be empty when we are called here. But could
1614 * potentially happen if the chain is messed up, check to be on the
1615 * safe side.
1616 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001617 while (!list_empty(&req->link_list)) {
1618 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1619 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001620
Pavel Begunkov44932332019-12-05 16:16:35 +03001621 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1622 (nxt->flags & REQ_F_TIMEOUT))) {
1623 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001624 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001625 req->flags &= ~REQ_F_LINK_TIMEOUT;
1626 continue;
1627 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001628
Pavel Begunkov44932332019-12-05 16:16:35 +03001629 list_del_init(&req->link_list);
1630 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001631 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001632 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001633 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001634 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001635
Jens Axboe4d7dd462019-11-20 13:03:52 -07001636 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001637 if (wake_ev)
1638 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001639}
1640
1641/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001642 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001643 */
1644static void io_fail_links(struct io_kiocb *req)
1645{
Jens Axboe2665abf2019-11-05 12:40:47 -07001646 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001647 unsigned long flags;
1648
1649 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001650
1651 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001652 struct io_kiocb *link = list_first_entry(&req->link_list,
1653 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001654
Pavel Begunkov44932332019-12-05 16:16:35 +03001655 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001656 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001657
1658 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001659 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001660 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001661 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001662 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001663 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001664 }
Jens Axboe5d960722019-11-19 15:31:28 -07001665 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001666 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001667
1668 io_commit_cqring(ctx);
1669 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1670 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001671}
1672
Jens Axboe4d7dd462019-11-20 13:03:52 -07001673static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001674{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001675 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001676 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001677
Jens Axboe9e645e112019-05-10 16:07:28 -06001678 /*
1679 * If LINK is set, we have dependent requests in this chain. If we
1680 * didn't fail this request, queue the first one up, moving any other
1681 * dependencies to the next request. In case of failure, fail the rest
1682 * of the chain.
1683 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001684 if (req->flags & REQ_F_FAIL_LINK) {
1685 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001686 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1687 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001688 struct io_ring_ctx *ctx = req->ctx;
1689 unsigned long flags;
1690
1691 /*
1692 * If this is a timeout link, we could be racing with the
1693 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001694 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001695 */
1696 spin_lock_irqsave(&ctx->completion_lock, flags);
1697 io_req_link_next(req, nxt);
1698 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1699 } else {
1700 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001701 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001702}
Jens Axboe9e645e112019-05-10 16:07:28 -06001703
Jens Axboec40f6372020-06-25 15:39:59 -06001704static void __io_req_task_cancel(struct io_kiocb *req, int error)
1705{
1706 struct io_ring_ctx *ctx = req->ctx;
1707
1708 spin_lock_irq(&ctx->completion_lock);
1709 io_cqring_fill_event(req, error);
1710 io_commit_cqring(ctx);
1711 spin_unlock_irq(&ctx->completion_lock);
1712
1713 io_cqring_ev_posted(ctx);
1714 req_set_fail_links(req);
1715 io_double_put_req(req);
1716}
1717
1718static void io_req_task_cancel(struct callback_head *cb)
1719{
1720 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1721
1722 __io_req_task_cancel(req, -ECANCELED);
1723}
1724
1725static void __io_req_task_submit(struct io_kiocb *req)
1726{
1727 struct io_ring_ctx *ctx = req->ctx;
1728
1729 __set_current_state(TASK_RUNNING);
1730 if (!__io_sq_thread_acquire_mm(ctx)) {
1731 mutex_lock(&ctx->uring_lock);
1732 __io_queue_sqe(req, NULL, NULL);
1733 mutex_unlock(&ctx->uring_lock);
1734 } else {
1735 __io_req_task_cancel(req, -EFAULT);
1736 }
1737}
1738
1739static void io_req_task_submit(struct callback_head *cb)
1740{
1741 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1742
1743 __io_req_task_submit(req);
1744}
1745
1746static void io_req_task_queue(struct io_kiocb *req)
1747{
1748 struct task_struct *tsk = req->task;
1749 int ret;
1750
1751 init_task_work(&req->task_work, io_req_task_submit);
1752
1753 ret = task_work_add(tsk, &req->task_work, true);
1754 if (unlikely(ret)) {
1755 init_task_work(&req->task_work, io_req_task_cancel);
1756 tsk = io_wq_get_task(req->ctx->io_wq);
1757 task_work_add(tsk, &req->task_work, true);
1758 }
1759 wake_up_process(tsk);
1760}
1761
Jackie Liuc69f8db2019-11-09 11:00:08 +08001762static void io_free_req(struct io_kiocb *req)
1763{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001764 struct io_kiocb *nxt = NULL;
1765
1766 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001767 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001768
Jens Axboec40f6372020-06-25 15:39:59 -06001769 if (nxt) {
1770 if (nxt->flags & REQ_F_WORK_INITIALIZED)
1771 io_queue_async_work(nxt);
1772 else
1773 io_req_task_queue(nxt);
1774 }
Jackie Liuc69f8db2019-11-09 11:00:08 +08001775}
1776
Jens Axboeba816ad2019-09-28 11:36:45 -06001777/*
1778 * Drop reference to request, return next in chain (if there is one) if this
1779 * was the last reference to this request.
1780 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001781__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001782static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001783{
Jens Axboe2a44f462020-02-25 13:25:41 -07001784 if (refcount_dec_and_test(&req->refs)) {
1785 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001786 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001787 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788}
1789
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790static void io_put_req(struct io_kiocb *req)
1791{
Jens Axboedef596e2019-01-09 08:59:42 -07001792 if (refcount_dec_and_test(&req->refs))
1793 io_free_req(req);
1794}
1795
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001796static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001797{
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001798 struct io_kiocb *link, *nxt = NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001799
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001800 /*
1801 * A ref is owned by io-wq in which context we're. So, if that's the
1802 * last one, it's safe to steal next work. False negatives are Ok,
1803 * it just will be re-punted async in io_put_work()
1804 */
1805 if (refcount_read(&req->refs) != 1)
1806 return NULL;
1807
1808 io_req_find_next(req, &nxt);
1809 if (!nxt)
1810 return NULL;
1811
1812 if ((nxt->flags & REQ_F_ISREG) && io_op_defs[nxt->opcode].hash_reg_file)
1813 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
1814
1815 link = io_prep_linked_timeout(nxt);
1816 if (link)
1817 nxt->flags |= REQ_F_QUEUE_TIMEOUT;
1818 return &nxt->work;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001819}
1820
Jens Axboe978db572019-11-14 22:39:04 -07001821/*
1822 * Must only be used if we don't need to care about links, usually from
1823 * within the completion handling itself.
1824 */
1825static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001826{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001827 /* drop both submit and complete references */
1828 if (refcount_sub_and_test(2, &req->refs))
1829 __io_free_req(req);
1830}
1831
Jens Axboe978db572019-11-14 22:39:04 -07001832static void io_double_put_req(struct io_kiocb *req)
1833{
1834 /* drop both submit and complete references */
1835 if (refcount_sub_and_test(2, &req->refs))
1836 io_free_req(req);
1837}
1838
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001839static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001840{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001841 struct io_rings *rings = ctx->rings;
1842
Jens Axboead3eb2c2019-12-18 17:12:20 -07001843 if (test_bit(0, &ctx->cq_check_overflow)) {
1844 /*
1845 * noflush == true is from the waitqueue handler, just ensure
1846 * we wake up the task, and the next invocation will flush the
1847 * entries. We cannot safely to it from here.
1848 */
1849 if (noflush && !list_empty(&ctx->cq_overflow_list))
1850 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001851
Jens Axboead3eb2c2019-12-18 17:12:20 -07001852 io_cqring_overflow_flush(ctx, false);
1853 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001854
Jens Axboea3a0e432019-08-20 11:03:11 -06001855 /* See comment at the top of this file */
1856 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001857 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001858}
1859
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001860static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1861{
1862 struct io_rings *rings = ctx->rings;
1863
1864 /* make sure SQ entry isn't read before tail */
1865 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1866}
1867
Jens Axboe8237e042019-12-28 10:48:22 -07001868static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001869{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001870 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001871 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001872
Jens Axboe9d9e88a2020-05-13 12:53:19 -06001873 if (req->file || req->io)
Jens Axboec6ca97b302019-12-28 12:11:08 -07001874 rb->need_iter++;
1875
1876 rb->reqs[rb->to_free++] = req;
1877 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1878 io_free_req_many(req->ctx, rb);
1879 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001880}
1881
Jens Axboebcda7ba2020-02-23 16:42:51 -07001882static int io_put_kbuf(struct io_kiocb *req)
1883{
Jens Axboe4d954c22020-02-27 07:31:19 -07001884 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001885 int cflags;
1886
Jens Axboe4d954c22020-02-27 07:31:19 -07001887 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001888 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1889 cflags |= IORING_CQE_F_BUFFER;
1890 req->rw.addr = 0;
1891 kfree(kbuf);
1892 return cflags;
1893}
1894
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001895static void io_iopoll_queue(struct list_head *again)
1896{
1897 struct io_kiocb *req;
1898
1899 do {
1900 req = list_first_entry(again, struct io_kiocb, list);
1901 list_del(&req->list);
Pavel Begunkovd60b5fb2020-06-25 12:37:11 +03001902
1903 /* shouldn't happen unless io_uring is dying, cancel reqs */
1904 if (unlikely(!current->mm)) {
Jens Axboe2237d762020-06-26 13:44:16 -06001905 io_complete_rw_common(&req->rw.kiocb, -EAGAIN, NULL);
Pavel Begunkovd60b5fb2020-06-25 12:37:11 +03001906 io_put_req(req);
1907 continue;
1908 }
1909
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001910 refcount_inc(&req->refs);
1911 io_queue_async_work(req);
1912 } while (!list_empty(again));
1913}
1914
Jens Axboedef596e2019-01-09 08:59:42 -07001915/*
1916 * Find and free completed poll iocbs
1917 */
1918static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1919 struct list_head *done)
1920{
Jens Axboe8237e042019-12-28 10:48:22 -07001921 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001922 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001923 LIST_HEAD(again);
1924
1925 /* order with ->result store in io_complete_rw_iopoll() */
1926 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001927
Jens Axboec6ca97b302019-12-28 12:11:08 -07001928 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001929 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001930 int cflags = 0;
1931
Jens Axboedef596e2019-01-09 08:59:42 -07001932 req = list_first_entry(done, struct io_kiocb, list);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001933 if (READ_ONCE(req->result) == -EAGAIN) {
1934 req->iopoll_completed = 0;
1935 list_move_tail(&req->list, &again);
1936 continue;
1937 }
Jens Axboedef596e2019-01-09 08:59:42 -07001938 list_del(&req->list);
1939
Jens Axboebcda7ba2020-02-23 16:42:51 -07001940 if (req->flags & REQ_F_BUFFER_SELECTED)
1941 cflags = io_put_kbuf(req);
1942
1943 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001944 (*nr_events)++;
1945
Jens Axboe8237e042019-12-28 10:48:22 -07001946 if (refcount_dec_and_test(&req->refs) &&
1947 !io_req_multi_free(&rb, req))
1948 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001949 }
Jens Axboedef596e2019-01-09 08:59:42 -07001950
Jens Axboe09bb8392019-03-13 12:39:28 -06001951 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001952 if (ctx->flags & IORING_SETUP_SQPOLL)
1953 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001954 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001955
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001956 if (!list_empty(&again))
1957 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001958}
1959
Jens Axboedef596e2019-01-09 08:59:42 -07001960static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1961 long min)
1962{
1963 struct io_kiocb *req, *tmp;
1964 LIST_HEAD(done);
1965 bool spin;
1966 int ret;
1967
1968 /*
1969 * Only spin for completions if we don't have multiple devices hanging
1970 * off our complete list, and we're under the requested amount.
1971 */
1972 spin = !ctx->poll_multi_file && *nr_events < min;
1973
1974 ret = 0;
1975 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001976 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001977
1978 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001979 * Move completed and retryable entries to our local lists.
1980 * If we find a request that requires polling, break out
1981 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001982 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08001983 if (READ_ONCE(req->iopoll_completed)) {
Jens Axboedef596e2019-01-09 08:59:42 -07001984 list_move_tail(&req->list, &done);
1985 continue;
1986 }
1987 if (!list_empty(&done))
1988 break;
1989
1990 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1991 if (ret < 0)
1992 break;
1993
1994 if (ret && spin)
1995 spin = false;
1996 ret = 0;
1997 }
1998
1999 if (!list_empty(&done))
2000 io_iopoll_complete(ctx, nr_events, &done);
2001
2002 return ret;
2003}
2004
2005/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002006 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002007 * non-spinning poll check - we'll still enter the driver poll loop, but only
2008 * as a non-spinning completion check.
2009 */
2010static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2011 long min)
2012{
Jens Axboe08f54392019-08-21 22:19:11 -06002013 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002014 int ret;
2015
2016 ret = io_do_iopoll(ctx, nr_events, min);
2017 if (ret < 0)
2018 return ret;
2019 if (!min || *nr_events >= min)
2020 return 0;
2021 }
2022
2023 return 1;
2024}
2025
2026/*
2027 * We can't just wait for polled events to come to us, we have to actively
2028 * find and complete them.
2029 */
2030static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
2031{
2032 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2033 return;
2034
2035 mutex_lock(&ctx->uring_lock);
2036 while (!list_empty(&ctx->poll_list)) {
2037 unsigned int nr_events = 0;
2038
2039 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06002040
2041 /*
2042 * Ensure we allow local-to-the-cpu processing to take place,
2043 * in this case we need to ensure that we reap all events.
2044 */
2045 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07002046 }
2047 mutex_unlock(&ctx->uring_lock);
2048}
2049
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002050static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
2051 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002052{
Jens Axboe2b2ed972019-10-25 10:06:15 -06002053 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002054
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002055 /*
2056 * We disallow the app entering submit/complete with polling, but we
2057 * still need to lock the ring to prevent racing with polled issue
2058 * that got punted to a workqueue.
2059 */
2060 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002061 do {
2062 int tmin = 0;
2063
Jens Axboe500f9fb2019-08-19 12:15:59 -06002064 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002065 * Don't enter poll loop if we already have events pending.
2066 * If we do, we can potentially be spinning for commands that
2067 * already triggered a CQE (eg in error).
2068 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002069 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002070 break;
2071
2072 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002073 * If a submit got punted to a workqueue, we can have the
2074 * application entering polling for a command before it gets
2075 * issued. That app will hold the uring_lock for the duration
2076 * of the poll right here, so we need to take a breather every
2077 * now and then to ensure that the issue has a chance to add
2078 * the poll to the issued list. Otherwise we can spin here
2079 * forever, while the workqueue is stuck trying to acquire the
2080 * very same mutex.
2081 */
2082 if (!(++iters & 7)) {
2083 mutex_unlock(&ctx->uring_lock);
2084 mutex_lock(&ctx->uring_lock);
2085 }
2086
Jens Axboedef596e2019-01-09 08:59:42 -07002087 if (*nr_events < min)
2088 tmin = min - *nr_events;
2089
2090 ret = io_iopoll_getevents(ctx, nr_events, tmin);
2091 if (ret <= 0)
2092 break;
2093 ret = 0;
2094 } while (min && !*nr_events && !need_resched());
2095
Jens Axboe500f9fb2019-08-19 12:15:59 -06002096 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002097 return ret;
2098}
2099
Jens Axboe491381ce2019-10-17 09:20:46 -06002100static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002101{
Jens Axboe491381ce2019-10-17 09:20:46 -06002102 /*
2103 * Tell lockdep we inherited freeze protection from submission
2104 * thread.
2105 */
2106 if (req->flags & REQ_F_ISREG) {
2107 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002108
Jens Axboe491381ce2019-10-17 09:20:46 -06002109 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002110 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002111 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002112}
2113
Jens Axboea1d7c392020-06-22 11:09:46 -06002114static void io_complete_rw_common(struct kiocb *kiocb, long res,
2115 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002116{
Jens Axboe9adbd452019-12-20 08:45:55 -07002117 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002118 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002119
Jens Axboe491381ce2019-10-17 09:20:46 -06002120 if (kiocb->ki_flags & IOCB_WRITE)
2121 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002122
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002123 if (res != req->result)
2124 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002125 if (req->flags & REQ_F_BUFFER_SELECTED)
2126 cflags = io_put_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002127 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002128}
2129
Jens Axboeb63534c2020-06-04 11:28:00 -06002130#ifdef CONFIG_BLOCK
2131static bool io_resubmit_prep(struct io_kiocb *req, int error)
2132{
2133 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2134 ssize_t ret = -ECANCELED;
2135 struct iov_iter iter;
2136 int rw;
2137
2138 if (error) {
2139 ret = error;
2140 goto end_req;
2141 }
2142
2143 switch (req->opcode) {
2144 case IORING_OP_READV:
2145 case IORING_OP_READ_FIXED:
2146 case IORING_OP_READ:
2147 rw = READ;
2148 break;
2149 case IORING_OP_WRITEV:
2150 case IORING_OP_WRITE_FIXED:
2151 case IORING_OP_WRITE:
2152 rw = WRITE;
2153 break;
2154 default:
2155 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2156 req->opcode);
2157 goto end_req;
2158 }
2159
2160 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2161 if (ret < 0)
2162 goto end_req;
2163 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2164 if (!ret)
2165 return true;
2166 kfree(iovec);
2167end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002168 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002169 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002170 return false;
2171}
2172
2173static void io_rw_resubmit(struct callback_head *cb)
2174{
2175 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2176 struct io_ring_ctx *ctx = req->ctx;
2177 int err;
2178
2179 __set_current_state(TASK_RUNNING);
2180
2181 err = io_sq_thread_acquire_mm(ctx, req);
2182
2183 if (io_resubmit_prep(req, err)) {
2184 refcount_inc(&req->refs);
2185 io_queue_async_work(req);
2186 }
2187}
2188#endif
2189
2190static bool io_rw_reissue(struct io_kiocb *req, long res)
2191{
2192#ifdef CONFIG_BLOCK
2193 struct task_struct *tsk;
2194 int ret;
2195
2196 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2197 return false;
2198
2199 tsk = req->task;
2200 init_task_work(&req->task_work, io_rw_resubmit);
2201 ret = task_work_add(tsk, &req->task_work, true);
2202 if (!ret)
2203 return true;
2204#endif
2205 return false;
2206}
2207
Jens Axboea1d7c392020-06-22 11:09:46 -06002208static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2209 struct io_comp_state *cs)
2210{
2211 if (!io_rw_reissue(req, res))
2212 io_complete_rw_common(&req->rw.kiocb, res, cs);
2213}
2214
Jens Axboeba816ad2019-09-28 11:36:45 -06002215static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2216{
Jens Axboe9adbd452019-12-20 08:45:55 -07002217 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002218
Jens Axboea1d7c392020-06-22 11:09:46 -06002219 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002220}
2221
Jens Axboedef596e2019-01-09 08:59:42 -07002222static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2223{
Jens Axboe9adbd452019-12-20 08:45:55 -07002224 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002225
Jens Axboe491381ce2019-10-17 09:20:46 -06002226 if (kiocb->ki_flags & IOCB_WRITE)
2227 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002228
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002229 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002230 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002231
2232 WRITE_ONCE(req->result, res);
2233 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002234 smp_wmb();
2235 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002236}
2237
2238/*
2239 * After the iocb has been issued, it's safe to be found on the poll list.
2240 * Adding the kiocb to the list AFTER submission ensures that we don't
2241 * find it from a io_iopoll_getevents() thread before the issuer is done
2242 * accessing the kiocb cookie.
2243 */
2244static void io_iopoll_req_issued(struct io_kiocb *req)
2245{
2246 struct io_ring_ctx *ctx = req->ctx;
2247
2248 /*
2249 * Track whether we have multiple files in our lists. This will impact
2250 * how we do polling eventually, not spinning if we're on potentially
2251 * different devices.
2252 */
2253 if (list_empty(&ctx->poll_list)) {
2254 ctx->poll_multi_file = false;
2255 } else if (!ctx->poll_multi_file) {
2256 struct io_kiocb *list_req;
2257
2258 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2259 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002260 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002261 ctx->poll_multi_file = true;
2262 }
2263
2264 /*
2265 * For fast devices, IO may have already completed. If it has, add
2266 * it to the front so we find it first.
2267 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002268 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002269 list_add(&req->list, &ctx->poll_list);
2270 else
2271 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002272
2273 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2274 wq_has_sleeper(&ctx->sqo_wait))
2275 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002276}
2277
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002278static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002279{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002280 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002281
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002282 if (diff)
2283 fput_many(state->file, diff);
2284 state->file = NULL;
2285}
2286
2287static inline void io_state_file_put(struct io_submit_state *state)
2288{
2289 if (state->file)
2290 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002291}
2292
2293/*
2294 * Get as many references to a file as we have IOs left in this submission,
2295 * assuming most submissions are for one file, or at least that each file
2296 * has more than one submission.
2297 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002298static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002299{
2300 if (!state)
2301 return fget(fd);
2302
2303 if (state->file) {
2304 if (state->fd == fd) {
2305 state->used_refs++;
2306 state->ios_left--;
2307 return state->file;
2308 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002309 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002310 }
2311 state->file = fget_many(fd, state->ios_left);
2312 if (!state->file)
2313 return NULL;
2314
2315 state->fd = fd;
2316 state->has_refs = state->ios_left;
2317 state->used_refs = 1;
2318 state->ios_left--;
2319 return state->file;
2320}
2321
Jens Axboe4503b762020-06-01 10:00:27 -06002322static bool io_bdev_nowait(struct block_device *bdev)
2323{
2324#ifdef CONFIG_BLOCK
2325 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2326#else
2327 return true;
2328#endif
2329}
2330
Jens Axboe2b188cc2019-01-07 10:46:33 -07002331/*
2332 * If we tracked the file through the SCM inflight mechanism, we could support
2333 * any file. For now, just ensure that anything potentially problematic is done
2334 * inline.
2335 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002336static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002337{
2338 umode_t mode = file_inode(file)->i_mode;
2339
Jens Axboe4503b762020-06-01 10:00:27 -06002340 if (S_ISBLK(mode)) {
2341 if (io_bdev_nowait(file->f_inode->i_bdev))
2342 return true;
2343 return false;
2344 }
2345 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002346 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002347 if (S_ISREG(mode)) {
2348 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2349 file->f_op != &io_uring_fops)
2350 return true;
2351 return false;
2352 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002353
Jens Axboec5b85622020-06-09 19:23:05 -06002354 /* any ->read/write should understand O_NONBLOCK */
2355 if (file->f_flags & O_NONBLOCK)
2356 return true;
2357
Jens Axboeaf197f52020-04-28 13:15:06 -06002358 if (!(file->f_mode & FMODE_NOWAIT))
2359 return false;
2360
2361 if (rw == READ)
2362 return file->f_op->read_iter != NULL;
2363
2364 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002365}
2366
Jens Axboe3529d8c2019-12-19 18:24:38 -07002367static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2368 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002369{
Jens Axboedef596e2019-01-09 08:59:42 -07002370 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002371 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002372 unsigned ioprio;
2373 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002374
Jens Axboe491381ce2019-10-17 09:20:46 -06002375 if (S_ISREG(file_inode(req->file)->i_mode))
2376 req->flags |= REQ_F_ISREG;
2377
Jens Axboe2b188cc2019-01-07 10:46:33 -07002378 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002379 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2380 req->flags |= REQ_F_CUR_POS;
2381 kiocb->ki_pos = req->file->f_pos;
2382 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002383 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002384 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2385 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2386 if (unlikely(ret))
2387 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002388
2389 ioprio = READ_ONCE(sqe->ioprio);
2390 if (ioprio) {
2391 ret = ioprio_check_cap(ioprio);
2392 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002393 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002394
2395 kiocb->ki_ioprio = ioprio;
2396 } else
2397 kiocb->ki_ioprio = get_current_ioprio();
2398
Stefan Bühler8449eed2019-04-27 20:34:19 +02002399 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002400 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002401 req->flags |= REQ_F_NOWAIT;
2402
Jens Axboeb63534c2020-06-04 11:28:00 -06002403 if (kiocb->ki_flags & IOCB_DIRECT)
2404 io_get_req_task(req);
2405
Stefan Bühler8449eed2019-04-27 20:34:19 +02002406 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002407 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002408
Jens Axboedef596e2019-01-09 08:59:42 -07002409 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002410 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2411 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002412 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002413
Jens Axboedef596e2019-01-09 08:59:42 -07002414 kiocb->ki_flags |= IOCB_HIPRI;
2415 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002416 req->result = 0;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002417 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002418 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002419 if (kiocb->ki_flags & IOCB_HIPRI)
2420 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002421 kiocb->ki_complete = io_complete_rw;
2422 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002423
Jens Axboe3529d8c2019-12-19 18:24:38 -07002424 req->rw.addr = READ_ONCE(sqe->addr);
2425 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002426 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002427 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002428}
2429
2430static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2431{
2432 switch (ret) {
2433 case -EIOCBQUEUED:
2434 break;
2435 case -ERESTARTSYS:
2436 case -ERESTARTNOINTR:
2437 case -ERESTARTNOHAND:
2438 case -ERESTART_RESTARTBLOCK:
2439 /*
2440 * We can't just restart the syscall, since previously
2441 * submitted sqes may already be in progress. Just fail this
2442 * IO with EINTR.
2443 */
2444 ret = -EINTR;
2445 /* fall through */
2446 default:
2447 kiocb->ki_complete(kiocb, ret, 0);
2448 }
2449}
2450
Jens Axboea1d7c392020-06-22 11:09:46 -06002451static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2452 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002453{
Jens Axboeba042912019-12-25 16:33:42 -07002454 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2455
2456 if (req->flags & REQ_F_CUR_POS)
2457 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002458 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002459 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002460 else
2461 io_rw_done(kiocb, ret);
2462}
2463
Jens Axboe9adbd452019-12-20 08:45:55 -07002464static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002465 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002466{
Jens Axboe9adbd452019-12-20 08:45:55 -07002467 struct io_ring_ctx *ctx = req->ctx;
2468 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002469 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002470 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002471 size_t offset;
2472 u64 buf_addr;
2473
2474 /* attempt to use fixed buffers without having provided iovecs */
2475 if (unlikely(!ctx->user_bufs))
2476 return -EFAULT;
2477
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002478 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002479 if (unlikely(buf_index >= ctx->nr_user_bufs))
2480 return -EFAULT;
2481
2482 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2483 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002484 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002485
2486 /* overflow */
2487 if (buf_addr + len < buf_addr)
2488 return -EFAULT;
2489 /* not inside the mapped region */
2490 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2491 return -EFAULT;
2492
2493 /*
2494 * May not be a start of buffer, set size appropriately
2495 * and advance us to the beginning.
2496 */
2497 offset = buf_addr - imu->ubuf;
2498 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002499
2500 if (offset) {
2501 /*
2502 * Don't use iov_iter_advance() here, as it's really slow for
2503 * using the latter parts of a big fixed buffer - it iterates
2504 * over each segment manually. We can cheat a bit here, because
2505 * we know that:
2506 *
2507 * 1) it's a BVEC iter, we set it up
2508 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2509 * first and last bvec
2510 *
2511 * So just find our index, and adjust the iterator afterwards.
2512 * If the offset is within the first bvec (or the whole first
2513 * bvec, just use iov_iter_advance(). This makes it easier
2514 * since we can just skip the first segment, which may not
2515 * be PAGE_SIZE aligned.
2516 */
2517 const struct bio_vec *bvec = imu->bvec;
2518
2519 if (offset <= bvec->bv_len) {
2520 iov_iter_advance(iter, offset);
2521 } else {
2522 unsigned long seg_skip;
2523
2524 /* skip first vec */
2525 offset -= bvec->bv_len;
2526 seg_skip = 1 + (offset >> PAGE_SHIFT);
2527
2528 iter->bvec = bvec + seg_skip;
2529 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002530 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002531 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002532 }
2533 }
2534
Jens Axboe5e559562019-11-13 16:12:46 -07002535 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002536}
2537
Jens Axboebcda7ba2020-02-23 16:42:51 -07002538static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2539{
2540 if (needs_lock)
2541 mutex_unlock(&ctx->uring_lock);
2542}
2543
2544static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2545{
2546 /*
2547 * "Normal" inline submissions always hold the uring_lock, since we
2548 * grab it from the system call. Same is true for the SQPOLL offload.
2549 * The only exception is when we've detached the request and issue it
2550 * from an async worker thread, grab the lock for that case.
2551 */
2552 if (needs_lock)
2553 mutex_lock(&ctx->uring_lock);
2554}
2555
2556static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2557 int bgid, struct io_buffer *kbuf,
2558 bool needs_lock)
2559{
2560 struct io_buffer *head;
2561
2562 if (req->flags & REQ_F_BUFFER_SELECTED)
2563 return kbuf;
2564
2565 io_ring_submit_lock(req->ctx, needs_lock);
2566
2567 lockdep_assert_held(&req->ctx->uring_lock);
2568
2569 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2570 if (head) {
2571 if (!list_empty(&head->list)) {
2572 kbuf = list_last_entry(&head->list, struct io_buffer,
2573 list);
2574 list_del(&kbuf->list);
2575 } else {
2576 kbuf = head;
2577 idr_remove(&req->ctx->io_buffer_idr, bgid);
2578 }
2579 if (*len > kbuf->len)
2580 *len = kbuf->len;
2581 } else {
2582 kbuf = ERR_PTR(-ENOBUFS);
2583 }
2584
2585 io_ring_submit_unlock(req->ctx, needs_lock);
2586
2587 return kbuf;
2588}
2589
Jens Axboe4d954c22020-02-27 07:31:19 -07002590static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2591 bool needs_lock)
2592{
2593 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002594 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002595
2596 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002597 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002598 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2599 if (IS_ERR(kbuf))
2600 return kbuf;
2601 req->rw.addr = (u64) (unsigned long) kbuf;
2602 req->flags |= REQ_F_BUFFER_SELECTED;
2603 return u64_to_user_ptr(kbuf->addr);
2604}
2605
2606#ifdef CONFIG_COMPAT
2607static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2608 bool needs_lock)
2609{
2610 struct compat_iovec __user *uiov;
2611 compat_ssize_t clen;
2612 void __user *buf;
2613 ssize_t len;
2614
2615 uiov = u64_to_user_ptr(req->rw.addr);
2616 if (!access_ok(uiov, sizeof(*uiov)))
2617 return -EFAULT;
2618 if (__get_user(clen, &uiov->iov_len))
2619 return -EFAULT;
2620 if (clen < 0)
2621 return -EINVAL;
2622
2623 len = clen;
2624 buf = io_rw_buffer_select(req, &len, needs_lock);
2625 if (IS_ERR(buf))
2626 return PTR_ERR(buf);
2627 iov[0].iov_base = buf;
2628 iov[0].iov_len = (compat_size_t) len;
2629 return 0;
2630}
2631#endif
2632
2633static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2634 bool needs_lock)
2635{
2636 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2637 void __user *buf;
2638 ssize_t len;
2639
2640 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2641 return -EFAULT;
2642
2643 len = iov[0].iov_len;
2644 if (len < 0)
2645 return -EINVAL;
2646 buf = io_rw_buffer_select(req, &len, needs_lock);
2647 if (IS_ERR(buf))
2648 return PTR_ERR(buf);
2649 iov[0].iov_base = buf;
2650 iov[0].iov_len = len;
2651 return 0;
2652}
2653
2654static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2655 bool needs_lock)
2656{
Jens Axboedddb3e22020-06-04 11:27:01 -06002657 if (req->flags & REQ_F_BUFFER_SELECTED) {
2658 struct io_buffer *kbuf;
2659
2660 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2661 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2662 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002663 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002664 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002665 if (!req->rw.len)
2666 return 0;
2667 else if (req->rw.len > 1)
2668 return -EINVAL;
2669
2670#ifdef CONFIG_COMPAT
2671 if (req->ctx->compat)
2672 return io_compat_import(req, iov, needs_lock);
2673#endif
2674
2675 return __io_iov_buffer_select(req, iov, needs_lock);
2676}
2677
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002678static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002679 struct iovec **iovec, struct iov_iter *iter,
2680 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002681{
Jens Axboe9adbd452019-12-20 08:45:55 -07002682 void __user *buf = u64_to_user_ptr(req->rw.addr);
2683 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002684 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002685 u8 opcode;
2686
Jens Axboed625c6e2019-12-17 19:53:05 -07002687 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002688 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002689 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002690 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002691 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002692
Jens Axboebcda7ba2020-02-23 16:42:51 -07002693 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002694 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002695 return -EINVAL;
2696
Jens Axboe3a6820f2019-12-22 15:19:35 -07002697 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002698 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002699 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2700 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002701 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002702 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002703 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002704 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002705 }
2706
Jens Axboe3a6820f2019-12-22 15:19:35 -07002707 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2708 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002709 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002710 }
2711
Jens Axboef67676d2019-12-02 11:03:47 -07002712 if (req->io) {
2713 struct io_async_rw *iorw = &req->io->rw;
2714
2715 *iovec = iorw->iov;
2716 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2717 if (iorw->iov == iorw->fast_iov)
2718 *iovec = NULL;
2719 return iorw->size;
2720 }
2721
Jens Axboe4d954c22020-02-27 07:31:19 -07002722 if (req->flags & REQ_F_BUFFER_SELECT) {
2723 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002724 if (!ret) {
2725 ret = (*iovec)->iov_len;
2726 iov_iter_init(iter, rw, *iovec, 1, ret);
2727 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002728 *iovec = NULL;
2729 return ret;
2730 }
2731
Jens Axboe2b188cc2019-01-07 10:46:33 -07002732#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002733 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002734 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2735 iovec, iter);
2736#endif
2737
2738 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2739}
2740
Jens Axboe32960612019-09-23 11:05:34 -06002741/*
2742 * For files that don't have ->read_iter() and ->write_iter(), handle them
2743 * by looping over ->read() or ->write() manually.
2744 */
2745static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2746 struct iov_iter *iter)
2747{
2748 ssize_t ret = 0;
2749
2750 /*
2751 * Don't support polled IO through this interface, and we can't
2752 * support non-blocking either. For the latter, this just causes
2753 * the kiocb to be handled from an async context.
2754 */
2755 if (kiocb->ki_flags & IOCB_HIPRI)
2756 return -EOPNOTSUPP;
2757 if (kiocb->ki_flags & IOCB_NOWAIT)
2758 return -EAGAIN;
2759
2760 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002761 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002762 ssize_t nr;
2763
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002764 if (!iov_iter_is_bvec(iter)) {
2765 iovec = iov_iter_iovec(iter);
2766 } else {
2767 /* fixed buffers import bvec */
2768 iovec.iov_base = kmap(iter->bvec->bv_page)
2769 + iter->iov_offset;
2770 iovec.iov_len = min(iter->count,
2771 iter->bvec->bv_len - iter->iov_offset);
2772 }
2773
Jens Axboe32960612019-09-23 11:05:34 -06002774 if (rw == READ) {
2775 nr = file->f_op->read(file, iovec.iov_base,
2776 iovec.iov_len, &kiocb->ki_pos);
2777 } else {
2778 nr = file->f_op->write(file, iovec.iov_base,
2779 iovec.iov_len, &kiocb->ki_pos);
2780 }
2781
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002782 if (iov_iter_is_bvec(iter))
2783 kunmap(iter->bvec->bv_page);
2784
Jens Axboe32960612019-09-23 11:05:34 -06002785 if (nr < 0) {
2786 if (!ret)
2787 ret = nr;
2788 break;
2789 }
2790 ret += nr;
2791 if (nr != iovec.iov_len)
2792 break;
2793 iov_iter_advance(iter, nr);
2794 }
2795
2796 return ret;
2797}
2798
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002799static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002800 struct iovec *iovec, struct iovec *fast_iov,
2801 struct iov_iter *iter)
2802{
2803 req->io->rw.nr_segs = iter->nr_segs;
2804 req->io->rw.size = io_size;
2805 req->io->rw.iov = iovec;
2806 if (!req->io->rw.iov) {
2807 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002808 if (req->io->rw.iov != fast_iov)
2809 memcpy(req->io->rw.iov, fast_iov,
2810 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002811 } else {
2812 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002813 }
2814}
2815
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002816static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2817{
2818 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2819 return req->io == NULL;
2820}
2821
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002822static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002823{
Jens Axboed3656342019-12-18 09:50:26 -07002824 if (!io_op_defs[req->opcode].async_ctx)
2825 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002826
2827 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002828}
2829
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002830static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2831 struct iovec *iovec, struct iovec *fast_iov,
2832 struct iov_iter *iter)
2833{
Jens Axboe980ad262020-01-24 23:08:54 -07002834 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002835 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002836 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002837 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002838 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002839
Jens Axboe5d204bc2020-01-31 12:06:52 -07002840 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2841 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002842 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002843}
2844
Jens Axboe3529d8c2019-12-19 18:24:38 -07002845static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2846 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002847{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002848 struct io_async_ctx *io;
2849 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002850 ssize_t ret;
2851
Jens Axboe3529d8c2019-12-19 18:24:38 -07002852 ret = io_prep_rw(req, sqe, force_nonblock);
2853 if (ret)
2854 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002855
Jens Axboe3529d8c2019-12-19 18:24:38 -07002856 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2857 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002858
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002859 /* either don't need iovec imported or already have it */
2860 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002861 return 0;
2862
2863 io = req->io;
2864 io->rw.iov = io->rw.fast_iov;
2865 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002866 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002867 req->io = io;
2868 if (ret < 0)
2869 return ret;
2870
2871 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2872 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002873}
2874
Jens Axboebcf5a062020-05-22 09:24:42 -06002875static void io_async_buf_cancel(struct callback_head *cb)
2876{
2877 struct io_async_rw *rw;
2878 struct io_kiocb *req;
2879
2880 rw = container_of(cb, struct io_async_rw, task_work);
2881 req = rw->wpq.wait.private;
Jens Axboec40f6372020-06-25 15:39:59 -06002882 __io_req_task_cancel(req, -ECANCELED);
Jens Axboebcf5a062020-05-22 09:24:42 -06002883}
2884
2885static void io_async_buf_retry(struct callback_head *cb)
2886{
2887 struct io_async_rw *rw;
Jens Axboebcf5a062020-05-22 09:24:42 -06002888 struct io_kiocb *req;
2889
2890 rw = container_of(cb, struct io_async_rw, task_work);
2891 req = rw->wpq.wait.private;
Jens Axboebcf5a062020-05-22 09:24:42 -06002892
Jens Axboec40f6372020-06-25 15:39:59 -06002893 __io_req_task_submit(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06002894}
2895
2896static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2897 int sync, void *arg)
2898{
2899 struct wait_page_queue *wpq;
2900 struct io_kiocb *req = wait->private;
2901 struct io_async_rw *rw = &req->io->rw;
2902 struct wait_page_key *key = arg;
2903 struct task_struct *tsk;
2904 int ret;
2905
2906 wpq = container_of(wait, struct wait_page_queue, wait);
2907
2908 ret = wake_page_match(wpq, key);
2909 if (ret != 1)
2910 return ret;
2911
2912 list_del_init(&wait->entry);
2913
2914 init_task_work(&rw->task_work, io_async_buf_retry);
2915 /* submit ref gets dropped, acquire a new one */
2916 refcount_inc(&req->refs);
2917 tsk = req->task;
2918 ret = task_work_add(tsk, &rw->task_work, true);
2919 if (unlikely(ret)) {
2920 /* queue just for cancelation */
2921 init_task_work(&rw->task_work, io_async_buf_cancel);
2922 tsk = io_wq_get_task(req->ctx->io_wq);
2923 task_work_add(tsk, &rw->task_work, true);
2924 }
2925 wake_up_process(tsk);
2926 return 1;
2927}
2928
2929static bool io_rw_should_retry(struct io_kiocb *req)
2930{
2931 struct kiocb *kiocb = &req->rw.kiocb;
2932 int ret;
2933
2934 /* never retry for NOWAIT, we just complete with -EAGAIN */
2935 if (req->flags & REQ_F_NOWAIT)
2936 return false;
2937
2938 /* already tried, or we're doing O_DIRECT */
2939 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
2940 return false;
2941 /*
2942 * just use poll if we can, and don't attempt if the fs doesn't
2943 * support callback based unlocks
2944 */
2945 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
2946 return false;
2947
2948 /*
2949 * If request type doesn't require req->io to defer in general,
2950 * we need to allocate it here
2951 */
2952 if (!req->io && __io_alloc_async_ctx(req))
2953 return false;
2954
2955 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
2956 io_async_buf_func, req);
2957 if (!ret) {
2958 io_get_req_task(req);
2959 return true;
2960 }
2961
2962 return false;
2963}
2964
2965static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
2966{
2967 if (req->file->f_op->read_iter)
2968 return call_read_iter(req->file, &req->rw.kiocb, iter);
2969 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
2970}
2971
Jens Axboea1d7c392020-06-22 11:09:46 -06002972static int io_read(struct io_kiocb *req, bool force_nonblock,
2973 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002974{
2975 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002976 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002977 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002978 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002979 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002980
Jens Axboebcda7ba2020-02-23 16:42:51 -07002981 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002982 if (ret < 0)
2983 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002984
Jens Axboefd6c2e42019-12-18 12:19:41 -07002985 /* Ensure we clear previously set non-block flag */
2986 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002987 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002988
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002989 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002990 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002991 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002992 req->result = io_size;
2993
Pavel Begunkov24c74672020-06-21 13:09:51 +03002994 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06002995 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002996 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002997
Jens Axboe31b51512019-01-18 22:56:34 -07002998 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002999 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003000 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003001 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06003002 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003003
Jens Axboebcf5a062020-05-22 09:24:42 -06003004 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003005
Jens Axboe9d93a3f2019-05-15 13:53:07 -06003006 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06003007 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003008 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003009 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003010 iter.count = iov_count;
3011 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003012copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003013 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003014 inline_vecs, &iter);
3015 if (ret)
3016 goto out_free;
Jens Axboebcf5a062020-05-22 09:24:42 -06003017 /* if we can retry, do so with the callbacks armed */
3018 if (io_rw_should_retry(req)) {
3019 ret2 = io_iter_do_read(req, &iter);
3020 if (ret2 == -EIOCBQUEUED) {
3021 goto out_free;
3022 } else if (ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003023 kiocb_done(kiocb, ret2, cs);
Jens Axboebcf5a062020-05-22 09:24:42 -06003024 goto out_free;
3025 }
3026 }
3027 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07003028 return -EAGAIN;
3029 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003030 }
Jens Axboef67676d2019-12-02 11:03:47 -07003031out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003032 if (!(req->flags & REQ_F_NEED_CLEANUP))
3033 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003034 return ret;
3035}
3036
Jens Axboe3529d8c2019-12-19 18:24:38 -07003037static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3038 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003039{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003040 struct io_async_ctx *io;
3041 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07003042 ssize_t ret;
3043
Jens Axboe3529d8c2019-12-19 18:24:38 -07003044 ret = io_prep_rw(req, sqe, force_nonblock);
3045 if (ret)
3046 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003047
Jens Axboe3529d8c2019-12-19 18:24:38 -07003048 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3049 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003050
Jens Axboe4ed734b2020-03-20 11:23:41 -06003051 req->fsize = rlimit(RLIMIT_FSIZE);
3052
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003053 /* either don't need iovec imported or already have it */
3054 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003055 return 0;
3056
3057 io = req->io;
3058 io->rw.iov = io->rw.fast_iov;
3059 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003060 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003061 req->io = io;
3062 if (ret < 0)
3063 return ret;
3064
3065 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
3066 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003067}
3068
Jens Axboea1d7c392020-06-22 11:09:46 -06003069static int io_write(struct io_kiocb *req, bool force_nonblock,
3070 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003071{
3072 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003073 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003074 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003075 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003076 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003077
Jens Axboebcda7ba2020-02-23 16:42:51 -07003078 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003079 if (ret < 0)
3080 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003081
Jens Axboefd6c2e42019-12-18 12:19:41 -07003082 /* Ensure we clear previously set non-block flag */
3083 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003084 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003085
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08003086 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003087 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03003088 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07003089 req->result = io_size;
3090
Pavel Begunkov24c74672020-06-21 13:09:51 +03003091 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003092 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003093 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003094
Jens Axboe10d59342019-12-09 20:16:22 -07003095 /* file path doesn't support NOWAIT for non-direct_IO */
3096 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3097 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003098 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003099
Jens Axboe31b51512019-01-18 22:56:34 -07003100 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003101 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003102 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003103 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003104 ssize_t ret2;
3105
Jens Axboe2b188cc2019-01-07 10:46:33 -07003106 /*
3107 * Open-code file_start_write here to grab freeze protection,
3108 * which will be released by another thread in
3109 * io_complete_rw(). Fool lockdep by telling it the lock got
3110 * released so that it doesn't complain about the held lock when
3111 * we return to userspace.
3112 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003113 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003114 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003115 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003116 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003117 SB_FREEZE_WRITE);
3118 }
3119 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003120
Jens Axboe4ed734b2020-03-20 11:23:41 -06003121 if (!force_nonblock)
3122 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3123
Jens Axboe9adbd452019-12-20 08:45:55 -07003124 if (req->file->f_op->write_iter)
3125 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003126 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003127 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003128
3129 if (!force_nonblock)
3130 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3131
Jens Axboefaac9962020-02-07 15:45:22 -07003132 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003133 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003134 * retry them without IOCB_NOWAIT.
3135 */
3136 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3137 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003138 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003139 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003140 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003141 iter.count = iov_count;
3142 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003143copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003144 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003145 inline_vecs, &iter);
3146 if (ret)
3147 goto out_free;
3148 return -EAGAIN;
3149 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003150 }
Jens Axboe31b51512019-01-18 22:56:34 -07003151out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003152 if (!(req->flags & REQ_F_NEED_CLEANUP))
3153 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003154 return ret;
3155}
3156
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003157static int __io_splice_prep(struct io_kiocb *req,
3158 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003159{
3160 struct io_splice* sp = &req->splice;
3161 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3162 int ret;
3163
3164 if (req->flags & REQ_F_NEED_CLEANUP)
3165 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003166 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3167 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003168
3169 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003170 sp->len = READ_ONCE(sqe->len);
3171 sp->flags = READ_ONCE(sqe->splice_flags);
3172
3173 if (unlikely(sp->flags & ~valid_flags))
3174 return -EINVAL;
3175
3176 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3177 (sp->flags & SPLICE_F_FD_IN_FIXED));
3178 if (ret)
3179 return ret;
3180 req->flags |= REQ_F_NEED_CLEANUP;
3181
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003182 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3183 /*
3184 * Splice operation will be punted aync, and here need to
3185 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3186 */
3187 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003188 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003189 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003190
3191 return 0;
3192}
3193
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003194static int io_tee_prep(struct io_kiocb *req,
3195 const struct io_uring_sqe *sqe)
3196{
3197 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3198 return -EINVAL;
3199 return __io_splice_prep(req, sqe);
3200}
3201
3202static int io_tee(struct io_kiocb *req, bool force_nonblock)
3203{
3204 struct io_splice *sp = &req->splice;
3205 struct file *in = sp->file_in;
3206 struct file *out = sp->file_out;
3207 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3208 long ret = 0;
3209
3210 if (force_nonblock)
3211 return -EAGAIN;
3212 if (sp->len)
3213 ret = do_tee(in, out, sp->len, flags);
3214
3215 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3216 req->flags &= ~REQ_F_NEED_CLEANUP;
3217
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003218 if (ret != sp->len)
3219 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003220 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003221 return 0;
3222}
3223
3224static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3225{
3226 struct io_splice* sp = &req->splice;
3227
3228 sp->off_in = READ_ONCE(sqe->splice_off_in);
3229 sp->off_out = READ_ONCE(sqe->off);
3230 return __io_splice_prep(req, sqe);
3231}
3232
Pavel Begunkov014db002020-03-03 21:33:12 +03003233static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003234{
3235 struct io_splice *sp = &req->splice;
3236 struct file *in = sp->file_in;
3237 struct file *out = sp->file_out;
3238 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3239 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003240 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003241
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003242 if (force_nonblock)
3243 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003244
3245 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3246 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003247
Jens Axboe948a7742020-05-17 14:21:38 -06003248 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003249 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003250
3251 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3252 req->flags &= ~REQ_F_NEED_CLEANUP;
3253
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003254 if (ret != sp->len)
3255 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003256 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003257 return 0;
3258}
3259
Jens Axboe2b188cc2019-01-07 10:46:33 -07003260/*
3261 * IORING_OP_NOP just posts a completion event, nothing else.
3262 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003263static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003264{
3265 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266
Jens Axboedef596e2019-01-09 08:59:42 -07003267 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3268 return -EINVAL;
3269
Jens Axboe229a7b62020-06-22 10:13:11 -06003270 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003271 return 0;
3272}
3273
Jens Axboe3529d8c2019-12-19 18:24:38 -07003274static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003275{
Jens Axboe6b063142019-01-10 22:13:58 -07003276 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003277
Jens Axboe09bb8392019-03-13 12:39:28 -06003278 if (!req->file)
3279 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003280
Jens Axboe6b063142019-01-10 22:13:58 -07003281 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003282 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003283 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003284 return -EINVAL;
3285
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003286 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3287 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3288 return -EINVAL;
3289
3290 req->sync.off = READ_ONCE(sqe->off);
3291 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003292 return 0;
3293}
3294
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003295static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003296{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003297 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003298 int ret;
3299
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003300 /* fsync always requires a blocking context */
3301 if (force_nonblock)
3302 return -EAGAIN;
3303
Jens Axboe9adbd452019-12-20 08:45:55 -07003304 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003305 end > 0 ? end : LLONG_MAX,
3306 req->sync.flags & IORING_FSYNC_DATASYNC);
3307 if (ret < 0)
3308 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003309 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003310 return 0;
3311}
3312
Jens Axboed63d1b52019-12-10 10:38:56 -07003313static int io_fallocate_prep(struct io_kiocb *req,
3314 const struct io_uring_sqe *sqe)
3315{
3316 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3317 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003318 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3319 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003320
3321 req->sync.off = READ_ONCE(sqe->off);
3322 req->sync.len = READ_ONCE(sqe->addr);
3323 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003324 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07003325 return 0;
3326}
3327
Pavel Begunkov014db002020-03-03 21:33:12 +03003328static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003329{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003330 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003331
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003332 /* fallocate always requiring blocking context */
3333 if (force_nonblock)
3334 return -EAGAIN;
3335
3336 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3337 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3338 req->sync.len);
3339 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3340 if (ret < 0)
3341 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003342 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003343 return 0;
3344}
3345
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003346static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003347{
Jens Axboef8748882020-01-08 17:47:02 -07003348 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003349 int ret;
3350
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003351 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003352 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003353 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003354 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003355 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003356 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003357
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003358 /* open.how should be already initialised */
3359 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003360 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003361
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003362 req->open.dfd = READ_ONCE(sqe->fd);
3363 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003364 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003365 if (IS_ERR(req->open.filename)) {
3366 ret = PTR_ERR(req->open.filename);
3367 req->open.filename = NULL;
3368 return ret;
3369 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003370 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003371 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003372 return 0;
3373}
3374
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003375static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3376{
3377 u64 flags, mode;
3378
3379 if (req->flags & REQ_F_NEED_CLEANUP)
3380 return 0;
3381 mode = READ_ONCE(sqe->len);
3382 flags = READ_ONCE(sqe->open_flags);
3383 req->open.how = build_open_how(flags, mode);
3384 return __io_openat_prep(req, sqe);
3385}
3386
Jens Axboecebdb982020-01-08 17:59:24 -07003387static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3388{
3389 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003390 size_t len;
3391 int ret;
3392
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003393 if (req->flags & REQ_F_NEED_CLEANUP)
3394 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003395 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3396 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003397 if (len < OPEN_HOW_SIZE_VER0)
3398 return -EINVAL;
3399
3400 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3401 len);
3402 if (ret)
3403 return ret;
3404
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003405 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003406}
3407
Pavel Begunkov014db002020-03-03 21:33:12 +03003408static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003409{
3410 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003411 struct file *file;
3412 int ret;
3413
Jens Axboef86cd202020-01-29 13:46:44 -07003414 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003415 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003416
Jens Axboecebdb982020-01-08 17:59:24 -07003417 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003418 if (ret)
3419 goto err;
3420
Jens Axboe4022e7a2020-03-19 19:23:18 -06003421 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003422 if (ret < 0)
3423 goto err;
3424
3425 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3426 if (IS_ERR(file)) {
3427 put_unused_fd(ret);
3428 ret = PTR_ERR(file);
3429 } else {
3430 fsnotify_open(file);
3431 fd_install(ret, file);
3432 }
3433err:
3434 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003435 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003436 if (ret < 0)
3437 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003438 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003439 return 0;
3440}
3441
Pavel Begunkov014db002020-03-03 21:33:12 +03003442static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003443{
Pavel Begunkov014db002020-03-03 21:33:12 +03003444 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003445}
3446
Jens Axboe067524e2020-03-02 16:32:28 -07003447static int io_remove_buffers_prep(struct io_kiocb *req,
3448 const struct io_uring_sqe *sqe)
3449{
3450 struct io_provide_buf *p = &req->pbuf;
3451 u64 tmp;
3452
3453 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3454 return -EINVAL;
3455
3456 tmp = READ_ONCE(sqe->fd);
3457 if (!tmp || tmp > USHRT_MAX)
3458 return -EINVAL;
3459
3460 memset(p, 0, sizeof(*p));
3461 p->nbufs = tmp;
3462 p->bgid = READ_ONCE(sqe->buf_group);
3463 return 0;
3464}
3465
3466static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3467 int bgid, unsigned nbufs)
3468{
3469 unsigned i = 0;
3470
3471 /* shouldn't happen */
3472 if (!nbufs)
3473 return 0;
3474
3475 /* the head kbuf is the list itself */
3476 while (!list_empty(&buf->list)) {
3477 struct io_buffer *nxt;
3478
3479 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3480 list_del(&nxt->list);
3481 kfree(nxt);
3482 if (++i == nbufs)
3483 return i;
3484 }
3485 i++;
3486 kfree(buf);
3487 idr_remove(&ctx->io_buffer_idr, bgid);
3488
3489 return i;
3490}
3491
Jens Axboe229a7b62020-06-22 10:13:11 -06003492static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3493 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003494{
3495 struct io_provide_buf *p = &req->pbuf;
3496 struct io_ring_ctx *ctx = req->ctx;
3497 struct io_buffer *head;
3498 int ret = 0;
3499
3500 io_ring_submit_lock(ctx, !force_nonblock);
3501
3502 lockdep_assert_held(&ctx->uring_lock);
3503
3504 ret = -ENOENT;
3505 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3506 if (head)
3507 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3508
3509 io_ring_submit_lock(ctx, !force_nonblock);
3510 if (ret < 0)
3511 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003512 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003513 return 0;
3514}
3515
Jens Axboeddf0322d2020-02-23 16:41:33 -07003516static int io_provide_buffers_prep(struct io_kiocb *req,
3517 const struct io_uring_sqe *sqe)
3518{
3519 struct io_provide_buf *p = &req->pbuf;
3520 u64 tmp;
3521
3522 if (sqe->ioprio || sqe->rw_flags)
3523 return -EINVAL;
3524
3525 tmp = READ_ONCE(sqe->fd);
3526 if (!tmp || tmp > USHRT_MAX)
3527 return -E2BIG;
3528 p->nbufs = tmp;
3529 p->addr = READ_ONCE(sqe->addr);
3530 p->len = READ_ONCE(sqe->len);
3531
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003532 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003533 return -EFAULT;
3534
3535 p->bgid = READ_ONCE(sqe->buf_group);
3536 tmp = READ_ONCE(sqe->off);
3537 if (tmp > USHRT_MAX)
3538 return -E2BIG;
3539 p->bid = tmp;
3540 return 0;
3541}
3542
3543static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3544{
3545 struct io_buffer *buf;
3546 u64 addr = pbuf->addr;
3547 int i, bid = pbuf->bid;
3548
3549 for (i = 0; i < pbuf->nbufs; i++) {
3550 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3551 if (!buf)
3552 break;
3553
3554 buf->addr = addr;
3555 buf->len = pbuf->len;
3556 buf->bid = bid;
3557 addr += pbuf->len;
3558 bid++;
3559 if (!*head) {
3560 INIT_LIST_HEAD(&buf->list);
3561 *head = buf;
3562 } else {
3563 list_add_tail(&buf->list, &(*head)->list);
3564 }
3565 }
3566
3567 return i ? i : -ENOMEM;
3568}
3569
Jens Axboe229a7b62020-06-22 10:13:11 -06003570static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3571 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003572{
3573 struct io_provide_buf *p = &req->pbuf;
3574 struct io_ring_ctx *ctx = req->ctx;
3575 struct io_buffer *head, *list;
3576 int ret = 0;
3577
3578 io_ring_submit_lock(ctx, !force_nonblock);
3579
3580 lockdep_assert_held(&ctx->uring_lock);
3581
3582 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3583
3584 ret = io_add_buffers(p, &head);
3585 if (ret < 0)
3586 goto out;
3587
3588 if (!list) {
3589 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3590 GFP_KERNEL);
3591 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003592 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003593 goto out;
3594 }
3595 }
3596out:
3597 io_ring_submit_unlock(ctx, !force_nonblock);
3598 if (ret < 0)
3599 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003600 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003601 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003602}
3603
Jens Axboe3e4827b2020-01-08 15:18:09 -07003604static int io_epoll_ctl_prep(struct io_kiocb *req,
3605 const struct io_uring_sqe *sqe)
3606{
3607#if defined(CONFIG_EPOLL)
3608 if (sqe->ioprio || sqe->buf_index)
3609 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003610 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3611 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003612
3613 req->epoll.epfd = READ_ONCE(sqe->fd);
3614 req->epoll.op = READ_ONCE(sqe->len);
3615 req->epoll.fd = READ_ONCE(sqe->off);
3616
3617 if (ep_op_has_event(req->epoll.op)) {
3618 struct epoll_event __user *ev;
3619
3620 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3621 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3622 return -EFAULT;
3623 }
3624
3625 return 0;
3626#else
3627 return -EOPNOTSUPP;
3628#endif
3629}
3630
Jens Axboe229a7b62020-06-22 10:13:11 -06003631static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3632 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003633{
3634#if defined(CONFIG_EPOLL)
3635 struct io_epoll *ie = &req->epoll;
3636 int ret;
3637
3638 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3639 if (force_nonblock && ret == -EAGAIN)
3640 return -EAGAIN;
3641
3642 if (ret < 0)
3643 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003644 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003645 return 0;
3646#else
3647 return -EOPNOTSUPP;
3648#endif
3649}
3650
Jens Axboec1ca7572019-12-25 22:18:28 -07003651static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3652{
3653#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3654 if (sqe->ioprio || sqe->buf_index || sqe->off)
3655 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003656 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3657 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003658
3659 req->madvise.addr = READ_ONCE(sqe->addr);
3660 req->madvise.len = READ_ONCE(sqe->len);
3661 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3662 return 0;
3663#else
3664 return -EOPNOTSUPP;
3665#endif
3666}
3667
Pavel Begunkov014db002020-03-03 21:33:12 +03003668static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003669{
3670#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3671 struct io_madvise *ma = &req->madvise;
3672 int ret;
3673
3674 if (force_nonblock)
3675 return -EAGAIN;
3676
3677 ret = do_madvise(ma->addr, ma->len, ma->advice);
3678 if (ret < 0)
3679 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003680 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003681 return 0;
3682#else
3683 return -EOPNOTSUPP;
3684#endif
3685}
3686
Jens Axboe4840e412019-12-25 22:03:45 -07003687static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3688{
3689 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3690 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003691 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3692 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003693
3694 req->fadvise.offset = READ_ONCE(sqe->off);
3695 req->fadvise.len = READ_ONCE(sqe->len);
3696 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3697 return 0;
3698}
3699
Pavel Begunkov014db002020-03-03 21:33:12 +03003700static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003701{
3702 struct io_fadvise *fa = &req->fadvise;
3703 int ret;
3704
Jens Axboe3e694262020-02-01 09:22:49 -07003705 if (force_nonblock) {
3706 switch (fa->advice) {
3707 case POSIX_FADV_NORMAL:
3708 case POSIX_FADV_RANDOM:
3709 case POSIX_FADV_SEQUENTIAL:
3710 break;
3711 default:
3712 return -EAGAIN;
3713 }
3714 }
Jens Axboe4840e412019-12-25 22:03:45 -07003715
3716 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3717 if (ret < 0)
3718 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003719 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003720 return 0;
3721}
3722
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003723static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3724{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003725 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3726 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003727 if (sqe->ioprio || sqe->buf_index)
3728 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003729 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003730 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003731
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003732 req->statx.dfd = READ_ONCE(sqe->fd);
3733 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003734 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003735 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3736 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003737
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003738 return 0;
3739}
3740
Pavel Begunkov014db002020-03-03 21:33:12 +03003741static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003742{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003743 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003744 int ret;
3745
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003746 if (force_nonblock) {
3747 /* only need file table for an actual valid fd */
3748 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3749 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003750 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003751 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003752
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003753 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3754 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003755
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003756 if (ret < 0)
3757 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003758 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003759 return 0;
3760}
3761
Jens Axboeb5dba592019-12-11 14:02:38 -07003762static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3763{
3764 /*
3765 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003766 * leave the 'file' in an undeterminate state, and here need to modify
3767 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003768 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003769 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003770 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3771
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003772 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3773 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003774 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3775 sqe->rw_flags || sqe->buf_index)
3776 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003777 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003778 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003779
3780 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003781 if ((req->file && req->file->f_op == &io_uring_fops) ||
3782 req->close.fd == req->ctx->ring_fd)
3783 return -EBADF;
3784
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003785 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003786 return 0;
3787}
3788
Jens Axboe229a7b62020-06-22 10:13:11 -06003789static int io_close(struct io_kiocb *req, bool force_nonblock,
3790 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003791{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003792 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003793 int ret;
3794
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003795 /* might be already done during nonblock submission */
3796 if (!close->put_file) {
3797 ret = __close_fd_get_file(close->fd, &close->put_file);
3798 if (ret < 0)
3799 return (ret == -ENOENT) ? -EBADF : ret;
3800 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003801
3802 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003803 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003804 /* was never set, but play safe */
3805 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003806 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003807 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003808 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003809 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003810
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003811 /* No ->flush() or already async, safely close from here */
3812 ret = filp_close(close->put_file, req->work.files);
3813 if (ret < 0)
3814 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003815 fput(close->put_file);
3816 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003817 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003818 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003819}
3820
Jens Axboe3529d8c2019-12-19 18:24:38 -07003821static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003822{
3823 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003824
3825 if (!req->file)
3826 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003827
3828 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3829 return -EINVAL;
3830 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3831 return -EINVAL;
3832
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003833 req->sync.off = READ_ONCE(sqe->off);
3834 req->sync.len = READ_ONCE(sqe->len);
3835 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003836 return 0;
3837}
3838
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003839static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003840{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003841 int ret;
3842
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003843 /* sync_file_range always requires a blocking context */
3844 if (force_nonblock)
3845 return -EAGAIN;
3846
Jens Axboe9adbd452019-12-20 08:45:55 -07003847 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003848 req->sync.flags);
3849 if (ret < 0)
3850 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003851 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003852 return 0;
3853}
3854
YueHaibing469956e2020-03-04 15:53:52 +08003855#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003856static int io_setup_async_msg(struct io_kiocb *req,
3857 struct io_async_msghdr *kmsg)
3858{
3859 if (req->io)
3860 return -EAGAIN;
3861 if (io_alloc_async_ctx(req)) {
3862 if (kmsg->iov != kmsg->fast_iov)
3863 kfree(kmsg->iov);
3864 return -ENOMEM;
3865 }
3866 req->flags |= REQ_F_NEED_CLEANUP;
3867 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3868 return -EAGAIN;
3869}
3870
Jens Axboe3529d8c2019-12-19 18:24:38 -07003871static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003872{
Jens Axboee47293f2019-12-20 08:58:21 -07003873 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003874 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003875 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003876
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003877 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3878 return -EINVAL;
3879
Jens Axboee47293f2019-12-20 08:58:21 -07003880 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3881 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003882 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003883
Jens Axboed8768362020-02-27 14:17:49 -07003884#ifdef CONFIG_COMPAT
3885 if (req->ctx->compat)
3886 sr->msg_flags |= MSG_CMSG_COMPAT;
3887#endif
3888
Jens Axboefddafac2020-01-04 20:19:44 -07003889 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003890 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003891 /* iovec is already imported */
3892 if (req->flags & REQ_F_NEED_CLEANUP)
3893 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003894
Jens Axboed9688562019-12-09 19:35:20 -07003895 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003896 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003897 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003898 if (!ret)
3899 req->flags |= REQ_F_NEED_CLEANUP;
3900 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003901}
3902
Jens Axboe229a7b62020-06-22 10:13:11 -06003903static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3904 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07003905{
Jens Axboe0b416c32019-12-15 10:57:46 -07003906 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003907 struct socket *sock;
3908 int ret;
3909
Jens Axboe03b12302019-12-02 18:50:25 -07003910 sock = sock_from_file(req->file, &ret);
3911 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003912 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003913 unsigned flags;
3914
Jens Axboe03b12302019-12-02 18:50:25 -07003915 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003916 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003917 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003918 /* if iov is set, it's allocated already */
3919 if (!kmsg->iov)
3920 kmsg->iov = kmsg->fast_iov;
3921 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003922 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003923 struct io_sr_msg *sr = &req->sr_msg;
3924
Jens Axboe0b416c32019-12-15 10:57:46 -07003925 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003926 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003927
3928 io.msg.iov = io.msg.fast_iov;
3929 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3930 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003931 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003933 }
3934
Jens Axboee47293f2019-12-20 08:58:21 -07003935 flags = req->sr_msg.msg_flags;
3936 if (flags & MSG_DONTWAIT)
3937 req->flags |= REQ_F_NOWAIT;
3938 else if (force_nonblock)
3939 flags |= MSG_DONTWAIT;
3940
Jens Axboe0b416c32019-12-15 10:57:46 -07003941 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003942 if (force_nonblock && ret == -EAGAIN)
3943 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003944 if (ret == -ERESTARTSYS)
3945 ret = -EINTR;
3946 }
3947
Pavel Begunkov1e950812020-02-06 19:51:16 +03003948 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003949 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003950 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003951 if (ret < 0)
3952 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003953 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07003954 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003955}
3956
Jens Axboe229a7b62020-06-22 10:13:11 -06003957static int io_send(struct io_kiocb *req, bool force_nonblock,
3958 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07003959{
Jens Axboefddafac2020-01-04 20:19:44 -07003960 struct socket *sock;
3961 int ret;
3962
Jens Axboefddafac2020-01-04 20:19:44 -07003963 sock = sock_from_file(req->file, &ret);
3964 if (sock) {
3965 struct io_sr_msg *sr = &req->sr_msg;
3966 struct msghdr msg;
3967 struct iovec iov;
3968 unsigned flags;
3969
3970 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3971 &msg.msg_iter);
3972 if (ret)
3973 return ret;
3974
3975 msg.msg_name = NULL;
3976 msg.msg_control = NULL;
3977 msg.msg_controllen = 0;
3978 msg.msg_namelen = 0;
3979
3980 flags = req->sr_msg.msg_flags;
3981 if (flags & MSG_DONTWAIT)
3982 req->flags |= REQ_F_NOWAIT;
3983 else if (force_nonblock)
3984 flags |= MSG_DONTWAIT;
3985
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003986 msg.msg_flags = flags;
3987 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003988 if (force_nonblock && ret == -EAGAIN)
3989 return -EAGAIN;
3990 if (ret == -ERESTARTSYS)
3991 ret = -EINTR;
3992 }
3993
Jens Axboefddafac2020-01-04 20:19:44 -07003994 if (ret < 0)
3995 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003996 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07003997 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003998}
3999
Jens Axboe52de1fe2020-02-27 10:15:42 -07004000static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
4001{
4002 struct io_sr_msg *sr = &req->sr_msg;
4003 struct iovec __user *uiov;
4004 size_t iov_len;
4005 int ret;
4006
4007 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
4008 &uiov, &iov_len);
4009 if (ret)
4010 return ret;
4011
4012 if (req->flags & REQ_F_BUFFER_SELECT) {
4013 if (iov_len > 1)
4014 return -EINVAL;
4015 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
4016 return -EFAULT;
4017 sr->len = io->msg.iov[0].iov_len;
4018 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
4019 sr->len);
4020 io->msg.iov = NULL;
4021 } else {
4022 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4023 &io->msg.iov, &io->msg.msg.msg_iter);
4024 if (ret > 0)
4025 ret = 0;
4026 }
4027
4028 return ret;
4029}
4030
4031#ifdef CONFIG_COMPAT
4032static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
4033 struct io_async_ctx *io)
4034{
4035 struct compat_msghdr __user *msg_compat;
4036 struct io_sr_msg *sr = &req->sr_msg;
4037 struct compat_iovec __user *uiov;
4038 compat_uptr_t ptr;
4039 compat_size_t len;
4040 int ret;
4041
4042 msg_compat = (struct compat_msghdr __user *) sr->msg;
4043 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
4044 &ptr, &len);
4045 if (ret)
4046 return ret;
4047
4048 uiov = compat_ptr(ptr);
4049 if (req->flags & REQ_F_BUFFER_SELECT) {
4050 compat_ssize_t clen;
4051
4052 if (len > 1)
4053 return -EINVAL;
4054 if (!access_ok(uiov, sizeof(*uiov)))
4055 return -EFAULT;
4056 if (__get_user(clen, &uiov->iov_len))
4057 return -EFAULT;
4058 if (clen < 0)
4059 return -EINVAL;
4060 sr->len = io->msg.iov[0].iov_len;
4061 io->msg.iov = NULL;
4062 } else {
4063 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
4064 &io->msg.iov,
4065 &io->msg.msg.msg_iter);
4066 if (ret < 0)
4067 return ret;
4068 }
4069
4070 return 0;
4071}
Jens Axboe03b12302019-12-02 18:50:25 -07004072#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004073
4074static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
4075{
4076 io->msg.iov = io->msg.fast_iov;
4077
4078#ifdef CONFIG_COMPAT
4079 if (req->ctx->compat)
4080 return __io_compat_recvmsg_copy_hdr(req, io);
4081#endif
4082
4083 return __io_recvmsg_copy_hdr(req, io);
4084}
4085
Jens Axboebcda7ba2020-02-23 16:42:51 -07004086static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
4087 int *cflags, bool needs_lock)
4088{
4089 struct io_sr_msg *sr = &req->sr_msg;
4090 struct io_buffer *kbuf;
4091
4092 if (!(req->flags & REQ_F_BUFFER_SELECT))
4093 return NULL;
4094
4095 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4096 if (IS_ERR(kbuf))
4097 return kbuf;
4098
4099 sr->kbuf = kbuf;
4100 req->flags |= REQ_F_BUFFER_SELECTED;
4101
4102 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
4103 *cflags |= IORING_CQE_F_BUFFER;
4104 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004105}
4106
Jens Axboe3529d8c2019-12-19 18:24:38 -07004107static int io_recvmsg_prep(struct io_kiocb *req,
4108 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004109{
Jens Axboee47293f2019-12-20 08:58:21 -07004110 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004111 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004112 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004113
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004114 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4115 return -EINVAL;
4116
Jens Axboe3529d8c2019-12-19 18:24:38 -07004117 sr->msg_flags = READ_ONCE(sqe->msg_flags);
4118 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004119 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004120 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004121
Jens Axboed8768362020-02-27 14:17:49 -07004122#ifdef CONFIG_COMPAT
4123 if (req->ctx->compat)
4124 sr->msg_flags |= MSG_CMSG_COMPAT;
4125#endif
4126
Jens Axboefddafac2020-01-04 20:19:44 -07004127 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004128 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004129 /* iovec is already imported */
4130 if (req->flags & REQ_F_NEED_CLEANUP)
4131 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004132
Jens Axboe52de1fe2020-02-27 10:15:42 -07004133 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004134 if (!ret)
4135 req->flags |= REQ_F_NEED_CLEANUP;
4136 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004137}
4138
Jens Axboe229a7b62020-06-22 10:13:11 -06004139static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4140 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004141{
Jens Axboe0b416c32019-12-15 10:57:46 -07004142 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004143 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004144 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004145
Jens Axboe0fa03c62019-04-19 13:34:07 -06004146 sock = sock_from_file(req->file, &ret);
4147 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07004148 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004149 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004150 unsigned flags;
4151
Jens Axboe03b12302019-12-02 18:50:25 -07004152 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07004153 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004154 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07004155 /* if iov is set, it's allocated already */
4156 if (!kmsg->iov)
4157 kmsg->iov = kmsg->fast_iov;
4158 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004159 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07004160 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004161 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004162
Jens Axboe52de1fe2020-02-27 10:15:42 -07004163 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07004164 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004166 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06004167
Jens Axboe52de1fe2020-02-27 10:15:42 -07004168 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4169 if (IS_ERR(kbuf)) {
4170 return PTR_ERR(kbuf);
4171 } else if (kbuf) {
4172 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4173 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4174 1, req->sr_msg.len);
4175 }
4176
Jens Axboee47293f2019-12-20 08:58:21 -07004177 flags = req->sr_msg.msg_flags;
4178 if (flags & MSG_DONTWAIT)
4179 req->flags |= REQ_F_NOWAIT;
4180 else if (force_nonblock)
4181 flags |= MSG_DONTWAIT;
4182
4183 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
4184 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004185 if (force_nonblock && ret == -EAGAIN)
4186 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07004187 if (ret == -ERESTARTSYS)
4188 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004189 }
4190
Pavel Begunkov1e950812020-02-06 19:51:16 +03004191 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004192 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004193 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004194 if (ret < 0)
4195 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004196 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004197 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004198}
4199
Jens Axboe229a7b62020-06-22 10:13:11 -06004200static int io_recv(struct io_kiocb *req, bool force_nonblock,
4201 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004202{
Jens Axboebcda7ba2020-02-23 16:42:51 -07004203 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07004204 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004205 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004206
Jens Axboefddafac2020-01-04 20:19:44 -07004207 sock = sock_from_file(req->file, &ret);
4208 if (sock) {
4209 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004210 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004211 struct msghdr msg;
4212 struct iovec iov;
4213 unsigned flags;
4214
Jens Axboebcda7ba2020-02-23 16:42:51 -07004215 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4216 if (IS_ERR(kbuf))
4217 return PTR_ERR(kbuf);
4218 else if (kbuf)
4219 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004220
Jens Axboebcda7ba2020-02-23 16:42:51 -07004221 ret = import_single_range(READ, buf, sr->len, &iov,
4222 &msg.msg_iter);
4223 if (ret) {
4224 kfree(kbuf);
4225 return ret;
4226 }
4227
4228 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004229 msg.msg_name = NULL;
4230 msg.msg_control = NULL;
4231 msg.msg_controllen = 0;
4232 msg.msg_namelen = 0;
4233 msg.msg_iocb = NULL;
4234 msg.msg_flags = 0;
4235
4236 flags = req->sr_msg.msg_flags;
4237 if (flags & MSG_DONTWAIT)
4238 req->flags |= REQ_F_NOWAIT;
4239 else if (force_nonblock)
4240 flags |= MSG_DONTWAIT;
4241
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004242 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07004243 if (force_nonblock && ret == -EAGAIN)
4244 return -EAGAIN;
4245 if (ret == -ERESTARTSYS)
4246 ret = -EINTR;
4247 }
4248
Jens Axboebcda7ba2020-02-23 16:42:51 -07004249 kfree(kbuf);
4250 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004251 if (ret < 0)
4252 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004253 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004254 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004255}
4256
Jens Axboe3529d8c2019-12-19 18:24:38 -07004257static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004258{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004259 struct io_accept *accept = &req->accept;
4260
Jens Axboe17f2fe32019-10-17 14:42:58 -06004261 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4262 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004263 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004264 return -EINVAL;
4265
Jens Axboed55e5f52019-12-11 16:12:15 -07004266 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4267 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004268 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004269 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004270 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004271}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004272
Jens Axboe229a7b62020-06-22 10:13:11 -06004273static int io_accept(struct io_kiocb *req, bool force_nonblock,
4274 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004275{
4276 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004277 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004278 int ret;
4279
Jiufei Xuee697dee2020-06-10 13:41:59 +08004280 if (req->file->f_flags & O_NONBLOCK)
4281 req->flags |= REQ_F_NOWAIT;
4282
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004283 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004284 accept->addr_len, accept->flags,
4285 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004286 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004287 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004288 if (ret < 0) {
4289 if (ret == -ERESTARTSYS)
4290 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004291 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004292 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004293 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004294 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004295}
4296
Jens Axboe3529d8c2019-12-19 18:24:38 -07004297static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004298{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004299 struct io_connect *conn = &req->connect;
4300 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004301
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004302 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4303 return -EINVAL;
4304 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4305 return -EINVAL;
4306
Jens Axboe3529d8c2019-12-19 18:24:38 -07004307 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4308 conn->addr_len = READ_ONCE(sqe->addr2);
4309
4310 if (!io)
4311 return 0;
4312
4313 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004314 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004315}
4316
Jens Axboe229a7b62020-06-22 10:13:11 -06004317static int io_connect(struct io_kiocb *req, bool force_nonblock,
4318 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004319{
Jens Axboef499a022019-12-02 16:28:46 -07004320 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004321 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004322 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004323
Jens Axboef499a022019-12-02 16:28:46 -07004324 if (req->io) {
4325 io = req->io;
4326 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004327 ret = move_addr_to_kernel(req->connect.addr,
4328 req->connect.addr_len,
4329 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004330 if (ret)
4331 goto out;
4332 io = &__io;
4333 }
4334
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004335 file_flags = force_nonblock ? O_NONBLOCK : 0;
4336
4337 ret = __sys_connect_file(req->file, &io->connect.address,
4338 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004339 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004340 if (req->io)
4341 return -EAGAIN;
4342 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004343 ret = -ENOMEM;
4344 goto out;
4345 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004346 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004347 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004348 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004349 if (ret == -ERESTARTSYS)
4350 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004351out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004352 if (ret < 0)
4353 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004354 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004355 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004356}
YueHaibing469956e2020-03-04 15:53:52 +08004357#else /* !CONFIG_NET */
4358static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4359{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004360 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004361}
4362
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004363static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4364 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004365{
YueHaibing469956e2020-03-04 15:53:52 +08004366 return -EOPNOTSUPP;
4367}
4368
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004369static int io_send(struct io_kiocb *req, bool force_nonblock,
4370 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004371{
4372 return -EOPNOTSUPP;
4373}
4374
4375static int io_recvmsg_prep(struct io_kiocb *req,
4376 const struct io_uring_sqe *sqe)
4377{
4378 return -EOPNOTSUPP;
4379}
4380
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004381static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4382 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004383{
4384 return -EOPNOTSUPP;
4385}
4386
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004387static int io_recv(struct io_kiocb *req, bool force_nonblock,
4388 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004389{
4390 return -EOPNOTSUPP;
4391}
4392
4393static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4394{
4395 return -EOPNOTSUPP;
4396}
4397
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004398static int io_accept(struct io_kiocb *req, bool force_nonblock,
4399 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004400{
4401 return -EOPNOTSUPP;
4402}
4403
4404static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4405{
4406 return -EOPNOTSUPP;
4407}
4408
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004409static int io_connect(struct io_kiocb *req, bool force_nonblock,
4410 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004411{
4412 return -EOPNOTSUPP;
4413}
4414#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004415
Jens Axboed7718a92020-02-14 22:23:12 -07004416struct io_poll_table {
4417 struct poll_table_struct pt;
4418 struct io_kiocb *req;
4419 int error;
4420};
4421
Jens Axboed7718a92020-02-14 22:23:12 -07004422static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4423 __poll_t mask, task_work_func_t func)
4424{
4425 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004426 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004427
4428 /* for instances that support it check for an event match first: */
4429 if (mask && !(mask & poll->events))
4430 return 0;
4431
4432 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4433
4434 list_del_init(&poll->wait.entry);
4435
4436 tsk = req->task;
4437 req->result = mask;
4438 init_task_work(&req->task_work, func);
4439 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004440 * If this fails, then the task is exiting. When a task exits, the
4441 * work gets canceled, so just cancel this request as well instead
4442 * of executing it. We can't safely execute it anyway, as we may not
4443 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004444 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004445 ret = task_work_add(tsk, &req->task_work, true);
4446 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004447 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004448 tsk = io_wq_get_task(req->ctx->io_wq);
4449 task_work_add(tsk, &req->task_work, true);
4450 }
Jens Axboed7718a92020-02-14 22:23:12 -07004451 wake_up_process(tsk);
4452 return 1;
4453}
4454
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004455static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4456 __acquires(&req->ctx->completion_lock)
4457{
4458 struct io_ring_ctx *ctx = req->ctx;
4459
4460 if (!req->result && !READ_ONCE(poll->canceled)) {
4461 struct poll_table_struct pt = { ._key = poll->events };
4462
4463 req->result = vfs_poll(req->file, &pt) & poll->events;
4464 }
4465
4466 spin_lock_irq(&ctx->completion_lock);
4467 if (!req->result && !READ_ONCE(poll->canceled)) {
4468 add_wait_queue(poll->head, &poll->wait);
4469 return true;
4470 }
4471
4472 return false;
4473}
4474
Jens Axboe18bceab2020-05-15 11:56:54 -06004475static void io_poll_remove_double(struct io_kiocb *req)
4476{
4477 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4478
4479 lockdep_assert_held(&req->ctx->completion_lock);
4480
4481 if (poll && poll->head) {
4482 struct wait_queue_head *head = poll->head;
4483
4484 spin_lock(&head->lock);
4485 list_del_init(&poll->wait.entry);
4486 if (poll->wait.private)
4487 refcount_dec(&req->refs);
4488 poll->head = NULL;
4489 spin_unlock(&head->lock);
4490 }
4491}
4492
4493static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4494{
4495 struct io_ring_ctx *ctx = req->ctx;
4496
4497 io_poll_remove_double(req);
4498 req->poll.done = true;
4499 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4500 io_commit_cqring(ctx);
4501}
4502
4503static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4504{
4505 struct io_ring_ctx *ctx = req->ctx;
4506
4507 if (io_poll_rewait(req, &req->poll)) {
4508 spin_unlock_irq(&ctx->completion_lock);
4509 return;
4510 }
4511
4512 hash_del(&req->hash_node);
4513 io_poll_complete(req, req->result, 0);
4514 req->flags |= REQ_F_COMP_LOCKED;
4515 io_put_req_find_next(req, nxt);
4516 spin_unlock_irq(&ctx->completion_lock);
4517
4518 io_cqring_ev_posted(ctx);
4519}
4520
4521static void io_poll_task_func(struct callback_head *cb)
4522{
4523 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4524 struct io_kiocb *nxt = NULL;
4525
4526 io_poll_task_handler(req, &nxt);
4527 if (nxt) {
4528 struct io_ring_ctx *ctx = nxt->ctx;
4529
4530 mutex_lock(&ctx->uring_lock);
Jens Axboef13fad72020-06-22 09:34:30 -06004531 __io_queue_sqe(nxt, NULL, NULL);
Jens Axboe18bceab2020-05-15 11:56:54 -06004532 mutex_unlock(&ctx->uring_lock);
4533 }
4534}
4535
4536static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4537 int sync, void *key)
4538{
4539 struct io_kiocb *req = wait->private;
4540 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4541 __poll_t mask = key_to_poll(key);
4542
4543 /* for instances that support it check for an event match first: */
4544 if (mask && !(mask & poll->events))
4545 return 0;
4546
4547 if (req->poll.head) {
4548 bool done;
4549
4550 spin_lock(&req->poll.head->lock);
4551 done = list_empty(&req->poll.wait.entry);
4552 if (!done)
4553 list_del_init(&req->poll.wait.entry);
4554 spin_unlock(&req->poll.head->lock);
4555 if (!done)
4556 __io_async_wake(req, poll, mask, io_poll_task_func);
4557 }
4558 refcount_dec(&req->refs);
4559 return 1;
4560}
4561
4562static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4563 wait_queue_func_t wake_func)
4564{
4565 poll->head = NULL;
4566 poll->done = false;
4567 poll->canceled = false;
4568 poll->events = events;
4569 INIT_LIST_HEAD(&poll->wait.entry);
4570 init_waitqueue_func_entry(&poll->wait, wake_func);
4571}
4572
4573static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4574 struct wait_queue_head *head)
4575{
4576 struct io_kiocb *req = pt->req;
4577
4578 /*
4579 * If poll->head is already set, it's because the file being polled
4580 * uses multiple waitqueues for poll handling (eg one for read, one
4581 * for write). Setup a separate io_poll_iocb if this happens.
4582 */
4583 if (unlikely(poll->head)) {
4584 /* already have a 2nd entry, fail a third attempt */
4585 if (req->io) {
4586 pt->error = -EINVAL;
4587 return;
4588 }
4589 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4590 if (!poll) {
4591 pt->error = -ENOMEM;
4592 return;
4593 }
4594 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4595 refcount_inc(&req->refs);
4596 poll->wait.private = req;
4597 req->io = (void *) poll;
4598 }
4599
4600 pt->error = 0;
4601 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004602
4603 if (poll->events & EPOLLEXCLUSIVE)
4604 add_wait_queue_exclusive(head, &poll->wait);
4605 else
4606 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004607}
4608
4609static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4610 struct poll_table_struct *p)
4611{
4612 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4613
4614 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4615}
4616
Jens Axboed7718a92020-02-14 22:23:12 -07004617static void io_async_task_func(struct callback_head *cb)
4618{
4619 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4620 struct async_poll *apoll = req->apoll;
4621 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004622 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004623
4624 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4625
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004626 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004627 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004628 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004629 }
4630
Jens Axboe31067252020-05-17 17:43:31 -06004631 /* If req is still hashed, it cannot have been canceled. Don't check. */
4632 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004633 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004634 } else {
4635 canceled = READ_ONCE(apoll->poll.canceled);
4636 if (canceled) {
4637 io_cqring_fill_event(req, -ECANCELED);
4638 io_commit_cqring(ctx);
4639 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004640 }
4641
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004642 spin_unlock_irq(&ctx->completion_lock);
4643
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004644 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004645 if (req->flags & REQ_F_WORK_INITIALIZED)
4646 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004647 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004648
Jens Axboe31067252020-05-17 17:43:31 -06004649 if (!canceled) {
4650 __set_current_state(TASK_RUNNING);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004651 if (io_sq_thread_acquire_mm(ctx, req)) {
Jens Axboee1e16092020-06-22 09:17:17 -06004652 io_cqring_add_event(req, -EFAULT, 0);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004653 goto end_req;
4654 }
Jens Axboe31067252020-05-17 17:43:31 -06004655 mutex_lock(&ctx->uring_lock);
Jens Axboef13fad72020-06-22 09:34:30 -06004656 __io_queue_sqe(req, NULL, NULL);
Jens Axboe31067252020-05-17 17:43:31 -06004657 mutex_unlock(&ctx->uring_lock);
4658 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004659 io_cqring_ev_posted(ctx);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004660end_req:
Jens Axboe2bae0472020-04-13 11:16:34 -06004661 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004662 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004663 }
Jens Axboed7718a92020-02-14 22:23:12 -07004664}
4665
4666static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4667 void *key)
4668{
4669 struct io_kiocb *req = wait->private;
4670 struct io_poll_iocb *poll = &req->apoll->poll;
4671
4672 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4673 key_to_poll(key));
4674
4675 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4676}
4677
4678static void io_poll_req_insert(struct io_kiocb *req)
4679{
4680 struct io_ring_ctx *ctx = req->ctx;
4681 struct hlist_head *list;
4682
4683 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4684 hlist_add_head(&req->hash_node, list);
4685}
4686
4687static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4688 struct io_poll_iocb *poll,
4689 struct io_poll_table *ipt, __poll_t mask,
4690 wait_queue_func_t wake_func)
4691 __acquires(&ctx->completion_lock)
4692{
4693 struct io_ring_ctx *ctx = req->ctx;
4694 bool cancel = false;
4695
Jens Axboe18bceab2020-05-15 11:56:54 -06004696 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004697 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004698 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004699
4700 ipt->pt._key = mask;
4701 ipt->req = req;
4702 ipt->error = -EINVAL;
4703
Jens Axboed7718a92020-02-14 22:23:12 -07004704 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4705
4706 spin_lock_irq(&ctx->completion_lock);
4707 if (likely(poll->head)) {
4708 spin_lock(&poll->head->lock);
4709 if (unlikely(list_empty(&poll->wait.entry))) {
4710 if (ipt->error)
4711 cancel = true;
4712 ipt->error = 0;
4713 mask = 0;
4714 }
4715 if (mask || ipt->error)
4716 list_del_init(&poll->wait.entry);
4717 else if (cancel)
4718 WRITE_ONCE(poll->canceled, true);
4719 else if (!poll->done) /* actually waiting for an event */
4720 io_poll_req_insert(req);
4721 spin_unlock(&poll->head->lock);
4722 }
4723
4724 return mask;
4725}
4726
4727static bool io_arm_poll_handler(struct io_kiocb *req)
4728{
4729 const struct io_op_def *def = &io_op_defs[req->opcode];
4730 struct io_ring_ctx *ctx = req->ctx;
4731 struct async_poll *apoll;
4732 struct io_poll_table ipt;
4733 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004734 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004735
4736 if (!req->file || !file_can_poll(req->file))
4737 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004738 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004739 return false;
4740 if (!def->pollin && !def->pollout)
4741 return false;
4742
4743 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4744 if (unlikely(!apoll))
4745 return false;
4746
4747 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004748 if (req->flags & REQ_F_WORK_INITIALIZED)
4749 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004750 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004751
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004752 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004753 req->apoll = apoll;
4754 INIT_HLIST_NODE(&req->hash_node);
4755
Nathan Chancellor8755d972020-03-02 16:01:19 -07004756 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004757 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004758 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004759 if (def->pollout)
4760 mask |= POLLOUT | POLLWRNORM;
4761 mask |= POLLERR | POLLPRI;
4762
4763 ipt.pt._qproc = io_async_queue_proc;
4764
4765 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4766 io_async_wake);
4767 if (ret) {
4768 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004769 /* only remove double add if we did it here */
4770 if (!had_io)
4771 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004772 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004773 if (req->flags & REQ_F_WORK_INITIALIZED)
4774 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004775 kfree(apoll);
4776 return false;
4777 }
4778 spin_unlock_irq(&ctx->completion_lock);
4779 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4780 apoll->poll.events);
4781 return true;
4782}
4783
4784static bool __io_poll_remove_one(struct io_kiocb *req,
4785 struct io_poll_iocb *poll)
4786{
Jens Axboeb41e9852020-02-17 09:52:41 -07004787 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004788
4789 spin_lock(&poll->head->lock);
4790 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004791 if (!list_empty(&poll->wait.entry)) {
4792 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004793 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004794 }
4795 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004796 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004797 return do_complete;
4798}
4799
4800static bool io_poll_remove_one(struct io_kiocb *req)
4801{
4802 bool do_complete;
4803
4804 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004805 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004806 do_complete = __io_poll_remove_one(req, &req->poll);
4807 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004808 struct async_poll *apoll = req->apoll;
4809
Jens Axboed7718a92020-02-14 22:23:12 -07004810 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004811 do_complete = __io_poll_remove_one(req, &apoll->poll);
4812 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004813 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004814 /*
4815 * restore ->work because we will call
4816 * io_req_work_drop_env below when dropping the
4817 * final reference.
4818 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004819 if (req->flags & REQ_F_WORK_INITIALIZED)
4820 memcpy(&req->work, &apoll->work,
4821 sizeof(req->work));
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004822 kfree(apoll);
4823 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004824 }
4825
Jens Axboeb41e9852020-02-17 09:52:41 -07004826 if (do_complete) {
4827 io_cqring_fill_event(req, -ECANCELED);
4828 io_commit_cqring(req->ctx);
4829 req->flags |= REQ_F_COMP_LOCKED;
4830 io_put_req(req);
4831 }
4832
4833 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004834}
4835
4836static void io_poll_remove_all(struct io_ring_ctx *ctx)
4837{
Jens Axboe78076bb2019-12-04 19:56:40 -07004838 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004839 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004840 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004841
4842 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004843 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4844 struct hlist_head *list;
4845
4846 list = &ctx->cancel_hash[i];
4847 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004848 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004849 }
4850 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004851
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004852 if (posted)
4853 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004854}
4855
Jens Axboe47f46762019-11-09 17:43:02 -07004856static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4857{
Jens Axboe78076bb2019-12-04 19:56:40 -07004858 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004859 struct io_kiocb *req;
4860
Jens Axboe78076bb2019-12-04 19:56:40 -07004861 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4862 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004863 if (sqe_addr != req->user_data)
4864 continue;
4865 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004866 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004867 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004868 }
4869
4870 return -ENOENT;
4871}
4872
Jens Axboe3529d8c2019-12-19 18:24:38 -07004873static int io_poll_remove_prep(struct io_kiocb *req,
4874 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004875{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004876 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4877 return -EINVAL;
4878 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4879 sqe->poll_events)
4880 return -EINVAL;
4881
Jens Axboe0969e782019-12-17 18:40:57 -07004882 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004883 return 0;
4884}
4885
4886/*
4887 * Find a running poll command that matches one specified in sqe->addr,
4888 * and remove it if found.
4889 */
4890static int io_poll_remove(struct io_kiocb *req)
4891{
4892 struct io_ring_ctx *ctx = req->ctx;
4893 u64 addr;
4894 int ret;
4895
Jens Axboe0969e782019-12-17 18:40:57 -07004896 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004897 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004898 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004899 spin_unlock_irq(&ctx->completion_lock);
4900
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004901 if (ret < 0)
4902 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004903 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004904 return 0;
4905}
4906
Jens Axboe221c5eb2019-01-17 09:41:58 -07004907static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4908 void *key)
4909{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004910 struct io_kiocb *req = wait->private;
4911 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004912
Jens Axboed7718a92020-02-14 22:23:12 -07004913 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004914}
4915
Jens Axboe221c5eb2019-01-17 09:41:58 -07004916static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4917 struct poll_table_struct *p)
4918{
4919 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4920
Jens Axboed7718a92020-02-14 22:23:12 -07004921 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004922}
4923
Jens Axboe3529d8c2019-12-19 18:24:38 -07004924static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004925{
4926 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004927 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004928
4929 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4930 return -EINVAL;
4931 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4932 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004933 if (!poll->file)
4934 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004935
Jiufei Xue5769a352020-06-17 17:53:55 +08004936 events = READ_ONCE(sqe->poll32_events);
4937#ifdef __BIG_ENDIAN
4938 events = swahw32(events);
4939#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004940 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4941 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004942
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004943 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004944 return 0;
4945}
4946
Pavel Begunkov014db002020-03-03 21:33:12 +03004947static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004948{
4949 struct io_poll_iocb *poll = &req->poll;
4950 struct io_ring_ctx *ctx = req->ctx;
4951 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004952 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004953
Jens Axboe78076bb2019-12-04 19:56:40 -07004954 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004955 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004956 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004957
Jens Axboed7718a92020-02-14 22:23:12 -07004958 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4959 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004960
Jens Axboe8c838782019-03-12 15:48:16 -06004961 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004962 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004963 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004964 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004965 spin_unlock_irq(&ctx->completion_lock);
4966
Jens Axboe8c838782019-03-12 15:48:16 -06004967 if (mask) {
4968 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004969 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004970 }
Jens Axboe8c838782019-03-12 15:48:16 -06004971 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004972}
4973
Jens Axboe5262f562019-09-17 12:26:57 -06004974static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4975{
Jens Axboead8a48a2019-11-15 08:49:11 -07004976 struct io_timeout_data *data = container_of(timer,
4977 struct io_timeout_data, timer);
4978 struct io_kiocb *req = data->req;
4979 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004980 unsigned long flags;
4981
Jens Axboe5262f562019-09-17 12:26:57 -06004982 atomic_inc(&ctx->cq_timeouts);
4983
4984 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004985 /*
Jens Axboe11365042019-10-16 09:08:32 -06004986 * We could be racing with timeout deletion. If the list is empty,
4987 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004988 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004989 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004990 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004991
Jens Axboe78e19bb2019-11-06 15:21:34 -07004992 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004993 io_commit_cqring(ctx);
4994 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4995
4996 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004997 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004998 io_put_req(req);
4999 return HRTIMER_NORESTART;
5000}
5001
Jens Axboe47f46762019-11-09 17:43:02 -07005002static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5003{
5004 struct io_kiocb *req;
5005 int ret = -ENOENT;
5006
5007 list_for_each_entry(req, &ctx->timeout_list, list) {
5008 if (user_data == req->user_data) {
5009 list_del_init(&req->list);
5010 ret = 0;
5011 break;
5012 }
5013 }
5014
5015 if (ret == -ENOENT)
5016 return ret;
5017
Jens Axboe2d283902019-12-04 11:08:05 -07005018 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005019 if (ret == -1)
5020 return -EALREADY;
5021
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005022 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005023 io_cqring_fill_event(req, -ECANCELED);
5024 io_put_req(req);
5025 return 0;
5026}
5027
Jens Axboe3529d8c2019-12-19 18:24:38 -07005028static int io_timeout_remove_prep(struct io_kiocb *req,
5029 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005030{
Jens Axboeb29472e2019-12-17 18:50:29 -07005031 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5032 return -EINVAL;
5033 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
5034 return -EINVAL;
5035
5036 req->timeout.addr = READ_ONCE(sqe->addr);
5037 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5038 if (req->timeout.flags)
5039 return -EINVAL;
5040
Jens Axboeb29472e2019-12-17 18:50:29 -07005041 return 0;
5042}
5043
Jens Axboe11365042019-10-16 09:08:32 -06005044/*
5045 * Remove or update an existing timeout command
5046 */
Jens Axboefc4df992019-12-10 14:38:45 -07005047static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005048{
5049 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005050 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005051
Jens Axboe11365042019-10-16 09:08:32 -06005052 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005053 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005054
Jens Axboe47f46762019-11-09 17:43:02 -07005055 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005056 io_commit_cqring(ctx);
5057 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005058 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005059 if (ret < 0)
5060 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005061 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005062 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005063}
5064
Jens Axboe3529d8c2019-12-19 18:24:38 -07005065static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005066 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005067{
Jens Axboead8a48a2019-11-15 08:49:11 -07005068 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005069 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005070 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005071
Jens Axboead8a48a2019-11-15 08:49:11 -07005072 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005073 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005074 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005075 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005076 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005077 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005078 flags = READ_ONCE(sqe->timeout_flags);
5079 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005080 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005081
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005082 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005083
Jens Axboe3529d8c2019-12-19 18:24:38 -07005084 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005085 return -ENOMEM;
5086
5087 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005088 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005089 req->flags |= REQ_F_TIMEOUT;
5090
5091 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005092 return -EFAULT;
5093
Jens Axboe11365042019-10-16 09:08:32 -06005094 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005095 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005096 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005097 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005098
Jens Axboead8a48a2019-11-15 08:49:11 -07005099 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5100 return 0;
5101}
5102
Jens Axboefc4df992019-12-10 14:38:45 -07005103static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005104{
Jens Axboead8a48a2019-11-15 08:49:11 -07005105 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005106 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005107 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005108 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005109
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005110 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005111
Jens Axboe5262f562019-09-17 12:26:57 -06005112 /*
5113 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005114 * timeout event to be satisfied. If it isn't set, then this is
5115 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005116 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005117 if (!off) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005118 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07005119 entry = ctx->timeout_list.prev;
5120 goto add;
5121 }
Jens Axboe5262f562019-09-17 12:26:57 -06005122
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005123 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5124 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005125
5126 /*
5127 * Insertion sort, ensuring the first entry in the list is always
5128 * the one we need first.
5129 */
Jens Axboe5262f562019-09-17 12:26:57 -06005130 list_for_each_prev(entry, &ctx->timeout_list) {
5131 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06005132
Jens Axboe93bd25b2019-11-11 23:34:31 -07005133 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
5134 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005135 /* nxt.seq is behind @tail, otherwise would've been completed */
5136 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005137 break;
5138 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005139add:
Jens Axboe5262f562019-09-17 12:26:57 -06005140 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005141 data->timer.function = io_timeout_fn;
5142 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005143 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005144 return 0;
5145}
5146
Jens Axboe62755e32019-10-28 21:49:21 -06005147static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005148{
Jens Axboe62755e32019-10-28 21:49:21 -06005149 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005150
Jens Axboe62755e32019-10-28 21:49:21 -06005151 return req->user_data == (unsigned long) data;
5152}
5153
Jens Axboee977d6d2019-11-05 12:39:45 -07005154static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005155{
Jens Axboe62755e32019-10-28 21:49:21 -06005156 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005157 int ret = 0;
5158
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005159 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005160 switch (cancel_ret) {
5161 case IO_WQ_CANCEL_OK:
5162 ret = 0;
5163 break;
5164 case IO_WQ_CANCEL_RUNNING:
5165 ret = -EALREADY;
5166 break;
5167 case IO_WQ_CANCEL_NOTFOUND:
5168 ret = -ENOENT;
5169 break;
5170 }
5171
Jens Axboee977d6d2019-11-05 12:39:45 -07005172 return ret;
5173}
5174
Jens Axboe47f46762019-11-09 17:43:02 -07005175static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5176 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005177 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005178{
5179 unsigned long flags;
5180 int ret;
5181
5182 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5183 if (ret != -ENOENT) {
5184 spin_lock_irqsave(&ctx->completion_lock, flags);
5185 goto done;
5186 }
5187
5188 spin_lock_irqsave(&ctx->completion_lock, flags);
5189 ret = io_timeout_cancel(ctx, sqe_addr);
5190 if (ret != -ENOENT)
5191 goto done;
5192 ret = io_poll_cancel(ctx, sqe_addr);
5193done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005194 if (!ret)
5195 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005196 io_cqring_fill_event(req, ret);
5197 io_commit_cqring(ctx);
5198 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5199 io_cqring_ev_posted(ctx);
5200
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005201 if (ret < 0)
5202 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005203 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005204}
5205
Jens Axboe3529d8c2019-12-19 18:24:38 -07005206static int io_async_cancel_prep(struct io_kiocb *req,
5207 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005208{
Jens Axboefbf23842019-12-17 18:45:56 -07005209 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005210 return -EINVAL;
5211 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
5212 sqe->cancel_flags)
5213 return -EINVAL;
5214
Jens Axboefbf23842019-12-17 18:45:56 -07005215 req->cancel.addr = READ_ONCE(sqe->addr);
5216 return 0;
5217}
5218
Pavel Begunkov014db002020-03-03 21:33:12 +03005219static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005220{
5221 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005222
Pavel Begunkov014db002020-03-03 21:33:12 +03005223 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005224 return 0;
5225}
5226
Jens Axboe05f3fb32019-12-09 11:22:50 -07005227static int io_files_update_prep(struct io_kiocb *req,
5228 const struct io_uring_sqe *sqe)
5229{
5230 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
5231 return -EINVAL;
5232
5233 req->files_update.offset = READ_ONCE(sqe->off);
5234 req->files_update.nr_args = READ_ONCE(sqe->len);
5235 if (!req->files_update.nr_args)
5236 return -EINVAL;
5237 req->files_update.arg = READ_ONCE(sqe->addr);
5238 return 0;
5239}
5240
Jens Axboe229a7b62020-06-22 10:13:11 -06005241static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5242 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005243{
5244 struct io_ring_ctx *ctx = req->ctx;
5245 struct io_uring_files_update up;
5246 int ret;
5247
Jens Axboef86cd202020-01-29 13:46:44 -07005248 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005249 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005250
5251 up.offset = req->files_update.offset;
5252 up.fds = req->files_update.arg;
5253
5254 mutex_lock(&ctx->uring_lock);
5255 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5256 mutex_unlock(&ctx->uring_lock);
5257
5258 if (ret < 0)
5259 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005260 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005261 return 0;
5262}
5263
Jens Axboe3529d8c2019-12-19 18:24:38 -07005264static int io_req_defer_prep(struct io_kiocb *req,
Jens Axboec40f6372020-06-25 15:39:59 -06005265 const struct io_uring_sqe *sqe, bool for_async)
Jens Axboef67676d2019-12-02 11:03:47 -07005266{
Jens Axboee7815732019-12-17 19:45:06 -07005267 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005268
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005269 if (!sqe)
5270 return 0;
5271
Jens Axboec40f6372020-06-25 15:39:59 -06005272 if (for_async || (req->flags & REQ_F_WORK_INITIALIZED)) {
5273 io_req_init_async(req);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005274
Jens Axboec40f6372020-06-25 15:39:59 -06005275 if (io_op_defs[req->opcode].file_table) {
5276 ret = io_grab_files(req);
5277 if (unlikely(ret))
5278 return ret;
5279 }
5280
5281 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
Jens Axboef86cd202020-01-29 13:46:44 -07005282 }
5283
Jens Axboed625c6e2019-12-17 19:53:05 -07005284 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005285 case IORING_OP_NOP:
5286 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005287 case IORING_OP_READV:
5288 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005289 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005290 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005291 break;
5292 case IORING_OP_WRITEV:
5293 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005294 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005295 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005296 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005297 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005298 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005299 break;
5300 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005301 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005302 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005303 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005304 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005305 break;
5306 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005307 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005308 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005309 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005310 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005311 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005312 break;
5313 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005314 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005315 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005316 break;
Jens Axboef499a022019-12-02 16:28:46 -07005317 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005318 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005319 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005320 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005321 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005322 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005323 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005324 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005325 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005326 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005327 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005328 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005329 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005330 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005331 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005332 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005333 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005334 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005335 case IORING_OP_FALLOCATE:
5336 ret = io_fallocate_prep(req, sqe);
5337 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005338 case IORING_OP_OPENAT:
5339 ret = io_openat_prep(req, sqe);
5340 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005341 case IORING_OP_CLOSE:
5342 ret = io_close_prep(req, sqe);
5343 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005344 case IORING_OP_FILES_UPDATE:
5345 ret = io_files_update_prep(req, sqe);
5346 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005347 case IORING_OP_STATX:
5348 ret = io_statx_prep(req, sqe);
5349 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005350 case IORING_OP_FADVISE:
5351 ret = io_fadvise_prep(req, sqe);
5352 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005353 case IORING_OP_MADVISE:
5354 ret = io_madvise_prep(req, sqe);
5355 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005356 case IORING_OP_OPENAT2:
5357 ret = io_openat2_prep(req, sqe);
5358 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005359 case IORING_OP_EPOLL_CTL:
5360 ret = io_epoll_ctl_prep(req, sqe);
5361 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005362 case IORING_OP_SPLICE:
5363 ret = io_splice_prep(req, sqe);
5364 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005365 case IORING_OP_PROVIDE_BUFFERS:
5366 ret = io_provide_buffers_prep(req, sqe);
5367 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005368 case IORING_OP_REMOVE_BUFFERS:
5369 ret = io_remove_buffers_prep(req, sqe);
5370 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005371 case IORING_OP_TEE:
5372 ret = io_tee_prep(req, sqe);
5373 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005374 default:
Jens Axboee7815732019-12-17 19:45:06 -07005375 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5376 req->opcode);
5377 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005378 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005379 }
5380
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005381 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005382}
5383
Jens Axboe3529d8c2019-12-19 18:24:38 -07005384static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005385{
Jackie Liua197f662019-11-08 08:09:12 -07005386 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005387 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005388
Bob Liu9d858b22019-11-13 18:06:25 +08005389 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005390 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005391 return 0;
5392
Pavel Begunkov650b5482020-05-17 14:02:11 +03005393 if (!req->io) {
5394 if (io_alloc_async_ctx(req))
5395 return -EAGAIN;
Jens Axboec40f6372020-06-25 15:39:59 -06005396 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkov650b5482020-05-17 14:02:11 +03005397 if (ret < 0)
5398 return ret;
5399 }
Jens Axboe2d283902019-12-04 11:08:05 -07005400
Jens Axboede0617e2019-04-06 21:51:27 -06005401 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005402 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005403 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005404 return 0;
5405 }
5406
Jens Axboe915967f2019-11-21 09:01:20 -07005407 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005408 list_add_tail(&req->list, &ctx->defer_list);
5409 spin_unlock_irq(&ctx->completion_lock);
5410 return -EIOCBQUEUED;
5411}
5412
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005413static void io_cleanup_req(struct io_kiocb *req)
5414{
5415 struct io_async_ctx *io = req->io;
5416
5417 switch (req->opcode) {
5418 case IORING_OP_READV:
5419 case IORING_OP_READ_FIXED:
5420 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005421 if (req->flags & REQ_F_BUFFER_SELECTED)
5422 kfree((void *)(unsigned long)req->rw.addr);
5423 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005424 case IORING_OP_WRITEV:
5425 case IORING_OP_WRITE_FIXED:
5426 case IORING_OP_WRITE:
5427 if (io->rw.iov != io->rw.fast_iov)
5428 kfree(io->rw.iov);
5429 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005430 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005431 if (req->flags & REQ_F_BUFFER_SELECTED)
5432 kfree(req->sr_msg.kbuf);
5433 /* fallthrough */
5434 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005435 if (io->msg.iov != io->msg.fast_iov)
5436 kfree(io->msg.iov);
5437 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005438 case IORING_OP_RECV:
5439 if (req->flags & REQ_F_BUFFER_SELECTED)
5440 kfree(req->sr_msg.kbuf);
5441 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005442 case IORING_OP_OPENAT:
5443 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005444 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005445 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005446 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005447 io_put_file(req, req->splice.file_in,
5448 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5449 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005450 }
5451
5452 req->flags &= ~REQ_F_NEED_CLEANUP;
5453}
5454
Jens Axboe3529d8c2019-12-19 18:24:38 -07005455static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005456 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005457{
Jackie Liua197f662019-11-08 08:09:12 -07005458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005459 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005460
Jens Axboed625c6e2019-12-17 19:53:05 -07005461 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005462 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005463 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005464 break;
5465 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005466 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005467 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005468 if (sqe) {
5469 ret = io_read_prep(req, sqe, force_nonblock);
5470 if (ret < 0)
5471 break;
5472 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005473 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005474 break;
5475 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005476 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005477 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005478 if (sqe) {
5479 ret = io_write_prep(req, sqe, force_nonblock);
5480 if (ret < 0)
5481 break;
5482 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005483 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005484 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005485 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005486 if (sqe) {
5487 ret = io_prep_fsync(req, sqe);
5488 if (ret < 0)
5489 break;
5490 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005491 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005492 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005493 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005494 if (sqe) {
5495 ret = io_poll_add_prep(req, sqe);
5496 if (ret)
5497 break;
5498 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005499 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005500 break;
5501 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005502 if (sqe) {
5503 ret = io_poll_remove_prep(req, sqe);
5504 if (ret < 0)
5505 break;
5506 }
Jens Axboefc4df992019-12-10 14:38:45 -07005507 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005508 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005509 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005510 if (sqe) {
5511 ret = io_prep_sfr(req, sqe);
5512 if (ret < 0)
5513 break;
5514 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005515 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005516 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005517 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005518 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005519 if (sqe) {
5520 ret = io_sendmsg_prep(req, sqe);
5521 if (ret < 0)
5522 break;
5523 }
Jens Axboefddafac2020-01-04 20:19:44 -07005524 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005525 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005526 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005527 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005528 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005529 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005530 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005531 if (sqe) {
5532 ret = io_recvmsg_prep(req, sqe);
5533 if (ret)
5534 break;
5535 }
Jens Axboefddafac2020-01-04 20:19:44 -07005536 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005537 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005538 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005539 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005540 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005541 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005542 if (sqe) {
5543 ret = io_timeout_prep(req, sqe, false);
5544 if (ret)
5545 break;
5546 }
Jens Axboefc4df992019-12-10 14:38:45 -07005547 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005548 break;
Jens Axboe11365042019-10-16 09:08:32 -06005549 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005550 if (sqe) {
5551 ret = io_timeout_remove_prep(req, sqe);
5552 if (ret)
5553 break;
5554 }
Jens Axboefc4df992019-12-10 14:38:45 -07005555 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005556 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005557 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005558 if (sqe) {
5559 ret = io_accept_prep(req, sqe);
5560 if (ret)
5561 break;
5562 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005563 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005564 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005565 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005566 if (sqe) {
5567 ret = io_connect_prep(req, sqe);
5568 if (ret)
5569 break;
5570 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005571 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005572 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005573 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005574 if (sqe) {
5575 ret = io_async_cancel_prep(req, sqe);
5576 if (ret)
5577 break;
5578 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005579 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005580 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005581 case IORING_OP_FALLOCATE:
5582 if (sqe) {
5583 ret = io_fallocate_prep(req, sqe);
5584 if (ret)
5585 break;
5586 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005587 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005588 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005589 case IORING_OP_OPENAT:
5590 if (sqe) {
5591 ret = io_openat_prep(req, sqe);
5592 if (ret)
5593 break;
5594 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005595 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005596 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005597 case IORING_OP_CLOSE:
5598 if (sqe) {
5599 ret = io_close_prep(req, sqe);
5600 if (ret)
5601 break;
5602 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005603 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005604 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005605 case IORING_OP_FILES_UPDATE:
5606 if (sqe) {
5607 ret = io_files_update_prep(req, sqe);
5608 if (ret)
5609 break;
5610 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005611 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005612 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005613 case IORING_OP_STATX:
5614 if (sqe) {
5615 ret = io_statx_prep(req, sqe);
5616 if (ret)
5617 break;
5618 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005619 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005620 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005621 case IORING_OP_FADVISE:
5622 if (sqe) {
5623 ret = io_fadvise_prep(req, sqe);
5624 if (ret)
5625 break;
5626 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005627 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005628 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005629 case IORING_OP_MADVISE:
5630 if (sqe) {
5631 ret = io_madvise_prep(req, sqe);
5632 if (ret)
5633 break;
5634 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005635 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005636 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005637 case IORING_OP_OPENAT2:
5638 if (sqe) {
5639 ret = io_openat2_prep(req, sqe);
5640 if (ret)
5641 break;
5642 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005643 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005644 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005645 case IORING_OP_EPOLL_CTL:
5646 if (sqe) {
5647 ret = io_epoll_ctl_prep(req, sqe);
5648 if (ret)
5649 break;
5650 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005651 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005652 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005653 case IORING_OP_SPLICE:
5654 if (sqe) {
5655 ret = io_splice_prep(req, sqe);
5656 if (ret < 0)
5657 break;
5658 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005659 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005660 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005661 case IORING_OP_PROVIDE_BUFFERS:
5662 if (sqe) {
5663 ret = io_provide_buffers_prep(req, sqe);
5664 if (ret)
5665 break;
5666 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005667 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005668 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005669 case IORING_OP_REMOVE_BUFFERS:
5670 if (sqe) {
5671 ret = io_remove_buffers_prep(req, sqe);
5672 if (ret)
5673 break;
5674 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005675 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005676 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005677 case IORING_OP_TEE:
5678 if (sqe) {
5679 ret = io_tee_prep(req, sqe);
5680 if (ret < 0)
5681 break;
5682 }
5683 ret = io_tee(req, force_nonblock);
5684 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005685 default:
5686 ret = -EINVAL;
5687 break;
5688 }
5689
5690 if (ret)
5691 return ret;
5692
Jens Axboeb5325762020-05-19 21:20:27 -06005693 /* If the op doesn't have a file, we're not polling for it */
5694 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005695 const bool in_async = io_wq_current_is_worker();
5696
Jens Axboe11ba8202020-01-15 21:51:17 -07005697 /* workqueue context doesn't hold uring_lock, grab it now */
5698 if (in_async)
5699 mutex_lock(&ctx->uring_lock);
5700
Jens Axboe2b188cc2019-01-07 10:46:33 -07005701 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005702
5703 if (in_async)
5704 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005705 }
5706
5707 return 0;
5708}
5709
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005710static void io_arm_async_linked_timeout(struct io_kiocb *req)
5711{
5712 struct io_kiocb *link;
5713
5714 /* link head's timeout is queued in io_queue_async_work() */
5715 if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5716 return;
5717
5718 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5719 io_queue_linked_timeout(link);
5720}
5721
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005722static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Jens Axboedef596e2019-01-09 08:59:42 -07005723{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005724 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005725 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005726
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005727 io_arm_async_linked_timeout(req);
5728
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005729 /* if NO_CANCEL is set, we must still run the work */
5730 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5731 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005732 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005733 }
Jens Axboe31b51512019-01-18 22:56:34 -07005734
Jens Axboe561fb042019-10-24 07:25:42 -06005735 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005736 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005737 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005738 /*
5739 * We can get EAGAIN for polled IO even though we're
5740 * forcing a sync submission from here, since we can't
5741 * wait for request slots on the block side.
5742 */
5743 if (ret != -EAGAIN)
5744 break;
5745 cond_resched();
5746 } while (1);
5747 }
Jens Axboe31b51512019-01-18 22:56:34 -07005748
Jens Axboe561fb042019-10-24 07:25:42 -06005749 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005750 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005751 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005752 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005753
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005754 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005755}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005756
Jens Axboe65e19f52019-10-26 07:20:21 -06005757static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5758 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005759{
Jens Axboe65e19f52019-10-26 07:20:21 -06005760 struct fixed_file_table *table;
5761
Jens Axboe05f3fb32019-12-09 11:22:50 -07005762 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005763 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005764}
5765
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005766static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5767 int fd, struct file **out_file, bool fixed)
5768{
5769 struct io_ring_ctx *ctx = req->ctx;
5770 struct file *file;
5771
5772 if (fixed) {
5773 if (unlikely(!ctx->file_data ||
5774 (unsigned) fd >= ctx->nr_user_files))
5775 return -EBADF;
5776 fd = array_index_nospec(fd, ctx->nr_user_files);
5777 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005778 if (file) {
5779 req->fixed_file_refs = ctx->file_data->cur_refs;
5780 percpu_ref_get(req->fixed_file_refs);
5781 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005782 } else {
5783 trace_io_uring_file_get(ctx, fd);
5784 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005785 }
5786
Jens Axboefd2206e2020-06-02 16:40:47 -06005787 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5788 *out_file = file;
5789 return 0;
5790 }
5791 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005792}
5793
Jens Axboe3529d8c2019-12-19 18:24:38 -07005794static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005795 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005796{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005797 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005798
Jens Axboe63ff8222020-05-07 14:56:15 -06005799 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005800 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005801 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005802
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005803 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005804}
5805
Jackie Liua197f662019-11-08 08:09:12 -07005806static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005807{
Jens Axboefcb323c2019-10-24 12:39:47 -06005808 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005809 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005810
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005811 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005812 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005813 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005814 return -EBADF;
5815
Jens Axboefcb323c2019-10-24 12:39:47 -06005816 rcu_read_lock();
5817 spin_lock_irq(&ctx->inflight_lock);
5818 /*
5819 * We use the f_ops->flush() handler to ensure that we can flush
5820 * out work accessing these files if the fd is closed. Check if
5821 * the fd has changed since we started down this path, and disallow
5822 * this operation if it has.
5823 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005824 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005825 list_add(&req->inflight_entry, &ctx->inflight_list);
5826 req->flags |= REQ_F_INFLIGHT;
5827 req->work.files = current->files;
5828 ret = 0;
5829 }
5830 spin_unlock_irq(&ctx->inflight_lock);
5831 rcu_read_unlock();
5832
5833 return ret;
5834}
5835
Jens Axboe2665abf2019-11-05 12:40:47 -07005836static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5837{
Jens Axboead8a48a2019-11-15 08:49:11 -07005838 struct io_timeout_data *data = container_of(timer,
5839 struct io_timeout_data, timer);
5840 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005841 struct io_ring_ctx *ctx = req->ctx;
5842 struct io_kiocb *prev = NULL;
5843 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005844
5845 spin_lock_irqsave(&ctx->completion_lock, flags);
5846
5847 /*
5848 * We don't expect the list to be empty, that will only happen if we
5849 * race with the completion of the linked work.
5850 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005851 if (!list_empty(&req->link_list)) {
5852 prev = list_entry(req->link_list.prev, struct io_kiocb,
5853 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005854 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005855 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005856 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5857 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005858 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005859 }
5860
5861 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5862
5863 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005864 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005865 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005866 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005867 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005868 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005869 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005870 return HRTIMER_NORESTART;
5871}
5872
Jens Axboead8a48a2019-11-15 08:49:11 -07005873static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005874{
Jens Axboe76a46e02019-11-10 23:34:16 -07005875 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005876
Jens Axboe76a46e02019-11-10 23:34:16 -07005877 /*
5878 * If the list is now empty, then our linked request finished before
5879 * we got a chance to setup the timer
5880 */
5881 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005882 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005883 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005884
Jens Axboead8a48a2019-11-15 08:49:11 -07005885 data->timer.function = io_link_timeout_fn;
5886 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5887 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005888 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005889 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005890
Jens Axboe2665abf2019-11-05 12:40:47 -07005891 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005892 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005893}
5894
Jens Axboead8a48a2019-11-15 08:49:11 -07005895static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005896{
5897 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005898
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005899 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005900 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005901 /* for polled retry, if flag is set, we already went through here */
5902 if (req->flags & REQ_F_POLLED)
5903 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005904
Pavel Begunkov44932332019-12-05 16:16:35 +03005905 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5906 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005907 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005908 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005909
Jens Axboe76a46e02019-11-10 23:34:16 -07005910 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005911 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005912}
5913
Jens Axboef13fad72020-06-22 09:34:30 -06005914static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5915 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005916{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005917 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005918 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005919 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005920 int ret;
5921
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005922again:
5923 linked_timeout = io_prep_linked_timeout(req);
5924
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005925 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5926 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005927 if (old_creds)
5928 revert_creds(old_creds);
5929 if (old_creds == req->work.creds)
5930 old_creds = NULL; /* restored original creds */
5931 else
5932 old_creds = override_creds(req->work.creds);
5933 }
5934
Jens Axboef13fad72020-06-22 09:34:30 -06005935 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06005936
5937 /*
5938 * We async punt it if the file wasn't marked NOWAIT, or if the file
5939 * doesn't support non-blocking read/write attempts
5940 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005941 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005942 if (io_arm_poll_handler(req)) {
5943 if (linked_timeout)
5944 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005945 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005946 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005947punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005948 io_req_init_async(req);
5949
Jens Axboef86cd202020-01-29 13:46:44 -07005950 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005951 ret = io_grab_files(req);
5952 if (ret)
5953 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005954 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005955
5956 /*
5957 * Queued up for async execution, worker will release
5958 * submit reference when the iocb is actually submitted.
5959 */
5960 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005961 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005962 }
Jens Axboee65ef562019-03-12 10:16:44 -06005963
Jens Axboefcb323c2019-10-24 12:39:47 -06005964err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005965 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005966 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005967 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005968
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005969 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005970 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005971 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005972 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005973 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005974 }
5975
Jens Axboee65ef562019-03-12 10:16:44 -06005976 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005977 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005978 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005979 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06005980 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005981 if (nxt) {
5982 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005983
5984 if (req->flags & REQ_F_FORCE_ASYNC)
5985 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005986 goto again;
5987 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005988exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005989 if (old_creds)
5990 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005991}
5992
Jens Axboef13fad72020-06-22 09:34:30 -06005993static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5994 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005995{
5996 int ret;
5997
Jens Axboe3529d8c2019-12-19 18:24:38 -07005998 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005999 if (ret) {
6000 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006001fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006002 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006003 io_put_req(req);
6004 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006005 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006006 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006007 if (!req->io) {
6008 ret = -EAGAIN;
6009 if (io_alloc_async_ctx(req))
6010 goto fail_req;
Jens Axboec40f6372020-06-25 15:39:59 -06006011 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006012 if (unlikely(ret < 0))
6013 goto fail_req;
6014 }
6015
Jens Axboece35a472019-12-17 08:04:44 -07006016 /*
6017 * Never try inline submit of IOSQE_ASYNC is set, go straight
6018 * to async execution.
6019 */
6020 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6021 io_queue_async_work(req);
6022 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006023 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006024 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006025}
6026
Jens Axboef13fad72020-06-22 09:34:30 -06006027static inline void io_queue_link_head(struct io_kiocb *req,
6028 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006029{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006030 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006031 io_put_req(req);
6032 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006033 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006034 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006035}
6036
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006037static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006038 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006039{
Jackie Liua197f662019-11-08 08:09:12 -07006040 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006041 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006042
Jens Axboe9e645e112019-05-10 16:07:28 -06006043 /*
6044 * If we already have a head request, queue this one for async
6045 * submittal once the head completes. If we don't have a head but
6046 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6047 * submitted sync once the chain is complete. If none of those
6048 * conditions are true (normal request), then just queue it.
6049 */
6050 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006051 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006052
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006053 /*
6054 * Taking sequential execution of a link, draining both sides
6055 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6056 * requests in the link. So, it drains the head and the
6057 * next after the link request. The last one is done via
6058 * drain_next flag to persist the effect across calls.
6059 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006060 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006061 head->flags |= REQ_F_IO_DRAIN;
6062 ctx->drain_next = 1;
6063 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006064 if (io_alloc_async_ctx(req))
6065 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06006066
Jens Axboec40f6372020-06-25 15:39:59 -06006067 ret = io_req_defer_prep(req, sqe, false);
Jens Axboe2d283902019-12-04 11:08:05 -07006068 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006069 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006070 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006071 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006072 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006073 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006074 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006075 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006076
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006077 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006078 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006079 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006080 *link = NULL;
6081 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006082 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006083 if (unlikely(ctx->drain_next)) {
6084 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006085 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006086 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006087 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006088 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006089 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006090
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006091 if (io_alloc_async_ctx(req))
6092 return -EAGAIN;
6093
Jens Axboec40f6372020-06-25 15:39:59 -06006094 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkov711be032020-01-17 03:57:59 +03006095 if (ret)
6096 req->flags |= REQ_F_FAIL_LINK;
6097 *link = req;
6098 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006099 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006100 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006101 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006102
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006103 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006104}
6105
Jens Axboe9a56a232019-01-09 09:06:50 -07006106/*
6107 * Batched submission is done, ensure local IO is flushed out.
6108 */
6109static void io_submit_state_end(struct io_submit_state *state)
6110{
Jens Axboef13fad72020-06-22 09:34:30 -06006111 if (!list_empty(&state->comp.list))
6112 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006113 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006114 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006115 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006116 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006117}
6118
6119/*
6120 * Start submission side cache.
6121 */
6122static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006123 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006124{
6125 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006126#ifdef CONFIG_BLOCK
6127 state->plug.nowait = true;
6128#endif
Jens Axboe013538b2020-06-22 09:29:15 -06006129 state->comp.nr = 0;
6130 INIT_LIST_HEAD(&state->comp.list);
6131 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006132 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006133 state->file = NULL;
6134 state->ios_left = max_ios;
6135}
6136
Jens Axboe2b188cc2019-01-07 10:46:33 -07006137static void io_commit_sqring(struct io_ring_ctx *ctx)
6138{
Hristo Venev75b28af2019-08-26 17:23:46 +00006139 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006140
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006141 /*
6142 * Ensure any loads from the SQEs are done at this point,
6143 * since once we write the new head, the application could
6144 * write new data to them.
6145 */
6146 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147}
6148
6149/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006150 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006151 * that is mapped by userspace. This means that care needs to be taken to
6152 * ensure that reads are stable, as we cannot rely on userspace always
6153 * being a good citizen. If members of the sqe are validated and then later
6154 * used, it's important that those reads are done through READ_ONCE() to
6155 * prevent a re-load down the line.
6156 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006157static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006158{
Hristo Venev75b28af2019-08-26 17:23:46 +00006159 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006160 unsigned head;
6161
6162 /*
6163 * The cached sq head (or cq tail) serves two purposes:
6164 *
6165 * 1) allows us to batch the cost of updating the user visible
6166 * head updates.
6167 * 2) allows the kernel side to track the head on its own, even
6168 * though the application is the one updating it.
6169 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006170 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006171 if (likely(head < ctx->sq_entries))
6172 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006173
6174 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006175 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006176 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006177 return NULL;
6178}
6179
6180static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6181{
6182 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006183}
6184
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006185#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6186 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6187 IOSQE_BUFFER_SELECT)
6188
6189static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6190 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006191 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006192{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006193 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006194 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006195
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006196 /*
6197 * All io need record the previous position, if LINK vs DARIN,
6198 * it can be used to mark the position of the first IO in the
6199 * link list.
6200 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03006201 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006202 req->opcode = READ_ONCE(sqe->opcode);
6203 req->user_data = READ_ONCE(sqe->user_data);
6204 req->io = NULL;
6205 req->file = NULL;
6206 req->ctx = ctx;
6207 req->flags = 0;
6208 /* one is dropped after submission, the other at completion */
6209 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006210 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006211 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006212
6213 if (unlikely(req->opcode >= IORING_OP_LAST))
6214 return -EINVAL;
6215
Jens Axboe9d8426a2020-06-16 18:42:49 -06006216 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6217 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006218
6219 sqe_flags = READ_ONCE(sqe->flags);
6220 /* enforce forwards compatibility on users */
6221 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6222 return -EINVAL;
6223
6224 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6225 !io_op_defs[req->opcode].buffer_select)
6226 return -EOPNOTSUPP;
6227
6228 id = READ_ONCE(sqe->personality);
6229 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006230 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006231 req->work.creds = idr_find(&ctx->personality_idr, id);
6232 if (unlikely(!req->work.creds))
6233 return -EINVAL;
6234 get_cred(req->work.creds);
6235 }
6236
6237 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006238 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006239
Jens Axboe63ff8222020-05-07 14:56:15 -06006240 if (!io_op_defs[req->opcode].needs_file)
6241 return 0;
6242
6243 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006244}
6245
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006246static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006247 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006248{
Jens Axboeac8691c2020-06-01 08:30:41 -06006249 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006250 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006251 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006252
Jens Axboec4a2ed72019-11-21 21:01:26 -07006253 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006254 if (test_bit(0, &ctx->sq_check_overflow)) {
6255 if (!list_empty(&ctx->cq_overflow_list) &&
6256 !io_cqring_overflow_flush(ctx, false))
6257 return -EBUSY;
6258 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006259
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006260 /* make sure SQ entry isn't read before tail */
6261 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006262
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006263 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6264 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006265
Jens Axboe013538b2020-06-22 09:29:15 -06006266 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006267
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006268 ctx->ring_fd = ring_fd;
6269 ctx->ring_file = ring_file;
6270
Jens Axboe6c271ce2019-01-10 11:22:30 -07006271 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006272 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006273 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006274 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006275
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006276 sqe = io_get_sqe(ctx);
6277 if (unlikely(!sqe)) {
6278 io_consume_sqe(ctx);
6279 break;
6280 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006281 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006282 if (unlikely(!req)) {
6283 if (!submitted)
6284 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006285 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006286 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006287
Jens Axboeac8691c2020-06-01 08:30:41 -06006288 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006289 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006290 /* will complete beyond this point, count as submitted */
6291 submitted++;
6292
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006293 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006294fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006295 io_put_req(req);
6296 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006297 break;
6298 }
6299
Jens Axboe354420f2020-01-08 18:55:15 -07006300 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006301 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006302 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006303 if (err)
6304 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006305 }
6306
Pavel Begunkov9466f432020-01-25 22:34:01 +03006307 if (unlikely(submitted != nr)) {
6308 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6309
6310 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6311 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006312 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006313 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006314 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006315
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006316 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6317 io_commit_sqring(ctx);
6318
Jens Axboe6c271ce2019-01-10 11:22:30 -07006319 return submitted;
6320}
6321
6322static int io_sq_thread(void *data)
6323{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006324 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006325 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006326 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006327 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006328 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006329
Jens Axboe0f158b42020-05-14 17:18:39 -06006330 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006331
Jens Axboe181e4482019-11-25 08:52:30 -07006332 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006333
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006334 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006335 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006336 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006337
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006338 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006339 unsigned nr_events = 0;
6340
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006341 mutex_lock(&ctx->uring_lock);
6342 if (!list_empty(&ctx->poll_list))
6343 io_iopoll_getevents(ctx, &nr_events, 0);
6344 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006345 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006346 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006347 }
6348
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006349 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006350
6351 /*
6352 * If submit got -EBUSY, flag us as needing the application
6353 * to enter the kernel to reap and flush events.
6354 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006355 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006356 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006357 * Drop cur_mm before scheduling, we can't hold it for
6358 * long periods (or over schedule()). Do this before
6359 * adding ourselves to the waitqueue, as the unuse/drop
6360 * may sleep.
6361 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006362 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006363
6364 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006365 * We're polling. If we're within the defined idle
6366 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006367 * to sleep. The exception is if we got EBUSY doing
6368 * more IO, we should wait for the application to
6369 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006370 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006371 if (!list_empty(&ctx->poll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006372 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6373 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006374 if (current->task_works)
6375 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006376 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006377 continue;
6378 }
6379
Jens Axboe6c271ce2019-01-10 11:22:30 -07006380 prepare_to_wait(&ctx->sqo_wait, &wait,
6381 TASK_INTERRUPTIBLE);
6382
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006383 /*
6384 * While doing polled IO, before going to sleep, we need
6385 * to check if there are new reqs added to poll_list, it
6386 * is because reqs may have been punted to io worker and
6387 * will be added to poll_list later, hence check the
6388 * poll_list again.
6389 */
6390 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6391 !list_empty_careful(&ctx->poll_list)) {
6392 finish_wait(&ctx->sqo_wait, &wait);
6393 continue;
6394 }
6395
Jens Axboe6c271ce2019-01-10 11:22:30 -07006396 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006397 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006398 /* make sure to read SQ tail after writing flags */
6399 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006400
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006401 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006402 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006403 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006404 finish_wait(&ctx->sqo_wait, &wait);
6405 break;
6406 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006407 if (current->task_works) {
6408 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006409 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006410 continue;
6411 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006412 if (signal_pending(current))
6413 flush_signals(current);
6414 schedule();
6415 finish_wait(&ctx->sqo_wait, &wait);
6416
Hristo Venev75b28af2019-08-26 17:23:46 +00006417 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006418 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006419 continue;
6420 }
6421 finish_wait(&ctx->sqo_wait, &wait);
6422
Hristo Venev75b28af2019-08-26 17:23:46 +00006423 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006424 }
6425
Jens Axboe8a4955f2019-12-09 14:52:35 -07006426 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006427 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6428 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006429 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006430 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006431 }
6432
Jens Axboeb41e9852020-02-17 09:52:41 -07006433 if (current->task_works)
6434 task_work_run();
6435
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006436 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006437 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006438
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006439 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006440
Jens Axboe6c271ce2019-01-10 11:22:30 -07006441 return 0;
6442}
6443
Jens Axboebda52162019-09-24 13:47:15 -06006444struct io_wait_queue {
6445 struct wait_queue_entry wq;
6446 struct io_ring_ctx *ctx;
6447 unsigned to_wait;
6448 unsigned nr_timeouts;
6449};
6450
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006451static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006452{
6453 struct io_ring_ctx *ctx = iowq->ctx;
6454
6455 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006456 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006457 * started waiting. For timeouts, we always want to return to userspace,
6458 * regardless of event count.
6459 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006460 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006461 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6462}
6463
6464static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6465 int wake_flags, void *key)
6466{
6467 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6468 wq);
6469
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006470 /* use noflush == true, as we can't safely rely on locking context */
6471 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006472 return -1;
6473
6474 return autoremove_wake_function(curr, mode, wake_flags, key);
6475}
6476
Jens Axboe2b188cc2019-01-07 10:46:33 -07006477/*
6478 * Wait until events become available, if we don't already have some. The
6479 * application must reap them itself, as they reside on the shared cq ring.
6480 */
6481static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6482 const sigset_t __user *sig, size_t sigsz)
6483{
Jens Axboebda52162019-09-24 13:47:15 -06006484 struct io_wait_queue iowq = {
6485 .wq = {
6486 .private = current,
6487 .func = io_wake_function,
6488 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6489 },
6490 .ctx = ctx,
6491 .to_wait = min_events,
6492 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006493 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006494 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006495
Jens Axboeb41e9852020-02-17 09:52:41 -07006496 do {
6497 if (io_cqring_events(ctx, false) >= min_events)
6498 return 0;
6499 if (!current->task_works)
6500 break;
6501 task_work_run();
6502 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006503
6504 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006505#ifdef CONFIG_COMPAT
6506 if (in_compat_syscall())
6507 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006508 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006509 else
6510#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006511 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006512
Jens Axboe2b188cc2019-01-07 10:46:33 -07006513 if (ret)
6514 return ret;
6515 }
6516
Jens Axboebda52162019-09-24 13:47:15 -06006517 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006518 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006519 do {
6520 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6521 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006522 if (current->task_works)
6523 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006524 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006525 break;
6526 schedule();
6527 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006528 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006529 break;
6530 }
6531 } while (1);
6532 finish_wait(&ctx->wait, &iowq.wq);
6533
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006534 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006535
Hristo Venev75b28af2019-08-26 17:23:46 +00006536 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006537}
6538
Jens Axboe6b063142019-01-10 22:13:58 -07006539static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6540{
6541#if defined(CONFIG_UNIX)
6542 if (ctx->ring_sock) {
6543 struct sock *sock = ctx->ring_sock->sk;
6544 struct sk_buff *skb;
6545
6546 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6547 kfree_skb(skb);
6548 }
6549#else
6550 int i;
6551
Jens Axboe65e19f52019-10-26 07:20:21 -06006552 for (i = 0; i < ctx->nr_user_files; i++) {
6553 struct file *file;
6554
6555 file = io_file_from_index(ctx, i);
6556 if (file)
6557 fput(file);
6558 }
Jens Axboe6b063142019-01-10 22:13:58 -07006559#endif
6560}
6561
Jens Axboe05f3fb32019-12-09 11:22:50 -07006562static void io_file_ref_kill(struct percpu_ref *ref)
6563{
6564 struct fixed_file_data *data;
6565
6566 data = container_of(ref, struct fixed_file_data, refs);
6567 complete(&data->done);
6568}
6569
Jens Axboe6b063142019-01-10 22:13:58 -07006570static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6571{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006572 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006573 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006574 unsigned nr_tables, i;
6575
Jens Axboe05f3fb32019-12-09 11:22:50 -07006576 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006577 return -ENXIO;
6578
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006579 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006580 if (!list_empty(&data->ref_list))
6581 ref_node = list_first_entry(&data->ref_list,
6582 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006583 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006584 if (ref_node)
6585 percpu_ref_kill(&ref_node->refs);
6586
6587 percpu_ref_kill(&data->refs);
6588
6589 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006590 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006591 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006592
Jens Axboe6b063142019-01-10 22:13:58 -07006593 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006594 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6595 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006596 kfree(data->table[i].files);
6597 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006598 percpu_ref_exit(&data->refs);
6599 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006600 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006601 ctx->nr_user_files = 0;
6602 return 0;
6603}
6604
Jens Axboe6c271ce2019-01-10 11:22:30 -07006605static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6606{
6607 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006608 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006609 /*
6610 * The park is a bit of a work-around, without it we get
6611 * warning spews on shutdown with SQPOLL set and affinity
6612 * set to a single CPU.
6613 */
Jens Axboe06058632019-04-13 09:26:03 -06006614 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006615 kthread_stop(ctx->sqo_thread);
6616 ctx->sqo_thread = NULL;
6617 }
6618}
6619
Jens Axboe6b063142019-01-10 22:13:58 -07006620static void io_finish_async(struct io_ring_ctx *ctx)
6621{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006622 io_sq_thread_stop(ctx);
6623
Jens Axboe561fb042019-10-24 07:25:42 -06006624 if (ctx->io_wq) {
6625 io_wq_destroy(ctx->io_wq);
6626 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006627 }
6628}
6629
6630#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006631/*
6632 * Ensure the UNIX gc is aware of our file set, so we are certain that
6633 * the io_uring can be safely unregistered on process exit, even if we have
6634 * loops in the file referencing.
6635 */
6636static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6637{
6638 struct sock *sk = ctx->ring_sock->sk;
6639 struct scm_fp_list *fpl;
6640 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006641 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006642
Jens Axboe6b063142019-01-10 22:13:58 -07006643 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6644 if (!fpl)
6645 return -ENOMEM;
6646
6647 skb = alloc_skb(0, GFP_KERNEL);
6648 if (!skb) {
6649 kfree(fpl);
6650 return -ENOMEM;
6651 }
6652
6653 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006654
Jens Axboe08a45172019-10-03 08:11:03 -06006655 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006656 fpl->user = get_uid(ctx->user);
6657 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006658 struct file *file = io_file_from_index(ctx, i + offset);
6659
6660 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006661 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006662 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006663 unix_inflight(fpl->user, fpl->fp[nr_files]);
6664 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006665 }
6666
Jens Axboe08a45172019-10-03 08:11:03 -06006667 if (nr_files) {
6668 fpl->max = SCM_MAX_FD;
6669 fpl->count = nr_files;
6670 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006671 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006672 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6673 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006674
Jens Axboe08a45172019-10-03 08:11:03 -06006675 for (i = 0; i < nr_files; i++)
6676 fput(fpl->fp[i]);
6677 } else {
6678 kfree_skb(skb);
6679 kfree(fpl);
6680 }
Jens Axboe6b063142019-01-10 22:13:58 -07006681
6682 return 0;
6683}
6684
6685/*
6686 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6687 * causes regular reference counting to break down. We rely on the UNIX
6688 * garbage collection to take care of this problem for us.
6689 */
6690static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6691{
6692 unsigned left, total;
6693 int ret = 0;
6694
6695 total = 0;
6696 left = ctx->nr_user_files;
6697 while (left) {
6698 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006699
6700 ret = __io_sqe_files_scm(ctx, this_files, total);
6701 if (ret)
6702 break;
6703 left -= this_files;
6704 total += this_files;
6705 }
6706
6707 if (!ret)
6708 return 0;
6709
6710 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006711 struct file *file = io_file_from_index(ctx, total);
6712
6713 if (file)
6714 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006715 total++;
6716 }
6717
6718 return ret;
6719}
6720#else
6721static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6722{
6723 return 0;
6724}
6725#endif
6726
Jens Axboe65e19f52019-10-26 07:20:21 -06006727static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6728 unsigned nr_files)
6729{
6730 int i;
6731
6732 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006733 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006734 unsigned this_files;
6735
6736 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6737 table->files = kcalloc(this_files, sizeof(struct file *),
6738 GFP_KERNEL);
6739 if (!table->files)
6740 break;
6741 nr_files -= this_files;
6742 }
6743
6744 if (i == nr_tables)
6745 return 0;
6746
6747 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006748 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006749 kfree(table->files);
6750 }
6751 return 1;
6752}
6753
Jens Axboe05f3fb32019-12-09 11:22:50 -07006754static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006755{
6756#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006757 struct sock *sock = ctx->ring_sock->sk;
6758 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6759 struct sk_buff *skb;
6760 int i;
6761
6762 __skb_queue_head_init(&list);
6763
6764 /*
6765 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6766 * remove this entry and rearrange the file array.
6767 */
6768 skb = skb_dequeue(head);
6769 while (skb) {
6770 struct scm_fp_list *fp;
6771
6772 fp = UNIXCB(skb).fp;
6773 for (i = 0; i < fp->count; i++) {
6774 int left;
6775
6776 if (fp->fp[i] != file)
6777 continue;
6778
6779 unix_notinflight(fp->user, fp->fp[i]);
6780 left = fp->count - 1 - i;
6781 if (left) {
6782 memmove(&fp->fp[i], &fp->fp[i + 1],
6783 left * sizeof(struct file *));
6784 }
6785 fp->count--;
6786 if (!fp->count) {
6787 kfree_skb(skb);
6788 skb = NULL;
6789 } else {
6790 __skb_queue_tail(&list, skb);
6791 }
6792 fput(file);
6793 file = NULL;
6794 break;
6795 }
6796
6797 if (!file)
6798 break;
6799
6800 __skb_queue_tail(&list, skb);
6801
6802 skb = skb_dequeue(head);
6803 }
6804
6805 if (skb_peek(&list)) {
6806 spin_lock_irq(&head->lock);
6807 while ((skb = __skb_dequeue(&list)) != NULL)
6808 __skb_queue_tail(head, skb);
6809 spin_unlock_irq(&head->lock);
6810 }
6811#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006812 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006813#endif
6814}
6815
Jens Axboe05f3fb32019-12-09 11:22:50 -07006816struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006817 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006818 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006819};
6820
Jens Axboe4a38aed22020-05-14 17:21:15 -06006821static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006822{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006823 struct fixed_file_data *file_data = ref_node->file_data;
6824 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006825 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006826
6827 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006828 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006829 io_ring_file_put(ctx, pfile->file);
6830 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006831 }
6832
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006833 spin_lock(&file_data->lock);
6834 list_del(&ref_node->node);
6835 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006836
Xiaoguang Wang05589552020-03-31 14:05:18 +08006837 percpu_ref_exit(&ref_node->refs);
6838 kfree(ref_node);
6839 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006840}
6841
Jens Axboe4a38aed22020-05-14 17:21:15 -06006842static void io_file_put_work(struct work_struct *work)
6843{
6844 struct io_ring_ctx *ctx;
6845 struct llist_node *node;
6846
6847 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6848 node = llist_del_all(&ctx->file_put_llist);
6849
6850 while (node) {
6851 struct fixed_file_ref_node *ref_node;
6852 struct llist_node *next = node->next;
6853
6854 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6855 __io_file_put_work(ref_node);
6856 node = next;
6857 }
6858}
6859
Jens Axboe05f3fb32019-12-09 11:22:50 -07006860static void io_file_data_ref_zero(struct percpu_ref *ref)
6861{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006862 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006863 struct io_ring_ctx *ctx;
6864 bool first_add;
6865 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006866
Xiaoguang Wang05589552020-03-31 14:05:18 +08006867 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006868 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006869
Jens Axboe4a38aed22020-05-14 17:21:15 -06006870 if (percpu_ref_is_dying(&ctx->file_data->refs))
6871 delay = 0;
6872
6873 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6874 if (!delay)
6875 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6876 else if (first_add)
6877 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006878}
6879
6880static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6881 struct io_ring_ctx *ctx)
6882{
6883 struct fixed_file_ref_node *ref_node;
6884
6885 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6886 if (!ref_node)
6887 return ERR_PTR(-ENOMEM);
6888
6889 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6890 0, GFP_KERNEL)) {
6891 kfree(ref_node);
6892 return ERR_PTR(-ENOMEM);
6893 }
6894 INIT_LIST_HEAD(&ref_node->node);
6895 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006896 ref_node->file_data = ctx->file_data;
6897 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006898}
6899
6900static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6901{
6902 percpu_ref_exit(&ref_node->refs);
6903 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006904}
6905
6906static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6907 unsigned nr_args)
6908{
6909 __s32 __user *fds = (__s32 __user *) arg;
6910 unsigned nr_tables;
6911 struct file *file;
6912 int fd, ret = 0;
6913 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006914 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006915
6916 if (ctx->file_data)
6917 return -EBUSY;
6918 if (!nr_args)
6919 return -EINVAL;
6920 if (nr_args > IORING_MAX_FIXED_FILES)
6921 return -EMFILE;
6922
6923 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6924 if (!ctx->file_data)
6925 return -ENOMEM;
6926 ctx->file_data->ctx = ctx;
6927 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006928 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006929 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006930
6931 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6932 ctx->file_data->table = kcalloc(nr_tables,
6933 sizeof(struct fixed_file_table),
6934 GFP_KERNEL);
6935 if (!ctx->file_data->table) {
6936 kfree(ctx->file_data);
6937 ctx->file_data = NULL;
6938 return -ENOMEM;
6939 }
6940
Xiaoguang Wang05589552020-03-31 14:05:18 +08006941 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006942 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6943 kfree(ctx->file_data->table);
6944 kfree(ctx->file_data);
6945 ctx->file_data = NULL;
6946 return -ENOMEM;
6947 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006948
6949 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6950 percpu_ref_exit(&ctx->file_data->refs);
6951 kfree(ctx->file_data->table);
6952 kfree(ctx->file_data);
6953 ctx->file_data = NULL;
6954 return -ENOMEM;
6955 }
6956
6957 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6958 struct fixed_file_table *table;
6959 unsigned index;
6960
6961 ret = -EFAULT;
6962 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6963 break;
6964 /* allow sparse sets */
6965 if (fd == -1) {
6966 ret = 0;
6967 continue;
6968 }
6969
6970 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6971 index = i & IORING_FILE_TABLE_MASK;
6972 file = fget(fd);
6973
6974 ret = -EBADF;
6975 if (!file)
6976 break;
6977
6978 /*
6979 * Don't allow io_uring instances to be registered. If UNIX
6980 * isn't enabled, then this causes a reference cycle and this
6981 * instance can never get freed. If UNIX is enabled we'll
6982 * handle it just fine, but there's still no point in allowing
6983 * a ring fd as it doesn't support regular read/write anyway.
6984 */
6985 if (file->f_op == &io_uring_fops) {
6986 fput(file);
6987 break;
6988 }
6989 ret = 0;
6990 table->files[index] = file;
6991 }
6992
6993 if (ret) {
6994 for (i = 0; i < ctx->nr_user_files; i++) {
6995 file = io_file_from_index(ctx, i);
6996 if (file)
6997 fput(file);
6998 }
6999 for (i = 0; i < nr_tables; i++)
7000 kfree(ctx->file_data->table[i].files);
7001
7002 kfree(ctx->file_data->table);
7003 kfree(ctx->file_data);
7004 ctx->file_data = NULL;
7005 ctx->nr_user_files = 0;
7006 return ret;
7007 }
7008
7009 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007010 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007011 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007012 return ret;
7013 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007014
Xiaoguang Wang05589552020-03-31 14:05:18 +08007015 ref_node = alloc_fixed_file_ref_node(ctx);
7016 if (IS_ERR(ref_node)) {
7017 io_sqe_files_unregister(ctx);
7018 return PTR_ERR(ref_node);
7019 }
7020
7021 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007022 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007023 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007024 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007025 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007026 return ret;
7027}
7028
Jens Axboec3a31e62019-10-03 13:59:56 -06007029static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7030 int index)
7031{
7032#if defined(CONFIG_UNIX)
7033 struct sock *sock = ctx->ring_sock->sk;
7034 struct sk_buff_head *head = &sock->sk_receive_queue;
7035 struct sk_buff *skb;
7036
7037 /*
7038 * See if we can merge this file into an existing skb SCM_RIGHTS
7039 * file set. If there's no room, fall back to allocating a new skb
7040 * and filling it in.
7041 */
7042 spin_lock_irq(&head->lock);
7043 skb = skb_peek(head);
7044 if (skb) {
7045 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7046
7047 if (fpl->count < SCM_MAX_FD) {
7048 __skb_unlink(skb, head);
7049 spin_unlock_irq(&head->lock);
7050 fpl->fp[fpl->count] = get_file(file);
7051 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7052 fpl->count++;
7053 spin_lock_irq(&head->lock);
7054 __skb_queue_head(head, skb);
7055 } else {
7056 skb = NULL;
7057 }
7058 }
7059 spin_unlock_irq(&head->lock);
7060
7061 if (skb) {
7062 fput(file);
7063 return 0;
7064 }
7065
7066 return __io_sqe_files_scm(ctx, 1, index);
7067#else
7068 return 0;
7069#endif
7070}
7071
Hillf Dantona5318d32020-03-23 17:47:15 +08007072static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007073 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007074{
Hillf Dantona5318d32020-03-23 17:47:15 +08007075 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007076 struct percpu_ref *refs = data->cur_refs;
7077 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007078
Jens Axboe05f3fb32019-12-09 11:22:50 -07007079 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007080 if (!pfile)
7081 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007082
Xiaoguang Wang05589552020-03-31 14:05:18 +08007083 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007084 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007085 list_add(&pfile->list, &ref_node->file_list);
7086
Hillf Dantona5318d32020-03-23 17:47:15 +08007087 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007088}
7089
7090static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7091 struct io_uring_files_update *up,
7092 unsigned nr_args)
7093{
7094 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007095 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007096 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007097 __s32 __user *fds;
7098 int fd, i, err;
7099 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007100 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007101
Jens Axboe05f3fb32019-12-09 11:22:50 -07007102 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007103 return -EOVERFLOW;
7104 if (done > ctx->nr_user_files)
7105 return -EINVAL;
7106
Xiaoguang Wang05589552020-03-31 14:05:18 +08007107 ref_node = alloc_fixed_file_ref_node(ctx);
7108 if (IS_ERR(ref_node))
7109 return PTR_ERR(ref_node);
7110
Jens Axboec3a31e62019-10-03 13:59:56 -06007111 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007112 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007113 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007114 struct fixed_file_table *table;
7115 unsigned index;
7116
Jens Axboec3a31e62019-10-03 13:59:56 -06007117 err = 0;
7118 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7119 err = -EFAULT;
7120 break;
7121 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007122 i = array_index_nospec(up->offset, ctx->nr_user_files);
7123 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007124 index = i & IORING_FILE_TABLE_MASK;
7125 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007126 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007127 err = io_queue_file_removal(data, file);
7128 if (err)
7129 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007130 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007131 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007132 }
7133 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007134 file = fget(fd);
7135 if (!file) {
7136 err = -EBADF;
7137 break;
7138 }
7139 /*
7140 * Don't allow io_uring instances to be registered. If
7141 * UNIX isn't enabled, then this causes a reference
7142 * cycle and this instance can never get freed. If UNIX
7143 * is enabled we'll handle it just fine, but there's
7144 * still no point in allowing a ring fd as it doesn't
7145 * support regular read/write anyway.
7146 */
7147 if (file->f_op == &io_uring_fops) {
7148 fput(file);
7149 err = -EBADF;
7150 break;
7151 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007152 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007153 err = io_sqe_file_register(ctx, file, i);
7154 if (err)
7155 break;
7156 }
7157 nr_args--;
7158 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007159 up->offset++;
7160 }
7161
Xiaoguang Wang05589552020-03-31 14:05:18 +08007162 if (needs_switch) {
7163 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007164 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007165 list_add(&ref_node->node, &data->ref_list);
7166 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007167 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007168 percpu_ref_get(&ctx->file_data->refs);
7169 } else
7170 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007171
7172 return done ? done : err;
7173}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007174
Jens Axboe05f3fb32019-12-09 11:22:50 -07007175static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7176 unsigned nr_args)
7177{
7178 struct io_uring_files_update up;
7179
7180 if (!ctx->file_data)
7181 return -ENXIO;
7182 if (!nr_args)
7183 return -EINVAL;
7184 if (copy_from_user(&up, arg, sizeof(up)))
7185 return -EFAULT;
7186 if (up.resv)
7187 return -EINVAL;
7188
7189 return __io_sqe_files_update(ctx, &up, nr_args);
7190}
Jens Axboec3a31e62019-10-03 13:59:56 -06007191
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007192static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007193{
7194 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7195
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007196 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007197 io_put_req(req);
7198}
7199
Pavel Begunkov24369c22020-01-28 03:15:48 +03007200static int io_init_wq_offload(struct io_ring_ctx *ctx,
7201 struct io_uring_params *p)
7202{
7203 struct io_wq_data data;
7204 struct fd f;
7205 struct io_ring_ctx *ctx_attach;
7206 unsigned int concurrency;
7207 int ret = 0;
7208
7209 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007210 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007211 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007212
7213 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7214 /* Do QD, or 4 * CPUS, whatever is smallest */
7215 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7216
7217 ctx->io_wq = io_wq_create(concurrency, &data);
7218 if (IS_ERR(ctx->io_wq)) {
7219 ret = PTR_ERR(ctx->io_wq);
7220 ctx->io_wq = NULL;
7221 }
7222 return ret;
7223 }
7224
7225 f = fdget(p->wq_fd);
7226 if (!f.file)
7227 return -EBADF;
7228
7229 if (f.file->f_op != &io_uring_fops) {
7230 ret = -EINVAL;
7231 goto out_fput;
7232 }
7233
7234 ctx_attach = f.file->private_data;
7235 /* @io_wq is protected by holding the fd */
7236 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7237 ret = -EINVAL;
7238 goto out_fput;
7239 }
7240
7241 ctx->io_wq = ctx_attach->io_wq;
7242out_fput:
7243 fdput(f);
7244 return ret;
7245}
7246
Jens Axboe6c271ce2019-01-10 11:22:30 -07007247static int io_sq_offload_start(struct io_ring_ctx *ctx,
7248 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007249{
7250 int ret;
7251
7252 mmgrab(current->mm);
7253 ctx->sqo_mm = current->mm;
7254
Jens Axboe6c271ce2019-01-10 11:22:30 -07007255 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007256 ret = -EPERM;
7257 if (!capable(CAP_SYS_ADMIN))
7258 goto err;
7259
Jens Axboe917257d2019-04-13 09:28:55 -06007260 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7261 if (!ctx->sq_thread_idle)
7262 ctx->sq_thread_idle = HZ;
7263
Jens Axboe6c271ce2019-01-10 11:22:30 -07007264 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007265 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007266
Jens Axboe917257d2019-04-13 09:28:55 -06007267 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007268 if (cpu >= nr_cpu_ids)
7269 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007270 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007271 goto err;
7272
Jens Axboe6c271ce2019-01-10 11:22:30 -07007273 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7274 ctx, cpu,
7275 "io_uring-sq");
7276 } else {
7277 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7278 "io_uring-sq");
7279 }
7280 if (IS_ERR(ctx->sqo_thread)) {
7281 ret = PTR_ERR(ctx->sqo_thread);
7282 ctx->sqo_thread = NULL;
7283 goto err;
7284 }
7285 wake_up_process(ctx->sqo_thread);
7286 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7287 /* Can't have SQ_AFF without SQPOLL */
7288 ret = -EINVAL;
7289 goto err;
7290 }
7291
Pavel Begunkov24369c22020-01-28 03:15:48 +03007292 ret = io_init_wq_offload(ctx, p);
7293 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007294 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007295
7296 return 0;
7297err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007298 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007299 mmdrop(ctx->sqo_mm);
7300 ctx->sqo_mm = NULL;
7301 return ret;
7302}
7303
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007304static inline void __io_unaccount_mem(struct user_struct *user,
7305 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007306{
7307 atomic_long_sub(nr_pages, &user->locked_vm);
7308}
7309
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007310static inline int __io_account_mem(struct user_struct *user,
7311 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007312{
7313 unsigned long page_limit, cur_pages, new_pages;
7314
7315 /* Don't allow more pages than we can safely lock */
7316 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7317
7318 do {
7319 cur_pages = atomic_long_read(&user->locked_vm);
7320 new_pages = cur_pages + nr_pages;
7321 if (new_pages > page_limit)
7322 return -ENOMEM;
7323 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7324 new_pages) != cur_pages);
7325
7326 return 0;
7327}
7328
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007329static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7330 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007331{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007332 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007333 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007334
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007335 if (ctx->sqo_mm) {
7336 if (acct == ACCT_LOCKED)
7337 ctx->sqo_mm->locked_vm -= nr_pages;
7338 else if (acct == ACCT_PINNED)
7339 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7340 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007341}
7342
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007343static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7344 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007345{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007346 int ret;
7347
7348 if (ctx->limit_mem) {
7349 ret = __io_account_mem(ctx->user, nr_pages);
7350 if (ret)
7351 return ret;
7352 }
7353
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007354 if (ctx->sqo_mm) {
7355 if (acct == ACCT_LOCKED)
7356 ctx->sqo_mm->locked_vm += nr_pages;
7357 else if (acct == ACCT_PINNED)
7358 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7359 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007360
7361 return 0;
7362}
7363
Jens Axboe2b188cc2019-01-07 10:46:33 -07007364static void io_mem_free(void *ptr)
7365{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007366 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007367
Mark Rutland52e04ef2019-04-30 17:30:21 +01007368 if (!ptr)
7369 return;
7370
7371 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007372 if (put_page_testzero(page))
7373 free_compound_page(page);
7374}
7375
7376static void *io_mem_alloc(size_t size)
7377{
7378 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7379 __GFP_NORETRY;
7380
7381 return (void *) __get_free_pages(gfp_flags, get_order(size));
7382}
7383
Hristo Venev75b28af2019-08-26 17:23:46 +00007384static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7385 size_t *sq_offset)
7386{
7387 struct io_rings *rings;
7388 size_t off, sq_array_size;
7389
7390 off = struct_size(rings, cqes, cq_entries);
7391 if (off == SIZE_MAX)
7392 return SIZE_MAX;
7393
7394#ifdef CONFIG_SMP
7395 off = ALIGN(off, SMP_CACHE_BYTES);
7396 if (off == 0)
7397 return SIZE_MAX;
7398#endif
7399
7400 sq_array_size = array_size(sizeof(u32), sq_entries);
7401 if (sq_array_size == SIZE_MAX)
7402 return SIZE_MAX;
7403
7404 if (check_add_overflow(off, sq_array_size, &off))
7405 return SIZE_MAX;
7406
7407 if (sq_offset)
7408 *sq_offset = off;
7409
7410 return off;
7411}
7412
Jens Axboe2b188cc2019-01-07 10:46:33 -07007413static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7414{
Hristo Venev75b28af2019-08-26 17:23:46 +00007415 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007416
Hristo Venev75b28af2019-08-26 17:23:46 +00007417 pages = (size_t)1 << get_order(
7418 rings_size(sq_entries, cq_entries, NULL));
7419 pages += (size_t)1 << get_order(
7420 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007421
Hristo Venev75b28af2019-08-26 17:23:46 +00007422 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007423}
7424
Jens Axboeedafcce2019-01-09 09:16:05 -07007425static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7426{
7427 int i, j;
7428
7429 if (!ctx->user_bufs)
7430 return -ENXIO;
7431
7432 for (i = 0; i < ctx->nr_user_bufs; i++) {
7433 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7434
7435 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007436 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007437
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007438 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007439 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007440 imu->nr_bvecs = 0;
7441 }
7442
7443 kfree(ctx->user_bufs);
7444 ctx->user_bufs = NULL;
7445 ctx->nr_user_bufs = 0;
7446 return 0;
7447}
7448
7449static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7450 void __user *arg, unsigned index)
7451{
7452 struct iovec __user *src;
7453
7454#ifdef CONFIG_COMPAT
7455 if (ctx->compat) {
7456 struct compat_iovec __user *ciovs;
7457 struct compat_iovec ciov;
7458
7459 ciovs = (struct compat_iovec __user *) arg;
7460 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7461 return -EFAULT;
7462
Jens Axboed55e5f52019-12-11 16:12:15 -07007463 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007464 dst->iov_len = ciov.iov_len;
7465 return 0;
7466 }
7467#endif
7468 src = (struct iovec __user *) arg;
7469 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7470 return -EFAULT;
7471 return 0;
7472}
7473
7474static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7475 unsigned nr_args)
7476{
7477 struct vm_area_struct **vmas = NULL;
7478 struct page **pages = NULL;
7479 int i, j, got_pages = 0;
7480 int ret = -EINVAL;
7481
7482 if (ctx->user_bufs)
7483 return -EBUSY;
7484 if (!nr_args || nr_args > UIO_MAXIOV)
7485 return -EINVAL;
7486
7487 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7488 GFP_KERNEL);
7489 if (!ctx->user_bufs)
7490 return -ENOMEM;
7491
7492 for (i = 0; i < nr_args; i++) {
7493 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7494 unsigned long off, start, end, ubuf;
7495 int pret, nr_pages;
7496 struct iovec iov;
7497 size_t size;
7498
7499 ret = io_copy_iov(ctx, &iov, arg, i);
7500 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007501 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007502
7503 /*
7504 * Don't impose further limits on the size and buffer
7505 * constraints here, we'll -EINVAL later when IO is
7506 * submitted if they are wrong.
7507 */
7508 ret = -EFAULT;
7509 if (!iov.iov_base || !iov.iov_len)
7510 goto err;
7511
7512 /* arbitrary limit, but we need something */
7513 if (iov.iov_len > SZ_1G)
7514 goto err;
7515
7516 ubuf = (unsigned long) iov.iov_base;
7517 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7518 start = ubuf >> PAGE_SHIFT;
7519 nr_pages = end - start;
7520
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007521 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007522 if (ret)
7523 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007524
7525 ret = 0;
7526 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007527 kvfree(vmas);
7528 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007529 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007530 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007531 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007532 sizeof(struct vm_area_struct *),
7533 GFP_KERNEL);
7534 if (!pages || !vmas) {
7535 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007536 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007537 goto err;
7538 }
7539 got_pages = nr_pages;
7540 }
7541
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007542 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007543 GFP_KERNEL);
7544 ret = -ENOMEM;
7545 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007546 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007547 goto err;
7548 }
7549
7550 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007551 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007552 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007553 FOLL_WRITE | FOLL_LONGTERM,
7554 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007555 if (pret == nr_pages) {
7556 /* don't support file backed memory */
7557 for (j = 0; j < nr_pages; j++) {
7558 struct vm_area_struct *vma = vmas[j];
7559
7560 if (vma->vm_file &&
7561 !is_file_hugepages(vma->vm_file)) {
7562 ret = -EOPNOTSUPP;
7563 break;
7564 }
7565 }
7566 } else {
7567 ret = pret < 0 ? pret : -EFAULT;
7568 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007569 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007570 if (ret) {
7571 /*
7572 * if we did partial map, or found file backed vmas,
7573 * release any pages we did get
7574 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007575 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007576 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007577 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007578 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007579 goto err;
7580 }
7581
7582 off = ubuf & ~PAGE_MASK;
7583 size = iov.iov_len;
7584 for (j = 0; j < nr_pages; j++) {
7585 size_t vec_len;
7586
7587 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7588 imu->bvec[j].bv_page = pages[j];
7589 imu->bvec[j].bv_len = vec_len;
7590 imu->bvec[j].bv_offset = off;
7591 off = 0;
7592 size -= vec_len;
7593 }
7594 /* store original address for later verification */
7595 imu->ubuf = ubuf;
7596 imu->len = iov.iov_len;
7597 imu->nr_bvecs = nr_pages;
7598
7599 ctx->nr_user_bufs++;
7600 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007601 kvfree(pages);
7602 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007603 return 0;
7604err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007605 kvfree(pages);
7606 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007607 io_sqe_buffer_unregister(ctx);
7608 return ret;
7609}
7610
Jens Axboe9b402842019-04-11 11:45:41 -06007611static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7612{
7613 __s32 __user *fds = arg;
7614 int fd;
7615
7616 if (ctx->cq_ev_fd)
7617 return -EBUSY;
7618
7619 if (copy_from_user(&fd, fds, sizeof(*fds)))
7620 return -EFAULT;
7621
7622 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7623 if (IS_ERR(ctx->cq_ev_fd)) {
7624 int ret = PTR_ERR(ctx->cq_ev_fd);
7625 ctx->cq_ev_fd = NULL;
7626 return ret;
7627 }
7628
7629 return 0;
7630}
7631
7632static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7633{
7634 if (ctx->cq_ev_fd) {
7635 eventfd_ctx_put(ctx->cq_ev_fd);
7636 ctx->cq_ev_fd = NULL;
7637 return 0;
7638 }
7639
7640 return -ENXIO;
7641}
7642
Jens Axboe5a2e7452020-02-23 16:23:11 -07007643static int __io_destroy_buffers(int id, void *p, void *data)
7644{
7645 struct io_ring_ctx *ctx = data;
7646 struct io_buffer *buf = p;
7647
Jens Axboe067524e2020-03-02 16:32:28 -07007648 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007649 return 0;
7650}
7651
7652static void io_destroy_buffers(struct io_ring_ctx *ctx)
7653{
7654 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7655 idr_destroy(&ctx->io_buffer_idr);
7656}
7657
Jens Axboe2b188cc2019-01-07 10:46:33 -07007658static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7659{
Jens Axboe6b063142019-01-10 22:13:58 -07007660 io_finish_async(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007661 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007662 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007663 ctx->sqo_mm = NULL;
7664 }
Jens Axboedef596e2019-01-09 08:59:42 -07007665
7666 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007667 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007668 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007669 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007670 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007671 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007672
Jens Axboe2b188cc2019-01-07 10:46:33 -07007673#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007674 if (ctx->ring_sock) {
7675 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007676 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007677 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007678#endif
7679
Hristo Venev75b28af2019-08-26 17:23:46 +00007680 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007681 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007682
7683 percpu_ref_exit(&ctx->refs);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007684 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7685 ACCT_LOCKED);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007686 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007687 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007688 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007689 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007690 kfree(ctx);
7691}
7692
7693static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7694{
7695 struct io_ring_ctx *ctx = file->private_data;
7696 __poll_t mask = 0;
7697
7698 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007699 /*
7700 * synchronizes with barrier from wq_has_sleeper call in
7701 * io_commit_cqring
7702 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007703 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007704 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7705 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007706 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007707 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007708 mask |= EPOLLIN | EPOLLRDNORM;
7709
7710 return mask;
7711}
7712
7713static int io_uring_fasync(int fd, struct file *file, int on)
7714{
7715 struct io_ring_ctx *ctx = file->private_data;
7716
7717 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7718}
7719
Jens Axboe071698e2020-01-28 10:04:42 -07007720static int io_remove_personalities(int id, void *p, void *data)
7721{
7722 struct io_ring_ctx *ctx = data;
7723 const struct cred *cred;
7724
7725 cred = idr_remove(&ctx->personality_idr, id);
7726 if (cred)
7727 put_cred(cred);
7728 return 0;
7729}
7730
Jens Axboe85faa7b2020-04-09 18:14:00 -06007731static void io_ring_exit_work(struct work_struct *work)
7732{
7733 struct io_ring_ctx *ctx;
7734
7735 ctx = container_of(work, struct io_ring_ctx, exit_work);
7736 if (ctx->rings)
7737 io_cqring_overflow_flush(ctx, true);
7738
Jens Axboe56952e92020-06-17 15:00:04 -06007739 /*
7740 * If we're doing polled IO and end up having requests being
7741 * submitted async (out-of-line), then completions can come in while
7742 * we're waiting for refs to drop. We need to reap these manually,
7743 * as nobody else will be looking for them.
7744 */
7745 while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7746 io_iopoll_reap_events(ctx);
7747 if (ctx->rings)
7748 io_cqring_overflow_flush(ctx, true);
7749 }
Jens Axboe85faa7b2020-04-09 18:14:00 -06007750 io_ring_ctx_free(ctx);
7751}
7752
Jens Axboe2b188cc2019-01-07 10:46:33 -07007753static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7754{
7755 mutex_lock(&ctx->uring_lock);
7756 percpu_ref_kill(&ctx->refs);
7757 mutex_unlock(&ctx->uring_lock);
7758
Jens Axboe5262f562019-09-17 12:26:57 -06007759 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007760 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007761
7762 if (ctx->io_wq)
7763 io_wq_cancel_all(ctx->io_wq);
7764
Jens Axboedef596e2019-01-09 08:59:42 -07007765 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007766 /* if we failed setting up the ctx, we might not have any rings */
7767 if (ctx->rings)
7768 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007769 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007770 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7771 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007772}
7773
7774static int io_uring_release(struct inode *inode, struct file *file)
7775{
7776 struct io_ring_ctx *ctx = file->private_data;
7777
7778 file->private_data = NULL;
7779 io_ring_ctx_wait_and_kill(ctx);
7780 return 0;
7781}
7782
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007783static bool io_wq_files_match(struct io_wq_work *work, void *data)
7784{
7785 struct files_struct *files = data;
7786
7787 return work->files == files;
7788}
7789
Jens Axboefcb323c2019-10-24 12:39:47 -06007790static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7791 struct files_struct *files)
7792{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007793 if (list_empty_careful(&ctx->inflight_list))
7794 return;
7795
7796 /* cancel all at once, should be faster than doing it one by one*/
7797 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7798
Jens Axboefcb323c2019-10-24 12:39:47 -06007799 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007800 struct io_kiocb *cancel_req = NULL, *req;
7801 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007802
7803 spin_lock_irq(&ctx->inflight_lock);
7804 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007805 if (req->work.files != files)
7806 continue;
7807 /* req is being completed, ignore */
7808 if (!refcount_inc_not_zero(&req->refs))
7809 continue;
7810 cancel_req = req;
7811 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007812 }
Jens Axboe768134d2019-11-10 20:30:53 -07007813 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007814 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007815 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007816 spin_unlock_irq(&ctx->inflight_lock);
7817
Jens Axboe768134d2019-11-10 20:30:53 -07007818 /* We need to keep going until we don't find a matching req */
7819 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007820 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007821
Jens Axboe2ca10252020-02-13 17:17:35 -07007822 if (cancel_req->flags & REQ_F_OVERFLOW) {
7823 spin_lock_irq(&ctx->completion_lock);
7824 list_del(&cancel_req->list);
7825 cancel_req->flags &= ~REQ_F_OVERFLOW;
7826 if (list_empty(&ctx->cq_overflow_list)) {
7827 clear_bit(0, &ctx->sq_check_overflow);
7828 clear_bit(0, &ctx->cq_check_overflow);
7829 }
7830 spin_unlock_irq(&ctx->completion_lock);
7831
7832 WRITE_ONCE(ctx->rings->cq_overflow,
7833 atomic_inc_return(&ctx->cached_cq_overflow));
7834
7835 /*
7836 * Put inflight ref and overflow ref. If that's
7837 * all we had, then we're done with this request.
7838 */
7839 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007840 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007841 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007842 continue;
7843 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007844 } else {
7845 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7846 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007847 }
7848
Jens Axboefcb323c2019-10-24 12:39:47 -06007849 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007850 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007851 }
7852}
7853
Pavel Begunkov801dd572020-06-15 10:33:14 +03007854static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007855{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007856 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7857 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007858
Pavel Begunkov801dd572020-06-15 10:33:14 +03007859 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007860}
7861
Jens Axboefcb323c2019-10-24 12:39:47 -06007862static int io_uring_flush(struct file *file, void *data)
7863{
7864 struct io_ring_ctx *ctx = file->private_data;
7865
7866 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007867
7868 /*
7869 * If the task is going away, cancel work it may have pending
7870 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007871 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7872 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007873
Jens Axboefcb323c2019-10-24 12:39:47 -06007874 return 0;
7875}
7876
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007877static void *io_uring_validate_mmap_request(struct file *file,
7878 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007879{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007880 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007881 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007882 struct page *page;
7883 void *ptr;
7884
7885 switch (offset) {
7886 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007887 case IORING_OFF_CQ_RING:
7888 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007889 break;
7890 case IORING_OFF_SQES:
7891 ptr = ctx->sq_sqes;
7892 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007893 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007894 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007895 }
7896
7897 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007898 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007899 return ERR_PTR(-EINVAL);
7900
7901 return ptr;
7902}
7903
7904#ifdef CONFIG_MMU
7905
7906static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7907{
7908 size_t sz = vma->vm_end - vma->vm_start;
7909 unsigned long pfn;
7910 void *ptr;
7911
7912 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7913 if (IS_ERR(ptr))
7914 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007915
7916 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7917 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7918}
7919
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007920#else /* !CONFIG_MMU */
7921
7922static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7923{
7924 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7925}
7926
7927static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7928{
7929 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7930}
7931
7932static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7933 unsigned long addr, unsigned long len,
7934 unsigned long pgoff, unsigned long flags)
7935{
7936 void *ptr;
7937
7938 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7939 if (IS_ERR(ptr))
7940 return PTR_ERR(ptr);
7941
7942 return (unsigned long) ptr;
7943}
7944
7945#endif /* !CONFIG_MMU */
7946
Jens Axboe2b188cc2019-01-07 10:46:33 -07007947SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7948 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7949 size_t, sigsz)
7950{
7951 struct io_ring_ctx *ctx;
7952 long ret = -EBADF;
7953 int submitted = 0;
7954 struct fd f;
7955
Jens Axboeb41e9852020-02-17 09:52:41 -07007956 if (current->task_works)
7957 task_work_run();
7958
Jens Axboe6c271ce2019-01-10 11:22:30 -07007959 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007960 return -EINVAL;
7961
7962 f = fdget(fd);
7963 if (!f.file)
7964 return -EBADF;
7965
7966 ret = -EOPNOTSUPP;
7967 if (f.file->f_op != &io_uring_fops)
7968 goto out_fput;
7969
7970 ret = -ENXIO;
7971 ctx = f.file->private_data;
7972 if (!percpu_ref_tryget(&ctx->refs))
7973 goto out_fput;
7974
Jens Axboe6c271ce2019-01-10 11:22:30 -07007975 /*
7976 * For SQ polling, the thread will do all submissions and completions.
7977 * Just return the requested submit count, and wake the thread if
7978 * we were asked to.
7979 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007980 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007981 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007982 if (!list_empty_careful(&ctx->cq_overflow_list))
7983 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007984 if (flags & IORING_ENTER_SQ_WAKEUP)
7985 wake_up(&ctx->sqo_wait);
7986 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007987 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007988 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007989 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007990 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007991
7992 if (submitted != to_submit)
7993 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007994 }
7995 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007996 unsigned nr_events = 0;
7997
Jens Axboe2b188cc2019-01-07 10:46:33 -07007998 min_complete = min(min_complete, ctx->cq_entries);
7999
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008000 /*
8001 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8002 * space applications don't need to do io completion events
8003 * polling again, they can rely on io_sq_thread to do polling
8004 * work, which can reduce cpu usage and uring_lock contention.
8005 */
8006 if (ctx->flags & IORING_SETUP_IOPOLL &&
8007 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07008008 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008009 } else {
8010 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8011 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008012 }
8013
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008014out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008015 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008016out_fput:
8017 fdput(f);
8018 return submitted ? submitted : ret;
8019}
8020
Tobias Klauserbebdb652020-02-26 18:38:32 +01008021#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008022static int io_uring_show_cred(int id, void *p, void *data)
8023{
8024 const struct cred *cred = p;
8025 struct seq_file *m = data;
8026 struct user_namespace *uns = seq_user_ns(m);
8027 struct group_info *gi;
8028 kernel_cap_t cap;
8029 unsigned __capi;
8030 int g;
8031
8032 seq_printf(m, "%5d\n", id);
8033 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8034 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8035 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8036 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8037 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8038 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8039 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8040 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8041 seq_puts(m, "\n\tGroups:\t");
8042 gi = cred->group_info;
8043 for (g = 0; g < gi->ngroups; g++) {
8044 seq_put_decimal_ull(m, g ? " " : "",
8045 from_kgid_munged(uns, gi->gid[g]));
8046 }
8047 seq_puts(m, "\n\tCapEff:\t");
8048 cap = cred->cap_effective;
8049 CAP_FOR_EACH_U32(__capi)
8050 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8051 seq_putc(m, '\n');
8052 return 0;
8053}
8054
8055static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8056{
8057 int i;
8058
8059 mutex_lock(&ctx->uring_lock);
8060 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
8061 for (i = 0; i < ctx->nr_user_files; i++) {
8062 struct fixed_file_table *table;
8063 struct file *f;
8064
8065 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8066 f = table->files[i & IORING_FILE_TABLE_MASK];
8067 if (f)
8068 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8069 else
8070 seq_printf(m, "%5u: <none>\n", i);
8071 }
8072 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
8073 for (i = 0; i < ctx->nr_user_bufs; i++) {
8074 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8075
8076 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8077 (unsigned int) buf->len);
8078 }
8079 if (!idr_is_empty(&ctx->personality_idr)) {
8080 seq_printf(m, "Personalities:\n");
8081 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8082 }
Jens Axboed7718a92020-02-14 22:23:12 -07008083 seq_printf(m, "PollList:\n");
8084 spin_lock_irq(&ctx->completion_lock);
8085 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8086 struct hlist_head *list = &ctx->cancel_hash[i];
8087 struct io_kiocb *req;
8088
8089 hlist_for_each_entry(req, list, hash_node)
8090 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8091 req->task->task_works != NULL);
8092 }
8093 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008094 mutex_unlock(&ctx->uring_lock);
8095}
8096
8097static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8098{
8099 struct io_ring_ctx *ctx = f->private_data;
8100
8101 if (percpu_ref_tryget(&ctx->refs)) {
8102 __io_uring_show_fdinfo(ctx, m);
8103 percpu_ref_put(&ctx->refs);
8104 }
8105}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008106#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008107
Jens Axboe2b188cc2019-01-07 10:46:33 -07008108static const struct file_operations io_uring_fops = {
8109 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008110 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008111 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008112#ifndef CONFIG_MMU
8113 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8114 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8115#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008116 .poll = io_uring_poll,
8117 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008118#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008119 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008120#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008121};
8122
8123static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8124 struct io_uring_params *p)
8125{
Hristo Venev75b28af2019-08-26 17:23:46 +00008126 struct io_rings *rings;
8127 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008128
Hristo Venev75b28af2019-08-26 17:23:46 +00008129 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8130 if (size == SIZE_MAX)
8131 return -EOVERFLOW;
8132
8133 rings = io_mem_alloc(size);
8134 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008135 return -ENOMEM;
8136
Hristo Venev75b28af2019-08-26 17:23:46 +00008137 ctx->rings = rings;
8138 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8139 rings->sq_ring_mask = p->sq_entries - 1;
8140 rings->cq_ring_mask = p->cq_entries - 1;
8141 rings->sq_ring_entries = p->sq_entries;
8142 rings->cq_ring_entries = p->cq_entries;
8143 ctx->sq_mask = rings->sq_ring_mask;
8144 ctx->cq_mask = rings->cq_ring_mask;
8145 ctx->sq_entries = rings->sq_ring_entries;
8146 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008147
8148 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008149 if (size == SIZE_MAX) {
8150 io_mem_free(ctx->rings);
8151 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008152 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008153 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008154
8155 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008156 if (!ctx->sq_sqes) {
8157 io_mem_free(ctx->rings);
8158 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008159 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008160 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008161
Jens Axboe2b188cc2019-01-07 10:46:33 -07008162 return 0;
8163}
8164
8165/*
8166 * Allocate an anonymous fd, this is what constitutes the application
8167 * visible backing of an io_uring instance. The application mmaps this
8168 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8169 * we have to tie this fd to a socket for file garbage collection purposes.
8170 */
8171static int io_uring_get_fd(struct io_ring_ctx *ctx)
8172{
8173 struct file *file;
8174 int ret;
8175
8176#if defined(CONFIG_UNIX)
8177 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8178 &ctx->ring_sock);
8179 if (ret)
8180 return ret;
8181#endif
8182
8183 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8184 if (ret < 0)
8185 goto err;
8186
8187 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8188 O_RDWR | O_CLOEXEC);
8189 if (IS_ERR(file)) {
8190 put_unused_fd(ret);
8191 ret = PTR_ERR(file);
8192 goto err;
8193 }
8194
8195#if defined(CONFIG_UNIX)
8196 ctx->ring_sock->file = file;
8197#endif
8198 fd_install(ret, file);
8199 return ret;
8200err:
8201#if defined(CONFIG_UNIX)
8202 sock_release(ctx->ring_sock);
8203 ctx->ring_sock = NULL;
8204#endif
8205 return ret;
8206}
8207
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008208static int io_uring_create(unsigned entries, struct io_uring_params *p,
8209 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008210{
8211 struct user_struct *user = NULL;
8212 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008213 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008214 int ret;
8215
Jens Axboe8110c1a2019-12-28 15:39:54 -07008216 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008217 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008218 if (entries > IORING_MAX_ENTRIES) {
8219 if (!(p->flags & IORING_SETUP_CLAMP))
8220 return -EINVAL;
8221 entries = IORING_MAX_ENTRIES;
8222 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008223
8224 /*
8225 * Use twice as many entries for the CQ ring. It's possible for the
8226 * application to drive a higher depth than the size of the SQ ring,
8227 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008228 * some flexibility in overcommitting a bit. If the application has
8229 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8230 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008231 */
8232 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008233 if (p->flags & IORING_SETUP_CQSIZE) {
8234 /*
8235 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8236 * to a power-of-two, if it isn't already. We do NOT impose
8237 * any cq vs sq ring sizing.
8238 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008239 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008240 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008241 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8242 if (!(p->flags & IORING_SETUP_CLAMP))
8243 return -EINVAL;
8244 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8245 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008246 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8247 } else {
8248 p->cq_entries = 2 * p->sq_entries;
8249 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008250
8251 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008252 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008253
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008254 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008255 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008256 ring_pages(p->sq_entries, p->cq_entries));
8257 if (ret) {
8258 free_uid(user);
8259 return ret;
8260 }
8261 }
8262
8263 ctx = io_ring_ctx_alloc(p);
8264 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008265 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008266 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008267 p->cq_entries));
8268 free_uid(user);
8269 return -ENOMEM;
8270 }
8271 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008272 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008273 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008274
8275 ret = io_allocate_scq_urings(ctx, p);
8276 if (ret)
8277 goto err;
8278
Jens Axboe6c271ce2019-01-10 11:22:30 -07008279 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008280 if (ret)
8281 goto err;
8282
Jens Axboe2b188cc2019-01-07 10:46:33 -07008283 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008284 p->sq_off.head = offsetof(struct io_rings, sq.head);
8285 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8286 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8287 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8288 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8289 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8290 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008291
8292 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008293 p->cq_off.head = offsetof(struct io_rings, cq.head);
8294 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8295 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8296 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8297 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8298 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008299 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008300
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008301 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8302 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008303 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8304 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008305
8306 if (copy_to_user(params, p, sizeof(*p))) {
8307 ret = -EFAULT;
8308 goto err;
8309 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008310 /*
8311 * Install ring fd as the very last thing, so we don't risk someone
8312 * having closed it before we finish setup
8313 */
8314 ret = io_uring_get_fd(ctx);
8315 if (ret < 0)
8316 goto err;
8317
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008318 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008319 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8320 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008321 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008322 return ret;
8323err:
8324 io_ring_ctx_wait_and_kill(ctx);
8325 return ret;
8326}
8327
8328/*
8329 * Sets up an aio uring context, and returns the fd. Applications asks for a
8330 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8331 * params structure passed in.
8332 */
8333static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8334{
8335 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008336 int i;
8337
8338 if (copy_from_user(&p, params, sizeof(p)))
8339 return -EFAULT;
8340 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8341 if (p.resv[i])
8342 return -EINVAL;
8343 }
8344
Jens Axboe6c271ce2019-01-10 11:22:30 -07008345 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008346 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008347 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008348 return -EINVAL;
8349
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008350 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008351}
8352
8353SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8354 struct io_uring_params __user *, params)
8355{
8356 return io_uring_setup(entries, params);
8357}
8358
Jens Axboe66f4af92020-01-16 15:36:52 -07008359static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8360{
8361 struct io_uring_probe *p;
8362 size_t size;
8363 int i, ret;
8364
8365 size = struct_size(p, ops, nr_args);
8366 if (size == SIZE_MAX)
8367 return -EOVERFLOW;
8368 p = kzalloc(size, GFP_KERNEL);
8369 if (!p)
8370 return -ENOMEM;
8371
8372 ret = -EFAULT;
8373 if (copy_from_user(p, arg, size))
8374 goto out;
8375 ret = -EINVAL;
8376 if (memchr_inv(p, 0, size))
8377 goto out;
8378
8379 p->last_op = IORING_OP_LAST - 1;
8380 if (nr_args > IORING_OP_LAST)
8381 nr_args = IORING_OP_LAST;
8382
8383 for (i = 0; i < nr_args; i++) {
8384 p->ops[i].op = i;
8385 if (!io_op_defs[i].not_supported)
8386 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8387 }
8388 p->ops_len = i;
8389
8390 ret = 0;
8391 if (copy_to_user(arg, p, size))
8392 ret = -EFAULT;
8393out:
8394 kfree(p);
8395 return ret;
8396}
8397
Jens Axboe071698e2020-01-28 10:04:42 -07008398static int io_register_personality(struct io_ring_ctx *ctx)
8399{
8400 const struct cred *creds = get_current_cred();
8401 int id;
8402
8403 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8404 USHRT_MAX, GFP_KERNEL);
8405 if (id < 0)
8406 put_cred(creds);
8407 return id;
8408}
8409
8410static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8411{
8412 const struct cred *old_creds;
8413
8414 old_creds = idr_remove(&ctx->personality_idr, id);
8415 if (old_creds) {
8416 put_cred(old_creds);
8417 return 0;
8418 }
8419
8420 return -EINVAL;
8421}
8422
8423static bool io_register_op_must_quiesce(int op)
8424{
8425 switch (op) {
8426 case IORING_UNREGISTER_FILES:
8427 case IORING_REGISTER_FILES_UPDATE:
8428 case IORING_REGISTER_PROBE:
8429 case IORING_REGISTER_PERSONALITY:
8430 case IORING_UNREGISTER_PERSONALITY:
8431 return false;
8432 default:
8433 return true;
8434 }
8435}
8436
Jens Axboeedafcce2019-01-09 09:16:05 -07008437static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8438 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008439 __releases(ctx->uring_lock)
8440 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008441{
8442 int ret;
8443
Jens Axboe35fa71a2019-04-22 10:23:23 -06008444 /*
8445 * We're inside the ring mutex, if the ref is already dying, then
8446 * someone else killed the ctx or is already going through
8447 * io_uring_register().
8448 */
8449 if (percpu_ref_is_dying(&ctx->refs))
8450 return -ENXIO;
8451
Jens Axboe071698e2020-01-28 10:04:42 -07008452 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008453 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008454
Jens Axboe05f3fb32019-12-09 11:22:50 -07008455 /*
8456 * Drop uring mutex before waiting for references to exit. If
8457 * another thread is currently inside io_uring_enter() it might
8458 * need to grab the uring_lock to make progress. If we hold it
8459 * here across the drain wait, then we can deadlock. It's safe
8460 * to drop the mutex here, since no new references will come in
8461 * after we've killed the percpu ref.
8462 */
8463 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008464 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008465 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008466 if (ret) {
8467 percpu_ref_resurrect(&ctx->refs);
8468 ret = -EINTR;
8469 goto out;
8470 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008471 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008472
8473 switch (opcode) {
8474 case IORING_REGISTER_BUFFERS:
8475 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8476 break;
8477 case IORING_UNREGISTER_BUFFERS:
8478 ret = -EINVAL;
8479 if (arg || nr_args)
8480 break;
8481 ret = io_sqe_buffer_unregister(ctx);
8482 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008483 case IORING_REGISTER_FILES:
8484 ret = io_sqe_files_register(ctx, arg, nr_args);
8485 break;
8486 case IORING_UNREGISTER_FILES:
8487 ret = -EINVAL;
8488 if (arg || nr_args)
8489 break;
8490 ret = io_sqe_files_unregister(ctx);
8491 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008492 case IORING_REGISTER_FILES_UPDATE:
8493 ret = io_sqe_files_update(ctx, arg, nr_args);
8494 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008495 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008496 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008497 ret = -EINVAL;
8498 if (nr_args != 1)
8499 break;
8500 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008501 if (ret)
8502 break;
8503 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8504 ctx->eventfd_async = 1;
8505 else
8506 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008507 break;
8508 case IORING_UNREGISTER_EVENTFD:
8509 ret = -EINVAL;
8510 if (arg || nr_args)
8511 break;
8512 ret = io_eventfd_unregister(ctx);
8513 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008514 case IORING_REGISTER_PROBE:
8515 ret = -EINVAL;
8516 if (!arg || nr_args > 256)
8517 break;
8518 ret = io_probe(ctx, arg, nr_args);
8519 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008520 case IORING_REGISTER_PERSONALITY:
8521 ret = -EINVAL;
8522 if (arg || nr_args)
8523 break;
8524 ret = io_register_personality(ctx);
8525 break;
8526 case IORING_UNREGISTER_PERSONALITY:
8527 ret = -EINVAL;
8528 if (arg)
8529 break;
8530 ret = io_unregister_personality(ctx, nr_args);
8531 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008532 default:
8533 ret = -EINVAL;
8534 break;
8535 }
8536
Jens Axboe071698e2020-01-28 10:04:42 -07008537 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008538 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008539 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008540out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008541 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008542 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008543 return ret;
8544}
8545
8546SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8547 void __user *, arg, unsigned int, nr_args)
8548{
8549 struct io_ring_ctx *ctx;
8550 long ret = -EBADF;
8551 struct fd f;
8552
8553 f = fdget(fd);
8554 if (!f.file)
8555 return -EBADF;
8556
8557 ret = -EOPNOTSUPP;
8558 if (f.file->f_op != &io_uring_fops)
8559 goto out_fput;
8560
8561 ctx = f.file->private_data;
8562
8563 mutex_lock(&ctx->uring_lock);
8564 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8565 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008566 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8567 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008568out_fput:
8569 fdput(f);
8570 return ret;
8571}
8572
Jens Axboe2b188cc2019-01-07 10:46:33 -07008573static int __init io_uring_init(void)
8574{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008575#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8576 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8577 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8578} while (0)
8579
8580#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8581 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8582 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8583 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8584 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8585 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8586 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8587 BUILD_BUG_SQE_ELEM(8, __u64, off);
8588 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8589 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008590 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008591 BUILD_BUG_SQE_ELEM(24, __u32, len);
8592 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8593 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8594 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8595 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008596 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8597 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008598 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8599 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8600 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8601 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8602 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8603 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8604 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8605 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008606 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008607 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8608 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8609 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008610 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008611
Jens Axboed3656342019-12-18 09:50:26 -07008612 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008613 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008614 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8615 return 0;
8616};
8617__initcall(io_uring_init);